gtsocial-umbx

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

reportsget_test.go (12867B)


      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 reports_test
     19 
     20 import (
     21 	"encoding/json"
     22 	"fmt"
     23 	"io/ioutil"
     24 	"net/http"
     25 	"net/http/httptest"
     26 	"strconv"
     27 	"testing"
     28 
     29 	"github.com/stretchr/testify/suite"
     30 	"github.com/superseriousbusiness/gotosocial/internal/api/client/reports"
     31 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
     32 	"github.com/superseriousbusiness/gotosocial/internal/config"
     33 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
     34 	"github.com/superseriousbusiness/gotosocial/internal/oauth"
     35 	"github.com/superseriousbusiness/gotosocial/testrig"
     36 )
     37 
     38 type ReportsGetTestSuite struct {
     39 	ReportsStandardTestSuite
     40 }
     41 
     42 func (suite *ReportsGetTestSuite) getReports(
     43 	account *gtsmodel.Account,
     44 	token *gtsmodel.Token,
     45 	user *gtsmodel.User,
     46 	expectedHTTPStatus int,
     47 	resolved *bool,
     48 	targetAccountID string,
     49 	maxID string,
     50 	sinceID string,
     51 	minID string,
     52 	limit int,
     53 ) ([]*apimodel.Report, string, error) {
     54 	// instantiate recorder + test context
     55 	recorder := httptest.NewRecorder()
     56 	ctx, _ := testrig.CreateGinTestContext(recorder, nil)
     57 	ctx.Set(oauth.SessionAuthorizedAccount, account)
     58 	ctx.Set(oauth.SessionAuthorizedToken, oauth.DBTokenToToken(token))
     59 	ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["application_1"])
     60 	ctx.Set(oauth.SessionAuthorizedUser, user)
     61 
     62 	// create the request URI
     63 	requestPath := reports.BasePath + "?" + reports.LimitKey + "=" + strconv.Itoa(limit)
     64 	if resolved != nil {
     65 		requestPath = requestPath + "&" + reports.ResolvedKey + "=" + strconv.FormatBool(*resolved)
     66 	}
     67 	if targetAccountID != "" {
     68 		requestPath = requestPath + "&" + reports.TargetAccountIDKey + "=" + targetAccountID
     69 	}
     70 	if maxID != "" {
     71 		requestPath = requestPath + "&" + reports.MaxIDKey + "=" + maxID
     72 	}
     73 	if sinceID != "" {
     74 		requestPath = requestPath + "&" + reports.SinceIDKey + "=" + sinceID
     75 	}
     76 	if minID != "" {
     77 		requestPath = requestPath + "&" + reports.MinIDKey + "=" + minID
     78 	}
     79 	baseURI := config.GetProtocol() + "://" + config.GetHost()
     80 	requestURI := baseURI + "/api/" + requestPath
     81 
     82 	// create the request
     83 	ctx.Request = httptest.NewRequest(http.MethodGet, requestURI, nil)
     84 	ctx.Request.Header.Set("accept", "application/json")
     85 
     86 	// trigger the handler
     87 	suite.reportsModule.ReportsGETHandler(ctx)
     88 
     89 	// read the response
     90 	result := recorder.Result()
     91 	defer result.Body.Close()
     92 
     93 	if resultCode := recorder.Code; expectedHTTPStatus != resultCode {
     94 		return nil, "", fmt.Errorf("expected %d got %d", expectedHTTPStatus, resultCode)
     95 	}
     96 
     97 	b, err := ioutil.ReadAll(result.Body)
     98 	if err != nil {
     99 		return nil, "", err
    100 	}
    101 
    102 	resp := []*apimodel.Report{}
    103 	if err := json.Unmarshal(b, &resp); err != nil {
    104 		return nil, "", err
    105 	}
    106 
    107 	return resp, result.Header.Get("Link"), nil
    108 }
    109 
    110 func (suite *ReportsGetTestSuite) TestGetReports() {
    111 	testAccount := suite.testAccounts["local_account_2"]
    112 	testToken := suite.testTokens["local_account_2"]
    113 	testUser := suite.testUsers["local_account_2"]
    114 
    115 	reports, link, err := suite.getReports(testAccount, testToken, testUser, http.StatusOK, nil, "", "", "", "", 20)
    116 	suite.NoError(err)
    117 	suite.NotEmpty(reports)
    118 
    119 	b, err := json.MarshalIndent(&reports, "", "  ")
    120 	suite.NoError(err)
    121 
    122 	suite.Equal(`[
    123   {
    124     "id": "01GP3AWY4CRDVRNZKW0TEAMB5R",
    125     "created_at": "2022-05-14T10:20:03.000Z",
    126     "action_taken": false,
    127     "action_taken_at": null,
    128     "action_taken_comment": null,
    129     "category": "other",
    130     "comment": "dark souls sucks, please yeet this nerd",
    131     "forwarded": true,
    132     "status_ids": [
    133       "01FVW7JHQFSFK166WWKR8CBA6M"
    134     ],
    135     "rule_ids": [],
    136     "target_account": {
    137       "id": "01F8MH5ZK5VRH73AKHQM6Y9VNX",
    138       "username": "foss_satan",
    139       "acct": "foss_satan@fossbros-anonymous.io",
    140       "display_name": "big gerald",
    141       "locked": false,
    142       "discoverable": true,
    143       "bot": false,
    144       "created_at": "2021-09-26T10:52:36.000Z",
    145       "note": "i post about like, i dunno, stuff, or whatever!!!!",
    146       "url": "http://fossbros-anonymous.io/@foss_satan",
    147       "avatar": "",
    148       "avatar_static": "",
    149       "header": "http://localhost:8080/assets/default_header.png",
    150       "header_static": "http://localhost:8080/assets/default_header.png",
    151       "followers_count": 0,
    152       "following_count": 0,
    153       "statuses_count": 1,
    154       "last_status_at": "2021-09-20T10:40:37.000Z",
    155       "emojis": [],
    156       "fields": []
    157     }
    158   }
    159 ]`, string(b))
    160 
    161 	suite.Equal(`<http://localhost:8080/api/v1/reports?limit=20&max_id=01GP3AWY4CRDVRNZKW0TEAMB5R>; rel="next", <http://localhost:8080/api/v1/reports?limit=20&min_id=01GP3AWY4CRDVRNZKW0TEAMB5R>; rel="prev"`, link)
    162 }
    163 
    164 func (suite *ReportsGetTestSuite) TestGetReports2() {
    165 	testAccount := suite.testAccounts["local_account_2"]
    166 	testToken := suite.testTokens["local_account_2"]
    167 	testUser := suite.testUsers["local_account_2"]
    168 
    169 	reports, link, err := suite.getReports(testAccount, testToken, testUser, http.StatusOK, nil, "", "01GP3AWY4CRDVRNZKW0TEAMB5R", "", "", 20)
    170 	suite.NoError(err)
    171 	suite.Empty(reports)
    172 
    173 	b, err := json.MarshalIndent(&reports, "", "  ")
    174 	suite.NoError(err)
    175 
    176 	suite.Equal(`[]`, string(b))
    177 	suite.Empty(link)
    178 }
    179 
    180 func (suite *ReportsGetTestSuite) TestGetReports3() {
    181 	testAccount := suite.testAccounts["local_account_1"]
    182 	testToken := suite.testTokens["local_account_1"]
    183 	testUser := suite.testUsers["local_account_1"]
    184 
    185 	reports, link, err := suite.getReports(testAccount, testToken, testUser, http.StatusOK, nil, "", "", "", "", 20)
    186 	suite.NoError(err)
    187 	suite.Empty(reports)
    188 
    189 	b, err := json.MarshalIndent(&reports, "", "  ")
    190 	suite.NoError(err)
    191 
    192 	suite.Equal(`[]`, string(b))
    193 	suite.Empty(link)
    194 }
    195 
    196 func (suite *ReportsGetTestSuite) TestGetReports4() {
    197 	testAccount := suite.testAccounts["local_account_2"]
    198 	testToken := suite.testTokens["local_account_2"]
    199 	testUser := suite.testUsers["local_account_2"]
    200 	resolved := testrig.FalseBool()
    201 
    202 	reports, link, err := suite.getReports(testAccount, testToken, testUser, http.StatusOK, resolved, "", "", "", "", 20)
    203 	suite.NoError(err)
    204 	suite.NotEmpty(reports)
    205 
    206 	b, err := json.MarshalIndent(&reports, "", "  ")
    207 	suite.NoError(err)
    208 
    209 	suite.Equal(`[
    210   {
    211     "id": "01GP3AWY4CRDVRNZKW0TEAMB5R",
    212     "created_at": "2022-05-14T10:20:03.000Z",
    213     "action_taken": false,
    214     "action_taken_at": null,
    215     "action_taken_comment": null,
    216     "category": "other",
    217     "comment": "dark souls sucks, please yeet this nerd",
    218     "forwarded": true,
    219     "status_ids": [
    220       "01FVW7JHQFSFK166WWKR8CBA6M"
    221     ],
    222     "rule_ids": [],
    223     "target_account": {
    224       "id": "01F8MH5ZK5VRH73AKHQM6Y9VNX",
    225       "username": "foss_satan",
    226       "acct": "foss_satan@fossbros-anonymous.io",
    227       "display_name": "big gerald",
    228       "locked": false,
    229       "discoverable": true,
    230       "bot": false,
    231       "created_at": "2021-09-26T10:52:36.000Z",
    232       "note": "i post about like, i dunno, stuff, or whatever!!!!",
    233       "url": "http://fossbros-anonymous.io/@foss_satan",
    234       "avatar": "",
    235       "avatar_static": "",
    236       "header": "http://localhost:8080/assets/default_header.png",
    237       "header_static": "http://localhost:8080/assets/default_header.png",
    238       "followers_count": 0,
    239       "following_count": 0,
    240       "statuses_count": 1,
    241       "last_status_at": "2021-09-20T10:40:37.000Z",
    242       "emojis": [],
    243       "fields": []
    244     }
    245   }
    246 ]`, string(b))
    247 
    248 	suite.Equal(`<http://localhost:8080/api/v1/reports?limit=20&max_id=01GP3AWY4CRDVRNZKW0TEAMB5R&resolved=false>; rel="next", <http://localhost:8080/api/v1/reports?limit=20&min_id=01GP3AWY4CRDVRNZKW0TEAMB5R&resolved=false>; rel="prev"`, link)
    249 }
    250 
    251 func (suite *ReportsGetTestSuite) TestGetReports5() {
    252 	testAccount := suite.testAccounts["local_account_1"]
    253 	testToken := suite.testTokens["local_account_1"]
    254 	testUser := suite.testUsers["local_account_1"]
    255 	resolved := testrig.TrueBool()
    256 
    257 	reports, link, err := suite.getReports(testAccount, testToken, testUser, http.StatusOK, resolved, "", "", "", "", 20)
    258 	suite.NoError(err)
    259 	suite.Empty(reports)
    260 
    261 	b, err := json.MarshalIndent(&reports, "", "  ")
    262 	suite.NoError(err)
    263 
    264 	suite.Equal(`[]`, string(b))
    265 	suite.Empty(link)
    266 }
    267 
    268 func (suite *ReportsGetTestSuite) TestGetReports6() {
    269 	testAccount := suite.testAccounts["local_account_2"]
    270 	testToken := suite.testTokens["local_account_2"]
    271 	testUser := suite.testUsers["local_account_2"]
    272 
    273 	reports, link, err := suite.getReports(testAccount, testToken, testUser, http.StatusOK, nil, "01F8MH5ZK5VRH73AKHQM6Y9VNX", "", "", "", 20)
    274 	suite.NoError(err)
    275 	suite.NotEmpty(reports)
    276 
    277 	b, err := json.MarshalIndent(&reports, "", "  ")
    278 	suite.NoError(err)
    279 
    280 	suite.Equal(`[
    281   {
    282     "id": "01GP3AWY4CRDVRNZKW0TEAMB5R",
    283     "created_at": "2022-05-14T10:20:03.000Z",
    284     "action_taken": false,
    285     "action_taken_at": null,
    286     "action_taken_comment": null,
    287     "category": "other",
    288     "comment": "dark souls sucks, please yeet this nerd",
    289     "forwarded": true,
    290     "status_ids": [
    291       "01FVW7JHQFSFK166WWKR8CBA6M"
    292     ],
    293     "rule_ids": [],
    294     "target_account": {
    295       "id": "01F8MH5ZK5VRH73AKHQM6Y9VNX",
    296       "username": "foss_satan",
    297       "acct": "foss_satan@fossbros-anonymous.io",
    298       "display_name": "big gerald",
    299       "locked": false,
    300       "discoverable": true,
    301       "bot": false,
    302       "created_at": "2021-09-26T10:52:36.000Z",
    303       "note": "i post about like, i dunno, stuff, or whatever!!!!",
    304       "url": "http://fossbros-anonymous.io/@foss_satan",
    305       "avatar": "",
    306       "avatar_static": "",
    307       "header": "http://localhost:8080/assets/default_header.png",
    308       "header_static": "http://localhost:8080/assets/default_header.png",
    309       "followers_count": 0,
    310       "following_count": 0,
    311       "statuses_count": 1,
    312       "last_status_at": "2021-09-20T10:40:37.000Z",
    313       "emojis": [],
    314       "fields": []
    315     }
    316   }
    317 ]`, string(b))
    318 
    319 	suite.Equal(`<http://localhost:8080/api/v1/reports?limit=20&max_id=01GP3AWY4CRDVRNZKW0TEAMB5R&target_account_id=01F8MH5ZK5VRH73AKHQM6Y9VNX>; rel="next", <http://localhost:8080/api/v1/reports?limit=20&min_id=01GP3AWY4CRDVRNZKW0TEAMB5R&target_account_id=01F8MH5ZK5VRH73AKHQM6Y9VNX>; rel="prev"`, link)
    320 }
    321 
    322 func (suite *ReportsGetTestSuite) TestGetReports7() {
    323 	testAccount := suite.testAccounts["local_account_2"]
    324 	testToken := suite.testTokens["local_account_2"]
    325 	testUser := suite.testUsers["local_account_2"]
    326 	resolved := testrig.FalseBool()
    327 
    328 	reports, link, err := suite.getReports(testAccount, testToken, testUser, http.StatusOK, resolved, "01F8MH5ZK5VRH73AKHQM6Y9VNX", "", "", "", 20)
    329 	suite.NoError(err)
    330 	suite.NotEmpty(reports)
    331 
    332 	b, err := json.MarshalIndent(&reports, "", "  ")
    333 	suite.NoError(err)
    334 
    335 	suite.Equal(`[
    336   {
    337     "id": "01GP3AWY4CRDVRNZKW0TEAMB5R",
    338     "created_at": "2022-05-14T10:20:03.000Z",
    339     "action_taken": false,
    340     "action_taken_at": null,
    341     "action_taken_comment": null,
    342     "category": "other",
    343     "comment": "dark souls sucks, please yeet this nerd",
    344     "forwarded": true,
    345     "status_ids": [
    346       "01FVW7JHQFSFK166WWKR8CBA6M"
    347     ],
    348     "rule_ids": [],
    349     "target_account": {
    350       "id": "01F8MH5ZK5VRH73AKHQM6Y9VNX",
    351       "username": "foss_satan",
    352       "acct": "foss_satan@fossbros-anonymous.io",
    353       "display_name": "big gerald",
    354       "locked": false,
    355       "discoverable": true,
    356       "bot": false,
    357       "created_at": "2021-09-26T10:52:36.000Z",
    358       "note": "i post about like, i dunno, stuff, or whatever!!!!",
    359       "url": "http://fossbros-anonymous.io/@foss_satan",
    360       "avatar": "",
    361       "avatar_static": "",
    362       "header": "http://localhost:8080/assets/default_header.png",
    363       "header_static": "http://localhost:8080/assets/default_header.png",
    364       "followers_count": 0,
    365       "following_count": 0,
    366       "statuses_count": 1,
    367       "last_status_at": "2021-09-20T10:40:37.000Z",
    368       "emojis": [],
    369       "fields": []
    370     }
    371   }
    372 ]`, string(b))
    373 
    374 	suite.Equal(`<http://localhost:8080/api/v1/reports?limit=20&max_id=01GP3AWY4CRDVRNZKW0TEAMB5R&resolved=false&target_account_id=01F8MH5ZK5VRH73AKHQM6Y9VNX>; rel="next", <http://localhost:8080/api/v1/reports?limit=20&min_id=01GP3AWY4CRDVRNZKW0TEAMB5R&resolved=false&target_account_id=01F8MH5ZK5VRH73AKHQM6Y9VNX>; rel="prev"`, link)
    375 }
    376 
    377 func TestReportsGetTestSuite(t *testing.T) {
    378 	suite.Run(t, &ReportsGetTestSuite{})
    379 }