Registration always writes role-guest; a deleted standard role became a masked INTERNAL. Username unique violations are now a 400 instead of an infrastructure 500.
40 lines
1.2 KiB
Go
40 lines
1.2 KiB
Go
package auth
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
)
|
|
|
|
func TestIsUsernameConflict(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
err error
|
|
want bool
|
|
}{
|
|
{"username index", &pgconn.PgError{Code: "23505", ConstraintName: "uq_users_username"}, true},
|
|
{"account id", &pgconn.PgError{Code: "23505", ConstraintName: "users_account_id_key"}, true},
|
|
{"username key", &pgconn.PgError{Code: "23505", ConstraintName: "users_username_key"}, true},
|
|
{"fk is not username", &pgconn.PgError{Code: "23503", ConstraintName: "user_role_bindings_role_id_fkey"}, false},
|
|
{"other unique", &pgconn.PgError{Code: "23505", ConstraintName: "user_credentials_pkey"}, false},
|
|
{"plain error", errors.New("insert user: boom"), false},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if got := isUsernameConflict(tc.err); got != tc.want {
|
|
t.Fatalf("isUsernameConflict() = %v, want %v", got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestErrUsernameExistsIsNotInfrastructureShaped(t *testing.T) {
|
|
if !errors.Is(ErrUsernameExists, ErrUsernameExists) {
|
|
t.Fatal("sentinel identity")
|
|
}
|
|
if ErrUsernameExists.Error() != "用户名已存在" {
|
|
t.Fatalf("message = %q", ErrUsernameExists.Error())
|
|
}
|
|
}
|