gtsocial-umbx

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

statusget_test.go (5237B)


      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 	"io/ioutil"
     24 	"net/http"
     25 	"net/http/httptest"
     26 	"strings"
     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 StatusGetTestSuite struct {
     38 	UserStandardTestSuite
     39 }
     40 
     41 func (suite *StatusGetTestSuite) TestGetStatus() {
     42 	// the dereference we're gonna use
     43 	derefRequests := testrig.NewTestDereferenceRequests(suite.testAccounts)
     44 	signedRequest := derefRequests["foss_satan_dereference_local_account_1_status_1"]
     45 	targetAccount := suite.testAccounts["local_account_1"]
     46 	targetStatus := suite.testStatuses["local_account_1_status_1"]
     47 
     48 	// setup request
     49 	recorder := httptest.NewRecorder()
     50 	ctx, _ := testrig.CreateGinTestContext(recorder, nil)
     51 	ctx.Request = httptest.NewRequest(http.MethodGet, targetStatus.URI, nil) // the endpoint we're hitting
     52 	ctx.Request.Header.Set("accept", "application/activity+json")
     53 	ctx.Request.Header.Set("Signature", signedRequest.SignatureHeader)
     54 	ctx.Request.Header.Set("Date", signedRequest.DateHeader)
     55 
     56 	// we need to pass the context through signature check first to set appropriate values on it
     57 	suite.signatureCheck(ctx)
     58 
     59 	// normally the router would populate these params from the path values,
     60 	// but because we're calling the function directly, we need to set them manually.
     61 	ctx.Params = gin.Params{
     62 		gin.Param{
     63 			Key:   users.UsernameKey,
     64 			Value: targetAccount.Username,
     65 		},
     66 		gin.Param{
     67 			Key:   users.StatusIDKey,
     68 			Value: targetStatus.ID,
     69 		},
     70 	}
     71 
     72 	// trigger the function being tested
     73 	suite.userModule.StatusGETHandler(ctx)
     74 
     75 	// check response
     76 	suite.EqualValues(http.StatusOK, recorder.Code)
     77 
     78 	result := recorder.Result()
     79 	defer result.Body.Close()
     80 	b, err := ioutil.ReadAll(result.Body)
     81 	suite.NoError(err)
     82 
     83 	// should be a Note
     84 	m := make(map[string]interface{})
     85 	err = json.Unmarshal(b, &m)
     86 	suite.NoError(err)
     87 
     88 	t, err := streams.ToType(context.Background(), m)
     89 	suite.NoError(err)
     90 
     91 	note, ok := t.(vocab.ActivityStreamsNote)
     92 	suite.True(ok)
     93 
     94 	// convert note to status
     95 	a, err := suite.tc.ASStatusToStatus(context.Background(), note)
     96 	suite.NoError(err)
     97 	suite.EqualValues(targetStatus.Content, a.Content)
     98 }
     99 
    100 func (suite *StatusGetTestSuite) TestGetStatusLowercase() {
    101 	// the dereference we're gonna use
    102 	derefRequests := testrig.NewTestDereferenceRequests(suite.testAccounts)
    103 	signedRequest := derefRequests["foss_satan_dereference_local_account_1_status_1_lowercase"]
    104 	targetAccount := suite.testAccounts["local_account_1"]
    105 	targetStatus := suite.testStatuses["local_account_1_status_1"]
    106 
    107 	// setup request
    108 	recorder := httptest.NewRecorder()
    109 	ctx, _ := testrig.CreateGinTestContext(recorder, nil)
    110 	ctx.Request = httptest.NewRequest(http.MethodGet, strings.ToLower(targetStatus.URI), nil) // the endpoint we're hitting
    111 	ctx.Request.Header.Set("accept", "application/activity+json")
    112 	ctx.Request.Header.Set("Signature", signedRequest.SignatureHeader)
    113 	ctx.Request.Header.Set("Date", signedRequest.DateHeader)
    114 
    115 	// we need to pass the context through signature check first to set appropriate values on it
    116 	suite.signatureCheck(ctx)
    117 
    118 	// normally the router would populate these params from the path values,
    119 	// but because we're calling the function directly, we need to set them manually.
    120 	ctx.Params = gin.Params{
    121 		gin.Param{
    122 			Key:   users.UsernameKey,
    123 			Value: strings.ToLower(targetAccount.Username),
    124 		},
    125 		gin.Param{
    126 			Key:   users.StatusIDKey,
    127 			Value: strings.ToLower(targetStatus.ID),
    128 		},
    129 	}
    130 
    131 	// trigger the function being tested
    132 	suite.userModule.StatusGETHandler(ctx)
    133 
    134 	// check response
    135 	suite.EqualValues(http.StatusOK, recorder.Code)
    136 
    137 	result := recorder.Result()
    138 	defer result.Body.Close()
    139 	b, err := ioutil.ReadAll(result.Body)
    140 	suite.NoError(err)
    141 
    142 	// should be a Note
    143 	m := make(map[string]interface{})
    144 	err = json.Unmarshal(b, &m)
    145 	suite.NoError(err)
    146 
    147 	t, err := streams.ToType(context.Background(), m)
    148 	suite.NoError(err)
    149 
    150 	note, ok := t.(vocab.ActivityStreamsNote)
    151 	suite.True(ok)
    152 
    153 	// convert note to status
    154 	a, err := suite.tc.ASStatusToStatus(context.Background(), note)
    155 	suite.NoError(err)
    156 	suite.EqualValues(targetStatus.Content, a.Content)
    157 }
    158 
    159 func TestStatusGetTestSuite(t *testing.T) {
    160 	suite.Run(t, new(StatusGetTestSuite))
    161 }