fix: missing service tests #181
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"me-fit/db"
|
||||
"me-fit/mocks"
|
||||
"me-fit/types"
|
||||
"strings"
|
||||
|
||||
"errors"
|
||||
"testing"
|
||||
@@ -11,6 +12,7 @@ import (
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestSignIn(t *testing.T) {
|
||||
@@ -35,8 +37,9 @@ func TestSignIn(t *testing.T) {
|
||||
mockDbAuth.EXPECT().GetUser("test@test.de").Return(user, nil)
|
||||
mockRandom := mocks.NewMockRandomGenerator(t)
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
mockMail := mocks.NewMockMailService(t)
|
||||
|
||||
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, &types.ServerSettings{})
|
||||
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
||||
|
||||
actualUser, err := underTest.SignIn(user.Email, "password")
|
||||
assert.Nil(t, err)
|
||||
@@ -70,8 +73,9 @@ func TestSignIn(t *testing.T) {
|
||||
mockDbAuth.EXPECT().GetUser(user.Email).Return(user, nil)
|
||||
mockRandom := mocks.NewMockRandomGenerator(t)
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
mockMail := mocks.NewMockMailService(t)
|
||||
|
||||
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, &types.ServerSettings{})
|
||||
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
||||
|
||||
_, err := underTest.SignIn("test@test.de", "wrong password")
|
||||
|
||||
@@ -84,8 +88,9 @@ func TestSignIn(t *testing.T) {
|
||||
mockDbAuth.EXPECT().GetUser("test").Return(nil, db.ErrUserNotFound)
|
||||
mockRandom := mocks.NewMockRandomGenerator(t)
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
mockMail := mocks.NewMockMailService(t)
|
||||
|
||||
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, &types.ServerSettings{})
|
||||
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
||||
|
||||
_, err := underTest.SignIn("test", "test")
|
||||
assert.Equal(t, ErrInvaidCredentials, err)
|
||||
@@ -97,8 +102,9 @@ func TestSignIn(t *testing.T) {
|
||||
mockDbAuth.EXPECT().GetUser("test").Return(nil, errors.New("Some undefined error"))
|
||||
mockRandom := mocks.NewMockRandomGenerator(t)
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
mockMail := mocks.NewMockMailService(t)
|
||||
|
||||
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, &types.ServerSettings{})
|
||||
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
||||
|
||||
_, err := underTest.SignIn("test", "test")
|
||||
|
||||
@@ -114,8 +120,9 @@ func TestSignUp(t *testing.T) {
|
||||
mockDbAuth := mocks.NewMockDbAuth(t)
|
||||
mockRandom := mocks.NewMockRandomGenerator(t)
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
mockMail := mocks.NewMockMailService(t)
|
||||
|
||||
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, &types.ServerSettings{})
|
||||
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
||||
|
||||
_, err := underTest.SignUp("invalid email address", "SomeStrongPassword123!")
|
||||
|
||||
@@ -127,8 +134,9 @@ func TestSignUp(t *testing.T) {
|
||||
mockDbAuth := mocks.NewMockDbAuth(t)
|
||||
mockRandom := mocks.NewMockRandomGenerator(t)
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
mockMail := mocks.NewMockMailService(t)
|
||||
|
||||
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, &types.ServerSettings{})
|
||||
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
||||
|
||||
weakPasswords := []string{
|
||||
"123!ab", // too short
|
||||
@@ -148,6 +156,7 @@ func TestSignUp(t *testing.T) {
|
||||
mockDbAuth := mocks.NewMockDbAuth(t)
|
||||
mockRandom := mocks.NewMockRandomGenerator(t)
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
mockMail := mocks.NewMockMailService(t)
|
||||
|
||||
expected := User{
|
||||
Id: uuid.New(),
|
||||
@@ -169,7 +178,7 @@ func TestSignUp(t *testing.T) {
|
||||
|
||||
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{})
|
||||
underTest := NewServiceAuthImpl(mockDbAuth, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
||||
|
||||
actual, err := underTest.SignUp(expected.Email, password)
|
||||
|
||||
@@ -177,4 +186,61 @@ func TestSignUp(t *testing.T) {
|
||||
|
||||
assert.Equal(t, expected, *actual)
|
||||
})
|
||||
t.Run("should return ErrAccountExists", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
mockDbAuth := mocks.NewMockDbAuth(t)
|
||||
mockRandom := mocks.NewMockRandomGenerator(t)
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
mockMail := mocks.NewMockMailService(t)
|
||||
|
||||
user := User{
|
||||
Id: uuid.New(),
|
||||
Email: "some@valid.email",
|
||||
}
|
||||
|
||||
random := NewRandomGeneratorImpl()
|
||||
salt, err := random.Bytes(16)
|
||||
assert.Nil(t, err)
|
||||
password := "SomeStrongPassword123!"
|
||||
|
||||
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)
|
||||
|
||||
mockDbAuth.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{})
|
||||
|
||||
_, err = underTest.SignUp(user.Email, password)
|
||||
assert.Equal(t, ErrAccountExists, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSendVerificationMail(t *testing.T) {
|
||||
|
||||
t.Parallel()
|
||||
t.Run("should use stored token and send mail", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
token := "someRandomTokenToUse"
|
||||
email := "some@email.de"
|
||||
userId := uuid.New()
|
||||
|
||||
mockDbAuth := mocks.NewMockDbAuth(t)
|
||||
mockRandom := mocks.NewMockRandomGenerator(t)
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
mockMail := mocks.NewMockMailService(t)
|
||||
|
||||
mockDbAuth.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.SendVerificationMail(userId, email)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user