gtsocial-umbx

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

statusunfave_test.go (5362B)


      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 StatusUnfaveTestSuite struct {
     39 	StatusStandardTestSuite
     40 }
     41 
     42 // unfave a status
     43 func (suite *StatusUnfaveTestSuite) TestPostUnfave() {
     44 	t := suite.testTokens["local_account_1"]
     45 	oauthToken := oauth.DBTokenToToken(t)
     46 
     47 	// this is the status we wanna unfave: in the testrig it's already faved by this account
     48 	targetStatus := suite.testStatuses["admin_account_status_1"]
     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.UnfavouritePath, ":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.StatusUnfavePOSTHandler(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.False(suite.T(), statusReply.Sensitive)
     86 	assert.Equal(suite.T(), apimodel.VisibilityPublic, statusReply.Visibility)
     87 	assert.False(suite.T(), statusReply.Favourited)
     88 	assert.Equal(suite.T(), 0, statusReply.FavouritesCount)
     89 }
     90 
     91 // try to unfave a status that's already not faved
     92 func (suite *StatusUnfaveTestSuite) TestPostAlreadyNotFaved() {
     93 	t := suite.testTokens["local_account_1"]
     94 	oauthToken := oauth.DBTokenToToken(t)
     95 
     96 	// this is the status we wanna unfave: in the testrig it's not faved by this account
     97 	targetStatus := suite.testStatuses["admin_account_status_2"]
     98 
     99 	// setup
    100 	recorder := httptest.NewRecorder()
    101 	ctx, _ := testrig.CreateGinTestContext(recorder, nil)
    102 	ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["application_1"])
    103 	ctx.Set(oauth.SessionAuthorizedToken, oauthToken)
    104 	ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["local_account_1"])
    105 	ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["local_account_1"])
    106 	ctx.Request = httptest.NewRequest(http.MethodPost, fmt.Sprintf("http://localhost:8080%s", strings.Replace(statuses.UnfavouritePath, ":id", targetStatus.ID, 1)), nil) // the endpoint we're hitting
    107 	ctx.Request.Header.Set("accept", "application/json")
    108 
    109 	// normally the router would populate these params from the path values,
    110 	// but because we're calling the function directly, we need to set them manually.
    111 	ctx.Params = gin.Params{
    112 		gin.Param{
    113 			Key:   statuses.IDKey,
    114 			Value: targetStatus.ID,
    115 		},
    116 	}
    117 
    118 	suite.statusModule.StatusUnfavePOSTHandler(ctx)
    119 
    120 	// check response
    121 	suite.EqualValues(http.StatusOK, recorder.Code)
    122 
    123 	result := recorder.Result()
    124 	defer result.Body.Close()
    125 	b, err := ioutil.ReadAll(result.Body)
    126 	assert.NoError(suite.T(), err)
    127 
    128 	statusReply := &apimodel.Status{}
    129 	err = json.Unmarshal(b, statusReply)
    130 	assert.NoError(suite.T(), err)
    131 
    132 	assert.Equal(suite.T(), targetStatus.ContentWarning, statusReply.SpoilerText)
    133 	assert.Equal(suite.T(), targetStatus.Content, statusReply.Content)
    134 	assert.True(suite.T(), statusReply.Sensitive)
    135 	assert.Equal(suite.T(), apimodel.VisibilityPublic, statusReply.Visibility)
    136 	assert.False(suite.T(), statusReply.Favourited)
    137 	assert.Equal(suite.T(), 0, statusReply.FavouritesCount)
    138 }
    139 
    140 func TestStatusUnfaveTestSuite(t *testing.T) {
    141 	suite.Run(t, new(StatusUnfaveTestSuite))
    142 }