feat: #337 unify types for auth module
This commit was merged in pull request #338.
This commit is contained in:
@@ -22,7 +22,7 @@ func TestSignIn(t *testing.T) {
|
||||
t.Parallel()
|
||||
salt := []byte("salt")
|
||||
verifiedAt := time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC)
|
||||
user := db.NewUser(
|
||||
user := types.NewUser(
|
||||
uuid.New(),
|
||||
"test@test.de",
|
||||
true,
|
||||
@@ -33,12 +33,12 @@ func TestSignIn(t *testing.T) {
|
||||
time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||
)
|
||||
|
||||
dbSession := db.NewSession("sessionId", user.Id, time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC), time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC))
|
||||
session := types.NewSession("sessionId", user.Id, time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC), time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC))
|
||||
|
||||
mockAuthDb := mocks.NewMockAuth(t)
|
||||
mockAuthDb.EXPECT().GetUserByEmail("test@test.de").Return(user, nil)
|
||||
mockAuthDb.EXPECT().DeleteOldSessions(user.Id).Return(nil)
|
||||
mockAuthDb.EXPECT().InsertSession(dbSession).Return(nil)
|
||||
mockAuthDb.EXPECT().InsertSession(session).Return(nil)
|
||||
mockRandom := mocks.NewMockRandom(t)
|
||||
mockRandom.EXPECT().String(32).Return("sessionId", nil)
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
@@ -47,11 +47,11 @@ func TestSignIn(t *testing.T) {
|
||||
|
||||
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||
|
||||
actualSession, err := underTest.SignIn(user.Email, "password")
|
||||
actualSession, actualUser, err := underTest.SignIn(user.Email, "password")
|
||||
assert.Nil(t, err)
|
||||
|
||||
expectedSession := NewSession(dbSession, NewUser(user))
|
||||
assert.Equal(t, expectedSession, actualSession)
|
||||
assert.Equal(t, session, actualSession)
|
||||
assert.Equal(t, user, actualUser)
|
||||
})
|
||||
|
||||
t.Run("should return ErrInvalidCretentials if password is not correct", func(t *testing.T) {
|
||||
@@ -59,7 +59,7 @@ func TestSignIn(t *testing.T) {
|
||||
salt := []byte("salt")
|
||||
verifiedAt := time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC)
|
||||
|
||||
user := db.NewUser(
|
||||
user := types.NewUser(
|
||||
uuid.New(),
|
||||
"test@test.de",
|
||||
true,
|
||||
@@ -78,7 +78,7 @@ func TestSignIn(t *testing.T) {
|
||||
|
||||
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||
|
||||
_, err := underTest.SignIn("test@test.de", "wrong password")
|
||||
_, _, err := underTest.SignIn("test@test.de", "wrong password")
|
||||
|
||||
assert.Equal(t, ErrInvalidCredentials, err)
|
||||
})
|
||||
@@ -93,7 +93,7 @@ func TestSignIn(t *testing.T) {
|
||||
|
||||
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||
|
||||
_, err := underTest.SignIn("test", "test")
|
||||
_, _, err := underTest.SignIn("test", "test")
|
||||
assert.Equal(t, ErrInvalidCredentials, err)
|
||||
})
|
||||
t.Run("should forward ErrInternal on any other error", func(t *testing.T) {
|
||||
@@ -107,7 +107,7 @@ func TestSignIn(t *testing.T) {
|
||||
|
||||
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||
|
||||
_, err := underTest.SignIn("test", "test")
|
||||
_, _, err := underTest.SignIn("test", "test")
|
||||
|
||||
assert.Equal(t, types.ErrInternal, err)
|
||||
})
|
||||
@@ -159,33 +159,25 @@ func TestSignUp(t *testing.T) {
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
mockMail := mocks.NewMockMail(t)
|
||||
|
||||
expected := User{
|
||||
Id: uuid.New(),
|
||||
Email: "some@valid.email",
|
||||
EmailVerified: false,
|
||||
}
|
||||
|
||||
random := NewRandomImpl()
|
||||
salt, err := random.Bytes(16)
|
||||
assert.Nil(t, err)
|
||||
userId := uuid.New()
|
||||
email := "mail@mail.de"
|
||||
password := "SomeStrongPassword123!"
|
||||
|
||||
mockRandom.EXPECT().UUID().Return(expected.Id, nil)
|
||||
mockRandom.EXPECT().Bytes(16).Return(salt, nil)
|
||||
|
||||
salt := []byte("salt")
|
||||
createTime := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
|
||||
mockClock.EXPECT().Now().Return(createTime)
|
||||
expected := types.NewUser(userId, email, false, nil, false, GetHashPassword(password, salt), salt, createTime)
|
||||
|
||||
mockAuthDb.EXPECT().InsertUser(db.NewUser(expected.Id, expected.Email, false, nil, false, GetHashPassword(password, salt), salt, createTime)).Return(nil)
|
||||
mockRandom.EXPECT().UUID().Return(userId, nil)
|
||||
mockRandom.EXPECT().Bytes(16).Return(salt, nil)
|
||||
mockClock.EXPECT().Now().Return(createTime)
|
||||
mockAuthDb.EXPECT().InsertUser(expected).Return(nil)
|
||||
|
||||
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||
|
||||
actual, err := underTest.SignUp(expected.Email, password)
|
||||
actual, err := underTest.SignUp(email, password)
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Equal(t, expected, *actual)
|
||||
assert.Equal(t, expected, actual)
|
||||
})
|
||||
t.Run("should return ErrAccountExists", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
@@ -195,28 +187,22 @@ func TestSignUp(t *testing.T) {
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
mockMail := mocks.NewMockMail(t)
|
||||
|
||||
user := User{
|
||||
Id: uuid.New(),
|
||||
Email: "some@valid.email",
|
||||
}
|
||||
|
||||
random := NewRandomImpl()
|
||||
salt, err := random.Bytes(16)
|
||||
assert.Nil(t, err)
|
||||
userId := uuid.New()
|
||||
email := "some@valid.email"
|
||||
createTime := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
password := "SomeStrongPassword123!"
|
||||
salt := []byte("salt")
|
||||
user := types.NewUser(userId, email, false, nil, false, GetHashPassword(password, salt), salt, createTime)
|
||||
|
||||
mockRandom.EXPECT().UUID().Return(user.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)
|
||||
|
||||
mockAuthDb.EXPECT().InsertUser(db.NewUser(user.Id, user.Email, false, nil, false, GetHashPassword(password, salt), salt, createTime)).Return(db.ErrAlreadyExists)
|
||||
mockAuthDb.EXPECT().InsertUser(user).Return(db.ErrAlreadyExists)
|
||||
|
||||
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||
|
||||
_, err = underTest.SignUp(user.Email, password)
|
||||
_, err := underTest.SignUp(user.Email, password)
|
||||
assert.Equal(t, ErrAccountExists, err)
|
||||
})
|
||||
}
|
||||
@@ -227,8 +213,8 @@ func TestSendVerificationMail(t *testing.T) {
|
||||
t.Run("should use stored token and send mail", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
token := db.NewToken(uuid.New(), "sessionId", "someRandomTokenToUse", db.TokenTypeEmailVerify, time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC), time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC))
|
||||
tokens := []*db.Token{token}
|
||||
token := types.NewToken(uuid.New(), "sessionId", "someRandomTokenToUse", types.TokenTypeEmailVerify, time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC), time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC))
|
||||
tokens := []*types.Token{token}
|
||||
|
||||
email := "some@email.de"
|
||||
userId := uuid.New()
|
||||
@@ -238,7 +224,7 @@ func TestSendVerificationMail(t *testing.T) {
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
mockMail := mocks.NewMockMail(t)
|
||||
|
||||
mockAuthDb.EXPECT().GetTokensByUserIdAndType(userId, db.TokenTypeEmailVerify).Return(tokens, nil)
|
||||
mockAuthDb.EXPECT().GetTokensByUserIdAndType(userId, types.TokenTypeEmailVerify).Return(tokens, nil)
|
||||
|
||||
mockMail.EXPECT().SendMail(email, "Welcome to ME-FIT", mock.MatchedBy(func(message string) bool {
|
||||
return strings.Contains(message, token.Token)
|
||||
|
||||
Reference in New Issue
Block a user