authorize_test.go (3742B)
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 followrequests_test 19 20 import ( 21 "bytes" 22 "context" 23 "encoding/json" 24 "fmt" 25 "io/ioutil" 26 "net/http" 27 "net/http/httptest" 28 "testing" 29 "time" 30 31 "github.com/gin-gonic/gin" 32 "github.com/stretchr/testify/suite" 33 "github.com/superseriousbusiness/gotosocial/internal/api/client/followrequests" 34 "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" 35 ) 36 37 type AuthorizeTestSuite struct { 38 FollowRequestStandardTestSuite 39 } 40 41 func (suite *AuthorizeTestSuite) TestAuthorize() { 42 requestingAccount := suite.testAccounts["remote_account_2"] 43 targetAccount := suite.testAccounts["local_account_1"] 44 45 // put a follow request in the database 46 fr := >smodel.FollowRequest{ 47 ID: "01FJ1S8DX3STJJ6CEYPMZ1M0R3", 48 CreatedAt: time.Now(), 49 UpdatedAt: time.Now(), 50 URI: fmt.Sprintf("%s/follow/01FJ1S8DX3STJJ6CEYPMZ1M0R3", requestingAccount.URI), 51 AccountID: requestingAccount.ID, 52 TargetAccountID: targetAccount.ID, 53 } 54 55 err := suite.db.Put(context.Background(), fr) 56 suite.NoError(err) 57 58 recorder := httptest.NewRecorder() 59 ctx := suite.newContext(recorder, http.MethodPost, []byte{}, fmt.Sprintf("/api/v1/follow_requests/%s/authorize", requestingAccount.ID), "") 60 61 ctx.Params = gin.Params{ 62 gin.Param{ 63 Key: followrequests.IDKey, 64 Value: requestingAccount.ID, 65 }, 66 } 67 68 // call the handler 69 suite.followRequestModule.FollowRequestAuthorizePOSTHandler(ctx) 70 71 // 1. we should have OK because our request was valid 72 suite.Equal(http.StatusOK, recorder.Code) 73 74 // 2. we should have no error message in the result body 75 result := recorder.Result() 76 defer result.Body.Close() 77 78 // check the response 79 b, err := ioutil.ReadAll(result.Body) 80 suite.NoError(err) 81 dst := new(bytes.Buffer) 82 err = json.Indent(dst, b, "", " ") 83 suite.NoError(err) 84 suite.Equal(`{ 85 "id": "01FHMQX3GAABWSM0S2VZEC2SWC", 86 "following": false, 87 "showing_reblogs": false, 88 "notifying": false, 89 "followed_by": true, 90 "blocking": false, 91 "blocked_by": false, 92 "muting": false, 93 "muting_notifications": false, 94 "requested": false, 95 "domain_blocking": false, 96 "endorsed": false, 97 "note": "" 98 }`, dst.String()) 99 } 100 101 func (suite *AuthorizeTestSuite) TestAuthorizeNoFR() { 102 requestingAccount := suite.testAccounts["remote_account_2"] 103 104 recorder := httptest.NewRecorder() 105 ctx := suite.newContext(recorder, http.MethodPost, []byte{}, fmt.Sprintf("/api/v1/follow_requests/%s/authorize", requestingAccount.ID), "") 106 107 ctx.Params = gin.Params{ 108 gin.Param{ 109 Key: followrequests.IDKey, 110 Value: requestingAccount.ID, 111 }, 112 } 113 114 // call the handler 115 suite.followRequestModule.FollowRequestAuthorizePOSTHandler(ctx) 116 117 suite.Equal(http.StatusNotFound, recorder.Code) 118 119 result := recorder.Result() 120 defer result.Body.Close() 121 122 // check the response 123 b, err := ioutil.ReadAll(result.Body) 124 suite.NoError(err) 125 126 suite.Equal(`{"error":"Not Found"}`, string(b)) 127 } 128 129 func TestAuthorizeTestSuite(t *testing.T) { 130 suite.Run(t, &AuthorizeTestSuite{}) 131 }