memo_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. "github.com/usememos/memos/api"
  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 := &api.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. memoList, err := s.getMemoList()
  25. require.NoError(t, err)
  26. require.Len(t, memoList, 1)
  27. memo, err := s.postMemoCreate(&api.CreateMemoRequest{
  28. Content: "test memo",
  29. })
  30. require.NoError(t, err)
  31. require.Equal(t, "test memo", memo.Content)
  32. memoList, err = s.getMemoList()
  33. require.NoError(t, err)
  34. require.Len(t, memoList, 2)
  35. updatedContent := "updated memo"
  36. memo, err = s.patchMemo(&api.PatchMemoRequest{
  37. ID: memo.ID,
  38. Content: &updatedContent,
  39. })
  40. require.NoError(t, err)
  41. require.Equal(t, updatedContent, memo.Content)
  42. require.Equal(t, false, memo.Pinned)
  43. memo, err = s.postMemosOrganizer(&api.MemoOrganizerUpsert{
  44. MemoID: memo.ID,
  45. UserID: user.ID,
  46. Pinned: true,
  47. })
  48. require.NoError(t, err)
  49. memo, err = s.patchMemo(&api.PatchMemoRequest{
  50. ID: memo.ID,
  51. Content: &updatedContent,
  52. })
  53. require.NoError(t, err)
  54. require.Equal(t, updatedContent, memo.Content)
  55. require.Equal(t, true, memo.Pinned)
  56. err = s.deleteMemo(memo.ID)
  57. require.NoError(t, err)
  58. memoList, err = s.getMemoList()
  59. require.NoError(t, err)
  60. require.Len(t, memoList, 1)
  61. }
  62. func (s *TestingServer) getMemo(memoID int) (*api.MemoResponse, error) {
  63. body, err := s.get(fmt.Sprintf("/api/memo/%d", memoID), nil)
  64. if err != nil {
  65. return nil, err
  66. }
  67. buf := &bytes.Buffer{}
  68. _, err = buf.ReadFrom(body)
  69. if err != nil {
  70. return nil, errors.Wrap(err, "fail to read response body")
  71. }
  72. type MemoCreateResponse struct {
  73. Data *api.MemoResponse `json:"data"`
  74. }
  75. res := new(MemoCreateResponse)
  76. if err = json.Unmarshal(buf.Bytes(), res); err != nil {
  77. return nil, errors.Wrap(err, "fail to unmarshal get memo response")
  78. }
  79. return res.Data, nil
  80. }
  81. func (s *TestingServer) getMemoList() ([]*api.MemoResponse, error) {
  82. body, err := s.get("/api/memo", nil)
  83. if err != nil {
  84. return nil, err
  85. }
  86. buf := &bytes.Buffer{}
  87. _, err = buf.ReadFrom(body)
  88. if err != nil {
  89. return nil, errors.Wrap(err, "fail to read response body")
  90. }
  91. type MemoCreateResponse struct {
  92. Data []*api.MemoResponse `json:"data"`
  93. }
  94. res := new(MemoCreateResponse)
  95. if err = json.Unmarshal(buf.Bytes(), res); err != nil {
  96. return nil, errors.Wrap(err, "fail to unmarshal get memo list response")
  97. }
  98. return res.Data, nil
  99. }
  100. func (s *TestingServer) postMemoCreate(memoCreate *api.CreateMemoRequest) (*api.MemoResponse, error) {
  101. rawData, err := json.Marshal(&memoCreate)
  102. if err != nil {
  103. return nil, errors.Wrap(err, "failed to marshal memo create")
  104. }
  105. reader := bytes.NewReader(rawData)
  106. body, err := s.post("/api/memo", reader, nil)
  107. if err != nil {
  108. return nil, err
  109. }
  110. buf := &bytes.Buffer{}
  111. _, err = buf.ReadFrom(body)
  112. if err != nil {
  113. return nil, errors.Wrap(err, "fail to read response body")
  114. }
  115. type MemoCreateResponse struct {
  116. Data *api.MemoResponse `json:"data"`
  117. }
  118. res := new(MemoCreateResponse)
  119. if err = json.Unmarshal(buf.Bytes(), res); err != nil {
  120. return nil, errors.Wrap(err, "fail to unmarshal post memo create response")
  121. }
  122. return res.Data, nil
  123. }
  124. func (s *TestingServer) patchMemo(memoPatch *api.PatchMemoRequest) (*api.MemoResponse, error) {
  125. rawData, err := json.Marshal(&memoPatch)
  126. if err != nil {
  127. return nil, errors.Wrap(err, "failed to marshal memo patch")
  128. }
  129. reader := bytes.NewReader(rawData)
  130. body, err := s.patch(fmt.Sprintf("/api/memo/%d", memoPatch.ID), reader, nil)
  131. if err != nil {
  132. return nil, err
  133. }
  134. buf := &bytes.Buffer{}
  135. _, err = buf.ReadFrom(body)
  136. if err != nil {
  137. return nil, errors.Wrap(err, "fail to read response body")
  138. }
  139. type MemoPatchResponse struct {
  140. Data *api.MemoResponse `json:"data"`
  141. }
  142. res := new(MemoPatchResponse)
  143. if err = json.Unmarshal(buf.Bytes(), res); err != nil {
  144. return nil, errors.Wrap(err, "fail to unmarshal patch memo response")
  145. }
  146. return res.Data, nil
  147. }
  148. func (s *TestingServer) deleteMemo(memoID int) error {
  149. _, err := s.delete(fmt.Sprintf("/api/memo/%d", memoID), nil)
  150. return err
  151. }
  152. func (s *TestingServer) postMemosOrganizer(memosOrganizer *api.MemoOrganizerUpsert) (*api.MemoResponse, error) {
  153. rawData, err := json.Marshal(&memosOrganizer)
  154. if err != nil {
  155. return nil, errors.Wrap(err, "failed to marshal memos organizer")
  156. }
  157. reader := bytes.NewReader(rawData)
  158. body, err := s.post(fmt.Sprintf("/api/memo/%d/organizer", memosOrganizer.MemoID), reader, nil)
  159. if err != nil {
  160. return nil, err
  161. }
  162. buf := &bytes.Buffer{}
  163. _, err = buf.ReadFrom(body)
  164. if err != nil {
  165. return nil, errors.Wrap(err, "fail to read response body")
  166. }
  167. type MemoOrganizerResponse struct {
  168. Data *api.MemoResponse `json:"data"`
  169. }
  170. res := new(MemoOrganizerResponse)
  171. if err = json.Unmarshal(buf.Bytes(), res); err != nil {
  172. return nil, errors.Wrap(err, "fail to unmarshal organizer memo create response")
  173. }
  174. return res.Data, err
  175. }