statusbookmark_test.go (2818B)
1 /* 2 GoToSocial 3 Copyright (C) GoToSocial Authors admin@gotosocial.org 4 This program is free software: you can redistribute it and/or modify 5 it under the terms of the GNU Affero General Public License as published by 6 the Free Software Foundation, either version 3 of the License, or 7 (at your option) any later version. 8 This program is distributed in the hope that it will be useful, 9 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 GNU Affero General Public License for more details. 12 You should have received a copy of the GNU Affero General Public License 13 along with this program. If not, see <http://www.gnu.org/licenses/>. 14 */ 15 16 package statuses_test 17 18 import ( 19 "encoding/json" 20 "fmt" 21 "io/ioutil" 22 "net/http" 23 "net/http/httptest" 24 "strings" 25 "testing" 26 27 "github.com/gin-gonic/gin" 28 "github.com/stretchr/testify/suite" 29 "github.com/superseriousbusiness/gotosocial/internal/api/client/statuses" 30 "github.com/superseriousbusiness/gotosocial/internal/api/model" 31 "github.com/superseriousbusiness/gotosocial/internal/oauth" 32 "github.com/superseriousbusiness/gotosocial/testrig" 33 ) 34 35 type StatusBookmarkTestSuite struct { 36 StatusStandardTestSuite 37 } 38 39 func (suite *StatusBookmarkTestSuite) TestPostBookmark() { 40 t := suite.testTokens["local_account_1"] 41 oauthToken := oauth.DBTokenToToken(t) 42 43 targetStatus := suite.testStatuses["admin_account_status_1"] 44 45 // setup 46 recorder := httptest.NewRecorder() 47 ctx, _ := testrig.CreateGinTestContext(recorder, nil) 48 ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["application_1"]) 49 ctx.Set(oauth.SessionAuthorizedToken, oauthToken) 50 ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["local_account_1"]) 51 ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["local_account_1"]) 52 ctx.Request = httptest.NewRequest(http.MethodPost, fmt.Sprintf("http://localhost:8080%s", strings.Replace(statuses.BookmarkPath, ":id", targetStatus.ID, 1)), nil) // the endpoint we're hitting 53 ctx.Request.Header.Set("accept", "application/json") 54 55 // normally the router would populate these params from the path values, 56 // but because we're calling the function directly, we need to set them manually. 57 ctx.Params = gin.Params{ 58 gin.Param{ 59 Key: statuses.IDKey, 60 Value: targetStatus.ID, 61 }, 62 } 63 64 suite.statusModule.StatusBookmarkPOSTHandler(ctx) 65 66 // check response 67 suite.EqualValues(http.StatusOK, recorder.Code) 68 69 result := recorder.Result() 70 defer result.Body.Close() 71 b, err := ioutil.ReadAll(result.Body) 72 suite.NoError(err) 73 74 statusReply := &model.Status{} 75 err = json.Unmarshal(b, statusReply) 76 suite.NoError(err) 77 78 suite.True(statusReply.Bookmarked) 79 } 80 81 func TestStatusBookmarkTestSuite(t *testing.T) { 82 suite.Run(t, new(StatusBookmarkTestSuite)) 83 }