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