user_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. }
  43. passwordHash, err := bcrypt.GenerateFromPassword([]byte("test_password"), bcrypt.DefaultCost)
  44. if err != nil {
  45. return nil, err
  46. }
  47. userCreate.PasswordHash = string(passwordHash)
  48. user, err := ts.CreateUser(ctx, userCreate)
  49. return user, err
  50. }