100% test coverage on account service!
This commit is contained in:
parent
5a540184c9
commit
8be785cd8b
5 changed files with 76 additions and 31 deletions
|
|
@ -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