statusfavedby_test.go (3029B)
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 statuses_test 19 20 import ( 21 "encoding/json" 22 "fmt" 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/assert" 31 "github.com/stretchr/testify/suite" 32 "github.com/superseriousbusiness/gotosocial/internal/api/client/statuses" 33 apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" 34 "github.com/superseriousbusiness/gotosocial/internal/oauth" 35 "github.com/superseriousbusiness/gotosocial/testrig" 36 ) 37 38 type StatusFavedByTestSuite struct { 39 StatusStandardTestSuite 40 } 41 42 func (suite *StatusFavedByTestSuite) TestGetFavedBy() { 43 t := suite.testTokens["local_account_2"] 44 oauthToken := oauth.DBTokenToToken(t) 45 46 targetStatus := suite.testStatuses["admin_account_status_1"] // this status is faved by local_account_1 47 48 // setup 49 recorder := httptest.NewRecorder() 50 ctx, _ := testrig.CreateGinTestContext(recorder, nil) 51 ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["application_2"]) 52 ctx.Set(oauth.SessionAuthorizedToken, oauthToken) 53 ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["local_account_2"]) 54 ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["local_account_2"]) 55 ctx.Request = httptest.NewRequest(http.MethodPost, fmt.Sprintf("http://localhost:8080%s", strings.Replace(statuses.FavouritedPath, ":id", targetStatus.ID, 1)), nil) // the endpoint we're hitting 56 ctx.Request.Header.Set("accept", "application/json") 57 58 // normally the router would populate these params from the path values, 59 // but because we're calling the function directly, we need to set them manually. 60 ctx.Params = gin.Params{ 61 gin.Param{ 62 Key: statuses.IDKey, 63 Value: targetStatus.ID, 64 }, 65 } 66 67 suite.statusModule.StatusFavedByGETHandler(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 assert.NoError(suite.T(), err) 76 77 accts := []apimodel.Account{} 78 err = json.Unmarshal(b, &accts) 79 assert.NoError(suite.T(), err) 80 81 assert.Len(suite.T(), accts, 1) 82 assert.Equal(suite.T(), "the_mighty_zork", accts[0].Username) 83 } 84 85 func TestStatusFavedByTestSuite(t *testing.T) { 86 suite.Run(t, new(StatusFavedByTestSuite)) 87 }