2022-12-23 13:11:11 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2023-04-19 15:50:33 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2022-12-23 13:11:11 +00:00
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_validatePassword(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
password string
|
|
|
|
wantError error
|
|
|
|
wantJSON *util.JSONResponse
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "password too short",
|
|
|
|
password: "shortpw",
|
|
|
|
wantError: ErrPasswordWeak,
|
2023-05-09 23:46:49 +01:00
|
|
|
wantJSON: &util.JSONResponse{Code: http.StatusBadRequest, JSON: spec.WeakPassword(ErrPasswordWeak.Error())},
|
2022-12-23 13:11:11 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "password too long",
|
|
|
|
password: strings.Repeat("a", maxPasswordLength+1),
|
|
|
|
wantError: ErrPasswordTooLong,
|
2023-05-09 23:46:49 +01:00
|
|
|
wantJSON: &util.JSONResponse{Code: http.StatusBadRequest, JSON: spec.BadJSON(ErrPasswordTooLong.Error())},
|
2022-12-23 13:11:11 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "password OK",
|
|
|
|
password: util.RandomString(10),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
gotErr := ValidatePassword(tt.password)
|
|
|
|
if !reflect.DeepEqual(gotErr, tt.wantError) {
|
|
|
|
t.Errorf("validatePassword() = %v, wantJSON %v", gotErr, tt.wantError)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got := PasswordResponse(gotErr); !reflect.DeepEqual(got, tt.wantJSON) {
|
|
|
|
t.Errorf("validatePassword() = %v, wantJSON %v", got, tt.wantJSON)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_validateUsername(t *testing.T) {
|
|
|
|
tooLongUsername := strings.Repeat("a", maxUsernameLength)
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
localpart string
|
2023-04-19 15:50:33 +01:00
|
|
|
domain spec.ServerName
|
2022-12-23 13:11:11 +00:00
|
|
|
wantErr error
|
|
|
|
wantJSON *util.JSONResponse
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty username",
|
|
|
|
localpart: "",
|
|
|
|
domain: "localhost",
|
|
|
|
wantErr: ErrUsernameInvalid,
|
|
|
|
wantJSON: &util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-09 23:46:49 +01:00
|
|
|
JSON: spec.InvalidUsername(ErrUsernameInvalid.Error()),
|
2022-12-23 13:11:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid username",
|
|
|
|
localpart: "INVALIDUSERNAME",
|
|
|
|
domain: "localhost",
|
|
|
|
wantErr: ErrUsernameInvalid,
|
|
|
|
wantJSON: &util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-09 23:46:49 +01:00
|
|
|
JSON: spec.InvalidUsername(ErrUsernameInvalid.Error()),
|
2022-12-23 13:11:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "username too long",
|
|
|
|
localpart: tooLongUsername,
|
|
|
|
domain: "localhost",
|
|
|
|
wantErr: ErrUsernameTooLong,
|
|
|
|
wantJSON: &util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-09 23:46:49 +01:00
|
|
|
JSON: spec.BadJSON(ErrUsernameTooLong.Error()),
|
2022-12-23 13:11:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "localpart starting with an underscore",
|
|
|
|
localpart: "_notvalid",
|
|
|
|
domain: "localhost",
|
|
|
|
wantErr: ErrUsernameUnderscore,
|
|
|
|
wantJSON: &util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-09 23:46:49 +01:00
|
|
|
JSON: spec.InvalidUsername(ErrUsernameUnderscore.Error()),
|
2022-12-23 13:11:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid username",
|
|
|
|
localpart: "valid",
|
|
|
|
domain: "localhost",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "complex username",
|
|
|
|
localpart: "f00_bar-baz.=40/",
|
|
|
|
domain: "localhost",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "rejects emoji username 💥",
|
|
|
|
localpart: "💥",
|
|
|
|
domain: "localhost",
|
|
|
|
wantErr: ErrUsernameInvalid,
|
|
|
|
wantJSON: &util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-09 23:46:49 +01:00
|
|
|
JSON: spec.InvalidUsername(ErrUsernameInvalid.Error()),
|
2022-12-23 13:11:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "special characters are allowed",
|
|
|
|
localpart: "/dev/null",
|
|
|
|
domain: "localhost",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "special characters are allowed 2",
|
|
|
|
localpart: "i_am_allowed=1",
|
|
|
|
domain: "localhost",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not all special characters are allowed",
|
|
|
|
localpart: "notallowed#", // contains #
|
|
|
|
domain: "localhost",
|
|
|
|
wantErr: ErrUsernameInvalid,
|
|
|
|
wantJSON: &util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-09 23:46:49 +01:00
|
|
|
JSON: spec.InvalidUsername(ErrUsernameInvalid.Error()),
|
2022-12-23 13:11:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "username containing numbers",
|
|
|
|
localpart: "hello1337",
|
|
|
|
domain: "localhost",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
gotErr := ValidateUsername(tt.localpart, tt.domain)
|
|
|
|
if !reflect.DeepEqual(gotErr, tt.wantErr) {
|
|
|
|
t.Errorf("ValidateUsername() = %v, wantErr %v", gotErr, tt.wantErr)
|
|
|
|
}
|
|
|
|
if gotJSON := UsernameResponse(gotErr); !reflect.DeepEqual(gotJSON, tt.wantJSON) {
|
|
|
|
t.Errorf("UsernameResponse() = %v, wantJSON %v", gotJSON, tt.wantJSON)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Application services are allowed usernames starting with an underscore
|
|
|
|
if tt.wantErr == ErrUsernameUnderscore {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
gotErr = ValidateApplicationServiceUsername(tt.localpart, tt.domain)
|
|
|
|
if !reflect.DeepEqual(gotErr, tt.wantErr) {
|
|
|
|
t.Errorf("ValidateUsername() = %v, wantErr %v", gotErr, tt.wantErr)
|
|
|
|
}
|
|
|
|
if gotJSON := UsernameResponse(gotErr); !reflect.DeepEqual(gotJSON, tt.wantJSON) {
|
|
|
|
t.Errorf("UsernameResponse() = %v, wantJSON %v", gotJSON, tt.wantJSON)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|