reportget_test.go (5219B)
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 "testing" 27 28 "github.com/stretchr/testify/suite" 29 "github.com/superseriousbusiness/gotosocial/internal/api/client/reports" 30 apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" 31 "github.com/superseriousbusiness/gotosocial/internal/config" 32 "github.com/superseriousbusiness/gotosocial/internal/gtserror" 33 "github.com/superseriousbusiness/gotosocial/internal/oauth" 34 "github.com/superseriousbusiness/gotosocial/testrig" 35 ) 36 37 type ReportGetTestSuite struct { 38 ReportsStandardTestSuite 39 } 40 41 func (suite *ReportGetTestSuite) getReport(expectedHTTPStatus int, expectedBody string, reportID string) (*apimodel.Report, error) { 42 // instantiate recorder + test context 43 recorder := httptest.NewRecorder() 44 ctx, _ := testrig.CreateGinTestContext(recorder, nil) 45 ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["local_account_2"]) 46 ctx.Set(oauth.SessionAuthorizedToken, oauth.DBTokenToToken(suite.testTokens["local_account_2"])) 47 ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["application_2"]) 48 ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["local_account_2"]) 49 50 // create the request 51 ctx.Request = httptest.NewRequest(http.MethodGet, config.GetProtocol()+"://"+config.GetHost()+"/api/"+reports.BasePath+"/"+reportID, nil) 52 ctx.Request.Header.Set("accept", "application/json") 53 ctx.AddParam("id", reportID) 54 55 // trigger the handler 56 suite.reportsModule.ReportGETHandler(ctx) 57 58 // read the response 59 result := recorder.Result() 60 defer result.Body.Close() 61 62 b, err := ioutil.ReadAll(result.Body) 63 if err != nil { 64 return nil, err 65 } 66 67 errs := gtserror.MultiError{} 68 69 // check code + body 70 if resultCode := recorder.Code; expectedHTTPStatus != resultCode { 71 errs = append(errs, fmt.Sprintf("expected %d got %d", expectedHTTPStatus, resultCode)) 72 } 73 74 // if we got an expected body, return early 75 if expectedBody != "" { 76 if string(b) != expectedBody { 77 errs = append(errs, fmt.Sprintf("expected %s got %s", expectedBody, string(b))) 78 } 79 return nil, errs.Combine() 80 } 81 82 resp := &apimodel.Report{} 83 if err := json.Unmarshal(b, resp); err != nil { 84 return nil, err 85 } 86 87 return resp, nil 88 } 89 90 func (suite *ReportGetTestSuite) TestGetReport1() { 91 targetReport := suite.testReports["local_account_2_report_remote_account_1"] 92 93 report, err := suite.getReport(http.StatusOK, "", targetReport.ID) 94 suite.NoError(err) 95 suite.NotNil(report) 96 97 b, err := json.MarshalIndent(&report, "", " ") 98 suite.NoError(err) 99 100 suite.Equal(`{ 101 "id": "01GP3AWY4CRDVRNZKW0TEAMB5R", 102 "created_at": "2022-05-14T10:20:03.000Z", 103 "action_taken": false, 104 "action_taken_at": null, 105 "action_taken_comment": null, 106 "category": "other", 107 "comment": "dark souls sucks, please yeet this nerd", 108 "forwarded": true, 109 "status_ids": [ 110 "01FVW7JHQFSFK166WWKR8CBA6M" 111 ], 112 "rule_ids": [], 113 "target_account": { 114 "id": "01F8MH5ZK5VRH73AKHQM6Y9VNX", 115 "username": "foss_satan", 116 "acct": "foss_satan@fossbros-anonymous.io", 117 "display_name": "big gerald", 118 "locked": false, 119 "discoverable": true, 120 "bot": false, 121 "created_at": "2021-09-26T10:52:36.000Z", 122 "note": "i post about like, i dunno, stuff, or whatever!!!!", 123 "url": "http://fossbros-anonymous.io/@foss_satan", 124 "avatar": "", 125 "avatar_static": "", 126 "header": "http://localhost:8080/assets/default_header.png", 127 "header_static": "http://localhost:8080/assets/default_header.png", 128 "followers_count": 0, 129 "following_count": 0, 130 "statuses_count": 1, 131 "last_status_at": "2021-09-20T10:40:37.000Z", 132 "emojis": [], 133 "fields": [] 134 } 135 }`, string(b)) 136 } 137 138 func (suite *ReportGetTestSuite) TestGetReport2() { 139 targetReport := suite.testReports["remote_account_1_report_local_account_2"] 140 report, err := suite.getReport(http.StatusNotFound, `{"error":"Not Found"}`, targetReport.ID) 141 suite.NoError(err) 142 suite.Nil(report) 143 } 144 145 func (suite *ReportGetTestSuite) TestGetReport3() { 146 report, err := suite.getReport(http.StatusBadRequest, `{"error":"Bad Request: no report id specified"}`, "") 147 suite.NoError(err) 148 suite.Nil(report) 149 } 150 151 func (suite *ReportGetTestSuite) TestGetReport4() { 152 report, err := suite.getReport(http.StatusNotFound, `{"error":"Not Found"}`, "01GPJWHQS1BG0SF0WZ1SABC4RZ") 153 suite.NoError(err) 154 suite.Nil(report) 155 } 156 157 func TestReportGetTestSuite(t *testing.T) { 158 suite.Run(t, &ReportGetTestSuite{}) 159 }