2023-04-17 21:34:59 +08:00
|
|
|
package testserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/usememos/memos/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMemoServer(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
s, err := NewTestingServer(ctx, t)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer s.Shutdown(ctx)
|
|
|
|
|
|
|
|
signup := &api.SignUp{
|
|
|
|
Username: "testuser",
|
|
|
|
Password: "testpassword",
|
|
|
|
}
|
|
|
|
user, err := s.postAuthSignup(signup)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, signup.Username, user.Username)
|
|
|
|
memoList, err := s.getMemoList()
|
|
|
|
require.NoError(t, err)
|
2023-05-13 22:25:15 +08:00
|
|
|
require.Len(t, memoList, 1)
|
2023-04-17 21:34:59 +08:00
|
|
|
memo, err := s.postMemoCreate(&api.MemoCreate{
|
|
|
|
Content: "test memo",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "test memo", memo.Content)
|
|
|
|
memoList, err = s.getMemoList()
|
|
|
|
require.NoError(t, err)
|
2023-05-13 22:25:15 +08:00
|
|
|
require.Len(t, memoList, 2)
|
2023-04-17 21:34:59 +08:00
|
|
|
updatedContent := "updated memo"
|
2023-05-01 16:09:41 +08:00
|
|
|
memo, err = s.patchMemo(&api.MemoPatch{
|
2023-04-17 21:34:59 +08:00
|
|
|
ID: memo.ID,
|
|
|
|
Content: &updatedContent,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, updatedContent, memo.Content)
|
2023-05-09 09:03:09 +08:00
|
|
|
require.Equal(t, false, memo.Pinned)
|
|
|
|
memo, err = s.postMemosOrganizer(&api.MemoOrganizerUpsert{
|
|
|
|
MemoID: memo.ID,
|
|
|
|
UserID: user.ID,
|
|
|
|
Pinned: true,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
memo, err = s.patchMemo(&api.MemoPatch{
|
|
|
|
ID: memo.ID,
|
|
|
|
Content: &updatedContent,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, updatedContent, memo.Content)
|
|
|
|
require.Equal(t, true, memo.Pinned)
|
2023-05-01 16:09:41 +08:00
|
|
|
err = s.deleteMemo(&api.MemoDelete{
|
2023-04-17 21:34:59 +08:00
|
|
|
ID: memo.ID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
memoList, err = s.getMemoList()
|
|
|
|
require.NoError(t, err)
|
2023-05-13 22:25:15 +08:00
|
|
|
require.Len(t, memoList, 1)
|
2023-04-17 21:34:59 +08:00
|
|
|
}
|
|
|
|
|
2023-05-01 16:09:41 +08:00
|
|
|
func (s *TestingServer) getMemo(memoID int) (*api.Memo, error) {
|
|
|
|
body, err := s.get(fmt.Sprintf("/api/memo/%d", memoID), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
_, err = buf.ReadFrom(body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "fail to read response body")
|
|
|
|
}
|
|
|
|
|
|
|
|
type MemoCreateResponse struct {
|
|
|
|
Data *api.Memo `json:"data"`
|
|
|
|
}
|
|
|
|
res := new(MemoCreateResponse)
|
|
|
|
if err = json.Unmarshal(buf.Bytes(), res); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "fail to unmarshal get memo response")
|
|
|
|
}
|
|
|
|
return res.Data, nil
|
|
|
|
}
|
|
|
|
|
2023-04-17 21:34:59 +08:00
|
|
|
func (s *TestingServer) getMemoList() ([]*api.Memo, error) {
|
|
|
|
body, err := s.get("/api/memo", nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
_, err = buf.ReadFrom(body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "fail to read response body")
|
|
|
|
}
|
|
|
|
|
|
|
|
type MemoCreateResponse struct {
|
|
|
|
Data []*api.Memo `json:"data"`
|
|
|
|
}
|
|
|
|
res := new(MemoCreateResponse)
|
|
|
|
if err = json.Unmarshal(buf.Bytes(), res); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "fail to unmarshal get memo list response")
|
|
|
|
}
|
|
|
|
return res.Data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TestingServer) postMemoCreate(memoCreate *api.MemoCreate) (*api.Memo, error) {
|
|
|
|
rawData, err := json.Marshal(&memoCreate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to marshal memo create")
|
|
|
|
}
|
|
|
|
reader := bytes.NewReader(rawData)
|
|
|
|
body, err := s.post("/api/memo", reader, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
_, err = buf.ReadFrom(body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "fail to read response body")
|
|
|
|
}
|
|
|
|
|
|
|
|
type MemoCreateResponse struct {
|
|
|
|
Data *api.Memo `json:"data"`
|
|
|
|
}
|
|
|
|
res := new(MemoCreateResponse)
|
|
|
|
if err = json.Unmarshal(buf.Bytes(), res); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "fail to unmarshal post memo create response")
|
|
|
|
}
|
|
|
|
return res.Data, nil
|
|
|
|
}
|
|
|
|
|
2023-05-01 16:09:41 +08:00
|
|
|
func (s *TestingServer) patchMemo(memoPatch *api.MemoPatch) (*api.Memo, error) {
|
2023-04-17 21:34:59 +08:00
|
|
|
rawData, err := json.Marshal(&memoPatch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to marshal memo patch")
|
|
|
|
}
|
|
|
|
reader := bytes.NewReader(rawData)
|
|
|
|
body, err := s.patch(fmt.Sprintf("/api/memo/%d", memoPatch.ID), reader, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
_, err = buf.ReadFrom(body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "fail to read response body")
|
|
|
|
}
|
|
|
|
|
|
|
|
type MemoPatchResponse struct {
|
|
|
|
Data *api.Memo `json:"data"`
|
|
|
|
}
|
|
|
|
res := new(MemoPatchResponse)
|
|
|
|
if err = json.Unmarshal(buf.Bytes(), res); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "fail to unmarshal patch memo response")
|
|
|
|
}
|
|
|
|
return res.Data, nil
|
|
|
|
}
|
|
|
|
|
2023-05-01 16:09:41 +08:00
|
|
|
func (s *TestingServer) deleteMemo(memoDelete *api.MemoDelete) error {
|
2023-04-17 21:34:59 +08:00
|
|
|
_, err := s.delete(fmt.Sprintf("/api/memo/%d", memoDelete.ID), nil)
|
|
|
|
return err
|
|
|
|
}
|
2023-05-09 09:03:09 +08:00
|
|
|
|
|
|
|
func (s *TestingServer) postMemosOrganizer(memosOrganizer *api.MemoOrganizerUpsert) (*api.Memo, error) {
|
|
|
|
rawData, err := json.Marshal(&memosOrganizer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to marshal memos organizer")
|
|
|
|
}
|
|
|
|
reader := bytes.NewReader(rawData)
|
|
|
|
body, err := s.post(fmt.Sprintf("/api/memo/%d/organizer", memosOrganizer.MemoID), reader, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
_, err = buf.ReadFrom(body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "fail to read response body")
|
|
|
|
}
|
|
|
|
|
|
|
|
type MemoOrganizerResponse struct {
|
|
|
|
Data *api.Memo `json:"data"`
|
|
|
|
}
|
|
|
|
res := new(MemoOrganizerResponse)
|
|
|
|
if err = json.Unmarshal(buf.Bytes(), res); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "fail to unmarshal organizer memo create response")
|
|
|
|
}
|
|
|
|
return res.Data, err
|
|
|
|
}
|