user_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. }
  35. func createTestingHostUser(ctx context.Context, ts *store.Store) (*store.User, error) {
  36. userCreate := &store.User{
  37. Username: "test",
  38. Role: store.RoleHost,
  39. Email: "test@test.com",
  40. Nickname: "test_nickname",
  41. }
  42. passwordHash, err := bcrypt.GenerateFromPassword([]byte("test_password"), bcrypt.DefaultCost)
  43. if err != nil {
  44. return nil, err
  45. }
  46. userCreate.PasswordHash = string(passwordHash)
  47. user, err := ts.CreateUser(ctx, userCreate)
  48. return user, err
  49. }