gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

outboxget_test.go (8859B)


      1 // GoToSocial
      2 // Copyright (C) GoToSocial Authors admin@gotosocial.org
      3 // SPDX-License-Identifier: AGPL-3.0-or-later
      4 //
      5 // This program is free software: you can redistribute it and/or modify
      6 // it under the terms of the GNU Affero General Public License as published by
      7 // the Free Software Foundation, either version 3 of the License, or
      8 // (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU Affero General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU Affero General Public License
     16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 package users_test
     19 
     20 import (
     21 	"bytes"
     22 	"context"
     23 	"encoding/json"
     24 	"io/ioutil"
     25 	"net/http"
     26 	"net/http/httptest"
     27 	"testing"
     28 
     29 	"github.com/gin-gonic/gin"
     30 	"github.com/stretchr/testify/suite"
     31 	"github.com/superseriousbusiness/activity/streams"
     32 	"github.com/superseriousbusiness/activity/streams/vocab"
     33 	"github.com/superseriousbusiness/gotosocial/internal/api/activitypub/users"
     34 	"github.com/superseriousbusiness/gotosocial/testrig"
     35 )
     36 
     37 type OutboxGetTestSuite struct {
     38 	UserStandardTestSuite
     39 }
     40 
     41 func (suite *OutboxGetTestSuite) TestGetOutbox() {
     42 	// the dereference we're gonna use
     43 	derefRequests := testrig.NewTestDereferenceRequests(suite.testAccounts)
     44 	signedRequest := derefRequests["foss_satan_dereference_zork_outbox"]
     45 	targetAccount := suite.testAccounts["local_account_1"]
     46 
     47 	// setup request
     48 	recorder := httptest.NewRecorder()
     49 	ctx, _ := testrig.CreateGinTestContext(recorder, nil)
     50 	ctx.Request = httptest.NewRequest(http.MethodGet, targetAccount.OutboxURI, nil) // the endpoint we're hitting
     51 	ctx.Request.Header.Set("accept", "application/activity+json")
     52 	ctx.Request.Header.Set("Signature", signedRequest.SignatureHeader)
     53 	ctx.Request.Header.Set("Date", signedRequest.DateHeader)
     54 
     55 	// we need to pass the context through signature check first to set appropriate values on it
     56 	suite.signatureCheck(ctx)
     57 
     58 	// normally the router would populate these params from the path values,
     59 	// but because we're calling the function directly, we need to set them manually.
     60 	ctx.Params = gin.Params{
     61 		gin.Param{
     62 			Key:   users.UsernameKey,
     63 			Value: targetAccount.Username,
     64 		},
     65 	}
     66 
     67 	// trigger the function being tested
     68 	suite.userModule.OutboxGETHandler(ctx)
     69 
     70 	// check response
     71 	suite.EqualValues(http.StatusOK, recorder.Code)
     72 
     73 	result := recorder.Result()
     74 	defer result.Body.Close()
     75 	b, err := ioutil.ReadAll(result.Body)
     76 	suite.NoError(err)
     77 	dst := new(bytes.Buffer)
     78 	err = json.Indent(dst, b, "", "  ")
     79 	suite.NoError(err)
     80 	suite.Equal(`{
     81   "@context": "https://www.w3.org/ns/activitystreams",
     82   "first": "http://localhost:8080/users/the_mighty_zork/outbox?page=true",
     83   "id": "http://localhost:8080/users/the_mighty_zork/outbox",
     84   "type": "OrderedCollection"
     85 }`, dst.String())
     86 
     87 	m := make(map[string]interface{})
     88 	err = json.Unmarshal(b, &m)
     89 	suite.NoError(err)
     90 
     91 	t, err := streams.ToType(context.Background(), m)
     92 	suite.NoError(err)
     93 
     94 	_, ok := t.(vocab.ActivityStreamsOrderedCollection)
     95 	suite.True(ok)
     96 }
     97 
     98 func (suite *OutboxGetTestSuite) TestGetOutboxFirstPage() {
     99 	// the dereference we're gonna use
    100 	derefRequests := testrig.NewTestDereferenceRequests(suite.testAccounts)
    101 	signedRequest := derefRequests["foss_satan_dereference_zork_outbox_first"]
    102 	targetAccount := suite.testAccounts["local_account_1"]
    103 
    104 	tc := testrig.NewTestTransportController(&suite.state, testrig.NewMockHTTPClient(nil, "../../../../testrig/media"))
    105 	federator := testrig.NewTestFederator(&suite.state, tc, suite.mediaManager)
    106 	emailSender := testrig.NewEmailSender("../../../../web/template/", nil)
    107 	processor := testrig.NewTestProcessor(&suite.state, federator, emailSender, suite.mediaManager)
    108 	userModule := users.New(processor)
    109 
    110 	// setup request
    111 	recorder := httptest.NewRecorder()
    112 	ctx, _ := testrig.CreateGinTestContext(recorder, nil)
    113 	ctx.Request = httptest.NewRequest(http.MethodGet, targetAccount.OutboxURI+"?page=true", nil) // the endpoint we're hitting
    114 	ctx.Request.Header.Set("accept", "application/activity+json")
    115 	ctx.Request.Header.Set("Signature", signedRequest.SignatureHeader)
    116 	ctx.Request.Header.Set("Date", signedRequest.DateHeader)
    117 
    118 	// we need to pass the context through signature check first to set appropriate values on it
    119 	suite.signatureCheck(ctx)
    120 
    121 	// normally the router would populate these params from the path values,
    122 	// but because we're calling the function directly, we need to set them manually.
    123 	ctx.Params = gin.Params{
    124 		gin.Param{
    125 			Key:   users.UsernameKey,
    126 			Value: targetAccount.Username,
    127 		},
    128 	}
    129 
    130 	// trigger the function being tested
    131 	userModule.OutboxGETHandler(ctx)
    132 
    133 	// check response
    134 	suite.EqualValues(http.StatusOK, recorder.Code)
    135 
    136 	result := recorder.Result()
    137 	defer result.Body.Close()
    138 	b, err := ioutil.ReadAll(result.Body)
    139 	suite.NoError(err)
    140 	dst := new(bytes.Buffer)
    141 	err = json.Indent(dst, b, "", "  ")
    142 	suite.NoError(err)
    143 	suite.Equal(`{
    144   "@context": "https://www.w3.org/ns/activitystreams",
    145   "id": "http://localhost:8080/users/the_mighty_zork/outbox?page=true",
    146   "next": "http://localhost:8080/users/the_mighty_zork/outbox?page=true\u0026max_id=01F8MHAMCHF6Y650WCRSCP4WMY",
    147   "orderedItems": {
    148     "actor": "http://localhost:8080/users/the_mighty_zork",
    149     "cc": "http://localhost:8080/users/the_mighty_zork/followers",
    150     "id": "http://localhost:8080/users/the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY/activity",
    151     "object": "http://localhost:8080/users/the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY",
    152     "published": "2021-10-20T10:40:37Z",
    153     "to": "https://www.w3.org/ns/activitystreams#Public",
    154     "type": "Create"
    155   },
    156   "partOf": "http://localhost:8080/users/the_mighty_zork/outbox",
    157   "prev": "http://localhost:8080/users/the_mighty_zork/outbox?page=true\u0026min_id=01F8MHAMCHF6Y650WCRSCP4WMY",
    158   "type": "OrderedCollectionPage"
    159 }`, dst.String())
    160 
    161 	m := make(map[string]interface{})
    162 	err = json.Unmarshal(b, &m)
    163 	suite.NoError(err)
    164 
    165 	t, err := streams.ToType(context.Background(), m)
    166 	suite.NoError(err)
    167 
    168 	_, ok := t.(vocab.ActivityStreamsOrderedCollectionPage)
    169 	suite.True(ok)
    170 }
    171 
    172 func (suite *OutboxGetTestSuite) TestGetOutboxNextPage() {
    173 	// the dereference we're gonna use
    174 	derefRequests := testrig.NewTestDereferenceRequests(suite.testAccounts)
    175 	signedRequest := derefRequests["foss_satan_dereference_zork_outbox_next"]
    176 	targetAccount := suite.testAccounts["local_account_1"]
    177 
    178 	tc := testrig.NewTestTransportController(&suite.state, testrig.NewMockHTTPClient(nil, "../../../../testrig/media"))
    179 	federator := testrig.NewTestFederator(&suite.state, tc, suite.mediaManager)
    180 	emailSender := testrig.NewEmailSender("../../../../web/template/", nil)
    181 	processor := testrig.NewTestProcessor(&suite.state, federator, emailSender, suite.mediaManager)
    182 	userModule := users.New(processor)
    183 
    184 	// setup request
    185 	recorder := httptest.NewRecorder()
    186 	ctx, _ := testrig.CreateGinTestContext(recorder, nil)
    187 	ctx.Request = httptest.NewRequest(http.MethodGet, targetAccount.OutboxURI+"?page=true&max_id=01F8MHAMCHF6Y650WCRSCP4WMY", nil) // the endpoint we're hitting
    188 	ctx.Request.Header.Set("accept", "application/activity+json")
    189 	ctx.Request.Header.Set("Signature", signedRequest.SignatureHeader)
    190 	ctx.Request.Header.Set("Date", signedRequest.DateHeader)
    191 
    192 	// we need to pass the context through signature check first to set appropriate values on it
    193 	suite.signatureCheck(ctx)
    194 
    195 	// normally the router would populate these params from the path values,
    196 	// but because we're calling the function directly, we need to set them manually.
    197 	ctx.Params = gin.Params{
    198 		gin.Param{
    199 			Key:   users.UsernameKey,
    200 			Value: targetAccount.Username,
    201 		},
    202 		gin.Param{
    203 			Key:   users.MaxIDKey,
    204 			Value: "01F8MHAMCHF6Y650WCRSCP4WMY",
    205 		},
    206 	}
    207 
    208 	// trigger the function being tested
    209 	userModule.OutboxGETHandler(ctx)
    210 
    211 	// check response
    212 	suite.EqualValues(http.StatusOK, recorder.Code)
    213 
    214 	result := recorder.Result()
    215 	defer result.Body.Close()
    216 	b, err := ioutil.ReadAll(result.Body)
    217 	suite.NoError(err)
    218 	dst := new(bytes.Buffer)
    219 	err = json.Indent(dst, b, "", "  ")
    220 	suite.NoError(err)
    221 	suite.Equal(`{
    222   "@context": "https://www.w3.org/ns/activitystreams",
    223   "id": "http://localhost:8080/users/the_mighty_zork/outbox?page=true\u0026maxID=01F8MHAMCHF6Y650WCRSCP4WMY",
    224   "orderedItems": [],
    225   "partOf": "http://localhost:8080/users/the_mighty_zork/outbox",
    226   "type": "OrderedCollectionPage"
    227 }`, dst.String())
    228 
    229 	m := make(map[string]interface{})
    230 	err = json.Unmarshal(b, &m)
    231 	suite.NoError(err)
    232 
    233 	t, err := streams.ToType(context.Background(), m)
    234 	suite.NoError(err)
    235 
    236 	_, ok := t.(vocab.ActivityStreamsOrderedCollectionPage)
    237 	suite.True(ok)
    238 }
    239 
    240 func TestOutboxGetTestSuite(t *testing.T) {
    241 	suite.Run(t, new(OutboxGetTestSuite))
    242 }