userget_test.go (5503B)
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 "testing" 27 28 "github.com/gin-gonic/gin" 29 "github.com/stretchr/testify/suite" 30 "github.com/superseriousbusiness/activity/streams" 31 "github.com/superseriousbusiness/activity/streams/vocab" 32 "github.com/superseriousbusiness/gotosocial/internal/api/activitypub/users" 33 "github.com/superseriousbusiness/gotosocial/testrig" 34 ) 35 36 type UserGetTestSuite struct { 37 UserStandardTestSuite 38 } 39 40 func (suite *UserGetTestSuite) TestGetUser() { 41 // the dereference we're gonna use 42 derefRequests := testrig.NewTestDereferenceRequests(suite.testAccounts) 43 signedRequest := derefRequests["foss_satan_dereference_zork"] 44 targetAccount := suite.testAccounts["local_account_1"] 45 46 // setup request 47 recorder := httptest.NewRecorder() 48 ctx, _ := testrig.CreateGinTestContext(recorder, nil) 49 ctx.Request = httptest.NewRequest(http.MethodGet, targetAccount.URI, nil) // the endpoint we're hitting 50 ctx.Request.Header.Set("accept", "application/activity+json") 51 ctx.Request.Header.Set("Signature", signedRequest.SignatureHeader) 52 ctx.Request.Header.Set("Date", signedRequest.DateHeader) 53 54 // we need to pass the context through signature check first to set appropriate values on it 55 suite.signatureCheck(ctx) 56 57 // normally the router would populate these params from the path values, 58 // but because we're calling the function directly, we need to set them manually. 59 ctx.Params = gin.Params{ 60 gin.Param{ 61 Key: users.UsernameKey, 62 Value: targetAccount.Username, 63 }, 64 } 65 66 // trigger the function being tested 67 suite.userModule.UsersGETHandler(ctx) 68 69 // check response 70 suite.EqualValues(http.StatusOK, recorder.Code) 71 72 result := recorder.Result() 73 defer result.Body.Close() 74 b, err := ioutil.ReadAll(result.Body) 75 suite.NoError(err) 76 77 // should be a Person 78 m := make(map[string]interface{}) 79 err = json.Unmarshal(b, &m) 80 suite.NoError(err) 81 82 t, err := streams.ToType(context.Background(), m) 83 suite.NoError(err) 84 85 person, ok := t.(vocab.ActivityStreamsPerson) 86 suite.True(ok) 87 88 // convert person to account 89 a, err := suite.tc.ASRepresentationToAccount(context.Background(), person, "") 90 suite.NoError(err) 91 suite.EqualValues(targetAccount.Username, a.Username) 92 } 93 94 // TestGetUserPublicKeyDeleted checks whether the public key of a deleted account can still be dereferenced. 95 // This is needed by remote instances for authenticating delete requests and stuff like that. 96 func (suite *UserGetTestSuite) TestGetUserPublicKeyDeleted() { 97 userModule := users.New(suite.processor) 98 targetAccount := suite.testAccounts["local_account_1"] 99 100 suite.processor.Account().DeleteSelf(context.Background(), suite.testAccounts["local_account_1"]) 101 102 // wait for the account delete to be processed 103 if !testrig.WaitFor(func() bool { 104 a, _ := suite.db.GetAccountByID(context.Background(), targetAccount.ID) 105 return !a.SuspendedAt.IsZero() 106 }) { 107 suite.FailNow("delete of account timed out") 108 } 109 110 // the dereference we're gonna use 111 derefRequests := testrig.NewTestDereferenceRequests(suite.testAccounts) 112 signedRequest := derefRequests["foss_satan_dereference_zork_public_key"] 113 114 // setup request 115 recorder := httptest.NewRecorder() 116 ctx, _ := testrig.CreateGinTestContext(recorder, nil) 117 ctx.Request = httptest.NewRequest(http.MethodGet, targetAccount.PublicKeyURI, nil) // the endpoint we're hitting 118 ctx.Request.Header.Set("accept", "application/activity+json") 119 ctx.Request.Header.Set("Signature", signedRequest.SignatureHeader) 120 ctx.Request.Header.Set("Date", signedRequest.DateHeader) 121 122 // we need to pass the context through signature check first to set appropriate values on it 123 suite.signatureCheck(ctx) 124 125 // normally the router would populate these params from the path values, 126 // but because we're calling the function directly, we need to set them manually. 127 ctx.Params = gin.Params{ 128 gin.Param{ 129 Key: users.UsernameKey, 130 Value: targetAccount.Username, 131 }, 132 } 133 134 // trigger the function being tested 135 userModule.UsersGETHandler(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 suite.NoError(err) 144 145 // should be a Person 146 m := make(map[string]interface{}) 147 err = json.Unmarshal(b, &m) 148 suite.NoError(err) 149 150 t, err := streams.ToType(context.Background(), m) 151 suite.NoError(err) 152 153 person, ok := t.(vocab.ActivityStreamsPerson) 154 suite.True(ok) 155 156 // convert person to account 157 a, err := suite.tc.ASRepresentationToAccount(context.Background(), person, "") 158 suite.NoError(err) 159 suite.EqualValues(targetAccount.Username, a.Username) 160 } 161 162 func TestUserGetTestSuite(t *testing.T) { 163 suite.Run(t, new(UserGetTestSuite)) 164 }