fix: new test and extract time.Now to mockable Clock #181
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
||||
)
|
||||
|
||||
func TestSignIn(t *testing.T) {
|
||||
|
||||
t.Parallel()
|
||||
t.Run("should return user if password is correct", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
@@ -33,8 +34,9 @@ func TestSignIn(t *testing.T) {
|
||||
mockDbAuth := mocks.NewMockDbAuth(t)
|
||||
mockDbAuth.EXPECT().GetUser("test@test.de").Return(user, nil)
|
||||
mockRandom := mocks.NewMockRandomGenerator(t)
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
|
||||
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, &types.ServerSettings{})
|
||||
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, &types.ServerSettings{})
|
||||
|
||||
actualUser, err := underTest.SignIn(user.Email, "password")
|
||||
assert.Nil(t, err)
|
||||
@@ -67,8 +69,9 @@ func TestSignIn(t *testing.T) {
|
||||
mockDbAuth := mocks.NewMockDbAuth(t)
|
||||
mockDbAuth.EXPECT().GetUser(user.Email).Return(user, nil)
|
||||
mockRandom := mocks.NewMockRandomGenerator(t)
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
|
||||
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, &types.ServerSettings{})
|
||||
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, &types.ServerSettings{})
|
||||
|
||||
_, err := underTest.SignIn("test@test.de", "wrong password")
|
||||
|
||||
@@ -80,8 +83,9 @@ func TestSignIn(t *testing.T) {
|
||||
mockDbAuth := mocks.NewMockDbAuth(t)
|
||||
mockDbAuth.EXPECT().GetUser("test").Return(nil, db.ErrUserNotFound)
|
||||
mockRandom := mocks.NewMockRandomGenerator(t)
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
|
||||
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, &types.ServerSettings{})
|
||||
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, &types.ServerSettings{})
|
||||
|
||||
_, err := underTest.SignIn("test", "test")
|
||||
assert.Equal(t, ErrInvaidCredentials, err)
|
||||
@@ -92,8 +96,9 @@ func TestSignIn(t *testing.T) {
|
||||
mockDbAuth := mocks.NewMockDbAuth(t)
|
||||
mockDbAuth.EXPECT().GetUser("test").Return(nil, errors.New("Some undefined error"))
|
||||
mockRandom := mocks.NewMockRandomGenerator(t)
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
|
||||
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, &types.ServerSettings{})
|
||||
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, &types.ServerSettings{})
|
||||
|
||||
_, err := underTest.SignIn("test", "test")
|
||||
|
||||
@@ -108,8 +113,9 @@ func TestSignUp(t *testing.T) {
|
||||
|
||||
mockDbAuth := mocks.NewMockDbAuth(t)
|
||||
mockRandom := mocks.NewMockRandomGenerator(t)
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
|
||||
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, &types.ServerSettings{})
|
||||
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, &types.ServerSettings{})
|
||||
|
||||
_, err := underTest.SignUp("invalid email address", "SomeStrongPassword123!")
|
||||
|
||||
@@ -120,8 +126,9 @@ func TestSignUp(t *testing.T) {
|
||||
|
||||
mockDbAuth := mocks.NewMockDbAuth(t)
|
||||
mockRandom := mocks.NewMockRandomGenerator(t)
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
|
||||
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, &types.ServerSettings{})
|
||||
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, &types.ServerSettings{})
|
||||
|
||||
weakPasswords := []string{
|
||||
"123!ab", // too short
|
||||
@@ -135,4 +142,39 @@ func TestSignUp(t *testing.T) {
|
||||
assert.Equal(t, ErrInvalidPassword, err)
|
||||
}
|
||||
})
|
||||
t.Run("should signup correctly", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
mockDbAuth := mocks.NewMockDbAuth(t)
|
||||
mockRandom := mocks.NewMockRandomGenerator(t)
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
|
||||
expected := User{
|
||||
Id: uuid.New(),
|
||||
Email: "some@valid.email",
|
||||
EmailVerified: false,
|
||||
}
|
||||
|
||||
random := NewRandomGeneratorImpl()
|
||||
salt, err := random.Bytes(16)
|
||||
assert.Nil(t, err)
|
||||
password := "SomeStrongPassword123!"
|
||||
|
||||
mockRandom.EXPECT().UUID().Return(expected.Id, nil)
|
||||
mockRandom.EXPECT().Bytes(16).Return(salt, nil)
|
||||
|
||||
createTime := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
|
||||
mockClock.EXPECT().Now().Return(createTime)
|
||||
|
||||
mockDbAuth.EXPECT().InsertUser(db.NewUser(expected.Id, expected.Email, false, nil, false, GetHashPassword(password, salt), salt, createTime)).Return(nil)
|
||||
|
||||
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, &types.ServerSettings{})
|
||||
|
||||
actual, err := underTest.SignUp(expected.Email, password)
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Equal(t, expected, *actual)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user