reportcreate_test.go (7999B)
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 "net/url" 27 "strconv" 28 "testing" 29 30 "github.com/stretchr/testify/suite" 31 "github.com/superseriousbusiness/gotosocial/internal/api/client/reports" 32 apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" 33 "github.com/superseriousbusiness/gotosocial/internal/config" 34 "github.com/superseriousbusiness/gotosocial/internal/gtserror" 35 "github.com/superseriousbusiness/gotosocial/internal/oauth" 36 "github.com/superseriousbusiness/gotosocial/testrig" 37 ) 38 39 type ReportCreateTestSuite struct { 40 ReportsStandardTestSuite 41 } 42 43 func (suite *ReportCreateTestSuite) createReport(expectedHTTPStatus int, expectedBody string, form *apimodel.ReportCreateRequest) (*apimodel.Report, error) { 44 // instantiate recorder + test context 45 recorder := httptest.NewRecorder() 46 ctx, _ := testrig.CreateGinTestContext(recorder, nil) 47 ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["local_account_1"]) 48 ctx.Set(oauth.SessionAuthorizedToken, oauth.DBTokenToToken(suite.testTokens["local_account_1"])) 49 ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["application_1"]) 50 ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["local_account_1"]) 51 52 // create the request 53 ctx.Request = httptest.NewRequest(http.MethodPost, config.GetProtocol()+"://"+config.GetHost()+"/api/"+reports.BasePath, nil) 54 ctx.Request.Header.Set("accept", "application/json") 55 ruleIDs := make([]string, 0, len(form.RuleIDs)) 56 for _, r := range form.RuleIDs { 57 ruleIDs = append(ruleIDs, strconv.Itoa(r)) 58 } 59 ctx.Request.Form = url.Values{ 60 "account_id": {form.AccountID}, 61 "status_ids[]": form.StatusIDs, 62 "comment": {form.Comment}, 63 "forward": {strconv.FormatBool(form.Forward)}, 64 "category": {form.Category}, 65 "rule_ids[]": ruleIDs, 66 } 67 68 // trigger the handler 69 suite.reportsModule.ReportPOSTHandler(ctx) 70 71 // read the response 72 result := recorder.Result() 73 defer result.Body.Close() 74 75 b, err := ioutil.ReadAll(result.Body) 76 if err != nil { 77 return nil, err 78 } 79 80 errs := gtserror.MultiError{} 81 82 // check code + body 83 if resultCode := recorder.Code; expectedHTTPStatus != resultCode { 84 errs = append(errs, fmt.Sprintf("expected %d got %d", expectedHTTPStatus, resultCode)) 85 } 86 87 // if we got an expected body, return early 88 if expectedBody != "" { 89 if string(b) != expectedBody { 90 errs = append(errs, fmt.Sprintf("expected %s got %s", expectedBody, string(b))) 91 } 92 return nil, errs.Combine() 93 } 94 95 resp := &apimodel.Report{} 96 if err := json.Unmarshal(b, resp); err != nil { 97 return nil, err 98 } 99 100 return resp, nil 101 } 102 103 func (suite *ReportCreateTestSuite) ReportOK(form *apimodel.ReportCreateRequest, report *apimodel.Report) { 104 suite.Equal(form.AccountID, report.TargetAccount.ID) 105 suite.Equal(form.StatusIDs, report.StatusIDs) 106 suite.Equal(form.Comment, report.Comment) 107 suite.Equal(form.Forward, report.Forwarded) 108 } 109 110 func (suite *ReportCreateTestSuite) TestCreateReport1() { 111 targetAccount := suite.testAccounts["remote_account_1"] 112 113 form := &apimodel.ReportCreateRequest{ 114 AccountID: targetAccount.ID, 115 StatusIDs: []string{}, 116 Comment: "", 117 Forward: false, 118 } 119 120 report, err := suite.createReport(http.StatusOK, "", form) 121 suite.NoError(err) 122 suite.NotEmpty(report) 123 suite.ReportOK(form, report) 124 } 125 126 func (suite *ReportCreateTestSuite) TestCreateReport2() { 127 targetAccount := suite.testAccounts["remote_account_1"] 128 targetStatus := suite.testStatuses["remote_account_1_status_1"] 129 130 form := &apimodel.ReportCreateRequest{ 131 AccountID: targetAccount.ID, 132 StatusIDs: []string{targetStatus.ID}, 133 Comment: "noooo don't post your so sexy aha", 134 Forward: true, 135 } 136 137 report, err := suite.createReport(http.StatusOK, "", form) 138 suite.NoError(err) 139 suite.NotEmpty(report) 140 suite.ReportOK(form, report) 141 } 142 143 func (suite *ReportCreateTestSuite) TestCreateReport3() { 144 form := &apimodel.ReportCreateRequest{} 145 146 report, err := suite.createReport(http.StatusBadRequest, `{"error":"Bad Request: account_id must be set"}`, form) 147 suite.NoError(err) 148 suite.Nil(report) 149 } 150 151 func (suite *ReportCreateTestSuite) TestCreateReport4() { 152 form := &apimodel.ReportCreateRequest{ 153 AccountID: "boobs", 154 StatusIDs: []string{}, 155 Comment: "", 156 Forward: true, 157 } 158 159 report, err := suite.createReport(http.StatusBadRequest, `{"error":"Bad Request: account_id was not valid"}`, form) 160 suite.NoError(err) 161 suite.Nil(report) 162 } 163 164 func (suite *ReportCreateTestSuite) TestCreateReport5() { 165 testAccount := suite.testAccounts["local_account_1"] 166 form := &apimodel.ReportCreateRequest{ 167 AccountID: testAccount.ID, 168 } 169 170 report, err := suite.createReport(http.StatusBadRequest, `{"error":"Bad Request: cannot report your own account"}`, form) 171 suite.NoError(err) 172 suite.Nil(report) 173 } 174 175 func (suite *ReportCreateTestSuite) TestCreateReport6() { 176 targetAccount := suite.testAccounts["remote_account_1"] 177 178 form := &apimodel.ReportCreateRequest{ 179 AccountID: targetAccount.ID, 180 Comment: "netus et malesuada fames ac turpis egestas sed tempus urna et pharetra pharetra massa massa ultricies mi quis hendrerit dolor magna eget est lorem ipsum dolor sit amet consectetur adipiscing elit pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas integer eget aliquet nibh praesent tristique magna sit amet purus gravida quis blandit turpis cursus in hac habitasse platea dictumst quisque sagittis purus sit amet volutpat consequat mauris nunc congue nisi vitae suscipit tellus mauris a diam maecenas sed enim ut sem viverra aliquet eget sit amet tellus cras adipiscing enim eu turpis egestas pretium aenean pharetra magna ac placerat vestibulum lectus mauris ultrices eros in cursus turpis massa tincidunt dui ut ornare lectus sit amet est placerat in egestas erat imperdiet sed euismod nisi porta lorem mollis aliquam ut porttitor leo a diam sollicitudin tempor id eu nisl nunc mi ipsum faucibus vitae aliquet nec ullamcorper sit amet risus nullam eget felis eget nunc lobortis mattis aliquam faucibus purus in massa tempor nec feugiat nisl pretium fusce id velit ut tortor pretium viverra suspendisse potenti nullam ac tortor vitae purus faucibus ornare suspendisse sed nisi lacus sed viverra tellus in hac habitasse platea dictumst vestibulum rhoncus est pellentesque elit ullamcorper dignissim cras tincidunt lobortis feugiat vivamus at augue eget arcu dictum varius duis at consectetur lorem donec massa sapien faucibus et molestie ac feugiat sed lectus vestibulum mattis ullamcorper velit sed ullamcorper morbi tincidunt ornare massa eget ", 181 } 182 183 report, err := suite.createReport(http.StatusBadRequest, `{"error":"Bad Request: comment length must be no more than 1000 chars, provided comment was 1588 chars"}`, form) 184 suite.NoError(err) 185 suite.Nil(report) 186 } 187 188 func (suite *ReportCreateTestSuite) TestCreateReport7() { 189 form := &apimodel.ReportCreateRequest{ 190 AccountID: "01GPGH5ENXWE5K65YNNXYWAJA4", 191 } 192 193 report, err := suite.createReport(http.StatusBadRequest, `{"error":"Bad Request: account with ID 01GPGH5ENXWE5K65YNNXYWAJA4 does not exist"}`, form) 194 suite.NoError(err) 195 suite.Nil(report) 196 } 197 198 func TestReportCreateTestSuite(t *testing.T) { 199 suite.Run(t, &ReportCreateTestSuite{}) 200 }