user_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package teststore
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/stretchr/testify/require"
  6. "golang.org/x/crypto/bcrypt"
  7. "github.com/usememos/memos/store"
  8. )
  9. func TestUserStore(t *testing.T) {
  10. ctx := context.Background()
  11. ts := NewTestingStore(ctx, t)
  12. user, err := createTestingHostUser(ctx, ts)
  13. require.NoError(t, err)
  14. users, err := ts.ListUsers(ctx, &store.FindUser{})
  15. require.NoError(t, err)
  16. require.Equal(t, 1, len(users))
  17. require.Equal(t, store.RoleHost, users[0].Role)
  18. require.Equal(t, user, users[0])
  19. userPatchNickname := "test_nickname_2"
  20. userPatch := &store.UpdateUser{
  21. ID: user.ID,
  22. Nickname: &userPatchNickname,
  23. }
  24. user, err = ts.UpdateUser(ctx, userPatch)
  25. require.NoError(t, err)
  26. require.Equal(t, userPatchNickname, user.Nickname)
  27. err = ts.DeleteUser(ctx, &store.DeleteUser{
  28. ID: user.ID,
  29. })
  30. require.NoError(t, err)
  31. users, err = ts.ListUsers(ctx, &store.FindUser{})
  32. require.NoError(t, err)
  33. require.Equal(t, 0, len(users))
  34. ts.Close()
  35. }
  36. func createTestingHostUser(ctx context.Context, ts *store.Store) (*store.User, error) {
  37. userCreate := &store.User{
  38. Username: "test",
  39. Role: store.RoleHost,
  40. Email: "test@test.com",
  41. Nickname: "test_nickname",
  42. Description: "test_description",
  43. }
  44. passwordHash, err := bcrypt.GenerateFromPassword([]byte("test_password"), bcrypt.DefaultCost)
  45. if err != nil {
  46. return nil, err
  47. }
  48. userCreate.PasswordHash = string(passwordHash)
  49. user, err := ts.CreateUser(ctx, userCreate)
  50. return user, err
  51. }