282 lines
7.9 KiB
Go
282 lines
7.9 KiB
Go
package user
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"mediarr/database"
|
|
)
|
|
|
|
func TestCreateUser(t *testing.T) {
|
|
service := newTestService(t)
|
|
ctx := context.Background()
|
|
|
|
created, err := service.CreateUser(ctx, CreateUserParams{
|
|
Login: "Alice",
|
|
Password: "correct horse battery staple",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("CreateUser : %v", err)
|
|
}
|
|
|
|
if created.ID.IsZero() {
|
|
t.Fatal("identifiant utilisateur vide")
|
|
}
|
|
if created.Login != "Alice" {
|
|
t.Fatalf("login = %q, attendu Alice", created.Login)
|
|
}
|
|
if created.Name != "Alice" {
|
|
t.Fatalf("name = %q, attendu Alice", created.Name)
|
|
}
|
|
if created.Status != "active" {
|
|
t.Fatalf("status = %q, attendu active", created.Status)
|
|
}
|
|
if created.AccountState != "active" {
|
|
t.Fatalf("état du compte = %q, attendu active", created.AccountState)
|
|
}
|
|
if len(created.Groups) != 1 || created.Groups[0].Name != "members" {
|
|
t.Fatalf("groupes = %+v, attendu members", created.Groups)
|
|
}
|
|
|
|
principal, err := service.db.Queries.GetPrincipal(ctx, created.ID)
|
|
if err != nil {
|
|
t.Fatalf("GetPrincipal : %v", err)
|
|
}
|
|
if principal.Kind != "user" {
|
|
t.Fatalf("kind = %q, attendu user", principal.Kind)
|
|
}
|
|
|
|
account, err := service.db.Queries.GetAccountByLoginKey(ctx, "alice")
|
|
if err != nil {
|
|
t.Fatalf("GetAccountByLoginKey : %v", err)
|
|
}
|
|
if !account.PasswordHash.Valid {
|
|
t.Fatal("hash du mot de passe absent")
|
|
}
|
|
if account.PasswordHash.String == "correct horse battery staple" {
|
|
t.Fatal("le mot de passe est stocké en clair")
|
|
}
|
|
}
|
|
|
|
func TestCreateUserRejectsDuplicateLogin(t *testing.T) {
|
|
service := newTestService(t)
|
|
ctx := context.Background()
|
|
|
|
mustCreateUser(t, service, "alice", "correct horse battery staple", RoleMember)
|
|
|
|
_, err := service.CreateUser(ctx, CreateUserParams{
|
|
Login: "ALICE",
|
|
Password: "another correct horse battery staple",
|
|
})
|
|
if !errors.Is(err, ErrLoginTaken) {
|
|
t.Fatalf("erreur = %v, attendu ErrLoginTaken", err)
|
|
}
|
|
}
|
|
|
|
func TestCreateUserRejectsInvalidInput(t *testing.T) {
|
|
service := newTestService(t)
|
|
ctx := context.Background()
|
|
|
|
_, err := service.CreateUser(ctx, CreateUserParams{
|
|
Login: " ",
|
|
Password: "correct horse battery staple",
|
|
})
|
|
if !errors.Is(err, ErrInvalidLogin) {
|
|
t.Fatalf("login vide : erreur = %v, attendu ErrInvalidLogin", err)
|
|
}
|
|
|
|
_, err = service.CreateUser(ctx, CreateUserParams{
|
|
Login: "alice",
|
|
Password: "court",
|
|
})
|
|
if !errors.Is(err, ErrWeakPassword) {
|
|
t.Fatalf("mot de passe faible : erreur = %v, attendu ErrWeakPassword", err)
|
|
}
|
|
|
|
_, err = service.CreateUser(ctx, CreateUserParams{
|
|
Login: "alice",
|
|
Password: "correct horse battery staple",
|
|
Role: "super-admin",
|
|
})
|
|
if !errors.Is(err, ErrInvalidRole) {
|
|
t.Fatalf("rôle invalide : erreur = %v, attendu ErrInvalidRole", err)
|
|
}
|
|
}
|
|
|
|
func TestCreateUserAdmin(t *testing.T) {
|
|
service := newTestService(t)
|
|
|
|
created := mustCreateUser(t, service, "root", "correct horse battery staple", RoleAdmin)
|
|
|
|
if len(created.Groups) != 1 || created.Groups[0].Name != "admins" {
|
|
t.Fatalf("groupes = %+v, attendu admins", created.Groups)
|
|
}
|
|
}
|
|
|
|
func TestAuthenticate(t *testing.T) {
|
|
service := newTestService(t)
|
|
ctx := context.Background()
|
|
|
|
created := mustCreateUser(t, service, "alice", "correct horse battery staple", RoleMember)
|
|
|
|
authenticated, err := service.Authenticate(ctx, "ALICE", "correct horse battery staple")
|
|
if err != nil {
|
|
t.Fatalf("Authenticate : %v", err)
|
|
}
|
|
if authenticated.ID != created.ID {
|
|
t.Fatalf("id = %s, attendu %s", authenticated.ID, created.ID)
|
|
}
|
|
|
|
account, err := service.db.Queries.GetAccount(ctx, created.ID)
|
|
if err != nil {
|
|
t.Fatalf("GetAccount : %v", err)
|
|
}
|
|
if !account.LastLoginAt.Valid {
|
|
t.Fatal("last_login_at non renseigné")
|
|
}
|
|
}
|
|
|
|
func TestAuthenticateRejectsUnknownLogin(t *testing.T) {
|
|
service := newTestService(t)
|
|
|
|
_, err := service.Authenticate(context.Background(), "inconnu", "correct horse battery staple")
|
|
if !errors.Is(err, ErrInvalidCredentials) {
|
|
t.Fatalf("erreur = %v, attendu ErrInvalidCredentials", err)
|
|
}
|
|
}
|
|
|
|
func TestAuthenticateRejectsWrongPassword(t *testing.T) {
|
|
service := newTestService(t)
|
|
ctx := context.Background()
|
|
|
|
created := mustCreateUser(t, service, "alice", "correct horse battery staple", RoleMember)
|
|
|
|
_, err := service.Authenticate(ctx, "alice", "mauvais mot de passe")
|
|
if !errors.Is(err, ErrInvalidCredentials) {
|
|
t.Fatalf("erreur = %v, attendu ErrInvalidCredentials", err)
|
|
}
|
|
|
|
account, err := service.db.Queries.GetAccount(ctx, created.ID)
|
|
if err != nil {
|
|
t.Fatalf("GetAccount : %v", err)
|
|
}
|
|
if account.FailedAttempts != 1 {
|
|
t.Fatalf("failed_attempts = %d, attendu 1", account.FailedAttempts)
|
|
}
|
|
}
|
|
|
|
func TestAuthenticateLocksAccountAfterTooManyFailures(t *testing.T) {
|
|
service := newTestService(t)
|
|
ctx := context.Background()
|
|
|
|
created := mustCreateUser(t, service, "alice", "correct horse battery staple", RoleMember)
|
|
|
|
for attempt := 1; attempt <= maxFailedAttempts; attempt++ {
|
|
_, err := service.Authenticate(ctx, "alice", "mauvais mot de passe")
|
|
if attempt < maxFailedAttempts {
|
|
if !errors.Is(err, ErrInvalidCredentials) {
|
|
t.Fatalf("tentative %d : erreur = %v, attendu ErrInvalidCredentials", attempt, err)
|
|
}
|
|
continue
|
|
}
|
|
|
|
if !errors.Is(err, ErrAccountLocked) {
|
|
t.Fatalf("tentative %d : erreur = %v, attendu ErrAccountLocked", attempt, err)
|
|
}
|
|
}
|
|
|
|
_, err := service.Authenticate(ctx, "alice", "correct horse battery staple")
|
|
if !errors.Is(err, ErrAccountLocked) {
|
|
t.Fatalf("erreur = %v, attendu ErrAccountLocked", err)
|
|
}
|
|
|
|
account, err := service.db.Queries.GetAccount(ctx, created.ID)
|
|
if err != nil {
|
|
t.Fatalf("GetAccount : %v", err)
|
|
}
|
|
if account.State != "locked" {
|
|
t.Fatalf("state = %q, attendu locked", account.State)
|
|
}
|
|
if !account.LockedUntil.Valid || account.LockedUntil.Int64 <= time.Now().UnixMilli() {
|
|
t.Fatalf("locked_until = %+v, attendu dans le futur", account.LockedUntil)
|
|
}
|
|
}
|
|
|
|
func TestAuthenticateUnlocksExpiredAccount(t *testing.T) {
|
|
service := newTestService(t)
|
|
ctx := context.Background()
|
|
|
|
created := mustCreateUser(t, service, "alice", "correct horse battery staple", RoleMember)
|
|
|
|
_, err := service.db.WriteQueries.LockAccount(ctx, database.LockAccountParams{
|
|
LockedUntil: sql.NullInt64{
|
|
Int64: time.Now().Add(-time.Minute).UnixMilli(),
|
|
Valid: true,
|
|
},
|
|
PrincipalID: created.ID,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("LockAccount : %v", err)
|
|
}
|
|
|
|
_, err = service.Authenticate(ctx, "alice", "correct horse battery staple")
|
|
if err != nil {
|
|
t.Fatalf("Authenticate : %v", err)
|
|
}
|
|
|
|
account, err := service.db.Queries.GetAccount(ctx, created.ID)
|
|
if err != nil {
|
|
t.Fatalf("GetAccount : %v", err)
|
|
}
|
|
if account.State != "active" {
|
|
t.Fatalf("state = %q, attendu active", account.State)
|
|
}
|
|
if account.FailedAttempts != 0 {
|
|
t.Fatalf("failed_attempts = %d, attendu 0", account.FailedAttempts)
|
|
}
|
|
}
|
|
|
|
func TestAuthenticateRejectsDisabledAccount(t *testing.T) {
|
|
service := newTestService(t)
|
|
ctx := context.Background()
|
|
|
|
created := mustCreateUser(t, service, "alice", "correct horse battery staple", RoleMember)
|
|
|
|
_, err := service.db.WriteQueries.UpdateAccountState(ctx, database.UpdateAccountStateParams{
|
|
State: "disabled",
|
|
PrincipalID: created.ID,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("UpdateAccountState : %v", err)
|
|
}
|
|
|
|
_, err = service.Authenticate(ctx, "alice", "correct horse battery staple")
|
|
if !errors.Is(err, ErrAccountDisabled) {
|
|
t.Fatalf("erreur = %v, attendu ErrAccountDisabled", err)
|
|
}
|
|
}
|
|
|
|
func TestSetPassword(t *testing.T) {
|
|
service := newTestService(t)
|
|
ctx := context.Background()
|
|
|
|
created := mustCreateUser(t, service, "alice", "correct horse battery staple", RoleMember)
|
|
|
|
if err := service.SetPassword(ctx, created.ID, "nouveau correct horse battery staple"); err != nil {
|
|
t.Fatalf("SetPassword : %v", err)
|
|
}
|
|
|
|
_, err := service.Authenticate(ctx, "alice", "correct horse battery staple")
|
|
if !errors.Is(err, ErrInvalidCredentials) {
|
|
t.Fatalf("ancien mot de passe : erreur = %v, attendu ErrInvalidCredentials", err)
|
|
}
|
|
|
|
_, err = service.Authenticate(ctx, "alice", "nouveau correct horse battery staple")
|
|
if err != nil {
|
|
t.Fatalf("nouveau mot de passe : %v", err)
|
|
}
|
|
}
|