100% test coverage on account service!

This commit is contained in:
ari melody 2026-07-31 04:46:32 +01:00
parent 5a540184c9
commit 8be785cd8b
Signed by: ari
GPG key ID: CF99829C92678188
5 changed files with 76 additions and 31 deletions

View file

@ -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

View file

@ -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