fix: refactor code to be testable #181
All checks were successful
Build and Push Docker Image / Build-And-Push-Docker-Image (push) Successful in 55s
Build Docker Image / Build-Docker-Image (push) Successful in 45s

This commit was merged in pull request #212.
This commit is contained in:
2024-10-12 21:57:39 +02:00
parent 9fd9f9649e
commit 1ed504c49b
19 changed files with 1022 additions and 710 deletions

View File

@@ -4,9 +4,9 @@ import (
"me-fit/db"
"me-fit/mocks"
"me-fit/types"
"strings"
"errors"
"strings"
"testing"
"time"
@@ -33,24 +33,25 @@ func TestSignIn(t *testing.T) {
time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
)
mockDbAuth := mocks.NewMockDbAuth(t)
mockDbAuth.EXPECT().GetUser("test@test.de").Return(user, nil)
mockRandom := mocks.NewMockRandomGenerator(t)
mockClock := mocks.NewMockClock(t)
dbSession := db.NewSession("sessionId", user.Id, time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC))
mockAuthDb := mocks.NewMockAuthDb(t)
mockAuthDb.EXPECT().GetUser("test@test.de").Return(user, nil)
mockAuthDb.EXPECT().DeleteOldSessions(user.Id).Return(nil)
mockAuthDb.EXPECT().InsertSession(dbSession).Return(nil)
mockRandom := mocks.NewMockRandomService(t)
mockRandom.EXPECT().String(32).Return("sessionId", nil)
mockClock := mocks.NewMockClockService(t)
mockClock.EXPECT().Now().Return(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC))
mockMail := mocks.NewMockMailService(t)
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, mockMail, &types.ServerSettings{})
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
actualUser, err := underTest.SignIn(user.Email, "password")
actualSession, err := underTest.SignIn(user.Email, "password")
assert.Nil(t, err)
expectedUser := User{
Id: user.Id,
Email: user.Email,
EmailVerified: user.EmailVerified,
}
assert.Equal(t, expectedUser, *actualUser)
expectedSession := NewSession(dbSession, NewUser(user))
assert.Equal(t, expectedSession, actualSession)
})
t.Run("should return ErrInvalidCretentials if password is not correct", func(t *testing.T) {
@@ -69,13 +70,13 @@ func TestSignIn(t *testing.T) {
time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
)
mockDbAuth := mocks.NewMockDbAuth(t)
mockDbAuth.EXPECT().GetUser(user.Email).Return(user, nil)
mockRandom := mocks.NewMockRandomGenerator(t)
mockClock := mocks.NewMockClock(t)
mockAuthDb := mocks.NewMockAuthDb(t)
mockAuthDb.EXPECT().GetUser(user.Email).Return(user, nil)
mockRandom := mocks.NewMockRandomService(t)
mockClock := mocks.NewMockClockService(t)
mockMail := mocks.NewMockMailService(t)
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, mockMail, &types.ServerSettings{})
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
_, err := underTest.SignIn("test@test.de", "wrong password")
@@ -84,13 +85,13 @@ func TestSignIn(t *testing.T) {
t.Run("should return ErrInvalidCretentials if user has not been found", func(t *testing.T) {
t.Parallel()
mockDbAuth := mocks.NewMockDbAuth(t)
mockDbAuth.EXPECT().GetUser("test").Return(nil, db.ErrUserNotFound)
mockRandom := mocks.NewMockRandomGenerator(t)
mockClock := mocks.NewMockClock(t)
mockAuthDb := mocks.NewMockAuthDb(t)
mockAuthDb.EXPECT().GetUser("test").Return(nil, db.ErrUserNotFound)
mockRandom := mocks.NewMockRandomService(t)
mockClock := mocks.NewMockClockService(t)
mockMail := mocks.NewMockMailService(t)
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, mockMail, &types.ServerSettings{})
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
_, err := underTest.SignIn("test", "test")
assert.Equal(t, ErrInvaidCredentials, err)
@@ -98,13 +99,13 @@ func TestSignIn(t *testing.T) {
t.Run("should forward ErrInternal on any other error", func(t *testing.T) {
t.Parallel()
mockDbAuth := mocks.NewMockDbAuth(t)
mockDbAuth.EXPECT().GetUser("test").Return(nil, errors.New("Some undefined error"))
mockRandom := mocks.NewMockRandomGenerator(t)
mockClock := mocks.NewMockClock(t)
mockAuthDb := mocks.NewMockAuthDb(t)
mockAuthDb.EXPECT().GetUser("test").Return(nil, errors.New("Some undefined error"))
mockRandom := mocks.NewMockRandomService(t)
mockClock := mocks.NewMockClockService(t)
mockMail := mocks.NewMockMailService(t)
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, mockMail, &types.ServerSettings{})
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
_, err := underTest.SignIn("test", "test")
@@ -117,12 +118,12 @@ func TestSignUp(t *testing.T) {
t.Run("should check for correct email address", func(t *testing.T) {
t.Parallel()
mockDbAuth := mocks.NewMockDbAuth(t)
mockRandom := mocks.NewMockRandomGenerator(t)
mockClock := mocks.NewMockClock(t)
mockAuthDb := mocks.NewMockAuthDb(t)
mockRandom := mocks.NewMockRandomService(t)
mockClock := mocks.NewMockClockService(t)
mockMail := mocks.NewMockMailService(t)
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, mockMail, &types.ServerSettings{})
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
_, err := underTest.SignUp("invalid email address", "SomeStrongPassword123!")
@@ -131,12 +132,12 @@ func TestSignUp(t *testing.T) {
t.Run("should check for password complexity", func(t *testing.T) {
t.Parallel()
mockDbAuth := mocks.NewMockDbAuth(t)
mockRandom := mocks.NewMockRandomGenerator(t)
mockClock := mocks.NewMockClock(t)
mockAuthDb := mocks.NewMockAuthDb(t)
mockRandom := mocks.NewMockRandomService(t)
mockClock := mocks.NewMockClockService(t)
mockMail := mocks.NewMockMailService(t)
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, mockMail, &types.ServerSettings{})
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
weakPasswords := []string{
"123!ab", // too short
@@ -153,9 +154,9 @@ func TestSignUp(t *testing.T) {
t.Run("should signup correctly", func(t *testing.T) {
t.Parallel()
mockDbAuth := mocks.NewMockDbAuth(t)
mockRandom := mocks.NewMockRandomGenerator(t)
mockClock := mocks.NewMockClock(t)
mockAuthDb := mocks.NewMockAuthDb(t)
mockRandom := mocks.NewMockRandomService(t)
mockClock := mocks.NewMockClockService(t)
mockMail := mocks.NewMockMailService(t)
expected := User{
@@ -164,7 +165,7 @@ func TestSignUp(t *testing.T) {
EmailVerified: false,
}
random := NewRandomGeneratorImpl()
random := NewRandomServiceImpl()
salt, err := random.Bytes(16)
assert.Nil(t, err)
password := "SomeStrongPassword123!"
@@ -176,9 +177,9 @@ func TestSignUp(t *testing.T) {
mockClock.EXPECT().Now().Return(createTime)
mockDbAuth.EXPECT().InsertUser(db.NewUser(expected.Id, expected.Email, false, nil, false, GetHashPassword(password, salt), salt, createTime)).Return(nil)
mockAuthDb.EXPECT().InsertUser(db.NewUser(expected.Id, expected.Email, false, nil, false, GetHashPassword(password, salt), salt, createTime)).Return(nil)
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, mockMail, &types.ServerSettings{})
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
actual, err := underTest.SignUp(expected.Email, password)
@@ -189,9 +190,9 @@ func TestSignUp(t *testing.T) {
t.Run("should return ErrAccountExists", func(t *testing.T) {
t.Parallel()
mockDbAuth := mocks.NewMockDbAuth(t)
mockRandom := mocks.NewMockRandomGenerator(t)
mockClock := mocks.NewMockClock(t)
mockAuthDb := mocks.NewMockAuthDb(t)
mockRandom := mocks.NewMockRandomService(t)
mockClock := mocks.NewMockClockService(t)
mockMail := mocks.NewMockMailService(t)
user := User{
@@ -199,7 +200,7 @@ func TestSignUp(t *testing.T) {
Email: "some@valid.email",
}
random := NewRandomGeneratorImpl()
random := NewRandomServiceImpl()
salt, err := random.Bytes(16)
assert.Nil(t, err)
password := "SomeStrongPassword123!"
@@ -211,9 +212,9 @@ func TestSignUp(t *testing.T) {
mockClock.EXPECT().Now().Return(createTime)
mockDbAuth.EXPECT().InsertUser(db.NewUser(user.Id, user.Email, false, nil, false, GetHashPassword(password, salt), salt, createTime)).Return(db.ErrUserExists)
mockAuthDb.EXPECT().InsertUser(db.NewUser(user.Id, user.Email, false, nil, false, GetHashPassword(password, salt), salt, createTime)).Return(db.ErrUserExists)
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, mockMail, &types.ServerSettings{})
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
_, err = underTest.SignUp(user.Email, password)
assert.Equal(t, ErrAccountExists, err)
@@ -230,16 +231,16 @@ func TestSendVerificationMail(t *testing.T) {
email := "some@email.de"
userId := uuid.New()
mockDbAuth := mocks.NewMockDbAuth(t)
mockRandom := mocks.NewMockRandomGenerator(t)
mockClock := mocks.NewMockClock(t)
mockAuthDb := mocks.NewMockAuthDb(t)
mockRandom := mocks.NewMockRandomService(t)
mockClock := mocks.NewMockClockService(t)
mockMail := mocks.NewMockMailService(t)
mockDbAuth.EXPECT().GetEmailVerificationToken(userId).Return(token, nil)
mockAuthDb.EXPECT().GetEmailVerificationToken(userId).Return(token, nil)
mockMail.EXPECT().SendMail(email, "Welcome to ME-FIT", mock.MatchedBy(func(message string) bool { return strings.Contains(message, token) })).Return(nil)
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, mockMail, &types.ServerSettings{})
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
underTest.SendVerificationMail(userId, email)
})