porting all github actions from bluesky-social/indigo to tangled CI
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

labeling: refactor tests a bit

+307 -251
+115
labeling/helpers_test.go
··· 1 + package labeling 2 + 3 + import ( 4 + "encoding/json" 5 + "net/http" 6 + "net/http/httptest" 7 + "net/url" 8 + "strconv" 9 + "strings" 10 + "testing" 11 + 12 + "github.com/labstack/echo/v4" 13 + "github.com/stretchr/testify/assert" 14 + 15 + comatproto "github.com/bluesky-social/indigo/api/atproto" 16 + ) 17 + 18 + // fetches report, both getModerationReport and getModerationReports, verifies match 19 + func testGetReport(t *testing.T, e *echo.Echo, lm *Server, reportId int64) comatproto.AdminDefs_ReportViewDetail { 20 + assert := assert.New(t) 21 + 22 + params := make(url.Values) 23 + params.Set("id", strconv.Itoa(int(reportId))) 24 + req := httptest.NewRequest(http.MethodGet, "/xrpc/com.atproto.admin.getModerationReport?"+params.Encode(), nil) 25 + recorder := httptest.NewRecorder() 26 + c := e.NewContext(req, recorder) 27 + assert.NoError(lm.HandleComAtprotoAdminGetModerationReport(c)) 28 + assert.Equal(200, recorder.Code) 29 + var reportViewDetail comatproto.AdminDefs_ReportViewDetail 30 + if err := json.Unmarshal([]byte(recorder.Body.String()), &reportViewDetail); err != nil { 31 + t.Fatal(err) 32 + } 33 + assert.Equal(reportId, reportViewDetail.Id) 34 + 35 + // read back (getModerationReports) and verify output 36 + // TODO: include 'subject' param 37 + req = httptest.NewRequest(http.MethodGet, "/xrpc/com.atproto.admin.getModerationReports", nil) 38 + recorder = httptest.NewRecorder() 39 + c = e.NewContext(req, recorder) 40 + assert.NoError(lm.HandleComAtprotoAdminGetModerationReports(c)) 41 + assert.Equal(200, recorder.Code) 42 + var reportsOut comatproto.AdminGetModerationReports_Output 43 + if err := json.Unmarshal([]byte(recorder.Body.String()), &reportsOut); err != nil { 44 + t.Fatal(err) 45 + } 46 + var reportView *comatproto.AdminDefs_ReportView 47 + for _, rv := range reportsOut.Reports { 48 + if rv.Id == reportId { 49 + reportView = rv 50 + break 51 + } 52 + } 53 + if reportView == nil { 54 + t.Fatal("expected to find report by subject") 55 + } 56 + 57 + assert.Equal(reportViewDetail.Id, reportView.Id) 58 + assert.Equal(reportViewDetail.CreatedAt, reportView.CreatedAt) 59 + assert.Equal(reportViewDetail.Reason, reportView.Reason) 60 + assert.Equal(reportViewDetail.ReasonType, reportView.ReasonType) 61 + assert.Equal(reportViewDetail.ReportedBy, reportView.ReportedBy) 62 + assert.Equal(len(reportViewDetail.ResolvedByActions), len(reportView.ResolvedByActionIds)) 63 + for i, actionId := range reportView.ResolvedByActionIds { 64 + assert.Equal(actionId, reportViewDetail.ResolvedByActions[i].Id) 65 + } 66 + if reportViewDetail.Subject.AdminDefs_RepoView != nil { 67 + assert.Equal(reportViewDetail.Subject.AdminDefs_RepoView.Did, reportView.Subject.AdminDefs_RepoRef.Did) 68 + } else if reportViewDetail.Subject.AdminDefs_RecordView != nil { 69 + assert.Equal(reportViewDetail.Subject.AdminDefs_RecordView.Uri, reportView.Subject.RepoStrongRef.Uri) 70 + assert.Equal(reportViewDetail.Subject.AdminDefs_RecordView.Cid, reportView.Subject.RepoStrongRef.Cid) 71 + } else { 72 + t.Fatal("expected non-empty reportviewdetail.subject enum") 73 + } 74 + 75 + return reportViewDetail 76 + } 77 + 78 + // "happy path" test helper. creates a report, reads it back 2x ways, verifies match, then returns the original output 79 + func testCreateReport(t *testing.T, e *echo.Echo, lm *Server, input *comatproto.ModerationCreateReport_Input) comatproto.ModerationCreateReport_Output { 80 + assert := assert.New(t) 81 + 82 + // create report and verify output 83 + reportJSON, err := json.Marshal(input) 84 + if err != nil { 85 + t.Fatal(err) 86 + } 87 + req := httptest.NewRequest(http.MethodPost, "/xrpc/com.atproto.report.create", strings.NewReader(string(reportJSON))) 88 + req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) 89 + recorder := httptest.NewRecorder() 90 + c := e.NewContext(req, recorder) 91 + 92 + assert.NoError(lm.HandleComAtprotoReportCreate(c)) 93 + assert.Equal(200, recorder.Code) 94 + 95 + var out comatproto.ModerationCreateReport_Output 96 + if err := json.Unmarshal([]byte(recorder.Body.String()), &out); err != nil { 97 + t.Fatal(err) 98 + } 99 + assert.Equal(input.Reason, out.Reason) 100 + assert.Equal(input.ReasonType, out.ReasonType) 101 + assert.Equal(input.Subject.RepoStrongRef, out.Subject.RepoStrongRef) 102 + assert.Equal(input.Subject.AdminDefs_RepoRef, out.Subject.AdminDefs_RepoRef) 103 + 104 + // read it back and verify output 105 + reportViewDetail := testGetReport(t, e, lm, out.Id) 106 + assert.Equal(out.Id, reportViewDetail.Id) 107 + assert.Equal(out.CreatedAt, reportViewDetail.CreatedAt) 108 + assert.Equal(out.Reason, reportViewDetail.Reason) 109 + assert.Equal(out.ReasonType, reportViewDetail.ReasonType) 110 + assert.Equal(0, len(reportViewDetail.ResolvedByActions)) 111 + // XXX: Subject 112 + // XXX: ReportedBy 113 + 114 + return out 115 + }
-251
labeling/moderation_test.go
··· 1 - package labeling 2 - 3 - import ( 4 - "encoding/json" 5 - "net/http" 6 - "net/http/httptest" 7 - "net/url" 8 - "strconv" 9 - "strings" 10 - "testing" 11 - 12 - "github.com/labstack/echo/v4" 13 - "github.com/stretchr/testify/assert" 14 - 15 - comatproto "github.com/bluesky-social/indigo/api/atproto" 16 - ) 17 - 18 - func TestLabelMakerXRPCReportRepo(t *testing.T) { 19 - e := echo.New() 20 - lm := testLabelMaker(t) 21 - 22 - // create and read back a basic repo report 23 - rt := "spam" 24 - reportedDid := "did:plc:123" 25 - report := comatproto.ModerationCreateReport_Input{ 26 - //Reason 27 - ReasonType: &rt, 28 - Subject: &comatproto.ModerationCreateReport_Input_Subject{ 29 - AdminDefs_RepoRef: &comatproto.AdminDefs_RepoRef{ 30 - Did: reportedDid, 31 - }, 32 - }, 33 - } 34 - reportJSON, err := json.Marshal(report) 35 - if err != nil { 36 - t.Fatal(err) 37 - } 38 - req := httptest.NewRequest(http.MethodPost, "/xrpc/com.atproto.report.create", strings.NewReader(string(reportJSON))) 39 - req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) 40 - recorder := httptest.NewRecorder() 41 - c := e.NewContext(req, recorder) 42 - 43 - assert.NoError(t, lm.HandleComAtprotoReportCreate(c)) 44 - // TODO: "Created" / 201 45 - assert.Equal(t, 200, recorder.Code) 46 - 47 - var out comatproto.ModerationCreateReport_Output 48 - if err := json.Unmarshal([]byte(recorder.Body.String()), &out); err != nil { 49 - t.Fatal(err) 50 - } 51 - assert.Equal(t, report.ReasonType, out.ReasonType) 52 - assert.Equal(t, report.Subject.AdminDefs_RepoRef, out.Subject.AdminDefs_RepoRef) 53 - reportId := out.Id 54 - 55 - // read it back 56 - params := make(url.Values) 57 - params.Set("id", strconv.Itoa(int(reportId))) 58 - req = httptest.NewRequest(http.MethodGet, "/xrpc/com.atproto.admin.getModerationReport?"+params.Encode(), nil) 59 - recorder = httptest.NewRecorder() 60 - c = e.NewContext(req, recorder) 61 - assert.NoError(t, lm.HandleComAtprotoAdminGetModerationReport(c)) 62 - assert.Equal(t, 200, recorder.Code) 63 - var vd comatproto.AdminDefs_ReportViewDetail 64 - if err := json.Unmarshal([]byte(recorder.Body.String()), &vd); err != nil { 65 - t.Fatal(err) 66 - } 67 - assert.Equal(t, vd.Id, reportId, vd.Id) 68 - assert.Equal(t, vd.ReasonType, report.ReasonType) 69 - assert.Nil(t, vd.Reason) 70 - assert.Equal(t, vd.Subject.AdminDefs_RepoView.Did, reportedDid) 71 - assert.Nil(t, vd.Subject.AdminDefs_RecordView) 72 - // TODO: additional AdminDefs_RecordView fields 73 - 74 - // read back via get multi 75 - req = httptest.NewRequest(http.MethodGet, "/xrpc/com.atproto.admin.getModerationReports", nil) 76 - recorder = httptest.NewRecorder() 77 - c = e.NewContext(req, recorder) 78 - assert.NoError(t, lm.HandleComAtprotoAdminGetModerationReports(c)) 79 - assert.Equal(t, 200, recorder.Code) 80 - var reportsOut comatproto.AdminGetModerationReports_Output 81 - if err := json.Unmarshal([]byte(recorder.Body.String()), &reportsOut); err != nil { 82 - t.Fatal(err) 83 - } 84 - assert.Equal(t, len(reportsOut.Reports), 1) 85 - assert.Equal(t, reportsOut.Reports[0].Id, reportId) 86 - 87 - } 88 - 89 - func TestLabelMakerXRPCReportRepoBad(t *testing.T) { 90 - e := echo.New() 91 - lm := testLabelMaker(t) 92 - 93 - table := []struct { 94 - rType string 95 - rDid string 96 - statusCode int 97 - }{ 98 - {"spam", "did:plc:123", 200}, 99 - {"", "did:plc:123", 400}, 100 - {"spam", "", 400}, 101 - } 102 - 103 - for _, row := range table { 104 - 105 - report := comatproto.ModerationCreateReport_Input{ 106 - //Reason 107 - ReasonType: &row.rType, 108 - Subject: &comatproto.ModerationCreateReport_Input_Subject{ 109 - AdminDefs_RepoRef: &comatproto.AdminDefs_RepoRef{ 110 - Did: row.rDid, 111 - }, 112 - }, 113 - } 114 - reportJSON, err := json.Marshal(report) 115 - if err != nil { 116 - t.Fatal(err) 117 - } 118 - req := httptest.NewRequest(http.MethodPost, "/xrpc/com.atproto.report.create", strings.NewReader(string(reportJSON))) 119 - req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) 120 - recorder := httptest.NewRecorder() 121 - c := e.NewContext(req, recorder) 122 - err = lm.HandleComAtprotoReportCreate(c) 123 - if err != nil { 124 - httpError, _ := err.(*echo.HTTPError) 125 - assert.Equal(t, row.statusCode, httpError.Code) 126 - } else { 127 - assert.Equal(t, row.statusCode, recorder.Code) 128 - } 129 - } 130 - } 131 - 132 - func TestLabelMakerXRPCReportRecord(t *testing.T) { 133 - e := echo.New() 134 - lm := testLabelMaker(t) 135 - // create a second report, on a record 136 - rt := "spam" 137 - reason := "I just don't like it!" 138 - uri := "at://did:plc:123/com.example.record/bcd234" 139 - cid := "bafyreie5cvv4h45feadgeuwhbcutmh6t2ceseocckahdoe6uat64zmz454" 140 - report := comatproto.ModerationCreateReport_Input{ 141 - Reason: &reason, 142 - ReasonType: &rt, 143 - Subject: &comatproto.ModerationCreateReport_Input_Subject{ 144 - RepoStrongRef: &comatproto.RepoStrongRef{ 145 - //com.atproto.repo.strongRef 146 - Uri: uri, 147 - Cid: cid, 148 - }, 149 - }, 150 - } 151 - reportJSON, err := json.Marshal(report) 152 - if err != nil { 153 - t.Fatal(err) 154 - } 155 - req := httptest.NewRequest(http.MethodPost, "/xrpc/com.atproto.report.create", strings.NewReader(string(reportJSON))) 156 - req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) 157 - recorder := httptest.NewRecorder() 158 - c := e.NewContext(req, recorder) 159 - 160 - assert.NoError(t, lm.HandleComAtprotoReportCreate(c)) 161 - // TODO: "Created" / 201 162 - assert.Equal(t, 200, recorder.Code) 163 - 164 - var out comatproto.ModerationCreateReport_Output 165 - if err := json.Unmarshal([]byte(recorder.Body.String()), &out); err != nil { 166 - t.Fatal(err) 167 - } 168 - assert.Equal(t, report.ReasonType, out.ReasonType) 169 - assert.Equal(t, report.Subject.AdminDefs_RepoRef, out.Subject.AdminDefs_RepoRef) 170 - reportId := out.Id 171 - 172 - // read it back 173 - params := make(url.Values) 174 - params.Set("id", strconv.Itoa(int(reportId))) 175 - req = httptest.NewRequest(http.MethodGet, "/xrpc/com.atproto.admin.getModerationReport?"+params.Encode(), nil) 176 - recorder = httptest.NewRecorder() 177 - c = e.NewContext(req, recorder) 178 - assert.NoError(t, lm.HandleComAtprotoAdminGetModerationReport(c)) 179 - assert.Equal(t, 200, recorder.Code) 180 - var vd comatproto.AdminDefs_ReportViewDetail 181 - if err := json.Unmarshal([]byte(recorder.Body.String()), &vd); err != nil { 182 - t.Fatal(err) 183 - } 184 - assert.Equal(t, reportId, vd.Id) 185 - assert.Equal(t, *report.Reason, reason) 186 - assert.Equal(t, report.ReasonType, vd.ReasonType) 187 - } 188 - 189 - func TestLabelMakerXRPCReportRecordBad(t *testing.T) { 190 - e := echo.New() 191 - lm := testLabelMaker(t) 192 - 193 - uriStr := "at://did:plc:123/com.example.record/bcd234" 194 - cidStr := "bafyreie5cvv4h45feadgeuwhbcutmh6t2ceseocckahdoe6uat64zmz454" 195 - emptyStr := "" 196 - table := []struct { 197 - rType string 198 - rUri string 199 - rCid string 200 - statusCode int 201 - }{ 202 - {"spam", uriStr, cidStr, 200}, 203 - {"", uriStr, cidStr, 400}, 204 - {"spam", "", cidStr, 400}, 205 - {"spam", uriStr, emptyStr, 400}, 206 - } 207 - 208 - for _, row := range table { 209 - 210 - report := comatproto.ModerationCreateReport_Input{ 211 - ReasonType: &row.rType, 212 - Subject: &comatproto.ModerationCreateReport_Input_Subject{ 213 - RepoStrongRef: &comatproto.RepoStrongRef{ 214 - //com.atproto.repo.strongRef 215 - Uri: row.rUri, 216 - Cid: row.rCid, 217 - }, 218 - }, 219 - } 220 - reportJSON, err := json.Marshal(report) 221 - if err != nil { 222 - t.Fatal(err) 223 - } 224 - req := httptest.NewRequest(http.MethodPost, "/xrpc/com.atproto.report.create", strings.NewReader(string(reportJSON))) 225 - req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) 226 - recorder := httptest.NewRecorder() 227 - c := e.NewContext(req, recorder) 228 - err = lm.HandleComAtprotoReportCreate(c) 229 - if err != nil { 230 - httpError, _ := err.(*echo.HTTPError) 231 - assert.Equal(t, row.statusCode, httpError.Code) 232 - } else { 233 - assert.Equal(t, row.statusCode, recorder.Code) 234 - } 235 - } 236 - } 237 - 238 - func TestLabelMakerXRPCReportAction(t *testing.T) { 239 - //e := echo.New() 240 - //lm := testLabelMaker(t) 241 - 242 - // XXX: create report 243 - // XXX: action report 244 - // XXX: get action 245 - // XXX: get actions (plural) 246 - // XXX: get report (should have action included) 247 - // XXX: reverse action 248 - // XXX: get action 249 - // XXX: get actions (plural) 250 - // XXX: get report (should not have action included) 251 - }
+192
labeling/xrpc_test.go
··· 1 + package labeling 2 + 3 + import ( 4 + "encoding/json" 5 + "net/http" 6 + "net/http/httptest" 7 + "strings" 8 + "testing" 9 + 10 + "github.com/labstack/echo/v4" 11 + "github.com/stretchr/testify/assert" 12 + 13 + comatproto "github.com/bluesky-social/indigo/api/atproto" 14 + ) 15 + 16 + func TestLabelMakerXRPCReportRepo(t *testing.T) { 17 + assert := assert.New(t) 18 + e := echo.New() 19 + lm := testLabelMaker(t) 20 + 21 + // create and read back a basic repo report 22 + rt := "spam" 23 + reportedDid := "did:plc:123" 24 + report := comatproto.ModerationCreateReport_Input{ 25 + //Reason 26 + ReasonType: &rt, 27 + Subject: &comatproto.ModerationCreateReport_Input_Subject{ 28 + AdminDefs_RepoRef: &comatproto.AdminDefs_RepoRef{ 29 + Did: reportedDid, 30 + }, 31 + }, 32 + } 33 + out := testCreateReport(t, e, lm, &report) 34 + assert.Equal(&rt, out.ReasonType) 35 + assert.Nil(out.Reason) 36 + assert.Equal(reportedDid, out.Subject.AdminDefs_RepoRef.Did) 37 + } 38 + 39 + func TestLabelMakerXRPCReportRepoBad(t *testing.T) { 40 + assert := assert.New(t) 41 + e := echo.New() 42 + lm := testLabelMaker(t) 43 + 44 + table := []struct { 45 + rType string 46 + rDid string 47 + statusCode int 48 + }{ 49 + {"spam", "did:plc:123", 200}, 50 + {"", "did:plc:123", 400}, 51 + {"spam", "", 400}, 52 + } 53 + 54 + for _, row := range table { 55 + 56 + report := comatproto.ModerationCreateReport_Input{ 57 + //Reason 58 + ReasonType: &row.rType, 59 + Subject: &comatproto.ModerationCreateReport_Input_Subject{ 60 + AdminDefs_RepoRef: &comatproto.AdminDefs_RepoRef{ 61 + Did: row.rDid, 62 + }, 63 + }, 64 + } 65 + reportJSON, err := json.Marshal(report) 66 + if err != nil { 67 + t.Fatal(err) 68 + } 69 + req := httptest.NewRequest(http.MethodPost, "/xrpc/com.atproto.report.create", strings.NewReader(string(reportJSON))) 70 + req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) 71 + recorder := httptest.NewRecorder() 72 + c := e.NewContext(req, recorder) 73 + err = lm.HandleComAtprotoReportCreate(c) 74 + if err != nil { 75 + httpError, _ := err.(*echo.HTTPError) 76 + assert.Equal(row.statusCode, httpError.Code) 77 + } else { 78 + assert.Equal(row.statusCode, recorder.Code) 79 + } 80 + } 81 + } 82 + 83 + func TestLabelMakerXRPCReportRecord(t *testing.T) { 84 + assert := assert.New(t) 85 + e := echo.New() 86 + lm := testLabelMaker(t) 87 + // create a second report, on a record 88 + rt := "spam" 89 + reason := "I just don't like it!" 90 + uri := "at://did:plc:123/com.example.record/bcd234" 91 + cid := "bafyreie5cvv4h45feadgeuwhbcutmh6t2ceseocckahdoe6uat64zmz454" 92 + report := comatproto.ModerationCreateReport_Input{ 93 + Reason: &reason, 94 + ReasonType: &rt, 95 + Subject: &comatproto.ModerationCreateReport_Input_Subject{ 96 + RepoStrongRef: &comatproto.RepoStrongRef{ 97 + //com.atproto.repo.strongRef 98 + Uri: uri, 99 + Cid: cid, 100 + }, 101 + }, 102 + } 103 + out := testCreateReport(t, e, lm, &report) 104 + assert.Equal(rt, *out.ReasonType) 105 + assert.Equal(reason, *out.Reason) 106 + // XXX: more fields 107 + } 108 + 109 + func TestLabelMakerXRPCReportRecordBad(t *testing.T) { 110 + assert := assert.New(t) 111 + e := echo.New() 112 + lm := testLabelMaker(t) 113 + 114 + uriStr := "at://did:plc:123/com.example.record/bcd234" 115 + cidStr := "bafyreie5cvv4h45feadgeuwhbcutmh6t2ceseocckahdoe6uat64zmz454" 116 + emptyStr := "" 117 + table := []struct { 118 + rType string 119 + rUri string 120 + rCid string 121 + statusCode int 122 + }{ 123 + {"spam", uriStr, cidStr, 200}, 124 + {"", uriStr, cidStr, 400}, 125 + {"spam", "", cidStr, 400}, 126 + {"spam", uriStr, emptyStr, 400}, 127 + } 128 + 129 + for _, row := range table { 130 + 131 + report := comatproto.ModerationCreateReport_Input{ 132 + ReasonType: &row.rType, 133 + Subject: &comatproto.ModerationCreateReport_Input_Subject{ 134 + RepoStrongRef: &comatproto.RepoStrongRef{ 135 + //com.atproto.repo.strongRef 136 + Uri: row.rUri, 137 + Cid: row.rCid, 138 + }, 139 + }, 140 + } 141 + reportJSON, err := json.Marshal(report) 142 + if err != nil { 143 + t.Fatal(err) 144 + } 145 + req := httptest.NewRequest(http.MethodPost, "/xrpc/com.atproto.report.create", strings.NewReader(string(reportJSON))) 146 + req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) 147 + recorder := httptest.NewRecorder() 148 + c := e.NewContext(req, recorder) 149 + err = lm.HandleComAtprotoReportCreate(c) 150 + if err != nil { 151 + httpError, _ := err.(*echo.HTTPError) 152 + assert.Equal(row.statusCode, httpError.Code) 153 + } else { 154 + assert.Equal(row.statusCode, recorder.Code) 155 + } 156 + } 157 + } 158 + 159 + func TestLabelMakerXRPCReportAction(t *testing.T) { 160 + assert := assert.New(t) 161 + e := echo.New() 162 + lm := testLabelMaker(t) 163 + 164 + // create a report 165 + rt := "spam" 166 + reason := "I just don't like it!" 167 + uri := "at://did:plc:123/com.example.record/bcd234" 168 + cid := "bafyreie5cvv4h45feadgeuwhbcutmh6t2ceseocckahdoe6uat64zmz454" 169 + report := comatproto.ModerationCreateReport_Input{ 170 + Reason: &reason, 171 + ReasonType: &rt, 172 + Subject: &comatproto.ModerationCreateReport_Input_Subject{ 173 + RepoStrongRef: &comatproto.RepoStrongRef{ 174 + //com.atproto.repo.strongRef 175 + Uri: uri, 176 + Cid: cid, 177 + }, 178 + }, 179 + } 180 + reportOut := testCreateReport(t, e, lm, &report) 181 + 182 + _ = assert 183 + _ = reportOut 184 + // TODO: getReport helper (does single and multi, verifies equal, returns single) 185 + // TODO: getAction helper (does single and multi, verifies equal, returns single) 186 + 187 + // XXX: create action (including get, get plural, verifications) 188 + // XXX: get report (should have action included) 189 + // XXX: reverse action 190 + // XXX: get action (single and plural) 191 + // XXX: get report (should not have action included) 192 + }