chore: add sign in tests
All checks were successful
Build and Push Docker Image / Explore-Gitea-Actions (push) Successful in 54s

This commit was merged in pull request #188.
This commit is contained in:
2024-09-27 12:33:52 +02:00
committed by Tim Wundenberg
parent 5d479b0811
commit da05ba5b56
3 changed files with 115 additions and 7 deletions

View File

@@ -51,9 +51,9 @@ type ServiceAuthImpl struct {
dbAuth db.DbAuth
}
func NewServiceAuthImpl(d *sql.DB) *ServiceAuthImpl {
func NewServiceAuthImpl(dbAuth db.DbAuth) *ServiceAuthImpl {
return &ServiceAuthImpl{
dbAuth: db.NewDbAuthSqlite(d),
dbAuth: dbAuth,
}
}
@@ -64,7 +64,7 @@ func (service ServiceAuthImpl) SignIn(email string, password string) (*User, err
if errors.Is(err, db.ErrUserNotFound) {
return nil, ErrInvaidCredentials
} else {
return nil, err
return nil, types.ErrInternal
}
}

107
service/auth_test.go Normal file
View File

@@ -0,0 +1,107 @@
package service
import (
"me-fit/db"
"me-fit/types"
"errors"
"testing"
"time"
"github.com/google/uuid"
)
type DbAuthStub struct {
user *db.User
err error
}
func (d DbAuthStub) GetUser(email string) (*db.User, error) {
return d.user, d.err
}
func TestSignIn(t *testing.T) {
t.Parallel()
t.Run("should return user if password is correct", func(t *testing.T) {
t.Parallel()
salt := []byte("salt")
stub := DbAuthStub{
user: db.NewUser(
uuid.New(),
"test@test.de",
true,
time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC),
false,
getHashPassword("password", salt),
salt,
time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
),
err: nil,
}
underTest := NewServiceAuthImpl(stub)
actualUser, err := underTest.SignIn("test@test.de", "password")
if err != nil {
t.Errorf("Expected nil, got %v", err)
}
expectedUser := User{
Id: stub.user.Id,
Email: stub.user.Email,
EmailVerified: stub.user.EmailVerified,
}
if *actualUser != expectedUser {
t.Errorf("Expected %v, got %v", expectedUser, actualUser)
}
})
t.Run("should return ErrInvalidCretentials if password is not correct", func(t *testing.T) {
t.Parallel()
salt := []byte("salt")
stub := DbAuthStub{
user: db.NewUser(
uuid.New(),
"test@test.de",
true,
time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC),
false,
getHashPassword("password", salt),
salt,
time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
),
err: nil,
}
underTest := NewServiceAuthImpl(stub)
_, err := underTest.SignIn("test@test.de", "wrong password")
if err != ErrInvaidCredentials {
t.Errorf("Expected %v, got %v", ErrInvaidCredentials, err)
}
})
t.Run("should return ErrInvalidCretentials if user has not been found", func(t *testing.T) {
t.Parallel()
stub := DbAuthStub{
user: nil,
err: db.ErrUserNotFound,
}
underTest := NewServiceAuthImpl(stub)
_, err := underTest.SignIn("test", "test")
if err != ErrInvaidCredentials {
t.Errorf("Expected %v, got %v", ErrInvaidCredentials, err)
}
})
t.Run("should forward ErrInternal on any other error", func(t *testing.T) {
t.Parallel()
stub := DbAuthStub{
user: nil,
err: errors.New("Some error"),
}
underTest := NewServiceAuthImpl(stub)
_, err := underTest.SignIn("test", "test")
if err != types.ErrInternal {
t.Errorf("Expected %v, got %v", types.ErrInternal, err)
}
})
}