100% test coverage on account service!
This commit is contained in:
parent
5a540184c9
commit
8be785cd8b
5 changed files with 76 additions and 31 deletions
10
main.go
10
main.go
|
|
@ -306,15 +306,7 @@ func main() {
|
|||
}
|
||||
account.Password = string(hashedPassword)
|
||||
|
||||
var email *string = nil
|
||||
if account.Email.Valid { email = &account.Email.String }
|
||||
var avatarURL *string = nil
|
||||
if account.AvatarURL.Valid { email = &account.AvatarURL.String }
|
||||
if err = app.AccountService.Update(
|
||||
account.ID,
|
||||
username, string(hashedPassword),
|
||||
email, avatarURL,
|
||||
); err != nil {
|
||||
if err = app.AccountService.ChangePassword(account.ID, string(hashedPassword)); err != nil {
|
||||
logger.Fatalf("FATAL: Failed to update password: %v\n", err)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,8 +16,7 @@ type AccountRepository interface {
|
|||
// Create an account, returning the new account ID.
|
||||
Create(username string, password string, email *string, avatarURL *string) (string, error)
|
||||
|
||||
// Intended for large profile updates. For smaller adjusments,
|
||||
// more specialised Change* and Remove* functions should be used.
|
||||
// Deprecated in favour of more specialised Change* and Remove* functions.
|
||||
Update(id string, username string, password string, email *string, avatarUrl *string) error
|
||||
ChangeUsername(id string, username string) error
|
||||
ChangePassword(id string, password string) error
|
||||
|
|
|
|||
|
|
@ -99,20 +99,27 @@ func (repo *AccountRepositoryMemory) ChangeUsername(id string, username string)
|
|||
} else if account != nil && account.ID != id {
|
||||
return errors.New("Account with this username already exists")
|
||||
}
|
||||
|
||||
account, err := repo.GetByID(id)
|
||||
if err != nil { return err }
|
||||
if account == nil { return errors.New("Account does not exist") }
|
||||
|
||||
account.Username = username
|
||||
return nil
|
||||
}
|
||||
func (repo *AccountRepositoryMemory) ChangePassword(id string, password string) error {
|
||||
account, err := repo.GetByID(id)
|
||||
if err != nil { return err }
|
||||
if account == nil { return errors.New("Account does not exist") }
|
||||
|
||||
account.Password = password
|
||||
return nil
|
||||
}
|
||||
func (repo *AccountRepositoryMemory) ChangeEmail(id string, email string) error {
|
||||
account, err := repo.GetByID(id)
|
||||
if err != nil { return err }
|
||||
if account == nil { return errors.New("Account does not exist") }
|
||||
|
||||
account.Email.Valid = true
|
||||
account.Email.String = email
|
||||
return nil
|
||||
|
|
@ -120,6 +127,8 @@ func (repo *AccountRepositoryMemory) ChangeEmail(id string, email string) error
|
|||
func (repo *AccountRepositoryMemory) RemoveEmail(id string) error {
|
||||
account, err := repo.GetByID(id)
|
||||
if err != nil { return err }
|
||||
if account == nil { return errors.New("Account does not exist") }
|
||||
|
||||
account.Email.Valid = false
|
||||
account.Email.String = ""
|
||||
return nil
|
||||
|
|
@ -127,6 +136,8 @@ func (repo *AccountRepositoryMemory) RemoveEmail(id string) error {
|
|||
func (repo *AccountRepositoryMemory) ChangeAvatarURL(id string, avatarURL string) error {
|
||||
account, err := repo.GetByID(id)
|
||||
if err != nil { return err }
|
||||
if account == nil { return errors.New("Account does not exist") }
|
||||
|
||||
account.AvatarURL.Valid = true
|
||||
account.AvatarURL.String = avatarURL
|
||||
return nil
|
||||
|
|
@ -134,6 +145,8 @@ func (repo *AccountRepositoryMemory) ChangeAvatarURL(id string, avatarURL string
|
|||
func (repo *AccountRepositoryMemory) RemoveAvatar(id string) error {
|
||||
account, err := repo.GetByID(id)
|
||||
if err != nil { return err }
|
||||
if account == nil { return errors.New("Account does not exist") }
|
||||
|
||||
account.AvatarURL.Valid = false
|
||||
account.AvatarURL.String = ""
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -60,26 +60,6 @@ func (s *AccountService) Create(
|
|||
return id, nil
|
||||
}
|
||||
|
||||
// Intended for large profile updates. For smaller adjusments,
|
||||
// more specialised Change* and Remove* functions should be used.
|
||||
func (s *AccountService) Update(
|
||||
id string,
|
||||
username string,
|
||||
password string,
|
||||
email *string,
|
||||
avatarUrl *string,
|
||||
) error {
|
||||
if len(username) == 0 { return errors.New("Username cannot be empty") }
|
||||
if len(password) == 0 { return errors.New("Password cannot be empty") }
|
||||
if email != nil && len(*email) == 0 { return errors.New("Email cannot be empty") }
|
||||
|
||||
if err := s.repo.Update(id, username, password, email, avatarUrl); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.log.Printf("Updated account '%s' (%s)", username, id)
|
||||
return nil
|
||||
}
|
||||
func (s *AccountService) ChangeUsername(id string, username string) error {
|
||||
if len(username) == 0 { return errors.New("Username cannot be empty") }
|
||||
if err := s.repo.ChangeUsername(id, username); err != nil {
|
||||
|
|
|
|||
|
|
@ -149,6 +149,12 @@ func Test_Account(t *testing.T) {
|
|||
} else if account.Username != testUsername {
|
||||
t.Error("Username did not update")
|
||||
}
|
||||
|
||||
t.Run("but not to an invalid value", func(t *testing.T) {
|
||||
if err := service.ChangeUsername(id, ""); err == nil {
|
||||
t.Error("Could change username to invalid value")
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("can change password", func(t *testing.T) {
|
||||
|
|
@ -164,6 +170,12 @@ func Test_Account(t *testing.T) {
|
|||
} else if account.Password != testPassword {
|
||||
t.Error("Password did not update")
|
||||
}
|
||||
|
||||
t.Run("but not to an invalid value", func(t *testing.T) {
|
||||
if err := service.ChangePassword(id, ""); err == nil {
|
||||
t.Error("Could change password to invalid value")
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("can change email", func(t *testing.T) {
|
||||
|
|
@ -294,6 +306,55 @@ func Test_Account(t *testing.T) {
|
|||
}
|
||||
})
|
||||
|
||||
t.Run("can't create an account with invalid", func(t *testing.T) {
|
||||
t.Run("username", func(t *testing.T) {
|
||||
if _, err := service.Create("", password, &email, &avatarURL); err == nil {
|
||||
t.Error("Could create account with empty username")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("password", func(t *testing.T) {
|
||||
if _, err := service.Create(username, "", &email, &avatarURL); err == nil {
|
||||
t.Error("Could create account with empty password")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("email", func(t *testing.T) {
|
||||
testEmail := ""
|
||||
if _, err := service.Create(username, password, &testEmail, &avatarURL); err == nil {
|
||||
t.Error("Could create account with empty (non-nil) email")
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("can't update account that doesn't exist", func(t *testing.T) {
|
||||
garbageAccountID := "adsginh534g9405gmb40i9bm"
|
||||
|
||||
t.Run("username", func(t *testing.T) {
|
||||
if err := service.ChangeUsername(garbageAccountID, "some-username"); err == nil {
|
||||
t.Error("Could update non-existent account's username")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("password", func(t *testing.T) {
|
||||
if err := service.ChangePassword(garbageAccountID, "some-password"); err == nil {
|
||||
t.Error("Could update non-existent account's password")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("email", func(t *testing.T) {
|
||||
if err := service.ChangeEmail(garbageAccountID, "some-email@real.gov"); err == nil {
|
||||
t.Error("Could update non-existent account's email")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("avatar URL", func(t *testing.T) {
|
||||
if err := service.ChangeAvatarURL(garbageAccountID, "/img/null.webp"); err == nil {
|
||||
t.Error("Could update non-existent account's avatar URL")
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
t.Cleanup(func() {
|
||||
if err := service.Delete(id); err != nil {
|
||||
t.Errorf("Failed to clean up test case: %v", err)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue