fix: create RandomGenerator interface and struct for testing purpose #181

This commit is contained in:
2024-10-05 13:49:43 +02:00
parent d36f880a01
commit 6b033e2c2e
6 changed files with 125 additions and 55 deletions

View File

@@ -32,8 +32,9 @@ func TestSignIn(t *testing.T) {
mockDbAuth := mocks.NewMockDbAuth(t)
mockDbAuth.EXPECT().GetUser("test@test.de").Return(user, nil)
mockRandom := mocks.NewMockRandomGenerator(t)
underTest := NewServiceAuthImpl(mockDbAuth, &types.ServerSettings{})
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, &types.ServerSettings{})
actualUser, err := underTest.SignIn(user.Email, "password")
assert.Nil(t, err)
@@ -65,8 +66,9 @@ func TestSignIn(t *testing.T) {
mockDbAuth := mocks.NewMockDbAuth(t)
mockDbAuth.EXPECT().GetUser(user.Email).Return(user, nil)
mockRandom := mocks.NewMockRandomGenerator(t)
underTest := NewServiceAuthImpl(mockDbAuth, &types.ServerSettings{})
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, &types.ServerSettings{})
_, err := underTest.SignIn("test@test.de", "wrong password")
@@ -77,8 +79,9 @@ func TestSignIn(t *testing.T) {
mockDbAuth := mocks.NewMockDbAuth(t)
mockDbAuth.EXPECT().GetUser("test").Return(nil, db.ErrUserNotFound)
mockRandom := mocks.NewMockRandomGenerator(t)
underTest := NewServiceAuthImpl(mockDbAuth, &types.ServerSettings{})
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, &types.ServerSettings{})
_, err := underTest.SignIn("test", "test")
assert.Equal(t, ErrInvaidCredentials, err)
@@ -88,11 +91,48 @@ func TestSignIn(t *testing.T) {
mockDbAuth := mocks.NewMockDbAuth(t)
mockDbAuth.EXPECT().GetUser("test").Return(nil, errors.New("Some undefined error"))
mockRandom := mocks.NewMockRandomGenerator(t)
underTest := NewServiceAuthImpl(mockDbAuth, &types.ServerSettings{})
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, &types.ServerSettings{})
_, err := underTest.SignIn("test", "test")
assert.Equal(t, types.ErrInternal, err)
})
}
func TestSignUp(t *testing.T) {
t.Parallel()
t.Run("should check for correct email address", func(t *testing.T) {
t.Parallel()
mockDbAuth := mocks.NewMockDbAuth(t)
mockRandom := mocks.NewMockRandomGenerator(t)
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, &types.ServerSettings{})
_, err := underTest.SignUp("invalid email address", "SomeStrongPassword123!")
assert.Equal(t, ErrInvalidEmail, err)
})
t.Run("should check for password complexity", func(t *testing.T) {
t.Parallel()
mockDbAuth := mocks.NewMockDbAuth(t)
mockRandom := mocks.NewMockRandomGenerator(t)
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, &types.ServerSettings{})
weakPasswords := []string{
"123!ab", // too short
"no_upper_case_123",
"NO_LOWER_CASE_123",
"noSpecialChar123",
}
for _, password := range weakPasswords {
_, err := underTest.SignUp("some@valid.email", password)
assert.Equal(t, ErrInvalidPassword, err)
}
})
}