2023-05-01 16:09:41 +08:00
|
|
|
package testserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-09-17 22:55:13 +08:00
|
|
|
|
2023-06-17 21:25:46 +08:00
|
|
|
apiv1 "github.com/usememos/memos/api/v1"
|
2023-05-01 16:09:41 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMemoRelationServer(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
s, err := NewTestingServer(ctx, t)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer s.Shutdown(ctx)
|
|
|
|
|
2023-06-17 21:25:46 +08:00
|
|
|
signup := &apiv1.SignUp{
|
2023-05-01 16:09:41 +08:00
|
|
|
Username: "testuser",
|
|
|
|
Password: "testpassword",
|
|
|
|
}
|
2023-08-10 09:01:38 +08:00
|
|
|
user, err := s.postAuthSignUp(signup)
|
2023-05-01 16:09:41 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, signup.Username, user.Username)
|
2023-07-06 21:56:42 +08:00
|
|
|
memo, err := s.postMemoCreate(&apiv1.CreateMemoRequest{
|
2023-05-01 16:09:41 +08:00
|
|
|
Content: "test memo",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "test memo", memo.Content)
|
2023-07-06 21:56:42 +08:00
|
|
|
memo2, err := s.postMemoCreate(&apiv1.CreateMemoRequest{
|
2023-05-01 16:09:41 +08:00
|
|
|
Content: "test memo2",
|
2023-07-06 21:56:42 +08:00
|
|
|
RelationList: []*apiv1.UpsertMemoRelationRequest{
|
2023-05-01 16:09:41 +08:00
|
|
|
{
|
|
|
|
RelatedMemoID: memo.ID,
|
2023-07-06 21:56:42 +08:00
|
|
|
Type: apiv1.MemoRelationReference,
|
2023-05-01 16:09:41 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "test memo2", memo2.Content)
|
2023-06-26 23:06:53 +08:00
|
|
|
memoList, err := s.getMemoList()
|
2023-05-01 16:09:41 +08:00
|
|
|
require.NoError(t, err)
|
2023-06-26 23:06:53 +08:00
|
|
|
require.Len(t, memoList, 2)
|
2023-05-01 16:09:41 +08:00
|
|
|
require.Len(t, memo2.RelationList, 1)
|
2023-07-06 21:56:42 +08:00
|
|
|
err = s.deleteMemoRelation(memo2.ID, memo.ID, apiv1.MemoRelationReference)
|
2023-05-01 16:09:41 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
memo2, err = s.getMemo(memo2.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, memo2.RelationList, 0)
|
2023-07-06 21:56:42 +08:00
|
|
|
memoRelation, err := s.postMemoRelationUpsert(memo2.ID, &apiv1.UpsertMemoRelationRequest{
|
2023-05-01 16:09:41 +08:00
|
|
|
RelatedMemoID: memo.ID,
|
2023-07-06 21:56:42 +08:00
|
|
|
Type: apiv1.MemoRelationReference,
|
2023-05-01 16:09:41 +08:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, memo.ID, memoRelation.RelatedMemoID)
|
|
|
|
memo2, err = s.getMemo(memo2.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, memo2.RelationList, 1)
|
|
|
|
}
|
|
|
|
|
2023-08-04 21:55:07 +08:00
|
|
|
func (s *TestingServer) postMemoRelationUpsert(memoID int32, memoRelationUpsert *apiv1.UpsertMemoRelationRequest) (*apiv1.MemoRelation, error) {
|
2023-05-01 16:09:41 +08:00
|
|
|
rawData, err := json.Marshal(&memoRelationUpsert)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to marshal memo relation upsert")
|
|
|
|
}
|
|
|
|
reader := bytes.NewReader(rawData)
|
2023-07-06 21:56:42 +08:00
|
|
|
body, err := s.post(fmt.Sprintf("/api/v1/memo/%d/relation", memoID), reader, nil)
|
2023-05-01 16:09:41 +08: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 21:56:42 +08:00
|
|
|
memoRelation := &apiv1.MemoRelation{}
|
|
|
|
if err = json.Unmarshal(buf.Bytes(), memoRelation); err != nil {
|
2023-05-01 16:09:41 +08:00
|
|
|
return nil, errors.Wrap(err, "fail to unmarshal post memo relation upsert response")
|
|
|
|
}
|
2023-07-06 21:56:42 +08:00
|
|
|
return memoRelation, nil
|
2023-05-01 16:09:41 +08:00
|
|
|
}
|
|
|
|
|
2023-08-04 21:55:07 +08:00
|
|
|
func (s *TestingServer) deleteMemoRelation(memoID int32, relatedMemoID int32, relationType apiv1.MemoRelationType) error {
|
2023-07-06 21:56:42 +08:00
|
|
|
_, err := s.delete(fmt.Sprintf("/api/v1/memo/%d/relation/%d/type/%s", memoID, relatedMemoID, relationType), nil)
|
2023-05-01 16:09:41 +08:00
|
|
|
return err
|
|
|
|
}
|