gtsocial-umbx

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

statusboostedby_test.go (4074B)


      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/stretchr/testify/suite"
     28 	"github.com/superseriousbusiness/gotosocial/internal/api/client/statuses"
     29 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
     30 	"github.com/superseriousbusiness/gotosocial/internal/oauth"
     31 	"github.com/superseriousbusiness/gotosocial/testrig"
     32 )
     33 
     34 type StatusBoostedByTestSuite struct {
     35 	StatusStandardTestSuite
     36 }
     37 
     38 func (suite *StatusBoostedByTestSuite) TestRebloggedByOK() {
     39 	t := suite.testTokens["local_account_1"]
     40 	oauthToken := oauth.DBTokenToToken(t)
     41 	targetStatus := suite.testStatuses["local_account_1_status_1"]
     42 
     43 	recorder := httptest.NewRecorder()
     44 	ctx, _ := testrig.CreateGinTestContext(recorder, nil)
     45 	ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["application_1"])
     46 	ctx.Set(oauth.SessionAuthorizedToken, oauthToken)
     47 	ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["local_account_1"])
     48 	ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["local_account_1"])
     49 	ctx.Request = httptest.NewRequest(http.MethodGet, fmt.Sprintf("http://localhost:8080%s", strings.Replace(statuses.RebloggedPath, ":id", targetStatus.ID, 1)), nil) // the endpoint we're hitting
     50 	ctx.Request.Header.Set("accept", "application/json")
     51 	ctx.AddParam("id", targetStatus.ID)
     52 
     53 	suite.statusModule.StatusBoostedByGETHandler(ctx)
     54 
     55 	suite.EqualValues(http.StatusOK, recorder.Code)
     56 
     57 	result := recorder.Result()
     58 	defer result.Body.Close()
     59 
     60 	b, err := ioutil.ReadAll(result.Body)
     61 	suite.NoError(err)
     62 
     63 	accounts := []*gtsmodel.Account{}
     64 	err = json.Unmarshal(b, &accounts)
     65 	suite.NoError(err)
     66 
     67 	if !suite.Len(accounts, 1) {
     68 		suite.FailNow("should have had 1 account")
     69 	}
     70 
     71 	suite.Equal(accounts[0].ID, suite.testAccounts["admin_account"].ID)
     72 }
     73 
     74 func (suite *StatusBoostedByTestSuite) TestRebloggedByUseBoostWrapperID() {
     75 	t := suite.testTokens["local_account_1"]
     76 	oauthToken := oauth.DBTokenToToken(t)
     77 	targetStatus := suite.testStatuses["admin_account_status_4"] // admin_account_status_4 is a boost of local_account_1_status_1
     78 
     79 	recorder := httptest.NewRecorder()
     80 	ctx, _ := testrig.CreateGinTestContext(recorder, nil)
     81 	ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["application_1"])
     82 	ctx.Set(oauth.SessionAuthorizedToken, oauthToken)
     83 	ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["local_account_1"])
     84 	ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["local_account_1"])
     85 	ctx.Request = httptest.NewRequest(http.MethodGet, fmt.Sprintf("http://localhost:8080%s", strings.Replace(statuses.RebloggedPath, ":id", targetStatus.ID, 1)), nil) // the endpoint we're hitting
     86 	ctx.Request.Header.Set("accept", "application/json")
     87 	ctx.AddParam("id", targetStatus.ID)
     88 
     89 	suite.statusModule.StatusBoostedByGETHandler(ctx)
     90 
     91 	suite.EqualValues(http.StatusOK, recorder.Code)
     92 
     93 	result := recorder.Result()
     94 	defer result.Body.Close()
     95 
     96 	b, err := ioutil.ReadAll(result.Body)
     97 	suite.NoError(err)
     98 
     99 	accounts := []*gtsmodel.Account{}
    100 	err = json.Unmarshal(b, &accounts)
    101 	suite.NoError(err)
    102 
    103 	if !suite.Len(accounts, 1) {
    104 		suite.FailNow("should have had 1 account")
    105 	}
    106 
    107 	suite.Equal(accounts[0].ID, suite.testAccounts["admin_account"].ID)
    108 }
    109 
    110 func TestStatusBoostedByTestSuite(t *testing.T) {
    111 	suite.Run(t, new(StatusBoostedByTestSuite))
    112 }