fix: remove redundante names
This commit was merged in pull request #299.
This commit is contained in:
110
service/auth.go
110
service/auth.go
@@ -53,7 +53,7 @@ func NewSession(session *db.Session, user *User) *Session {
|
||||
}
|
||||
}
|
||||
|
||||
type AuthService interface {
|
||||
type Auth interface {
|
||||
SignUp(email string, password string) (*User, error)
|
||||
SendVerificationMail(userId uuid.UUID, email string)
|
||||
VerifyUserEmail(token string) error
|
||||
@@ -71,26 +71,26 @@ type AuthService interface {
|
||||
GetUserFromSessionId(sessionId string) (*User, error)
|
||||
}
|
||||
|
||||
type AuthServiceImpl struct {
|
||||
dbAuth db.AuthDb
|
||||
randomGenerator RandomService
|
||||
clock ClockService
|
||||
mailService MailService
|
||||
serverSettings *types.ServerSettings
|
||||
type AuthImpl struct {
|
||||
db db.Auth
|
||||
random Random
|
||||
clock Clock
|
||||
mail Mail
|
||||
serverSettings *types.Settings
|
||||
}
|
||||
|
||||
func NewAuthServiceImpl(dbAuth db.AuthDb, randomGenerator RandomService, clock ClockService, mailService MailService, serverSettings *types.ServerSettings) *AuthServiceImpl {
|
||||
return &AuthServiceImpl{
|
||||
dbAuth: dbAuth,
|
||||
randomGenerator: randomGenerator,
|
||||
clock: clock,
|
||||
mailService: mailService,
|
||||
serverSettings: serverSettings,
|
||||
func NewAuthImpl(db db.Auth, random Random, clock Clock, mail Mail, serverSettings *types.Settings) *AuthImpl {
|
||||
return &AuthImpl{
|
||||
db: db,
|
||||
random: random,
|
||||
clock: clock,
|
||||
mail: mail,
|
||||
serverSettings: serverSettings,
|
||||
}
|
||||
}
|
||||
|
||||
func (service AuthServiceImpl) SignIn(email string, password string) (*Session, error) {
|
||||
user, err := service.dbAuth.GetUserByEmail(email)
|
||||
func (service AuthImpl) SignIn(email string, password string) (*Session, error) {
|
||||
user, err := service.db.GetUserByEmail(email)
|
||||
if err != nil {
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
return nil, ErrInvaidCredentials
|
||||
@@ -113,13 +113,13 @@ func (service AuthServiceImpl) SignIn(email string, password string) (*Session,
|
||||
return NewSession(session, NewUser(user)), nil
|
||||
}
|
||||
|
||||
func (service AuthServiceImpl) createSession(userId uuid.UUID) (*db.Session, error) {
|
||||
sessionId, err := service.randomGenerator.String(32)
|
||||
func (service AuthImpl) createSession(userId uuid.UUID) (*db.Session, error) {
|
||||
sessionId, err := service.random.String(32)
|
||||
if err != nil {
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
|
||||
err = service.dbAuth.DeleteOldSessions(userId)
|
||||
err = service.db.DeleteOldSessions(userId)
|
||||
|
||||
if err != nil {
|
||||
return nil, types.ErrInternal
|
||||
@@ -127,7 +127,7 @@ func (service AuthServiceImpl) createSession(userId uuid.UUID) (*db.Session, err
|
||||
|
||||
session := db.NewSession(sessionId, userId, service.clock.Now())
|
||||
|
||||
err = service.dbAuth.InsertSession(session)
|
||||
err = service.db.InsertSession(session)
|
||||
if err != nil {
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
@@ -135,7 +135,7 @@ func (service AuthServiceImpl) createSession(userId uuid.UUID) (*db.Session, err
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func (service AuthServiceImpl) SignUp(email string, password string) (*User, error) {
|
||||
func (service AuthImpl) SignUp(email string, password string) (*User, error) {
|
||||
_, err := mail.ParseAddress(email)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidEmail
|
||||
@@ -145,12 +145,12 @@ func (service AuthServiceImpl) SignUp(email string, password string) (*User, err
|
||||
return nil, ErrInvalidPassword
|
||||
}
|
||||
|
||||
userId, err := service.randomGenerator.UUID()
|
||||
userId, err := service.random.UUID()
|
||||
if err != nil {
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
|
||||
salt, err := service.randomGenerator.Bytes(16)
|
||||
salt, err := service.random.Bytes(16)
|
||||
if err != nil {
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
@@ -159,7 +159,7 @@ func (service AuthServiceImpl) SignUp(email string, password string) (*User, err
|
||||
|
||||
dbUser := db.NewUser(userId, email, false, nil, false, hash, salt, service.clock.Now())
|
||||
|
||||
err = service.dbAuth.InsertUser(dbUser)
|
||||
err = service.db.InsertUser(dbUser)
|
||||
if err != nil {
|
||||
if err == db.ErrUserExists {
|
||||
return nil, ErrAccountExists
|
||||
@@ -171,9 +171,9 @@ func (service AuthServiceImpl) SignUp(email string, password string) (*User, err
|
||||
return NewUser(dbUser), nil
|
||||
}
|
||||
|
||||
func (service AuthServiceImpl) SendVerificationMail(userId uuid.UUID, email string) {
|
||||
func (service AuthImpl) SendVerificationMail(userId uuid.UUID, email string) {
|
||||
|
||||
tokens, err := service.dbAuth.GetTokensByUserIdAndType(userId, db.TokenTypeEmailVerify)
|
||||
tokens, err := service.db.GetTokensByUserIdAndType(userId, db.TokenTypeEmailVerify)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -185,14 +185,14 @@ func (service AuthServiceImpl) SendVerificationMail(userId uuid.UUID, email stri
|
||||
}
|
||||
|
||||
if token == nil {
|
||||
newTokenStr, err := service.randomGenerator.String(32)
|
||||
newTokenStr, err := service.random.String(32)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
token = db.NewToken(userId, newTokenStr, db.TokenTypeEmailVerify, service.clock.Now(), service.clock.Now().Add(24*time.Hour))
|
||||
|
||||
err = service.dbAuth.InsertToken(token)
|
||||
err = service.db.InsertToken(token)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -205,21 +205,21 @@ func (service AuthServiceImpl) SendVerificationMail(userId uuid.UUID, email stri
|
||||
return
|
||||
}
|
||||
|
||||
service.mailService.SendMail(email, "Welcome to ME-FIT", w.String())
|
||||
service.mail.SendMail(email, "Welcome to ME-FIT", w.String())
|
||||
}
|
||||
|
||||
func (service AuthServiceImpl) VerifyUserEmail(tokenStr string) error {
|
||||
func (service AuthImpl) VerifyUserEmail(tokenStr string) error {
|
||||
|
||||
if tokenStr == "" {
|
||||
return types.ErrInternal
|
||||
}
|
||||
|
||||
token, err := service.dbAuth.GetToken(tokenStr)
|
||||
token, err := service.db.GetToken(tokenStr)
|
||||
if err != nil {
|
||||
return types.ErrInternal
|
||||
}
|
||||
|
||||
user, err := service.dbAuth.GetUser(token.UserId)
|
||||
user, err := service.db.GetUser(token.UserId)
|
||||
if err != nil {
|
||||
return types.ErrInternal
|
||||
}
|
||||
@@ -237,31 +237,31 @@ func (service AuthServiceImpl) VerifyUserEmail(tokenStr string) error {
|
||||
user.EmailVerified = true
|
||||
user.EmailVerifiedAt = &now
|
||||
|
||||
err = service.dbAuth.UpdateUser(user)
|
||||
err = service.db.UpdateUser(user)
|
||||
if err != nil {
|
||||
return types.ErrInternal
|
||||
}
|
||||
|
||||
_ = service.dbAuth.DeleteToken(token.Token)
|
||||
_ = service.db.DeleteToken(token.Token)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (service AuthServiceImpl) SignOut(sessionId string) error {
|
||||
func (service AuthImpl) SignOut(sessionId string) error {
|
||||
|
||||
return service.dbAuth.DeleteSession(sessionId)
|
||||
return service.db.DeleteSession(sessionId)
|
||||
}
|
||||
|
||||
func (service AuthServiceImpl) GetUserFromSessionId(sessionId string) (*User, error) {
|
||||
func (service AuthImpl) GetUserFromSessionId(sessionId string) (*User, error) {
|
||||
if sessionId == "" {
|
||||
return nil, ErrSessionIdInvalid
|
||||
}
|
||||
|
||||
session, err := service.dbAuth.GetSession(sessionId)
|
||||
session, err := service.db.GetSession(sessionId)
|
||||
if err != nil {
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
|
||||
user, err := service.dbAuth.GetUser(session.UserId)
|
||||
user, err := service.db.GetUser(session.UserId)
|
||||
if err != nil {
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
@@ -273,19 +273,19 @@ func (service AuthServiceImpl) GetUserFromSessionId(sessionId string) (*User, er
|
||||
}
|
||||
}
|
||||
|
||||
func (service AuthServiceImpl) DeleteAccount(user *User) error {
|
||||
func (service AuthImpl) DeleteAccount(user *User) error {
|
||||
|
||||
err := service.dbAuth.DeleteUser(user.Id)
|
||||
err := service.db.DeleteUser(user.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
go service.mailService.SendMail(user.Email, "Account deleted", "Your account has been deleted")
|
||||
go service.mail.SendMail(user.Email, "Account deleted", "Your account has been deleted")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (service AuthServiceImpl) ChangePassword(user *User, currPass, newPass string) error {
|
||||
func (service AuthImpl) ChangePassword(user *User, currPass, newPass string) error {
|
||||
|
||||
if !isPasswordValid(newPass) {
|
||||
return ErrInvalidPassword
|
||||
@@ -300,7 +300,7 @@ func (service AuthServiceImpl) ChangePassword(user *User, currPass, newPass stri
|
||||
return err
|
||||
}
|
||||
|
||||
userDb, err := service.dbAuth.GetUser(user.Id)
|
||||
userDb, err := service.db.GetUser(user.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -309,7 +309,7 @@ func (service AuthServiceImpl) ChangePassword(user *User, currPass, newPass stri
|
||||
|
||||
userDb.Password = newHash
|
||||
|
||||
err = service.dbAuth.UpdateUser(userDb)
|
||||
err = service.db.UpdateUser(userDb)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -317,14 +317,14 @@ func (service AuthServiceImpl) ChangePassword(user *User, currPass, newPass stri
|
||||
return nil
|
||||
}
|
||||
|
||||
func (service AuthServiceImpl) SendForgotPasswordMail(email string) error {
|
||||
func (service AuthImpl) SendForgotPasswordMail(email string) error {
|
||||
|
||||
tokenStr, err := service.randomGenerator.String(32)
|
||||
tokenStr, err := service.random.String(32)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
user, err := service.dbAuth.GetUserByEmail(email)
|
||||
user, err := service.db.GetUserByEmail(email)
|
||||
if err != nil {
|
||||
if err == db.ErrNotFound {
|
||||
return nil
|
||||
@@ -335,7 +335,7 @@ func (service AuthServiceImpl) SendForgotPasswordMail(email string) error {
|
||||
|
||||
token := db.NewToken(user.Id, tokenStr, db.TokenTypePasswordReset, service.clock.Now(), service.clock.Now().Add(15*time.Minute))
|
||||
|
||||
err = service.dbAuth.InsertToken(token)
|
||||
err = service.db.InsertToken(token)
|
||||
if err != nil {
|
||||
return types.ErrInternal
|
||||
}
|
||||
@@ -346,28 +346,28 @@ func (service AuthServiceImpl) SendForgotPasswordMail(email string) error {
|
||||
log.Error("Could not render reset password email: %v", err)
|
||||
return types.ErrInternal
|
||||
}
|
||||
go service.mailService.SendMail(email, "Reset Password", mail.String())
|
||||
go service.mail.SendMail(email, "Reset Password", mail.String())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (service AuthServiceImpl) ForgotPassword(tokenStr string, newPass string) error {
|
||||
func (service AuthImpl) ForgotPassword(tokenStr string, newPass string) error {
|
||||
|
||||
if !isPasswordValid(newPass) {
|
||||
return ErrInvalidPassword
|
||||
}
|
||||
|
||||
token, err := service.dbAuth.GetToken(tokenStr)
|
||||
token, err := service.db.GetToken(tokenStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = service.dbAuth.DeleteToken(tokenStr)
|
||||
err = service.db.DeleteToken(tokenStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
user, err := service.dbAuth.GetUser(token.UserId)
|
||||
user, err := service.db.GetUser(token.UserId)
|
||||
if err != nil {
|
||||
log.Error("Could not get user from token: %v", err)
|
||||
return types.ErrInternal
|
||||
@@ -376,7 +376,7 @@ func (service AuthServiceImpl) ForgotPassword(tokenStr string, newPass string) e
|
||||
passHash := GetHashPassword(newPass, user.Salt)
|
||||
|
||||
user.Password = passHash
|
||||
err = service.dbAuth.UpdateUser(user)
|
||||
err = service.db.UpdateUser(user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -35,17 +35,17 @@ func TestSignIn(t *testing.T) {
|
||||
|
||||
dbSession := db.NewSession("sessionId", user.Id, time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC))
|
||||
|
||||
mockAuthDb := mocks.NewMockAuthDb(t)
|
||||
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)
|
||||
mockRandom := mocks.NewMockRandomService(t)
|
||||
mockRandom := mocks.NewMockRandom(t)
|
||||
mockRandom.EXPECT().String(32).Return("sessionId", nil)
|
||||
mockClock := mocks.NewMockClockService(t)
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
mockClock.EXPECT().Now().Return(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC))
|
||||
mockMail := mocks.NewMockMailService(t)
|
||||
mockMail := mocks.NewMockMail(t)
|
||||
|
||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
||||
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||
|
||||
actualSession, err := underTest.SignIn(user.Email, "password")
|
||||
assert.Nil(t, err)
|
||||
@@ -70,13 +70,13 @@ func TestSignIn(t *testing.T) {
|
||||
time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||
)
|
||||
|
||||
mockAuthDb := mocks.NewMockAuthDb(t)
|
||||
mockAuthDb := mocks.NewMockAuth(t)
|
||||
mockAuthDb.EXPECT().GetUserByEmail(user.Email).Return(user, nil)
|
||||
mockRandom := mocks.NewMockRandomService(t)
|
||||
mockClock := mocks.NewMockClockService(t)
|
||||
mockMail := mocks.NewMockMailService(t)
|
||||
mockRandom := mocks.NewMockRandom(t)
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
mockMail := mocks.NewMockMail(t)
|
||||
|
||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
||||
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||
|
||||
_, err := underTest.SignIn("test@test.de", "wrong password")
|
||||
|
||||
@@ -85,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()
|
||||
|
||||
mockAuthDb := mocks.NewMockAuthDb(t)
|
||||
mockAuthDb := mocks.NewMockAuth(t)
|
||||
mockAuthDb.EXPECT().GetUserByEmail("test").Return(nil, db.ErrNotFound)
|
||||
mockRandom := mocks.NewMockRandomService(t)
|
||||
mockClock := mocks.NewMockClockService(t)
|
||||
mockMail := mocks.NewMockMailService(t)
|
||||
mockRandom := mocks.NewMockRandom(t)
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
mockMail := mocks.NewMockMail(t)
|
||||
|
||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
||||
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||
|
||||
_, err := underTest.SignIn("test", "test")
|
||||
assert.Equal(t, ErrInvaidCredentials, err)
|
||||
@@ -99,13 +99,13 @@ func TestSignIn(t *testing.T) {
|
||||
t.Run("should forward ErrInternal on any other error", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
mockAuthDb := mocks.NewMockAuthDb(t)
|
||||
mockAuthDb := mocks.NewMockAuth(t)
|
||||
mockAuthDb.EXPECT().GetUserByEmail("test").Return(nil, errors.New("Some undefined error"))
|
||||
mockRandom := mocks.NewMockRandomService(t)
|
||||
mockClock := mocks.NewMockClockService(t)
|
||||
mockMail := mocks.NewMockMailService(t)
|
||||
mockRandom := mocks.NewMockRandom(t)
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
mockMail := mocks.NewMockMail(t)
|
||||
|
||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
||||
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||
|
||||
_, err := underTest.SignIn("test", "test")
|
||||
|
||||
@@ -118,12 +118,12 @@ func TestSignUp(t *testing.T) {
|
||||
t.Run("should check for correct email address", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
mockAuthDb := mocks.NewMockAuthDb(t)
|
||||
mockRandom := mocks.NewMockRandomService(t)
|
||||
mockClock := mocks.NewMockClockService(t)
|
||||
mockMail := mocks.NewMockMailService(t)
|
||||
mockAuthDb := mocks.NewMockAuth(t)
|
||||
mockRandom := mocks.NewMockRandom(t)
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
mockMail := mocks.NewMockMail(t)
|
||||
|
||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
||||
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||
|
||||
_, err := underTest.SignUp("invalid email address", "SomeStrongPassword123!")
|
||||
|
||||
@@ -132,12 +132,12 @@ func TestSignUp(t *testing.T) {
|
||||
t.Run("should check for password complexity", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
mockAuthDb := mocks.NewMockAuthDb(t)
|
||||
mockRandom := mocks.NewMockRandomService(t)
|
||||
mockClock := mocks.NewMockClockService(t)
|
||||
mockMail := mocks.NewMockMailService(t)
|
||||
mockAuthDb := mocks.NewMockAuth(t)
|
||||
mockRandom := mocks.NewMockRandom(t)
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
mockMail := mocks.NewMockMail(t)
|
||||
|
||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
||||
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||
|
||||
weakPasswords := []string{
|
||||
"123!ab", // too short
|
||||
@@ -154,10 +154,10 @@ func TestSignUp(t *testing.T) {
|
||||
t.Run("should signup correctly", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
mockAuthDb := mocks.NewMockAuthDb(t)
|
||||
mockRandom := mocks.NewMockRandomService(t)
|
||||
mockClock := mocks.NewMockClockService(t)
|
||||
mockMail := mocks.NewMockMailService(t)
|
||||
mockAuthDb := mocks.NewMockAuth(t)
|
||||
mockRandom := mocks.NewMockRandom(t)
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
mockMail := mocks.NewMockMail(t)
|
||||
|
||||
expected := User{
|
||||
Id: uuid.New(),
|
||||
@@ -165,7 +165,7 @@ func TestSignUp(t *testing.T) {
|
||||
EmailVerified: false,
|
||||
}
|
||||
|
||||
random := NewRandomServiceImpl()
|
||||
random := NewRandomImpl()
|
||||
salt, err := random.Bytes(16)
|
||||
assert.Nil(t, err)
|
||||
password := "SomeStrongPassword123!"
|
||||
@@ -179,7 +179,7 @@ func TestSignUp(t *testing.T) {
|
||||
|
||||
mockAuthDb.EXPECT().InsertUser(db.NewUser(expected.Id, expected.Email, false, nil, false, GetHashPassword(password, salt), salt, createTime)).Return(nil)
|
||||
|
||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
||||
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||
|
||||
actual, err := underTest.SignUp(expected.Email, password)
|
||||
|
||||
@@ -190,17 +190,17 @@ func TestSignUp(t *testing.T) {
|
||||
t.Run("should return ErrAccountExists", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
mockAuthDb := mocks.NewMockAuthDb(t)
|
||||
mockRandom := mocks.NewMockRandomService(t)
|
||||
mockClock := mocks.NewMockClockService(t)
|
||||
mockMail := mocks.NewMockMailService(t)
|
||||
mockAuthDb := mocks.NewMockAuth(t)
|
||||
mockRandom := mocks.NewMockRandom(t)
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
mockMail := mocks.NewMockMail(t)
|
||||
|
||||
user := User{
|
||||
Id: uuid.New(),
|
||||
Email: "some@valid.email",
|
||||
}
|
||||
|
||||
random := NewRandomServiceImpl()
|
||||
random := NewRandomImpl()
|
||||
salt, err := random.Bytes(16)
|
||||
assert.Nil(t, err)
|
||||
password := "SomeStrongPassword123!"
|
||||
@@ -214,7 +214,7 @@ func TestSignUp(t *testing.T) {
|
||||
|
||||
mockAuthDb.EXPECT().InsertUser(db.NewUser(user.Id, user.Email, false, nil, false, GetHashPassword(password, salt), salt, createTime)).Return(db.ErrUserExists)
|
||||
|
||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
||||
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||
|
||||
_, err = underTest.SignUp(user.Email, password)
|
||||
assert.Equal(t, ErrAccountExists, err)
|
||||
@@ -233,10 +233,10 @@ func TestSendVerificationMail(t *testing.T) {
|
||||
email := "some@email.de"
|
||||
userId := uuid.New()
|
||||
|
||||
mockAuthDb := mocks.NewMockAuthDb(t)
|
||||
mockRandom := mocks.NewMockRandomService(t)
|
||||
mockClock := mocks.NewMockClockService(t)
|
||||
mockMail := mocks.NewMockMailService(t)
|
||||
mockAuthDb := mocks.NewMockAuth(t)
|
||||
mockRandom := mocks.NewMockRandom(t)
|
||||
mockClock := mocks.NewMockClock(t)
|
||||
mockMail := mocks.NewMockMail(t)
|
||||
|
||||
mockAuthDb.EXPECT().GetTokensByUserIdAndType(userId, db.TokenTypeEmailVerify).Return(tokens, nil)
|
||||
|
||||
@@ -244,7 +244,7 @@ func TestSendVerificationMail(t *testing.T) {
|
||||
return strings.Contains(message, token.Token)
|
||||
})).Return()
|
||||
|
||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
||||
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||
|
||||
underTest.SendVerificationMail(userId, email)
|
||||
})
|
||||
|
||||
@@ -2,16 +2,16 @@ package service
|
||||
|
||||
import "time"
|
||||
|
||||
type ClockService interface {
|
||||
type Clock interface {
|
||||
Now() time.Time
|
||||
}
|
||||
|
||||
type ClockServiceImpl struct{}
|
||||
type ClockImpl struct{}
|
||||
|
||||
func NewClockServiceImpl() ClockService {
|
||||
return &ClockServiceImpl{}
|
||||
func NewClockImpl() Clock {
|
||||
return &ClockImpl{}
|
||||
}
|
||||
|
||||
func (c *ClockServiceImpl) Now() time.Time {
|
||||
func (c *ClockImpl) Now() time.Time {
|
||||
return time.Now()
|
||||
}
|
||||
|
||||
@@ -8,25 +8,25 @@ import (
|
||||
"net/smtp"
|
||||
)
|
||||
|
||||
type MailService interface {
|
||||
type Mail interface {
|
||||
// Sending an email is a fire and forget operation. Thus no error handling
|
||||
SendMail(to string, subject string, message string)
|
||||
}
|
||||
|
||||
type MailServiceImpl struct {
|
||||
serverSettings *types.ServerSettings
|
||||
type MailImpl struct {
|
||||
server *types.Settings
|
||||
}
|
||||
|
||||
func NewMailServiceImpl(serverSettings *types.ServerSettings) MailServiceImpl {
|
||||
return MailServiceImpl{serverSettings: serverSettings}
|
||||
func NewMailImpl(server *types.Settings) MailImpl {
|
||||
return MailImpl{server: server}
|
||||
}
|
||||
|
||||
func (m MailServiceImpl) SendMail(to string, subject string, message string) {
|
||||
if m.serverSettings.Smtp == nil {
|
||||
func (m MailImpl) SendMail(to string, subject string, message string) {
|
||||
if m.server.Smtp == nil {
|
||||
return
|
||||
}
|
||||
|
||||
s := m.serverSettings.Smtp
|
||||
s := m.server.Smtp
|
||||
|
||||
auth := smtp.PlainAuth("", s.User, s.Pass, s.Host)
|
||||
|
||||
|
||||
@@ -10,20 +10,20 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type RandomService interface {
|
||||
type Random interface {
|
||||
Bytes(size int) ([]byte, error)
|
||||
String(size int) (string, error)
|
||||
UUID() (uuid.UUID, error)
|
||||
}
|
||||
|
||||
type RandomServiceImpl struct {
|
||||
type RandomImpl struct {
|
||||
}
|
||||
|
||||
func NewRandomServiceImpl() *RandomServiceImpl {
|
||||
return &RandomServiceImpl{}
|
||||
func NewRandomImpl() *RandomImpl {
|
||||
return &RandomImpl{}
|
||||
}
|
||||
|
||||
func (r *RandomServiceImpl) Bytes(size int) ([]byte, error) {
|
||||
func (r *RandomImpl) Bytes(size int) ([]byte, error) {
|
||||
b := make([]byte, 32)
|
||||
_, err := rand.Read(b)
|
||||
if err != nil {
|
||||
@@ -34,7 +34,7 @@ func (r *RandomServiceImpl) Bytes(size int) ([]byte, error) {
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func (r *RandomServiceImpl) String(size int) (string, error) {
|
||||
func (r *RandomImpl) String(size int) (string, error) {
|
||||
bytes, err := r.Bytes(size)
|
||||
if err != nil {
|
||||
return "", types.ErrInternal
|
||||
@@ -43,6 +43,6 @@ func (r *RandomServiceImpl) String(size int) (string, error) {
|
||||
return base64.StdEncoding.EncodeToString(bytes), nil
|
||||
}
|
||||
|
||||
func (r *RandomServiceImpl) UUID() (uuid.UUID, error) {
|
||||
func (r *RandomImpl) UUID() (uuid.UUID, error) {
|
||||
return uuid.NewRandom()
|
||||
}
|
||||
|
||||
@@ -9,27 +9,27 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type WorkoutService interface {
|
||||
type Workout interface {
|
||||
AddWorkout(user *User, workoutDto *WorkoutDto) (*WorkoutDto, error)
|
||||
DeleteWorkout(user *User, rowId int) error
|
||||
GetWorkouts(user *User) ([]*WorkoutDto, error)
|
||||
}
|
||||
|
||||
type WorkoutServiceImpl struct {
|
||||
dbWorkout db.WorkoutDb
|
||||
randomGenerator RandomService
|
||||
clock ClockService
|
||||
mailService MailService
|
||||
serverSettings *types.ServerSettings
|
||||
type WorkoutImpl struct {
|
||||
db db.WorkoutDb
|
||||
random Random
|
||||
clock Clock
|
||||
mail Mail
|
||||
settings *types.Settings
|
||||
}
|
||||
|
||||
func NewWorkoutServiceImpl(dbWorkout db.WorkoutDb, randomGenerator RandomService, clock ClockService, mailService MailService, serverSettings *types.ServerSettings) WorkoutService {
|
||||
return WorkoutServiceImpl{
|
||||
dbWorkout: dbWorkout,
|
||||
randomGenerator: randomGenerator,
|
||||
clock: clock,
|
||||
mailService: mailService,
|
||||
serverSettings: serverSettings,
|
||||
func NewWorkoutImpl(db db.WorkoutDb, random Random, clock Clock, mail Mail, settings *types.Settings) Workout {
|
||||
return WorkoutImpl{
|
||||
db: db,
|
||||
random: random,
|
||||
clock: clock,
|
||||
mail: mail,
|
||||
settings: settings,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ var (
|
||||
ErrInputValues = errors.New("invalid input values")
|
||||
)
|
||||
|
||||
func (service WorkoutServiceImpl) AddWorkout(user *User, workoutDto *WorkoutDto) (*WorkoutDto, error) {
|
||||
func (service WorkoutImpl) AddWorkout(user *User, workoutDto *WorkoutDto) (*WorkoutDto, error) {
|
||||
|
||||
if workoutDto.Date == "" || workoutDto.Type == "" || workoutDto.Sets == "" || workoutDto.Reps == "" {
|
||||
return nil, ErrInputValues
|
||||
@@ -87,7 +87,7 @@ func (service WorkoutServiceImpl) AddWorkout(user *User, workoutDto *WorkoutDto)
|
||||
|
||||
workoutInsert := db.NewWorkoutInsert(date, workoutDto.Type, sets, reps)
|
||||
|
||||
workout, err := service.dbWorkout.InsertWorkout(user.Id, workoutInsert)
|
||||
workout, err := service.db.InsertWorkout(user.Id, workoutInsert)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -95,20 +95,20 @@ func (service WorkoutServiceImpl) AddWorkout(user *User, workoutDto *WorkoutDto)
|
||||
return NewWorkoutDtoFromDb(workout), nil
|
||||
}
|
||||
|
||||
func (service WorkoutServiceImpl) DeleteWorkout(user *User, rowId int) error {
|
||||
func (service WorkoutImpl) DeleteWorkout(user *User, rowId int) error {
|
||||
if user == nil {
|
||||
return types.ErrInternal
|
||||
}
|
||||
|
||||
return service.dbWorkout.DeleteWorkout(user.Id, rowId)
|
||||
return service.db.DeleteWorkout(user.Id, rowId)
|
||||
}
|
||||
|
||||
func (service WorkoutServiceImpl) GetWorkouts(user *User) ([]*WorkoutDto, error) {
|
||||
func (service WorkoutImpl) GetWorkouts(user *User) ([]*WorkoutDto, error) {
|
||||
if user == nil {
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
|
||||
workouts, err := service.dbWorkout.GetWorkouts(user.Id)
|
||||
workouts, err := service.db.GetWorkouts(user.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user