gtsocial-umbx

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

repliesget_test.go (9682B)


      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 	"context"
     22 	"encoding/json"
     23 	"fmt"
     24 	"io/ioutil"
     25 	"net/http"
     26 	"net/http/httptest"
     27 	"testing"
     28 
     29 	"github.com/gin-gonic/gin"
     30 	"github.com/stretchr/testify/assert"
     31 	"github.com/stretchr/testify/suite"
     32 	"github.com/superseriousbusiness/activity/streams"
     33 	"github.com/superseriousbusiness/activity/streams/vocab"
     34 	"github.com/superseriousbusiness/gotosocial/internal/api/activitypub/users"
     35 	"github.com/superseriousbusiness/gotosocial/testrig"
     36 )
     37 
     38 type RepliesGetTestSuite struct {
     39 	UserStandardTestSuite
     40 }
     41 
     42 func (suite *RepliesGetTestSuite) TestGetReplies() {
     43 	// the dereference we're gonna use
     44 	derefRequests := testrig.NewTestDereferenceRequests(suite.testAccounts)
     45 	signedRequest := derefRequests["foss_satan_dereference_local_account_1_status_1_replies"]
     46 	targetAccount := suite.testAccounts["local_account_1"]
     47 	targetStatus := suite.testStatuses["local_account_1_status_1"]
     48 
     49 	// setup request
     50 	recorder := httptest.NewRecorder()
     51 	ctx, _ := testrig.CreateGinTestContext(recorder, nil)
     52 	ctx.Request = httptest.NewRequest(http.MethodGet, targetStatus.URI+"/replies", nil) // the endpoint we're hitting
     53 	ctx.Request.Header.Set("accept", "application/activity+json")
     54 	ctx.Request.Header.Set("Signature", signedRequest.SignatureHeader)
     55 	ctx.Request.Header.Set("Date", signedRequest.DateHeader)
     56 
     57 	// we need to pass the context through signature check first to set appropriate values on it
     58 	suite.signatureCheck(ctx)
     59 
     60 	// normally the router would populate these params from the path values,
     61 	// but because we're calling the function directly, we need to set them manually.
     62 	ctx.Params = gin.Params{
     63 		gin.Param{
     64 			Key:   users.UsernameKey,
     65 			Value: targetAccount.Username,
     66 		},
     67 		gin.Param{
     68 			Key:   users.StatusIDKey,
     69 			Value: targetStatus.ID,
     70 		},
     71 	}
     72 
     73 	// trigger the function being tested
     74 	suite.userModule.StatusRepliesGETHandler(ctx)
     75 
     76 	// check response
     77 	suite.EqualValues(http.StatusOK, recorder.Code)
     78 
     79 	result := recorder.Result()
     80 	defer result.Body.Close()
     81 	b, err := ioutil.ReadAll(result.Body)
     82 	assert.NoError(suite.T(), err)
     83 	assert.Equal(suite.T(), `{"@context":"https://www.w3.org/ns/activitystreams","first":{"id":"http://localhost:8080/users/the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY/replies?page=true","next":"http://localhost:8080/users/the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY/replies?only_other_accounts=false\u0026page=true","partOf":"http://localhost:8080/users/the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY/replies","type":"CollectionPage"},"id":"http://localhost:8080/users/the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY/replies","type":"Collection"}`, string(b))
     84 
     85 	// should be a Collection
     86 	m := make(map[string]interface{})
     87 	err = json.Unmarshal(b, &m)
     88 	assert.NoError(suite.T(), err)
     89 
     90 	t, err := streams.ToType(context.Background(), m)
     91 	assert.NoError(suite.T(), err)
     92 
     93 	_, ok := t.(vocab.ActivityStreamsCollection)
     94 	assert.True(suite.T(), ok)
     95 }
     96 
     97 func (suite *RepliesGetTestSuite) TestGetRepliesNext() {
     98 	// the dereference we're gonna use
     99 	derefRequests := testrig.NewTestDereferenceRequests(suite.testAccounts)
    100 	signedRequest := derefRequests["foss_satan_dereference_local_account_1_status_1_replies_next"]
    101 	targetAccount := suite.testAccounts["local_account_1"]
    102 	targetStatus := suite.testStatuses["local_account_1_status_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, targetStatus.URI+"/replies?only_other_accounts=false&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 		gin.Param{
    129 			Key:   users.StatusIDKey,
    130 			Value: targetStatus.ID,
    131 		},
    132 	}
    133 
    134 	// trigger the function being tested
    135 	userModule.StatusRepliesGETHandler(ctx)
    136 
    137 	// check response
    138 	suite.EqualValues(http.StatusOK, recorder.Code)
    139 
    140 	result := recorder.Result()
    141 	defer result.Body.Close()
    142 	b, err := ioutil.ReadAll(result.Body)
    143 	assert.NoError(suite.T(), err)
    144 
    145 	assert.Equal(suite.T(), `{"@context":"https://www.w3.org/ns/activitystreams","id":"http://localhost:8080/users/the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY/replies?page=true\u0026only_other_accounts=false","items":"http://localhost:8080/users/admin/statuses/01FF25D5Q0DH7CHD57CTRS6WK0","next":"http://localhost:8080/users/the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY/replies?only_other_accounts=false\u0026page=true\u0026min_id=01FF25D5Q0DH7CHD57CTRS6WK0","partOf":"http://localhost:8080/users/the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY/replies","type":"CollectionPage"}`, string(b))
    146 
    147 	// should be a Collection
    148 	m := make(map[string]interface{})
    149 	err = json.Unmarshal(b, &m)
    150 	assert.NoError(suite.T(), err)
    151 
    152 	t, err := streams.ToType(context.Background(), m)
    153 	assert.NoError(suite.T(), err)
    154 
    155 	page, ok := t.(vocab.ActivityStreamsCollectionPage)
    156 	assert.True(suite.T(), ok)
    157 
    158 	assert.Equal(suite.T(), page.GetActivityStreamsItems().Len(), 1)
    159 }
    160 
    161 func (suite *RepliesGetTestSuite) TestGetRepliesLast() {
    162 	// the dereference we're gonna use
    163 	derefRequests := testrig.NewTestDereferenceRequests(suite.testAccounts)
    164 	signedRequest := derefRequests["foss_satan_dereference_local_account_1_status_1_replies_last"]
    165 	targetAccount := suite.testAccounts["local_account_1"]
    166 	targetStatus := suite.testStatuses["local_account_1_status_1"]
    167 
    168 	tc := testrig.NewTestTransportController(&suite.state, testrig.NewMockHTTPClient(nil, "../../../../testrig/media"))
    169 	federator := testrig.NewTestFederator(&suite.state, tc, suite.mediaManager)
    170 	emailSender := testrig.NewEmailSender("../../../../web/template/", nil)
    171 	processor := testrig.NewTestProcessor(&suite.state, federator, emailSender, suite.mediaManager)
    172 	userModule := users.New(processor)
    173 
    174 	// setup request
    175 	recorder := httptest.NewRecorder()
    176 	ctx, _ := testrig.CreateGinTestContext(recorder, nil)
    177 	ctx.Request = httptest.NewRequest(http.MethodGet, targetStatus.URI+"/replies?only_other_accounts=false&page=true&min_id=01FF25D5Q0DH7CHD57CTRS6WK0", nil) // the endpoint we're hitting
    178 	ctx.Request.Header.Set("accept", "application/activity+json")
    179 	ctx.Request.Header.Set("Signature", signedRequest.SignatureHeader)
    180 	ctx.Request.Header.Set("Date", signedRequest.DateHeader)
    181 
    182 	// we need to pass the context through signature check first to set appropriate values on it
    183 	suite.signatureCheck(ctx)
    184 
    185 	// normally the router would populate these params from the path values,
    186 	// but because we're calling the function directly, we need to set them manually.
    187 	ctx.Params = gin.Params{
    188 		gin.Param{
    189 			Key:   users.UsernameKey,
    190 			Value: targetAccount.Username,
    191 		},
    192 		gin.Param{
    193 			Key:   users.StatusIDKey,
    194 			Value: targetStatus.ID,
    195 		},
    196 	}
    197 
    198 	// trigger the function being tested
    199 	userModule.StatusRepliesGETHandler(ctx)
    200 
    201 	// check response
    202 	suite.EqualValues(http.StatusOK, recorder.Code)
    203 
    204 	result := recorder.Result()
    205 	defer result.Body.Close()
    206 	b, err := ioutil.ReadAll(result.Body)
    207 	assert.NoError(suite.T(), err)
    208 
    209 	fmt.Println(string(b))
    210 	assert.Equal(suite.T(), `{"@context":"https://www.w3.org/ns/activitystreams","id":"http://localhost:8080/users/the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY/replies?page=true\u0026only_other_accounts=false\u0026min_id=01FF25D5Q0DH7CHD57CTRS6WK0","items":[],"next":"http://localhost:8080/users/the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY/replies?only_other_accounts=false\u0026page=true","partOf":"http://localhost:8080/users/the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY/replies","type":"CollectionPage"}`, string(b))
    211 
    212 	// should be a Collection
    213 	m := make(map[string]interface{})
    214 	err = json.Unmarshal(b, &m)
    215 	assert.NoError(suite.T(), err)
    216 
    217 	t, err := streams.ToType(context.Background(), m)
    218 	assert.NoError(suite.T(), err)
    219 
    220 	page, ok := t.(vocab.ActivityStreamsCollectionPage)
    221 	assert.True(suite.T(), ok)
    222 
    223 	assert.Equal(suite.T(), page.GetActivityStreamsItems().Len(), 0)
    224 }
    225 
    226 func TestRepliesGetTestSuite(t *testing.T) {
    227 	suite.Run(t, new(RepliesGetTestSuite))
    228 }