memo_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package testserver
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "testing"
  8. "github.com/pkg/errors"
  9. "github.com/stretchr/testify/require"
  10. apiv1 "github.com/usememos/memos/api/v1"
  11. )
  12. func TestMemoServer(t *testing.T) {
  13. ctx := context.Background()
  14. s, err := NewTestingServer(ctx, t)
  15. require.NoError(t, err)
  16. defer s.Shutdown(ctx)
  17. signup := &apiv1.SignUp{
  18. Username: "testuser",
  19. Password: "testpassword",
  20. }
  21. user, err := s.postAuthSignUp(signup)
  22. require.NoError(t, err)
  23. require.Equal(t, signup.Username, user.Username)
  24. memo, err := s.postMemoCreate(&apiv1.CreateMemoRequest{
  25. Content: "test memo",
  26. })
  27. require.NoError(t, err)
  28. require.Equal(t, "test memo", memo.Content)
  29. memoList, err := s.getMemoList()
  30. require.NoError(t, err)
  31. require.Len(t, memoList, 1)
  32. updatedContent := "updated memo"
  33. memo, err = s.patchMemo(&apiv1.PatchMemoRequest{
  34. ID: memo.ID,
  35. Content: &updatedContent,
  36. })
  37. require.NoError(t, err)
  38. require.Equal(t, updatedContent, memo.Content)
  39. require.Equal(t, false, memo.Pinned)
  40. _, err = s.postMemoOrganizer(memo.ID, &apiv1.UpsertMemoOrganizerRequest{
  41. Pinned: true,
  42. })
  43. require.NoError(t, err)
  44. memo, err = s.patchMemo(&apiv1.PatchMemoRequest{
  45. ID: memo.ID,
  46. Content: &updatedContent,
  47. })
  48. require.NoError(t, err)
  49. require.Equal(t, updatedContent, memo.Content)
  50. require.Equal(t, true, memo.Pinned)
  51. err = s.deleteMemo(memo.ID)
  52. require.NoError(t, err)
  53. memoList, err = s.getMemoList()
  54. require.NoError(t, err)
  55. require.Len(t, memoList, 0)
  56. }
  57. func (s *TestingServer) getMemo(memoID int32) (*apiv1.Memo, error) {
  58. body, err := s.get(fmt.Sprintf("/api/v1/memo/%d", memoID), nil)
  59. if err != nil {
  60. return nil, err
  61. }
  62. buf := &bytes.Buffer{}
  63. _, err = buf.ReadFrom(body)
  64. if err != nil {
  65. return nil, errors.Wrap(err, "fail to read response body")
  66. }
  67. memo := &apiv1.Memo{}
  68. if err = json.Unmarshal(buf.Bytes(), memo); err != nil {
  69. return nil, errors.Wrap(err, "fail to unmarshal get memo response")
  70. }
  71. return memo, nil
  72. }
  73. func (s *TestingServer) getMemoList() ([]*apiv1.Memo, error) {
  74. body, err := s.get("/api/v1/memo", nil)
  75. if err != nil {
  76. return nil, err
  77. }
  78. buf := &bytes.Buffer{}
  79. _, err = buf.ReadFrom(body)
  80. if err != nil {
  81. return nil, errors.Wrap(err, "fail to read response body")
  82. }
  83. memoList := []*apiv1.Memo{}
  84. if err = json.Unmarshal(buf.Bytes(), &memoList); err != nil {
  85. return nil, errors.Wrap(err, "fail to unmarshal get memo list response")
  86. }
  87. return memoList, nil
  88. }
  89. func (s *TestingServer) postMemoCreate(memoCreate *apiv1.CreateMemoRequest) (*apiv1.Memo, error) {
  90. rawData, err := json.Marshal(&memoCreate)
  91. if err != nil {
  92. return nil, errors.Wrap(err, "failed to marshal memo create")
  93. }
  94. reader := bytes.NewReader(rawData)
  95. body, err := s.post("/api/v1/memo", reader, nil)
  96. if err != nil {
  97. return nil, err
  98. }
  99. buf := &bytes.Buffer{}
  100. _, err = buf.ReadFrom(body)
  101. if err != nil {
  102. return nil, errors.Wrap(err, "fail to read response body")
  103. }
  104. memo := &apiv1.Memo{}
  105. if err = json.Unmarshal(buf.Bytes(), memo); err != nil {
  106. return nil, errors.Wrap(err, "fail to unmarshal post memo create response")
  107. }
  108. return memo, nil
  109. }
  110. func (s *TestingServer) patchMemo(memoPatch *apiv1.PatchMemoRequest) (*apiv1.Memo, error) {
  111. rawData, err := json.Marshal(&memoPatch)
  112. if err != nil {
  113. return nil, errors.Wrap(err, "failed to marshal memo patch")
  114. }
  115. reader := bytes.NewReader(rawData)
  116. body, err := s.patch(fmt.Sprintf("/api/v1/memo/%d", memoPatch.ID), reader, nil)
  117. if err != nil {
  118. return nil, err
  119. }
  120. buf := &bytes.Buffer{}
  121. _, err = buf.ReadFrom(body)
  122. if err != nil {
  123. return nil, errors.Wrap(err, "fail to read response body")
  124. }
  125. memo := &apiv1.Memo{}
  126. if err = json.Unmarshal(buf.Bytes(), memo); err != nil {
  127. return nil, errors.Wrap(err, "fail to unmarshal patch memo response")
  128. }
  129. return memo, nil
  130. }
  131. func (s *TestingServer) deleteMemo(memoID int32) error {
  132. _, err := s.delete(fmt.Sprintf("/api/v1/memo/%d", memoID), nil)
  133. return err
  134. }
  135. func (s *TestingServer) postMemoOrganizer(memoID int32, memosOrganizer *apiv1.UpsertMemoOrganizerRequest) (*apiv1.Memo, error) {
  136. rawData, err := json.Marshal(&memosOrganizer)
  137. if err != nil {
  138. return nil, errors.Wrap(err, "failed to marshal memos organizer")
  139. }
  140. reader := bytes.NewReader(rawData)
  141. body, err := s.post(fmt.Sprintf("/api/v1/memo/%d/organizer", memoID), reader, nil)
  142. if err != nil {
  143. return nil, err
  144. }
  145. buf := &bytes.Buffer{}
  146. _, err = buf.ReadFrom(body)
  147. if err != nil {
  148. return nil, errors.Wrap(err, "fail to read response body")
  149. }
  150. memo := &apiv1.Memo{}
  151. if err = json.Unmarshal(buf.Bytes(), memo); err != nil {
  152. return nil, errors.Wrap(err, "fail to unmarshal organizer memo create response")
  153. }
  154. return memo, err
  155. }