2023-03-12 16:00:57 +01:00
|
|
|
// GoToSocial
|
|
|
|
// Copyright (C) GoToSocial Authors admin@gotosocial.org
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2021-09-24 17:56:48 +02:00
|
|
|
|
2023-01-02 13:10:50 +01:00
|
|
|
package users_test
|
2021-08-10 13:32:39 +02:00
|
|
|
|
|
|
|
import (
|
2023-11-20 13:22:28 +01:00
|
|
|
"bytes"
|
2021-08-10 13:32:39 +02:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2023-11-20 13:22:28 +01:00
|
|
|
"io"
|
2021-08-10 13:32:39 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/suite"
|
2021-11-13 17:29:43 +01:00
|
|
|
"github.com/superseriousbusiness/activity/streams"
|
|
|
|
"github.com/superseriousbusiness/activity/streams/vocab"
|
2023-11-20 13:22:28 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/ap"
|
2023-01-02 13:10:50 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/activitypub/users"
|
2021-08-10 13:32:39 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/testrig"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RepliesGetTestSuite struct {
|
|
|
|
UserStandardTestSuite
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *RepliesGetTestSuite) TestGetReplies() {
|
|
|
|
// the dereference we're gonna use
|
|
|
|
derefRequests := testrig.NewTestDereferenceRequests(suite.testAccounts)
|
|
|
|
signedRequest := derefRequests["foss_satan_dereference_local_account_1_status_1_replies"]
|
|
|
|
targetAccount := suite.testAccounts["local_account_1"]
|
|
|
|
targetStatus := suite.testStatuses["local_account_1_status_1"]
|
|
|
|
|
|
|
|
// setup request
|
|
|
|
recorder := httptest.NewRecorder()
|
2022-07-12 09:32:20 +02:00
|
|
|
ctx, _ := testrig.CreateGinTestContext(recorder, nil)
|
2023-11-20 13:22:28 +01:00
|
|
|
ctx.Request = httptest.NewRequest(http.MethodGet, targetStatus.URI+"/replies?only_other_accounts=false", nil) // the endpoint we're hitting
|
2021-12-11 17:50:00 +01:00
|
|
|
ctx.Request.Header.Set("accept", "application/activity+json")
|
2021-08-10 13:32:39 +02:00
|
|
|
ctx.Request.Header.Set("Signature", signedRequest.SignatureHeader)
|
|
|
|
ctx.Request.Header.Set("Date", signedRequest.DateHeader)
|
|
|
|
|
|
|
|
// we need to pass the context through signature check first to set appropriate values on it
|
2023-01-02 13:10:50 +01:00
|
|
|
suite.signatureCheck(ctx)
|
2021-08-10 13:32:39 +02:00
|
|
|
|
|
|
|
// normally the router would populate these params from the path values,
|
|
|
|
// but because we're calling the function directly, we need to set them manually.
|
|
|
|
ctx.Params = gin.Params{
|
|
|
|
gin.Param{
|
2023-01-02 13:10:50 +01:00
|
|
|
Key: users.UsernameKey,
|
2021-08-10 13:32:39 +02:00
|
|
|
Value: targetAccount.Username,
|
|
|
|
},
|
|
|
|
gin.Param{
|
2023-01-02 13:10:50 +01:00
|
|
|
Key: users.StatusIDKey,
|
2021-08-10 13:32:39 +02:00
|
|
|
Value: targetStatus.ID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// trigger the function being tested
|
2022-08-31 17:31:21 +02:00
|
|
|
suite.userModule.StatusRepliesGETHandler(ctx)
|
2021-08-10 13:32:39 +02:00
|
|
|
|
|
|
|
// check response
|
|
|
|
suite.EqualValues(http.StatusOK, recorder.Code)
|
|
|
|
|
2023-11-20 13:22:28 +01:00
|
|
|
// Read response body.
|
2021-08-10 13:32:39 +02:00
|
|
|
result := recorder.Result()
|
|
|
|
defer result.Body.Close()
|
2023-11-20 13:22:28 +01:00
|
|
|
b, err := io.ReadAll(result.Body)
|
2021-08-10 13:32:39 +02:00
|
|
|
assert.NoError(suite.T(), err)
|
|
|
|
|
2023-11-20 13:22:28 +01:00
|
|
|
// Indent JSON
|
|
|
|
// for readability.
|
|
|
|
b = indentJSON(b)
|
|
|
|
|
|
|
|
// Create JSON string of expected output.
|
|
|
|
expect := toJSON(map[string]any{
|
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
"type": "OrderedCollection",
|
|
|
|
"id": targetStatus.URI + "/replies?only_other_accounts=false",
|
|
|
|
"first": targetStatus.URI + "/replies?limit=20&only_other_accounts=false",
|
|
|
|
"totalItems": 1,
|
|
|
|
})
|
|
|
|
assert.Equal(suite.T(), expect, string(b))
|
|
|
|
|
2021-08-10 13:32:39 +02:00
|
|
|
m := make(map[string]interface{})
|
|
|
|
err = json.Unmarshal(b, &m)
|
|
|
|
assert.NoError(suite.T(), err)
|
|
|
|
|
|
|
|
t, err := streams.ToType(context.Background(), m)
|
|
|
|
assert.NoError(suite.T(), err)
|
|
|
|
|
2023-11-20 13:22:28 +01:00
|
|
|
_, ok := t.(vocab.ActivityStreamsOrderedCollection)
|
2021-08-10 13:32:39 +02:00
|
|
|
assert.True(suite.T(), ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *RepliesGetTestSuite) TestGetRepliesNext() {
|
|
|
|
// the dereference we're gonna use
|
|
|
|
derefRequests := testrig.NewTestDereferenceRequests(suite.testAccounts)
|
|
|
|
signedRequest := derefRequests["foss_satan_dereference_local_account_1_status_1_replies_next"]
|
|
|
|
targetAccount := suite.testAccounts["local_account_1"]
|
|
|
|
targetStatus := suite.testStatuses["local_account_1_status_1"]
|
|
|
|
|
|
|
|
// setup request
|
|
|
|
recorder := httptest.NewRecorder()
|
2022-07-12 09:32:20 +02:00
|
|
|
ctx, _ := testrig.CreateGinTestContext(recorder, nil)
|
2021-08-10 13:32:39 +02:00
|
|
|
ctx.Request = httptest.NewRequest(http.MethodGet, targetStatus.URI+"/replies?only_other_accounts=false&page=true", nil) // the endpoint we're hitting
|
2021-12-11 17:50:00 +01:00
|
|
|
ctx.Request.Header.Set("accept", "application/activity+json")
|
2021-08-10 13:32:39 +02:00
|
|
|
ctx.Request.Header.Set("Signature", signedRequest.SignatureHeader)
|
|
|
|
ctx.Request.Header.Set("Date", signedRequest.DateHeader)
|
|
|
|
|
|
|
|
// we need to pass the context through signature check first to set appropriate values on it
|
2023-01-02 13:10:50 +01:00
|
|
|
suite.signatureCheck(ctx)
|
2021-08-10 13:32:39 +02:00
|
|
|
|
|
|
|
// normally the router would populate these params from the path values,
|
|
|
|
// but because we're calling the function directly, we need to set them manually.
|
|
|
|
ctx.Params = gin.Params{
|
|
|
|
gin.Param{
|
2023-01-02 13:10:50 +01:00
|
|
|
Key: users.UsernameKey,
|
2021-08-10 13:32:39 +02:00
|
|
|
Value: targetAccount.Username,
|
|
|
|
},
|
|
|
|
gin.Param{
|
2023-01-02 13:10:50 +01:00
|
|
|
Key: users.StatusIDKey,
|
2021-08-10 13:32:39 +02:00
|
|
|
Value: targetStatus.ID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// trigger the function being tested
|
2023-11-08 15:32:17 +01:00
|
|
|
suite.userModule.StatusRepliesGETHandler(ctx)
|
2021-08-10 13:32:39 +02:00
|
|
|
|
|
|
|
// check response
|
|
|
|
suite.EqualValues(http.StatusOK, recorder.Code)
|
|
|
|
|
2023-11-20 13:22:28 +01:00
|
|
|
// Read response body.
|
2021-08-10 13:32:39 +02:00
|
|
|
result := recorder.Result()
|
|
|
|
defer result.Body.Close()
|
2023-11-20 13:22:28 +01:00
|
|
|
b, err := io.ReadAll(result.Body)
|
2021-08-10 13:32:39 +02:00
|
|
|
assert.NoError(suite.T(), err)
|
|
|
|
|
2023-11-20 13:22:28 +01:00
|
|
|
// Indent JSON
|
|
|
|
// for readability.
|
|
|
|
b = indentJSON(b)
|
|
|
|
|
|
|
|
// Create JSON string of expected output.
|
|
|
|
expect := toJSON(map[string]any{
|
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
"type": "OrderedCollectionPage",
|
|
|
|
"id": targetStatus.URI + "/replies?limit=20&only_other_accounts=false",
|
|
|
|
"partOf": targetStatus.URI + "/replies?only_other_accounts=false",
|
2024-04-30 16:22:23 +02:00
|
|
|
"next": "http://localhost:8080/users/the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY/replies?limit=20&max_id=01FF25D5Q0DH7CHD57CTRS6WK0&only_other_accounts=false",
|
2024-04-30 12:19:33 +02:00
|
|
|
"prev": "http://localhost:8080/users/the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY/replies?limit=20&min_id=01FF25D5Q0DH7CHD57CTRS6WK0&only_other_accounts=false",
|
2023-12-10 12:36:00 +01:00
|
|
|
"orderedItems": []string{"http://localhost:8080/users/admin/statuses/01FF25D5Q0DH7CHD57CTRS6WK0"},
|
2023-11-20 13:22:28 +01:00
|
|
|
"totalItems": 1,
|
|
|
|
})
|
|
|
|
assert.Equal(suite.T(), expect, string(b))
|
2021-08-10 13:32:39 +02:00
|
|
|
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
err = json.Unmarshal(b, &m)
|
|
|
|
assert.NoError(suite.T(), err)
|
|
|
|
|
|
|
|
t, err := streams.ToType(context.Background(), m)
|
|
|
|
assert.NoError(suite.T(), err)
|
|
|
|
|
2023-11-20 13:22:28 +01:00
|
|
|
page, ok := t.(vocab.ActivityStreamsOrderedCollectionPage)
|
2021-08-10 13:32:39 +02:00
|
|
|
assert.True(suite.T(), ok)
|
|
|
|
|
2023-11-20 13:22:28 +01:00
|
|
|
assert.Equal(suite.T(), page.GetActivityStreamsOrderedItems().Len(), 1)
|
2021-08-10 13:32:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *RepliesGetTestSuite) TestGetRepliesLast() {
|
|
|
|
// the dereference we're gonna use
|
|
|
|
derefRequests := testrig.NewTestDereferenceRequests(suite.testAccounts)
|
|
|
|
signedRequest := derefRequests["foss_satan_dereference_local_account_1_status_1_replies_last"]
|
|
|
|
targetAccount := suite.testAccounts["local_account_1"]
|
|
|
|
targetStatus := suite.testStatuses["local_account_1_status_1"]
|
|
|
|
|
|
|
|
// setup request
|
|
|
|
recorder := httptest.NewRecorder()
|
2022-07-12 09:32:20 +02:00
|
|
|
ctx, _ := testrig.CreateGinTestContext(recorder, nil)
|
2023-11-20 13:22:28 +01:00
|
|
|
ctx.Request = httptest.NewRequest(http.MethodGet, targetStatus.URI+"/replies?min_id=01FF25D5Q0DH7CHD57CTRS6WK0&only_other_accounts=false", nil)
|
2021-12-11 17:50:00 +01:00
|
|
|
ctx.Request.Header.Set("accept", "application/activity+json")
|
2021-08-10 13:32:39 +02:00
|
|
|
ctx.Request.Header.Set("Signature", signedRequest.SignatureHeader)
|
|
|
|
ctx.Request.Header.Set("Date", signedRequest.DateHeader)
|
|
|
|
|
|
|
|
// we need to pass the context through signature check first to set appropriate values on it
|
2023-01-02 13:10:50 +01:00
|
|
|
suite.signatureCheck(ctx)
|
2021-08-10 13:32:39 +02:00
|
|
|
|
|
|
|
// normally the router would populate these params from the path values,
|
|
|
|
// but because we're calling the function directly, we need to set them manually.
|
|
|
|
ctx.Params = gin.Params{
|
|
|
|
gin.Param{
|
2023-01-02 13:10:50 +01:00
|
|
|
Key: users.UsernameKey,
|
2021-08-10 13:32:39 +02:00
|
|
|
Value: targetAccount.Username,
|
|
|
|
},
|
|
|
|
gin.Param{
|
2023-01-02 13:10:50 +01:00
|
|
|
Key: users.StatusIDKey,
|
2021-08-10 13:32:39 +02:00
|
|
|
Value: targetStatus.ID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// trigger the function being tested
|
2023-11-08 15:32:17 +01:00
|
|
|
suite.userModule.StatusRepliesGETHandler(ctx)
|
2021-08-10 13:32:39 +02:00
|
|
|
|
|
|
|
// check response
|
|
|
|
suite.EqualValues(http.StatusOK, recorder.Code)
|
|
|
|
|
2023-11-20 13:22:28 +01:00
|
|
|
// Read response body.
|
2021-08-10 13:32:39 +02:00
|
|
|
result := recorder.Result()
|
|
|
|
defer result.Body.Close()
|
2023-11-20 13:22:28 +01:00
|
|
|
b, err := io.ReadAll(result.Body)
|
2021-08-10 13:32:39 +02:00
|
|
|
assert.NoError(suite.T(), err)
|
|
|
|
|
2023-11-20 13:22:28 +01:00
|
|
|
// Indent JSON
|
|
|
|
// for readability.
|
|
|
|
b = indentJSON(b)
|
|
|
|
|
|
|
|
// Create JSON string of expected output.
|
|
|
|
expect := toJSON(map[string]any{
|
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
"type": "OrderedCollectionPage",
|
|
|
|
"id": targetStatus.URI + "/replies?min_id=01FF25D5Q0DH7CHD57CTRS6WK0&only_other_accounts=false",
|
|
|
|
"partOf": targetStatus.URI + "/replies?only_other_accounts=false",
|
|
|
|
"orderedItems": []any{}, // empty
|
|
|
|
"totalItems": 1,
|
|
|
|
})
|
|
|
|
assert.Equal(suite.T(), expect, string(b))
|
2021-08-10 13:32:39 +02:00
|
|
|
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
err = json.Unmarshal(b, &m)
|
|
|
|
assert.NoError(suite.T(), err)
|
|
|
|
|
|
|
|
t, err := streams.ToType(context.Background(), m)
|
|
|
|
assert.NoError(suite.T(), err)
|
|
|
|
|
2023-11-20 13:22:28 +01:00
|
|
|
page, ok := t.(vocab.ActivityStreamsOrderedCollectionPage)
|
2021-08-10 13:32:39 +02:00
|
|
|
assert.True(suite.T(), ok)
|
|
|
|
|
2023-11-20 13:22:28 +01:00
|
|
|
assert.Equal(suite.T(), page.GetActivityStreamsOrderedItems().Len(), 0)
|
2021-08-10 13:32:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepliesGetTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(RepliesGetTestSuite))
|
|
|
|
}
|
2023-11-20 13:22:28 +01:00
|
|
|
|
|
|
|
// toJSON will return indented JSON serialized form of 'a'.
|
|
|
|
func toJSON(a any) string {
|
|
|
|
v, ok := a.(vocab.Type)
|
|
|
|
if ok {
|
|
|
|
m, err := ap.Serialize(v)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
a = m
|
|
|
|
}
|
2023-11-27 15:00:57 +01:00
|
|
|
var dst bytes.Buffer
|
|
|
|
enc := json.NewEncoder(&dst)
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
enc.SetEscapeHTML(false)
|
|
|
|
err := enc.Encode(a)
|
2023-11-20 13:22:28 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-11-27 15:00:57 +01:00
|
|
|
dst.Truncate(dst.Len() - 1) // drop new-line
|
|
|
|
return dst.String()
|
2023-11-20 13:22:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// indentJSON will return indented JSON from raw provided JSON.
|
|
|
|
func indentJSON(b []byte) []byte {
|
|
|
|
var dst bytes.Buffer
|
|
|
|
err := json.Indent(&dst, b, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return dst.Bytes()
|
|
|
|
}
|