150 lines
4.1 KiB
Go
150 lines
4.1 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
|
|
}
|
|
|
|
func (f *fakeRepo) Login(context.Context, LoginDTO) (UserCredentials, error) {
|
|
return UserCredentials{}, errors.New("not implemented")
|
|
}
|
|
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
|
|
}
|
|
|
|
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 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)
|
|
}
|
|
}
|