HEAVY: finish music service migration, tidy up services, more tests
This commit is contained in:
parent
9e311df462
commit
90a671982c
47 changed files with 2698 additions and 902 deletions
|
|
@ -1,16 +1,19 @@
|
|||
package account
|
||||
package account_test
|
||||
|
||||
import (
|
||||
"arimelody-web/model"
|
||||
repository "arimelody-web/repository/account"
|
||||
service "arimelody-web/service/account"
|
||||
"arimelody-web/errors"
|
||||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
"gotest.tools/v3/assert"
|
||||
|
||||
"gotest.tools/v3/assert"
|
||||
)
|
||||
|
||||
var (
|
||||
service *AccountService
|
||||
s *service.AccountService
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
|
@ -19,7 +22,7 @@ func init() {
|
|||
defer devNullFile.Close()
|
||||
|
||||
repo := repository.NewAccountRepositoryMemory(make([]*model.Account, 0))
|
||||
service = NewAccountService(
|
||||
s = service.NewAccountService(
|
||||
repo,
|
||||
log.New(devNullFile, "", model.DEFAULT_LOG_FLAGS),
|
||||
)
|
||||
|
|
@ -41,14 +44,14 @@ func Test_Account(t *testing.T) {
|
|||
username := "testificate"
|
||||
password := "the amazing digital data breach"
|
||||
email := "goober@arimelody.space"
|
||||
avatarURL := "/img/default-avatar.webp"
|
||||
avatarURL := "/img/account-avatar.webp"
|
||||
|
||||
var id string
|
||||
var err error
|
||||
|
||||
t.Run("accounts should start empty", func(t *testing.T) {
|
||||
t.Run("count is zero", func(t *testing.T) {
|
||||
if num, err := service.GetCount(); err != nil {
|
||||
if num, err := s.GetCount(); err != nil {
|
||||
t.Errorf("Failed to get number of accounts: %v", err)
|
||||
} else {
|
||||
assert.Equal(t, num, 0)
|
||||
|
|
@ -56,8 +59,8 @@ func Test_Account(t *testing.T) {
|
|||
})
|
||||
|
||||
t.Run("service returns empty array", func(t *testing.T) {
|
||||
if accounts, err := service.GetAll(); err != nil {
|
||||
t.Errorf("Failed to get number of accounts: %v", err)
|
||||
if accounts, err := s.GetAll(); err != nil {
|
||||
t.Errorf("Failed to get accounts: %v", err)
|
||||
} else {
|
||||
assert.Equal(t, len(accounts), 0)
|
||||
}
|
||||
|
|
@ -65,13 +68,28 @@ func Test_Account(t *testing.T) {
|
|||
})
|
||||
|
||||
t.Run("can create account", func(t *testing.T) {
|
||||
id, err = service.Create(username, password, &email, &avatarURL)
|
||||
id, err = s.Create(username, password, &email, &avatarURL)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to create account: %v", err)
|
||||
}
|
||||
|
||||
t.Run("but not with invalid username", func(t *testing.T) {
|
||||
if _, err := s.Create("", password, &email, &avatarURL); err == nil {
|
||||
t.Error("Could create account with invalid username")
|
||||
} else if !errors.IsValidationError(err) {
|
||||
t.Error("Error is not validation error")
|
||||
}
|
||||
})
|
||||
t.Run("but not with invalid password", func(t *testing.T) {
|
||||
if _, err := s.Create("test-username", "", &email, &avatarURL); err == nil {
|
||||
t.Error("Could create account with invalid password")
|
||||
} else if !errors.IsValidationError(err) {
|
||||
t.Error("Error is not validation error")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("and fetch by ID", func(t *testing.T) {
|
||||
account, err := service.GetByID(id)
|
||||
account, err := s.GetByID(id)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to get account after creation: %v", err)
|
||||
}
|
||||
|
|
@ -84,7 +102,7 @@ func Test_Account(t *testing.T) {
|
|||
assert.Equal(t, account.Locked, false)
|
||||
})
|
||||
t.Run("and fetch by username", func(t *testing.T) {
|
||||
account, err := service.GetByUsername(username)
|
||||
account, err := s.GetByUsername(username)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to get account after creation: %v", err)
|
||||
}
|
||||
|
|
@ -97,7 +115,7 @@ func Test_Account(t *testing.T) {
|
|||
assert.Equal(t, account.Locked, false)
|
||||
})
|
||||
t.Run("and fetch by email", func(t *testing.T) {
|
||||
account, err := service.GetByEmail(email)
|
||||
account, err := s.GetByEmail(email)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to get account after creation: %v", err)
|
||||
}
|
||||
|
|
@ -113,7 +131,7 @@ func Test_Account(t *testing.T) {
|
|||
|
||||
t.Run("number of accounts should increment", func(t *testing.T) {
|
||||
t.Run("count is one", func(t *testing.T) {
|
||||
if num, err := service.GetCount(); err != nil {
|
||||
if num, err := s.GetCount(); err != nil {
|
||||
t.Errorf("Failed to get number of accounts: %v", err)
|
||||
} else {
|
||||
assert.Equal(t, num, 1)
|
||||
|
|
@ -121,8 +139,8 @@ func Test_Account(t *testing.T) {
|
|||
})
|
||||
|
||||
t.Run("service returns array with one account", func(t *testing.T) {
|
||||
if accounts, err := service.GetAll(); err != nil {
|
||||
t.Errorf("Failed to get number of accounts: %v", err)
|
||||
if accounts, err := s.GetAll(); err != nil {
|
||||
t.Errorf("Failed to get accounts: %v", err)
|
||||
} else {
|
||||
assert.Equal(t, len(accounts), 1)
|
||||
}
|
||||
|
|
@ -130,7 +148,7 @@ func Test_Account(t *testing.T) {
|
|||
})
|
||||
|
||||
t.Run("can't create duplicate account", func(t *testing.T) {
|
||||
_, err := service.Create(username, password, &email, &avatarURL)
|
||||
_, err := s.Create(username, password, &email, &avatarURL)
|
||||
if err == nil {
|
||||
t.Error("Duplicate account was created")
|
||||
}
|
||||
|
|
@ -138,11 +156,11 @@ func Test_Account(t *testing.T) {
|
|||
|
||||
t.Run("can change username", func(t *testing.T) {
|
||||
testUsername := "some_other_name"
|
||||
if err := service.ChangeUsername(id, testUsername); err != nil {
|
||||
if err := s.ChangeUsername(id, testUsername); err != nil {
|
||||
t.Errorf("Failed to change username: %v", err)
|
||||
}
|
||||
|
||||
if account, err := service.GetByID(id); err != nil {
|
||||
if account, err := s.GetByID(id); err != nil {
|
||||
t.Errorf("Failed to get account: %v", err)
|
||||
} else if account == nil {
|
||||
t.Error("Account is nil after update")
|
||||
|
|
@ -151,19 +169,21 @@ func Test_Account(t *testing.T) {
|
|||
}
|
||||
|
||||
t.Run("but not to an invalid value", func(t *testing.T) {
|
||||
if err := service.ChangeUsername(id, ""); err == nil {
|
||||
if err := s.ChangeUsername(id, ""); err == nil {
|
||||
t.Error("Could change username to invalid value")
|
||||
} else if !errors.IsValidationError(err) {
|
||||
t.Error("Error is not validation error")
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("can change password", func(t *testing.T) {
|
||||
testPassword := "other more different password"
|
||||
if err := service.ChangePassword(id, testPassword); err != nil {
|
||||
if err := s.ChangePassword(id, testPassword); err != nil {
|
||||
t.Errorf("Failed to change password: %v", err)
|
||||
}
|
||||
|
||||
if account, err := service.GetByID(id); err != nil {
|
||||
if account, err := s.GetByID(id); err != nil {
|
||||
t.Errorf("Failed to get account: %v", err)
|
||||
} else if account == nil {
|
||||
t.Error("Account is nil after update")
|
||||
|
|
@ -172,19 +192,21 @@ func Test_Account(t *testing.T) {
|
|||
}
|
||||
|
||||
t.Run("but not to an invalid value", func(t *testing.T) {
|
||||
if err := service.ChangePassword(id, ""); err == nil {
|
||||
if err := s.ChangePassword(id, ""); err == nil {
|
||||
t.Error("Could change password to invalid value")
|
||||
} else if !errors.IsValidationError(err) {
|
||||
t.Error("Error is not validation error")
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("can change email", func(t *testing.T) {
|
||||
testEmail := "brandnewemail@for.me"
|
||||
if err := service.ChangeEmail(id, testEmail); err != nil {
|
||||
if err := s.ChangeEmail(id, testEmail); err != nil {
|
||||
t.Errorf("Failed to change email: %v", err)
|
||||
}
|
||||
|
||||
if account, err := service.GetByID(id); err != nil {
|
||||
if account, err := s.GetByID(id); err != nil {
|
||||
t.Errorf("Failed to get account: %v", err)
|
||||
} else if account == nil {
|
||||
t.Error("Account is nil after update")
|
||||
|
|
@ -194,11 +216,11 @@ func Test_Account(t *testing.T) {
|
|||
})
|
||||
|
||||
t.Run("can remove email", func(t *testing.T) {
|
||||
if err := service.ChangeEmail(id, ""); err != nil {
|
||||
if err := s.ChangeEmail(id, ""); err != nil {
|
||||
t.Errorf("Failed to change email: %v", err)
|
||||
}
|
||||
|
||||
if account, err := service.GetByID(id); err != nil {
|
||||
if account, err := s.GetByID(id); err != nil {
|
||||
t.Errorf("Failed to get account: %v", err)
|
||||
} else if account == nil {
|
||||
t.Error("Account is nil after update")
|
||||
|
|
@ -209,11 +231,11 @@ func Test_Account(t *testing.T) {
|
|||
|
||||
t.Run("can change avatar URL", func(t *testing.T) {
|
||||
testAvatarURL := "/img/some-other-avatar.webp"
|
||||
if err := service.ChangeAvatarURL(id, testAvatarURL); err != nil {
|
||||
if err := s.ChangeAvatarURL(id, testAvatarURL); err != nil {
|
||||
t.Errorf("Failed to change avatar URL: %v", err)
|
||||
}
|
||||
|
||||
if account, err := service.GetByID(id); err != nil {
|
||||
if account, err := s.GetByID(id); err != nil {
|
||||
t.Errorf("Failed to get account: %v", err)
|
||||
} else if account == nil {
|
||||
t.Error("Account is nil after update")
|
||||
|
|
@ -223,11 +245,11 @@ func Test_Account(t *testing.T) {
|
|||
})
|
||||
|
||||
t.Run("can remove avatar URL", func(t *testing.T) {
|
||||
if err := service.ChangeAvatarURL(id, ""); err != nil {
|
||||
if err := s.ChangeAvatarURL(id, ""); err != nil {
|
||||
t.Errorf("Failed to change avatar URL: %v", err)
|
||||
}
|
||||
|
||||
if account, err := service.GetByID(id); err != nil {
|
||||
if account, err := s.GetByID(id); err != nil {
|
||||
t.Errorf("Failed to get account: %v", err)
|
||||
} else if account == nil {
|
||||
t.Error("Account is nil after update")
|
||||
|
|
@ -237,13 +259,13 @@ func Test_Account(t *testing.T) {
|
|||
})
|
||||
|
||||
t.Run("can increment auth failures", func(t *testing.T) {
|
||||
if num, err := service.IncrementFails(id); err != nil {
|
||||
if num, err := s.IncrementFails(id); err != nil {
|
||||
t.Errorf("Failed to increment account auth failures: %v", err)
|
||||
} else {
|
||||
assert.Equal(t, num, 1)
|
||||
}
|
||||
|
||||
if account, err := service.GetByID(id); err != nil {
|
||||
if account, err := s.GetByID(id); err != nil {
|
||||
t.Errorf("Failed to get account: %v", err)
|
||||
} else if account == nil {
|
||||
t.Error("Account is nil after update")
|
||||
|
|
@ -253,11 +275,11 @@ func Test_Account(t *testing.T) {
|
|||
})
|
||||
|
||||
t.Run("can reset auth failures", func(t *testing.T) {
|
||||
if err := service.ResetFails(id); err != nil {
|
||||
if err := s.ResetFails(id); err != nil {
|
||||
t.Errorf("Failed to reset account auth failures: %v", err)
|
||||
}
|
||||
|
||||
if account, err := service.GetByID(id); err != nil {
|
||||
if account, err := s.GetByID(id); err != nil {
|
||||
t.Errorf("Failed to get account: %v", err)
|
||||
} else if account == nil {
|
||||
t.Error("Account is nil after update")
|
||||
|
|
@ -267,11 +289,11 @@ func Test_Account(t *testing.T) {
|
|||
})
|
||||
|
||||
t.Run("can lock account", func(t *testing.T) {
|
||||
if err := service.Lock(id); err != nil {
|
||||
if err := s.Lock(id); err != nil {
|
||||
t.Errorf("Failed to lock account: %v", err)
|
||||
}
|
||||
|
||||
if account, err := service.GetByID(id); err != nil {
|
||||
if account, err := s.GetByID(id); err != nil {
|
||||
t.Errorf("Failed to get account: %v", err)
|
||||
} else if account == nil {
|
||||
t.Error("Account is nil after update")
|
||||
|
|
@ -281,11 +303,11 @@ func Test_Account(t *testing.T) {
|
|||
})
|
||||
|
||||
t.Run("can unlock account", func(t *testing.T) {
|
||||
if err := service.Unlock(id); err != nil {
|
||||
if err := s.Unlock(id); err != nil {
|
||||
t.Errorf("Failed to unlock account: %v", err)
|
||||
}
|
||||
|
||||
if account, err := service.GetByID(id); err != nil {
|
||||
if account, err := s.GetByID(id); err != nil {
|
||||
t.Errorf("Failed to get account: %v", err)
|
||||
} else if account == nil {
|
||||
t.Error("Account is nil after update")
|
||||
|
|
@ -295,12 +317,14 @@ func Test_Account(t *testing.T) {
|
|||
})
|
||||
|
||||
t.Run("can delete account", func(t *testing.T) {
|
||||
if err = service.Delete(id); err != nil {
|
||||
if err = s.Delete(id); err != nil {
|
||||
t.Errorf("Failed to delete account: %v", err)
|
||||
}
|
||||
|
||||
if account, err := service.GetByID(id); err != nil {
|
||||
t.Errorf("Failed to get account after deletion: %v", err)
|
||||
if account, err := s.GetByID(id); err != nil {
|
||||
if !errors.IsNotExistError(err) {
|
||||
t.Errorf("Failed to get account after deletion: %v", err)
|
||||
}
|
||||
} else if account != nil {
|
||||
t.Error("Account still exists after deletion")
|
||||
}
|
||||
|
|
@ -308,56 +332,76 @@ 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 {
|
||||
if _, err := s.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 {
|
||||
if _, err := s.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 {
|
||||
if _, err := s.Create(username, password, &testEmail, &avatarURL); err == nil {
|
||||
t.Error("Could create account with empty (non-nil) email")
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("can't fetch account that doesn't exist", func(t *testing.T) {
|
||||
t.Run("by ID", func(t *testing.T) {
|
||||
if account, err := s.GetByID("adsginh534g9405gmb40i9bm"); err != nil {
|
||||
if !errors.IsNotExistError(err) { t.Errorf("Failed to get account: %v", err) }
|
||||
} else if account != nil {
|
||||
t.Error("Could fetch non-existent account")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("by username", func(t *testing.T) {
|
||||
if account, err := s.GetByUsername("adsginh534g9405gmb40i9bm"); err != nil {
|
||||
if !errors.IsNotExistError(err) { t.Errorf("Failed to get account: %v", err) }
|
||||
} else if account != nil {
|
||||
t.Error("Could fetch non-existent account")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("email", func(t *testing.T) {
|
||||
if account, err := s.GetByEmail("adsginh534g9405gmb40i9bm"); err != nil {
|
||||
if !errors.IsNotExistError(err) { t.Errorf("Failed to get account: %v", err) }
|
||||
} else if account != nil {
|
||||
t.Error("Could fetch non-existent account")
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
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 {
|
||||
if err := s.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 {
|
||||
if err := s.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 {
|
||||
if err := s.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 {
|
||||
if err := s.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