Add forgot-password SMS reset, client/staff user lists, and regenerate Swagger.
Fold follow-up migrations into init, expose GET /users/clients and /users/staff, and refresh seed data. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -16,10 +16,19 @@ type fakeRepo struct {
|
||||
verification Verification
|
||||
getErr error
|
||||
userID int
|
||||
userByName UserIdentity
|
||||
userByNameErr error
|
||||
loginUser UserCredentials
|
||||
loginErr error
|
||||
lastLogin LoginDTO
|
||||
}
|
||||
|
||||
func (f *fakeRepo) Login(context.Context, LoginDTO) (UserCredentials, error) {
|
||||
return UserCredentials{}, errors.New("not implemented")
|
||||
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")
|
||||
@@ -50,6 +59,15 @@ func (f *fakeRepo) IncrementVerifyAttempts(context.Context, int64) error {
|
||||
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
|
||||
@@ -72,6 +90,54 @@ func validRegisterDTO() RegisterDTO {
|
||||
}
|
||||
}
|
||||
|
||||
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"
|
||||
@@ -147,3 +213,81 @@ func TestVerifyExpiredCode(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user