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()
|
2023-07-02 14:27:23 +08:00
|
|
|
ts := NewTestingStore(ctx, t)
|
|
|
|
user, err := createTestingHostUser(ctx, ts)
|
2023-04-01 22:47:19 +08:00
|
|
|
require.NoError(t, err)
|
2023-07-02 14:27:23 +08:00
|
|
|
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))
|
2023-07-02 18:56:25 +08:00
|
|
|
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"
|
2023-07-02 14:27:23 +08:00
|
|
|
userPatch := &store.UpdateUser{
|
2023-04-01 22:47:19 +08:00
|
|
|
ID: user.ID,
|
|
|
|
Nickname: &userPatchNickname,
|
|
|
|
}
|
2023-07-02 14:27:23 +08:00
|
|
|
user, err = ts.UpdateUser(ctx, userPatch)
|
2023-04-01 22:47:19 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, userPatchNickname, user.Nickname)
|
2023-07-02 18:56:25 +08:00
|
|
|
err = ts.DeleteUser(ctx, &store.DeleteUser{
|
2023-04-01 22:47:19 +08:00
|
|
|
ID: user.ID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-07-02 14:27:23 +08:00
|
|
|
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))
|
2024-01-16 13:51:26 +08:00
|
|
|
ts.Close()
|
2023-04-01 22:47:19 +08:00
|
|
|
}
|
2023-04-03 09:53:36 +08:00
|
|
|
|
2023-07-02 14:27:23 +08:00
|
|
|
func createTestingHostUser(ctx context.Context, ts *store.Store) (*store.User, error) {
|
|
|
|
userCreate := &store.User{
|
2024-03-13 21:24:16 +08:00
|
|
|
Username: "test",
|
|
|
|
Role: store.RoleHost,
|
|
|
|
Email: "test@test.com",
|
|
|
|
Nickname: "test_nickname",
|
|
|
|
Description: "test_description",
|
2023-04-03 09:53:36 +08:00
|
|
|
}
|
2023-07-02 14:27:23 +08:00
|
|
|
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)
|
2023-07-02 18:56:25 +08:00
|
|
|
user, err := ts.CreateUser(ctx, userCreate)
|
2023-04-03 09:53:36 +08:00
|
|
|
return user, err
|
|
|
|
}
|