2022-08-30 02:30:36 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-01-04 17:55:26 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-08-30 02:30:36 +00:00
|
|
|
)
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
func userFactory() UserCreate {
|
|
|
|
return UserCreate{
|
2022-09-12 22:47:27 +00:00
|
|
|
Name: fk.Str(10),
|
|
|
|
Email: fk.Email(),
|
|
|
|
Password: fk.Str(10),
|
|
|
|
IsSuperuser: fk.Bool(),
|
2022-09-05 08:26:21 +00:00
|
|
|
GroupID: tGroup.ID,
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func TestUserRepo_GetOneEmail(t *testing.T) {
|
2022-08-30 02:30:36 +00:00
|
|
|
assert := assert.New(t)
|
2022-09-05 08:26:21 +00:00
|
|
|
user := userFactory()
|
2022-08-30 02:30:36 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
_, err := tRepos.Users.Create(ctx, user)
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-30 02:30:36 +00:00
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
foundUser, err := tRepos.Users.GetOneEmail(ctx, user.Email)
|
2022-08-30 02:30:36 +00:00
|
|
|
|
|
|
|
assert.NotNil(foundUser)
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-30 02:30:36 +00:00
|
|
|
assert.Equal(user.Email, foundUser.Email)
|
|
|
|
assert.Equal(user.Name, foundUser.Name)
|
|
|
|
|
|
|
|
// Cleanup
|
2022-09-05 08:26:21 +00:00
|
|
|
err = tRepos.Users.DeleteAll(ctx)
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func TestUserRepo_GetOneId(t *testing.T) {
|
2022-08-30 02:30:36 +00:00
|
|
|
assert := assert.New(t)
|
2022-09-05 08:26:21 +00:00
|
|
|
user := userFactory()
|
2022-08-30 02:30:36 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
userOut, _ := tRepos.Users.Create(ctx, user)
|
2024-01-04 17:55:26 +00:00
|
|
|
foundUser, err := tRepos.Users.GetOneID(ctx, userOut.ID)
|
2022-08-30 02:30:36 +00:00
|
|
|
|
|
|
|
assert.NotNil(foundUser)
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-30 02:30:36 +00:00
|
|
|
assert.Equal(user.Email, foundUser.Email)
|
|
|
|
assert.Equal(user.Name, foundUser.Name)
|
|
|
|
|
|
|
|
// Cleanup
|
2022-09-05 08:26:21 +00:00
|
|
|
err = tRepos.Users.DeleteAll(ctx)
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func TestUserRepo_GetAll(t *testing.T) {
|
2022-08-30 02:30:36 +00:00
|
|
|
// Setup
|
2022-09-27 23:52:13 +00:00
|
|
|
toCreate := []UserCreate{
|
2022-09-05 08:26:21 +00:00
|
|
|
userFactory(),
|
|
|
|
userFactory(),
|
|
|
|
userFactory(),
|
|
|
|
userFactory(),
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
created := []UserOut{}
|
2022-08-30 02:30:36 +00:00
|
|
|
|
|
|
|
for _, usr := range toCreate {
|
2022-09-05 08:26:21 +00:00
|
|
|
usrOut, _ := tRepos.Users.Create(ctx, usr)
|
2022-08-30 02:30:36 +00:00
|
|
|
created = append(created, usrOut)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate
|
2022-09-05 08:26:21 +00:00
|
|
|
allUsers, err := tRepos.Users.GetAll(ctx)
|
2022-08-30 02:30:36 +00:00
|
|
|
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-30 02:30:36 +00:00
|
|
|
assert.Equal(t, len(created), len(allUsers))
|
|
|
|
|
|
|
|
for _, usr := range created {
|
2022-08-31 03:21:18 +00:00
|
|
|
for _, usr2 := range allUsers {
|
|
|
|
if usr.ID == usr2.ID {
|
|
|
|
assert.Equal(t, usr.Email, usr2.Email)
|
|
|
|
|
|
|
|
// Check groups are loaded
|
2022-09-27 23:52:13 +00:00
|
|
|
assert.NotNil(t, usr2.GroupID)
|
2022-08-31 03:21:18 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, usr := range created {
|
2022-09-05 08:26:21 +00:00
|
|
|
_ = tRepos.Users.Delete(ctx, usr.ID)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup
|
2022-09-05 08:26:21 +00:00
|
|
|
err = tRepos.Users.DeleteAll(ctx)
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func TestUserRepo_Update(t *testing.T) {
|
|
|
|
user, err := tRepos.Users.Create(context.Background(), userFactory())
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-05 08:26:21 +00:00
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
updateData := UserUpdate{
|
2022-09-12 22:47:27 +00:00
|
|
|
Name: fk.Str(10),
|
|
|
|
Email: fk.Email(),
|
2022-09-05 08:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update
|
|
|
|
err = tRepos.Users.Update(context.Background(), user.ID, updateData)
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-05 08:26:21 +00:00
|
|
|
|
|
|
|
// Validate
|
2024-01-04 17:55:26 +00:00
|
|
|
updated, err := tRepos.Users.GetOneID(context.Background(), user.ID)
|
|
|
|
require.NoError(t, err)
|
2022-09-05 08:26:21 +00:00
|
|
|
assert.NotEqual(t, user.Name, updated.Name)
|
|
|
|
assert.NotEqual(t, user.Email, updated.Email)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func TestUserRepo_Delete(t *testing.T) {
|
2022-08-30 02:30:36 +00:00
|
|
|
// Create 10 Users
|
|
|
|
for i := 0; i < 10; i++ {
|
2022-09-05 08:26:21 +00:00
|
|
|
user := userFactory()
|
2022-08-30 02:30:36 +00:00
|
|
|
ctx := context.Background()
|
2022-09-05 08:26:21 +00:00
|
|
|
_, _ = tRepos.Users.Create(ctx, user)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all
|
|
|
|
ctx := context.Background()
|
2022-09-05 08:26:21 +00:00
|
|
|
allUsers, _ := tRepos.Users.GetAll(ctx)
|
2022-08-30 02:30:36 +00:00
|
|
|
|
2024-01-04 17:55:26 +00:00
|
|
|
assert.NotEmpty(t, allUsers)
|
2022-09-05 08:26:21 +00:00
|
|
|
err := tRepos.Users.DeleteAll(ctx)
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-30 02:30:36 +00:00
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
allUsers, _ = tRepos.Users.GetAll(ctx)
|
2024-01-04 17:55:26 +00:00
|
|
|
assert.Empty(t, allUsers)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func TestUserRepo_GetSuperusers(t *testing.T) {
|
2022-08-30 02:30:36 +00:00
|
|
|
// Create 10 Users
|
|
|
|
superuser := 0
|
|
|
|
users := 0
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
2022-09-05 08:26:21 +00:00
|
|
|
user := userFactory()
|
2022-08-30 02:30:36 +00:00
|
|
|
ctx := context.Background()
|
2022-09-05 08:26:21 +00:00
|
|
|
_, _ = tRepos.Users.Create(ctx, user)
|
2022-08-30 02:30:36 +00:00
|
|
|
|
|
|
|
if user.IsSuperuser {
|
|
|
|
superuser++
|
|
|
|
} else {
|
|
|
|
users++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
superUsers, err := tRepos.Users.GetSuperusers(ctx)
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-30 02:30:36 +00:00
|
|
|
|
|
|
|
for _, usr := range superUsers {
|
|
|
|
assert.True(t, usr.IsSuperuser)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup
|
2022-09-05 08:26:21 +00:00
|
|
|
err = tRepos.Users.DeleteAll(ctx)
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|