memos/test/store/user_test.go

56 lines
1.5 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"
"github.com/usememos/memos/api"
2023-04-03 09:53:36 +08:00
"github.com/usememos/memos/store"
2023-04-01 22:47:19 +08:00
"golang.org/x/crypto/bcrypt"
)
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.Host, 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, &api.UserDelete{
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.Host,
2023-04-03 09:53:36 +08:00
Email: "test@test.com",
Nickname: "test_nickname",
OpenID: "test_open_id",
}
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.CreateUserV1(ctx, userCreate)
2023-04-03 09:53:36 +08:00
return user, err
}