reportresolve_test.go (5795B)
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 admin_test 19 20 import ( 21 "encoding/json" 22 "fmt" 23 "io/ioutil" 24 "net/http" 25 "net/http/httptest" 26 "net/url" 27 "testing" 28 "time" 29 30 "github.com/stretchr/testify/suite" 31 "github.com/superseriousbusiness/gotosocial/internal/api/client/admin" 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/gtsmodel" 36 "github.com/superseriousbusiness/gotosocial/internal/oauth" 37 "github.com/superseriousbusiness/gotosocial/internal/util" 38 "github.com/superseriousbusiness/gotosocial/testrig" 39 ) 40 41 type ReportResolveTestSuite struct { 42 AdminStandardTestSuite 43 } 44 45 func (suite *ReportResolveTestSuite) resolveReport( 46 account *gtsmodel.Account, 47 token *gtsmodel.Token, 48 user *gtsmodel.User, 49 targetReportID string, 50 expectedHTTPStatus int, 51 expectedBody string, 52 actionTakenComment *string, 53 ) (*apimodel.AdminReport, 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 := admin.ReportsPath + "/" + targetReportID + "/resolve" 64 baseURI := config.GetProtocol() + "://" + config.GetHost() 65 requestURI := baseURI + "/api/" + requestPath 66 67 // create the request 68 ctx.Request = httptest.NewRequest(http.MethodPost, requestURI, nil) 69 ctx.AddParam(admin.IDKey, targetReportID) 70 ctx.Request.Header.Set("accept", "application/json") 71 if actionTakenComment != nil { 72 ctx.Request.Form = url.Values{"action_taken_comment": {*actionTakenComment}} 73 } 74 75 // trigger the handler 76 suite.adminModule.ReportResolvePOSTHandler(ctx) 77 78 // read the response 79 result := recorder.Result() 80 defer result.Body.Close() 81 82 b, err := ioutil.ReadAll(result.Body) 83 if err != nil { 84 return nil, err 85 } 86 87 errs := gtserror.MultiError{} 88 89 if resultCode := recorder.Code; expectedHTTPStatus != resultCode { 90 errs = append(errs, fmt.Sprintf("expected %d got %d", expectedHTTPStatus, resultCode)) 91 } 92 93 // if we got an expected body, return early 94 if expectedBody != "" { 95 if string(b) != expectedBody { 96 errs = append(errs, fmt.Sprintf("expected %s got %s", expectedBody, string(b))) 97 } 98 return nil, errs.Combine() 99 } 100 101 resp := &apimodel.AdminReport{} 102 if err := json.Unmarshal(b, &resp); err != nil { 103 return nil, err 104 } 105 106 return resp, nil 107 } 108 109 func (suite *ReportResolveTestSuite) TestReportResolve1() { 110 testAccount := suite.testAccounts["admin_account"] 111 testToken := suite.testTokens["admin_account"] 112 testUser := suite.testUsers["admin_account"] 113 testReportID := suite.testReports["local_account_2_report_remote_account_1"].ID 114 var actionTakenComment *string = nil 115 116 report, err := suite.resolveReport(testAccount, testToken, testUser, testReportID, http.StatusOK, "", actionTakenComment) 117 suite.NoError(err) 118 suite.NotEmpty(report) 119 120 // report should be resolved 121 suite.True(report.ActionTaken) 122 actionTime, err := util.ParseISO8601(*report.ActionTakenAt) 123 if err != nil { 124 suite.FailNow(err.Error()) 125 } 126 suite.WithinDuration(time.Now(), actionTime, 1*time.Minute) 127 updatedTime, err := util.ParseISO8601(report.UpdatedAt) 128 if err != nil { 129 suite.FailNow(err.Error()) 130 } 131 suite.WithinDuration(time.Now(), updatedTime, 1*time.Minute) 132 suite.Equal(report.ActionTakenByAccount.ID, testAccount.ID) 133 suite.EqualValues(report.ActionTakenComment, actionTakenComment) 134 suite.EqualValues(report.AssignedAccount.ID, testAccount.ID) 135 } 136 137 func (suite *ReportResolveTestSuite) TestReportResolve2() { 138 testAccount := suite.testAccounts["admin_account"] 139 testToken := suite.testTokens["admin_account"] 140 testUser := suite.testUsers["admin_account"] 141 testReportID := suite.testReports["local_account_2_report_remote_account_1"].ID 142 var actionTakenComment *string = testrig.StringPtr("no action was taken, this is a frivolous report you boob") 143 144 report, err := suite.resolveReport(testAccount, testToken, testUser, testReportID, http.StatusOK, "", actionTakenComment) 145 suite.NoError(err) 146 suite.NotEmpty(report) 147 148 // report should be resolved 149 suite.True(report.ActionTaken) 150 actionTime, err := util.ParseISO8601(*report.ActionTakenAt) 151 if err != nil { 152 suite.FailNow(err.Error()) 153 } 154 suite.WithinDuration(time.Now(), actionTime, 1*time.Minute) 155 updatedTime, err := util.ParseISO8601(report.UpdatedAt) 156 if err != nil { 157 suite.FailNow(err.Error()) 158 } 159 suite.WithinDuration(time.Now(), updatedTime, 1*time.Minute) 160 suite.Equal(report.ActionTakenByAccount.ID, testAccount.ID) 161 suite.EqualValues(report.ActionTakenComment, actionTakenComment) 162 suite.EqualValues(report.AssignedAccount.ID, testAccount.ID) 163 } 164 165 func TestReportResolveTestSuite(t *testing.T) { 166 suite.Run(t, &ReportResolveTestSuite{}) 167 }