2023-04-17 16:34:59 +03:00
|
|
|
package testserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-09-17 17:55:13 +03:00
|
|
|
|
2023-06-17 16:25:46 +03:00
|
|
|
apiv1 "github.com/usememos/memos/api/v1"
|
2023-04-17 16:34:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMemoServer(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
s, err := NewTestingServer(ctx, t)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer s.Shutdown(ctx)
|
|
|
|
|
2023-06-17 16:25:46 +03:00
|
|
|
signup := &apiv1.SignUp{
|
2023-04-17 16:34:59 +03:00
|
|
|
Username: "testuser",
|
|
|
|
Password: "testpassword",
|
|
|
|
}
|
2023-08-10 04:01:38 +03:00
|
|
|
user, err := s.postAuthSignUp(signup)
|
2023-04-17 16:34:59 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, signup.Username, user.Username)
|
2023-07-06 16:56:42 +03:00
|
|
|
memo, err := s.postMemoCreate(&apiv1.CreateMemoRequest{
|
2023-04-17 16:34:59 +03:00
|
|
|
Content: "test memo",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "test memo", memo.Content)
|
2023-06-26 18:06:53 +03:00
|
|
|
memoList, err := s.getMemoList()
|
2023-04-17 16:34:59 +03:00
|
|
|
require.NoError(t, err)
|
2023-06-26 18:06:53 +03:00
|
|
|
require.Len(t, memoList, 1)
|
2023-04-17 16:34:59 +03:00
|
|
|
updatedContent := "updated memo"
|
2023-07-06 16:56:42 +03:00
|
|
|
memo, err = s.patchMemo(&apiv1.PatchMemoRequest{
|
2023-04-17 16:34:59 +03:00
|
|
|
ID: memo.ID,
|
|
|
|
Content: &updatedContent,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, updatedContent, memo.Content)
|
2023-05-09 04:03:09 +03:00
|
|
|
require.Equal(t, false, memo.Pinned)
|
2023-07-06 16:56:42 +03:00
|
|
|
_, err = s.postMemoOrganizer(memo.ID, &apiv1.UpsertMemoOrganizerRequest{
|
2023-05-09 04:03:09 +03:00
|
|
|
Pinned: true,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-07-06 16:56:42 +03:00
|
|
|
memo, err = s.patchMemo(&apiv1.PatchMemoRequest{
|
2023-05-09 04:03:09 +03:00
|
|
|
ID: memo.ID,
|
|
|
|
Content: &updatedContent,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, updatedContent, memo.Content)
|
|
|
|
require.Equal(t, true, memo.Pinned)
|
2023-05-25 16:50:37 +03:00
|
|
|
err = s.deleteMemo(memo.ID)
|
2023-04-17 16:34:59 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
memoList, err = s.getMemoList()
|
|
|
|
require.NoError(t, err)
|
2023-06-26 18:06:53 +03:00
|
|
|
require.Len(t, memoList, 0)
|
2023-04-17 16:34:59 +03:00
|
|
|
}
|
|
|
|
|
2023-08-04 16:55:07 +03:00
|
|
|
func (s *TestingServer) getMemo(memoID int32) (*apiv1.Memo, error) {
|
2023-07-06 16:56:42 +03:00
|
|
|
body, err := s.get(fmt.Sprintf("/api/v1/memo/%d", memoID), nil)
|
2023-05-01 11:09:41 +03:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2023-07-06 16:56:42 +03:00
|
|
|
memo := &apiv1.Memo{}
|
|
|
|
if err = json.Unmarshal(buf.Bytes(), memo); err != nil {
|
2023-05-01 11:09:41 +03:00
|
|
|
return nil, errors.Wrap(err, "fail to unmarshal get memo response")
|
|
|
|
}
|
2023-07-06 16:56:42 +03:00
|
|
|
return memo, nil
|
2023-05-01 11:09:41 +03:00
|
|
|
}
|
|
|
|
|
2023-07-06 16:56:42 +03:00
|
|
|
func (s *TestingServer) getMemoList() ([]*apiv1.Memo, error) {
|
|
|
|
body, err := s.get("/api/v1/memo", nil)
|
2023-04-17 16:34:59 +03:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2023-07-06 16:56:42 +03:00
|
|
|
memoList := []*apiv1.Memo{}
|
|
|
|
if err = json.Unmarshal(buf.Bytes(), &memoList); err != nil {
|
2023-04-17 16:34:59 +03:00
|
|
|
return nil, errors.Wrap(err, "fail to unmarshal get memo list response")
|
|
|
|
}
|
2023-07-06 16:56:42 +03:00
|
|
|
return memoList, nil
|
2023-04-17 16:34:59 +03:00
|
|
|
}
|
|
|
|
|
2023-07-06 16:56:42 +03:00
|
|
|
func (s *TestingServer) postMemoCreate(memoCreate *apiv1.CreateMemoRequest) (*apiv1.Memo, error) {
|
2023-04-17 16:34:59 +03:00
|
|
|
rawData, err := json.Marshal(&memoCreate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to marshal memo create")
|
|
|
|
}
|
|
|
|
reader := bytes.NewReader(rawData)
|
2023-07-06 16:56:42 +03:00
|
|
|
body, err := s.post("/api/v1/memo", reader, nil)
|
2023-04-17 16:34:59 +03:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2023-07-06 16:56:42 +03:00
|
|
|
memo := &apiv1.Memo{}
|
|
|
|
if err = json.Unmarshal(buf.Bytes(), memo); err != nil {
|
2023-04-17 16:34:59 +03:00
|
|
|
return nil, errors.Wrap(err, "fail to unmarshal post memo create response")
|
|
|
|
}
|
2023-07-06 16:56:42 +03:00
|
|
|
return memo, nil
|
2023-04-17 16:34:59 +03:00
|
|
|
}
|
|
|
|
|
2023-07-06 16:56:42 +03:00
|
|
|
func (s *TestingServer) patchMemo(memoPatch *apiv1.PatchMemoRequest) (*apiv1.Memo, error) {
|
2023-04-17 16:34:59 +03:00
|
|
|
rawData, err := json.Marshal(&memoPatch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to marshal memo patch")
|
|
|
|
}
|
|
|
|
reader := bytes.NewReader(rawData)
|
2023-07-06 16:56:42 +03:00
|
|
|
body, err := s.patch(fmt.Sprintf("/api/v1/memo/%d", memoPatch.ID), reader, nil)
|
2023-04-17 16:34:59 +03:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2023-07-06 16:56:42 +03:00
|
|
|
memo := &apiv1.Memo{}
|
|
|
|
if err = json.Unmarshal(buf.Bytes(), memo); err != nil {
|
2023-04-17 16:34:59 +03:00
|
|
|
return nil, errors.Wrap(err, "fail to unmarshal patch memo response")
|
|
|
|
}
|
2023-07-06 16:56:42 +03:00
|
|
|
return memo, nil
|
2023-04-17 16:34:59 +03:00
|
|
|
}
|
|
|
|
|
2023-08-04 16:55:07 +03:00
|
|
|
func (s *TestingServer) deleteMemo(memoID int32) error {
|
2023-07-06 16:56:42 +03:00
|
|
|
_, err := s.delete(fmt.Sprintf("/api/v1/memo/%d", memoID), nil)
|
2023-04-17 16:34:59 +03:00
|
|
|
return err
|
|
|
|
}
|
2023-05-09 04:03:09 +03:00
|
|
|
|
2023-08-04 16:55:07 +03:00
|
|
|
func (s *TestingServer) postMemoOrganizer(memoID int32, memosOrganizer *apiv1.UpsertMemoOrganizerRequest) (*apiv1.Memo, error) {
|
2023-05-09 04:03:09 +03:00
|
|
|
rawData, err := json.Marshal(&memosOrganizer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to marshal memos organizer")
|
|
|
|
}
|
|
|
|
reader := bytes.NewReader(rawData)
|
2023-07-06 16:56:42 +03:00
|
|
|
body, err := s.post(fmt.Sprintf("/api/v1/memo/%d/organizer", memoID), reader, nil)
|
2023-05-09 04:03:09 +03:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2023-07-06 16:56:42 +03:00
|
|
|
memo := &apiv1.Memo{}
|
|
|
|
if err = json.Unmarshal(buf.Bytes(), memo); err != nil {
|
2023-05-09 04:03:09 +03:00
|
|
|
return nil, errors.Wrap(err, "fail to unmarshal organizer memo create response")
|
|
|
|
}
|
2023-07-06 16:56:42 +03:00
|
|
|
return memo, err
|
2023-05-09 04:03:09 +03:00
|
|
|
}
|