memos/test/store/user_test.go

55 lines
1.4 KiB
Go
Raw Normal View History

2023-04-03 09:53:36 +08:00
package teststore
2023-04-01 22:47:19 +08:00
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/bcrypt"
2023-09-17 22:55:13 +08:00
"github.com/usememos/memos/store"
2023-04-01 22:47:19 +08:00
)
func TestUserStore(t *testing.T) {
ctx := context.Background()
ts := NewTestingStore(ctx, t)
user, err := createTestingHostUser(ctx, ts)
2023-04-01 22:47:19 +08:00
require.NoError(t, err)
users, err := ts.ListUsers(ctx, &store.FindUser{})
2023-04-01 22:47:19 +08:00
require.NoError(t, err)
require.Equal(t, 1, len(users))
require.Equal(t, store.RoleHost, users[0].Role)
2023-04-01 22:47:19 +08:00
require.Equal(t, user, users[0])
userPatchNickname := "test_nickname_2"
userPatch := &store.UpdateUser{
2023-04-01 22:47:19 +08:00
ID: user.ID,
Nickname: &userPatchNickname,
}
user, err = ts.UpdateUser(ctx, userPatch)
2023-04-01 22:47:19 +08:00
require.NoError(t, err)
require.Equal(t, userPatchNickname, user.Nickname)
err = ts.DeleteUser(ctx, &store.DeleteUser{
2023-04-01 22:47:19 +08:00
ID: user.ID,
})
require.NoError(t, err)
users, err = ts.ListUsers(ctx, &store.FindUser{})
2023-04-01 22:47:19 +08:00
require.NoError(t, err)
require.Equal(t, 0, len(users))
}
2023-04-03 09:53:36 +08:00
func createTestingHostUser(ctx context.Context, ts *store.Store) (*store.User, error) {
userCreate := &store.User{
2023-04-03 09:53:36 +08:00
Username: "test",
Role: store.RoleHost,
2023-04-03 09:53:36 +08:00
Email: "test@test.com",
Nickname: "test_nickname",
}
passwordHash, err := bcrypt.GenerateFromPassword([]byte("test_password"), bcrypt.DefaultCost)
2023-04-03 09:53:36 +08:00
if err != nil {
return nil, err
}
userCreate.PasswordHash = string(passwordHash)
user, err := ts.CreateUser(ctx, userCreate)
2023-04-03 09:53:36 +08:00
return user, err
}