gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

statusfave_test.go (4804B)


      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 
     34 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
     35 	"github.com/superseriousbusiness/gotosocial/internal/oauth"
     36 	"github.com/superseriousbusiness/gotosocial/testrig"
     37 )
     38 
     39 type StatusFaveTestSuite struct {
     40 	StatusStandardTestSuite
     41 }
     42 
     43 // fave a status
     44 func (suite *StatusFaveTestSuite) TestPostFave() {
     45 	t := suite.testTokens["local_account_1"]
     46 	oauthToken := oauth.DBTokenToToken(t)
     47 
     48 	targetStatus := suite.testStatuses["admin_account_status_2"]
     49 
     50 	// setup
     51 	recorder := httptest.NewRecorder()
     52 	ctx, _ := testrig.CreateGinTestContext(recorder, nil)
     53 	ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["application_1"])
     54 	ctx.Set(oauth.SessionAuthorizedToken, oauthToken)
     55 	ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["local_account_1"])
     56 	ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["local_account_1"])
     57 	ctx.Request = httptest.NewRequest(http.MethodPost, fmt.Sprintf("http://localhost:8080%s", strings.Replace(statuses.FavouritePath, ":id", targetStatus.ID, 1)), nil) // the endpoint we're hitting
     58 	ctx.Request.Header.Set("accept", "application/json")
     59 
     60 	// normally the router would populate these params from the path values,
     61 	// but because we're calling the function directly, we need to set them manually.
     62 	ctx.Params = gin.Params{
     63 		gin.Param{
     64 			Key:   statuses.IDKey,
     65 			Value: targetStatus.ID,
     66 		},
     67 	}
     68 
     69 	suite.statusModule.StatusFavePOSTHandler(ctx)
     70 
     71 	// check response
     72 	suite.EqualValues(http.StatusOK, recorder.Code)
     73 
     74 	result := recorder.Result()
     75 	defer result.Body.Close()
     76 	b, err := ioutil.ReadAll(result.Body)
     77 	assert.NoError(suite.T(), err)
     78 
     79 	statusReply := &apimodel.Status{}
     80 	err = json.Unmarshal(b, statusReply)
     81 	assert.NoError(suite.T(), err)
     82 
     83 	assert.Equal(suite.T(), targetStatus.ContentWarning, statusReply.SpoilerText)
     84 	assert.Equal(suite.T(), targetStatus.Content, statusReply.Content)
     85 	assert.True(suite.T(), statusReply.Sensitive)
     86 	assert.Equal(suite.T(), apimodel.VisibilityPublic, statusReply.Visibility)
     87 	assert.True(suite.T(), statusReply.Favourited)
     88 	assert.Equal(suite.T(), 1, statusReply.FavouritesCount)
     89 }
     90 
     91 // try to fave a status that's not faveable
     92 func (suite *StatusFaveTestSuite) TestPostUnfaveable() {
     93 	t := suite.testTokens["local_account_1"]
     94 	oauthToken := oauth.DBTokenToToken(t)
     95 
     96 	targetStatus := suite.testStatuses["local_account_2_status_3"] // this one is unlikeable and unreplyable
     97 
     98 	// setup
     99 	recorder := httptest.NewRecorder()
    100 	ctx, _ := testrig.CreateGinTestContext(recorder, nil)
    101 	ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["application_1"])
    102 	ctx.Set(oauth.SessionAuthorizedToken, oauthToken)
    103 	ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["local_account_1"])
    104 	ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["local_account_1"])
    105 	ctx.Request = httptest.NewRequest(http.MethodPost, fmt.Sprintf("http://localhost:8080%s", strings.Replace(statuses.FavouritePath, ":id", targetStatus.ID, 1)), nil) // the endpoint we're hitting
    106 	ctx.Request.Header.Set("accept", "application/json")
    107 
    108 	// normally the router would populate these params from the path values,
    109 	// but because we're calling the function directly, we need to set them manually.
    110 	ctx.Params = gin.Params{
    111 		gin.Param{
    112 			Key:   statuses.IDKey,
    113 			Value: targetStatus.ID,
    114 		},
    115 	}
    116 
    117 	suite.statusModule.StatusFavePOSTHandler(ctx)
    118 
    119 	// check response
    120 	suite.EqualValues(http.StatusForbidden, recorder.Code)
    121 
    122 	result := recorder.Result()
    123 	defer result.Body.Close()
    124 	b, err := ioutil.ReadAll(result.Body)
    125 	assert.NoError(suite.T(), err)
    126 	assert.Equal(suite.T(), `{"error":"Forbidden: status is not faveable"}`, string(b))
    127 }
    128 
    129 func TestStatusFaveTestSuite(t *testing.T) {
    130 	suite.Run(t, new(StatusFaveTestSuite))
    131 }