chore: add sign in tests
All checks were successful
Build and Push Docker Image / Explore-Gitea-Actions (push) Successful in 54s
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:
107
service/auth_test.go
Normal file
107
service/auth_test.go
Normal 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user