follow_test.go (2442B)
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 accounts_test 19 20 import ( 21 "fmt" 22 "io/ioutil" 23 "net/http" 24 "net/http/httptest" 25 "strings" 26 "testing" 27 28 "github.com/gin-gonic/gin" 29 "github.com/stretchr/testify/assert" 30 "github.com/stretchr/testify/suite" 31 "github.com/superseriousbusiness/gotosocial/internal/api/client/accounts" 32 "github.com/superseriousbusiness/gotosocial/internal/oauth" 33 "github.com/superseriousbusiness/gotosocial/testrig" 34 ) 35 36 type FollowTestSuite struct { 37 AccountStandardTestSuite 38 } 39 40 func (suite *FollowTestSuite) TestFollowSelf() { 41 testAcct := suite.testAccounts["local_account_1"] 42 recorder := httptest.NewRecorder() 43 ctx, _ := testrig.CreateGinTestContext(recorder, nil) 44 ctx.Set(oauth.SessionAuthorizedAccount, testAcct) 45 ctx.Set(oauth.SessionAuthorizedToken, oauth.DBTokenToToken(suite.testTokens["local_account_1"])) 46 ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["application_1"]) 47 ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["local_account_1"]) 48 ctx.Request = httptest.NewRequest(http.MethodPost, fmt.Sprintf("http://localhost:8080%s", strings.Replace(accounts.FollowPath, ":id", testAcct.ID, 1)), nil) 49 50 ctx.Params = gin.Params{ 51 gin.Param{ 52 Key: accounts.IDKey, 53 Value: testAcct.ID, 54 }, 55 } 56 57 // call the handler 58 suite.accountsModule.AccountFollowPOSTHandler(ctx) 59 60 // 1. status should be Not Acceptable due to self-follow attempt 61 suite.Equal(http.StatusNotAcceptable, recorder.Code) 62 63 result := recorder.Result() 64 defer result.Body.Close() 65 66 // check the response 67 b, err := ioutil.ReadAll(result.Body) 68 _ = b 69 assert.NoError(suite.T(), err) 70 } 71 72 func TestFollowTestSuite(t *testing.T) { 73 suite.Run(t, new(FollowTestSuite)) 74 }