report_test.go (5472B)
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 bundb_test 19 20 import ( 21 "context" 22 "testing" 23 24 "github.com/stretchr/testify/suite" 25 "github.com/superseriousbusiness/gotosocial/internal/db" 26 "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" 27 "github.com/superseriousbusiness/gotosocial/testrig" 28 ) 29 30 type ReportTestSuite struct { 31 BunDBStandardTestSuite 32 } 33 34 func (suite *ReportTestSuite) TestGetReportByID() { 35 report, err := suite.db.GetReportByID(context.Background(), suite.testReports["local_account_2_report_remote_account_1"].ID) 36 if err != nil { 37 suite.FailNow(err.Error()) 38 } 39 suite.NotNil(report) 40 suite.NotNil(report.Account) 41 suite.NotNil(report.TargetAccount) 42 suite.Zero(report.ActionTakenAt) 43 suite.Nil(report.ActionTakenByAccount) 44 suite.Empty(report.ActionTakenByAccountID) 45 suite.NotEmpty(report.URI) 46 } 47 48 func (suite *ReportTestSuite) TestGetReportByURI() { 49 report, err := suite.db.GetReportByID(context.Background(), suite.testReports["remote_account_1_report_local_account_2"].ID) 50 if err != nil { 51 suite.FailNow(err.Error()) 52 } 53 suite.NotNil(report) 54 suite.NotNil(report.Account) 55 suite.NotNil(report.TargetAccount) 56 suite.NotZero(report.ActionTakenAt) 57 suite.NotNil(report.ActionTakenByAccount) 58 suite.NotEmpty(report.ActionTakenByAccountID) 59 suite.NotEmpty(report.URI) 60 } 61 62 func (suite *ReportTestSuite) TestGetAllReports() { 63 reports, err := suite.db.GetReports(context.Background(), nil, "", "", "", "", "", 0) 64 suite.NoError(err) 65 suite.NotEmpty(reports) 66 } 67 68 func (suite *ReportTestSuite) TestGetAllReportsByAccountID() { 69 accountID := suite.testAccounts["local_account_2"].ID 70 reports, err := suite.db.GetReports(context.Background(), nil, accountID, "", "", "", "", 0) 71 suite.NoError(err) 72 suite.NotEmpty(reports) 73 for _, r := range reports { 74 suite.Equal(accountID, r.AccountID) 75 } 76 } 77 78 func (suite *ReportTestSuite) TestPutReport() { 79 ctx := context.Background() 80 81 reportID := "01GP3ECY8QJD8DBJSS8B1CR0AX" 82 report := >smodel.Report{ 83 ID: reportID, 84 CreatedAt: testrig.TimeMustParse("2022-05-14T12:20:03+02:00"), 85 UpdatedAt: testrig.TimeMustParse("2022-05-14T12:20:03+02:00"), 86 URI: "http://localhost:8080/01GP3ECY8QJD8DBJSS8B1CR0AX", 87 AccountID: "01F8MH5NBDF2MV7CTC4Q5128HF", 88 TargetAccountID: "01F8MH5ZK5VRH73AKHQM6Y9VNX", 89 Comment: "another report", 90 StatusIDs: []string{"01FVW7JHQFSFK166WWKR8CBA6M"}, 91 Forwarded: testrig.TrueBool(), 92 } 93 94 err := suite.db.PutReport(ctx, report) 95 suite.NoError(err) 96 } 97 98 func (suite *ReportTestSuite) TestUpdateReport() { 99 ctx := context.Background() 100 101 report := >smodel.Report{} 102 *report = *suite.testReports["local_account_2_report_remote_account_1"] 103 report.ActionTaken = "nothing" 104 report.ActionTakenByAccountID = suite.testAccounts["admin_account"].ID 105 report.ActionTakenAt = testrig.TimeMustParse("2022-05-14T12:20:03+02:00") 106 107 if _, err := suite.db.UpdateReport(ctx, report, "action_taken", "action_taken_by_account_id", "action_taken_at"); err != nil { 108 suite.FailNow(err.Error()) 109 } 110 111 dbReport, err := suite.db.GetReportByID(ctx, report.ID) 112 if err != nil { 113 suite.FailNow(err.Error()) 114 } 115 suite.NotNil(dbReport) 116 suite.NotNil(dbReport.Account) 117 suite.NotNil(dbReport.TargetAccount) 118 suite.NotZero(dbReport.ActionTakenAt) 119 suite.NotNil(dbReport.ActionTakenByAccount) 120 suite.NotEmpty(dbReport.ActionTakenByAccountID) 121 suite.NotEmpty(dbReport.URI) 122 } 123 124 func (suite *ReportTestSuite) TestUpdateReportAllColumns() { 125 ctx := context.Background() 126 127 report := >smodel.Report{} 128 *report = *suite.testReports["local_account_2_report_remote_account_1"] 129 report.ActionTaken = "nothing" 130 report.ActionTakenByAccountID = suite.testAccounts["admin_account"].ID 131 report.ActionTakenAt = testrig.TimeMustParse("2022-05-14T12:20:03+02:00") 132 133 if _, err := suite.db.UpdateReport(ctx, report); err != nil { 134 suite.FailNow(err.Error()) 135 } 136 137 dbReport, err := suite.db.GetReportByID(ctx, report.ID) 138 if err != nil { 139 suite.FailNow(err.Error()) 140 } 141 suite.NotNil(dbReport) 142 suite.NotNil(dbReport.Account) 143 suite.NotNil(dbReport.TargetAccount) 144 suite.NotZero(dbReport.ActionTakenAt) 145 suite.NotNil(dbReport.ActionTakenByAccount) 146 suite.NotEmpty(dbReport.ActionTakenByAccountID) 147 suite.NotEmpty(dbReport.URI) 148 } 149 150 func (suite *ReportTestSuite) TestDeleteReport() { 151 if err := suite.db.DeleteReportByID(context.Background(), suite.testReports["remote_account_1_report_local_account_2"].ID); err != nil { 152 suite.FailNow(err.Error()) 153 } 154 155 report, err := suite.db.GetReportByID(context.Background(), suite.testReports["remote_account_1_report_local_account_2"].ID) 156 suite.ErrorIs(err, db.ErrNoEntries) 157 suite.Nil(report) 158 } 159 160 func TestReportTestSuite(t *testing.T) { 161 suite.Run(t, new(ReportTestSuite)) 162 }