Fold follow-up migrations into init, expose GET /users/clients and /users/staff, and refresh seed data. Co-authored-by: Cursor <cursoragent@cursor.com>
294 lines
8.2 KiB
Go
294 lines
8.2 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
type fakeRepo struct {
|
|
exists bool
|
|
usernameTaken bool
|
|
verification Verification
|
|
getErr error
|
|
userID int
|
|
userByName UserIdentity
|
|
userByNameErr error
|
|
loginUser UserCredentials
|
|
loginErr error
|
|
lastLogin LoginDTO
|
|
}
|
|
|
|
func (f *fakeRepo) Login(_ context.Context, dto LoginDTO) (UserCredentials, error) {
|
|
f.lastLogin = dto
|
|
if f.loginErr != nil {
|
|
return UserCredentials{}, f.loginErr
|
|
}
|
|
return f.loginUser, nil
|
|
}
|
|
func (f *fakeRepo) GetUserByID(context.Context, int) (UserCredentials, error) {
|
|
return UserCredentials{}, errors.New("not implemented")
|
|
}
|
|
func (f *fakeRepo) GetProfile(context.Context, int) (UserProfileDTO, error) {
|
|
return UserProfileDTO{}, errors.New("not implemented")
|
|
}
|
|
func (f *fakeRepo) UpdateProfile(context.Context, int, UpdateProfileDTO) (UserProfileDTO, error) {
|
|
return UserProfileDTO{}, errors.New("not implemented")
|
|
}
|
|
func (f *fakeRepo) UserExists(context.Context, string, string) (bool, error) {
|
|
return f.exists, nil
|
|
}
|
|
func (f *fakeRepo) UsernameTaken(context.Context, string, string) (bool, error) {
|
|
return f.usernameTaken, nil
|
|
}
|
|
func (f *fakeRepo) UpsertVerification(_ context.Context, v Verification) error {
|
|
f.verification = v
|
|
return nil
|
|
}
|
|
func (f *fakeRepo) GetVerificationByPhone(context.Context, string) (Verification, error) {
|
|
return f.verification, f.getErr
|
|
}
|
|
func (f *fakeRepo) IncrementVerifyAttempts(context.Context, int64) error {
|
|
f.verification.Attempts++
|
|
return nil
|
|
}
|
|
func (f *fakeRepo) CompleteRegistration(context.Context, Verification) (int, error) {
|
|
return f.userID, nil
|
|
}
|
|
func (f *fakeRepo) GetUserByName(context.Context, string) (UserIdentity, error) {
|
|
if f.userByNameErr != nil {
|
|
return UserIdentity{}, f.userByNameErr
|
|
}
|
|
return f.userByName, nil
|
|
}
|
|
func (f *fakeRepo) CompletePasswordReset(context.Context, int, int64, string) error {
|
|
return nil
|
|
}
|
|
|
|
type fakeSMS struct {
|
|
phone, message string
|
|
err error
|
|
}
|
|
|
|
func (f *fakeSMS) Send(_ context.Context, phone, message string) error {
|
|
f.phone = phone
|
|
f.message = message
|
|
return f.err
|
|
}
|
|
|
|
func validRegisterDTO() RegisterDTO {
|
|
return RegisterDTO{
|
|
Name: "Berdi Berdiyew",
|
|
Phone: "+993 61 92 92 48",
|
|
Username: "berdi",
|
|
Password: "secret1",
|
|
PasswordConfirm: "secret1",
|
|
}
|
|
}
|
|
|
|
func TestLoginRequiresNameOrPhone(t *testing.T) {
|
|
_, err := NewService(&fakeRepo{}, &fakeSMS{}).Login(context.Background(), LoginDTO{
|
|
Password: "secret1",
|
|
})
|
|
if !errors.Is(err, ErrInvalidInput) {
|
|
t.Fatalf("error = %v, want ErrInvalidInput", err)
|
|
}
|
|
}
|
|
|
|
func TestLoginIgnoresInvalidPhoneWhenNamePresent(t *testing.T) {
|
|
repo := &fakeRepo{loginErr: errors.New("stop after lookup")}
|
|
_, _ = NewService(repo, &fakeSMS{}).Login(context.Background(), LoginDTO{
|
|
Name: "admin",
|
|
Phone: "string",
|
|
Password: "Test1234!",
|
|
})
|
|
if repo.lastLogin.Name != "admin" {
|
|
t.Fatalf("name = %q, want admin", repo.lastLogin.Name)
|
|
}
|
|
if repo.lastLogin.Phone != "" {
|
|
t.Fatalf("phone = %q, want empty", repo.lastLogin.Phone)
|
|
}
|
|
}
|
|
|
|
func TestLoginInvalidPhoneWithoutName(t *testing.T) {
|
|
_, err := NewService(&fakeRepo{}, &fakeSMS{}).Login(context.Background(), LoginDTO{
|
|
Phone: "string",
|
|
Password: "secret1",
|
|
})
|
|
if !errors.Is(err, ErrInvalidInput) {
|
|
t.Fatalf("error = %v, want ErrInvalidInput", err)
|
|
}
|
|
}
|
|
|
|
func TestLoginWrongPassword(t *testing.T) {
|
|
hash, err := bcrypt.GenerateFromPassword([]byte("secret1"), bcrypt.MinCost)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = NewService(&fakeRepo{loginUser: UserCredentials{Id: 7, Password: string(hash)}}, &fakeSMS{}).Login(context.Background(), LoginDTO{
|
|
Name: "Berdi Berdiyew",
|
|
Password: "wrong",
|
|
})
|
|
if !errors.Is(err, ErrInvalidCredentials) {
|
|
t.Fatalf("error = %v, want ErrInvalidCredentials", err)
|
|
}
|
|
}
|
|
|
|
func TestRegisterPasswordMismatch(t *testing.T) {
|
|
dto := validRegisterDTO()
|
|
dto.PasswordConfirm = "other"
|
|
_, err := NewService(&fakeRepo{}, &fakeSMS{}).Register(context.Background(), dto)
|
|
if !errors.Is(err, ErrPasswordMismatch) {
|
|
t.Fatalf("error = %v, want ErrPasswordMismatch", err)
|
|
}
|
|
}
|
|
|
|
func TestRegisterConflict(t *testing.T) {
|
|
_, err := NewService(&fakeRepo{exists: true}, &fakeSMS{}).Register(context.Background(), validRegisterDTO())
|
|
if !errors.Is(err, ErrConflict) {
|
|
t.Fatalf("error = %v, want ErrConflict", err)
|
|
}
|
|
}
|
|
|
|
func TestRegisterSendsSMS(t *testing.T) {
|
|
repo := &fakeRepo{}
|
|
sender := &fakeSMS{}
|
|
result, err := NewService(repo, sender).Register(context.Background(), validRegisterDTO())
|
|
if err != nil {
|
|
t.Fatalf("Register: %v", err)
|
|
}
|
|
if result.Phone != "+993 61 92 92 48" {
|
|
t.Errorf("phone = %q", result.Phone)
|
|
}
|
|
if result.ExpiresIn != 60 {
|
|
t.Errorf("expiresIn = %d", result.ExpiresIn)
|
|
}
|
|
if sender.phone != result.Phone {
|
|
t.Errorf("sms phone = %q", sender.phone)
|
|
}
|
|
if !strings.Contains(sender.message, "123456") {
|
|
t.Errorf("sms message = %q", sender.message)
|
|
}
|
|
if repo.verification.Password == "secret1" {
|
|
t.Fatal("password stored in plaintext")
|
|
}
|
|
}
|
|
|
|
func TestVerifyWrongCode(t *testing.T) {
|
|
hash, err := bcrypt.GenerateFromPassword([]byte("123456"), bcrypt.MinCost)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
repo := &fakeRepo{verification: Verification{
|
|
ID: 1,
|
|
Phone: "+993 61 92 92 48",
|
|
CodeHash: string(hash),
|
|
ExpiresAt: time.Now().UTC().Add(time.Minute),
|
|
}}
|
|
_, err = NewService(repo, &fakeSMS{}).Verify(context.Background(), VerifyDTO{
|
|
Phone: "+993 61 92 92 48",
|
|
Code: "000000",
|
|
})
|
|
if !errors.Is(err, ErrInvalidCode) {
|
|
t.Fatalf("error = %v, want ErrInvalidCode", err)
|
|
}
|
|
if repo.verification.Attempts != 1 {
|
|
t.Errorf("attempts = %d, want 1", repo.verification.Attempts)
|
|
}
|
|
}
|
|
|
|
func TestVerifyExpiredCode(t *testing.T) {
|
|
repo := &fakeRepo{verification: Verification{
|
|
ExpiresAt: time.Now().UTC().Add(-time.Minute),
|
|
}}
|
|
_, err := NewService(repo, &fakeSMS{}).Verify(context.Background(), VerifyDTO{
|
|
Phone: "+993 61 92 92 48",
|
|
Code: "123456",
|
|
})
|
|
if !errors.Is(err, ErrInvalidCode) {
|
|
t.Fatalf("error = %v, want ErrInvalidCode", err)
|
|
}
|
|
}
|
|
|
|
func TestForgotPasswordRequiresName(t *testing.T) {
|
|
_, err := NewService(&fakeRepo{}, &fakeSMS{}).ForgotPassword(context.Background(), ForgotPasswordDTO{})
|
|
if !errors.Is(err, ErrInvalidInput) {
|
|
t.Fatalf("error = %v, want ErrInvalidInput", err)
|
|
}
|
|
}
|
|
|
|
func TestForgotPasswordNotFound(t *testing.T) {
|
|
_, err := NewService(&fakeRepo{userByNameErr: ErrNotFound}, &fakeSMS{}).ForgotPassword(context.Background(), ForgotPasswordDTO{
|
|
Name: "Berdi Berdiyew",
|
|
})
|
|
if !errors.Is(err, ErrNotFound) {
|
|
t.Fatalf("error = %v, want ErrNotFound", err)
|
|
}
|
|
}
|
|
|
|
func TestForgotPasswordSendsSMS(t *testing.T) {
|
|
repo := &fakeRepo{userByName: UserIdentity{
|
|
Id: 7,
|
|
Name: "Berdi Berdiyew",
|
|
Username: "berdi",
|
|
Phone: "+993 61 92 92 48",
|
|
}}
|
|
sender := &fakeSMS{}
|
|
result, err := NewService(repo, sender).ForgotPassword(context.Background(), ForgotPasswordDTO{
|
|
Name: "Berdi Berdiyew",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("ForgotPassword: %v", err)
|
|
}
|
|
if result.Phone != "+993 61 92 92 48" {
|
|
t.Errorf("phone = %q", result.Phone)
|
|
}
|
|
if sender.phone != result.Phone {
|
|
t.Errorf("sms phone = %q", sender.phone)
|
|
}
|
|
if !strings.Contains(sender.message, "123456") {
|
|
t.Errorf("sms message = %q", sender.message)
|
|
}
|
|
}
|
|
|
|
func TestResetPasswordMismatch(t *testing.T) {
|
|
_, err := NewService(&fakeRepo{}, &fakeSMS{}).ResetPassword(context.Background(), ResetPasswordDTO{
|
|
Name: "Berdi Berdiyew",
|
|
Code: "123456",
|
|
Password: "secret1",
|
|
PasswordConfirm: "other",
|
|
})
|
|
if !errors.Is(err, ErrPasswordMismatch) {
|
|
t.Fatalf("error = %v, want ErrPasswordMismatch", err)
|
|
}
|
|
}
|
|
|
|
func TestResetPasswordWrongCode(t *testing.T) {
|
|
hash, err := bcrypt.GenerateFromPassword([]byte("123456"), bcrypt.MinCost)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
repo := &fakeRepo{
|
|
userByName: UserIdentity{Id: 7, Phone: "+993 61 92 92 48"},
|
|
verification: Verification{
|
|
ID: 1,
|
|
Phone: "+993 61 92 92 48",
|
|
CodeHash: string(hash),
|
|
ExpiresAt: time.Now().UTC().Add(time.Minute),
|
|
},
|
|
}
|
|
_, err = NewService(repo, &fakeSMS{}).ResetPassword(context.Background(), ResetPasswordDTO{
|
|
Name: "Berdi Berdiyew",
|
|
Code: "000000",
|
|
Password: "secret1",
|
|
PasswordConfirm: "secret1",
|
|
})
|
|
if !errors.Is(err, ErrInvalidCode) {
|
|
t.Fatalf("error = %v, want ErrInvalidCode", err)
|
|
}
|
|
}
|