All checks were successful
Build Docker Image / Explore-Gitea-Actions (push) Successful in 51s
110 lines
2.4 KiB
Go
110 lines
2.4 KiB
Go
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")
|
|
verifiedAt := time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC)
|
|
stub := DbAuthStub{
|
|
user: db.NewUser(
|
|
uuid.New(),
|
|
"test@test.de",
|
|
true,
|
|
&verifiedAt,
|
|
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")
|
|
verifiedAt := time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC)
|
|
stub := DbAuthStub{
|
|
user: db.NewUser(
|
|
uuid.New(),
|
|
"test@test.de",
|
|
true,
|
|
&verifiedAt,
|
|
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)
|
|
}
|
|
})
|
|
}
|