OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is govered by a BSD-style |
| 3 # license that can be found in the LICENSE file or at |
| 4 # https://developers.google.com/open-source/licenses/bsd |
| 5 |
| 6 """Tests for the spam service.""" |
| 7 |
| 8 import unittest |
| 9 |
| 10 import mox |
| 11 |
| 12 from google.appengine.ext import testbed |
| 13 |
| 14 import settings |
| 15 from framework import sql |
| 16 from proto import user_pb2 |
| 17 from services import spam_svc |
| 18 from testing import fake |
| 19 |
| 20 class SpamServiceTest(unittest.TestCase): |
| 21 |
| 22 def setUp(self): |
| 23 self.testbed = testbed.Testbed() |
| 24 self.testbed.activate() |
| 25 |
| 26 self.mox = mox.Mox() |
| 27 self.mock_report_tbl = self.mox.CreateMock(sql.SQLTableManager) |
| 28 self.mock_verdict_tbl = self.mox.CreateMock(sql.SQLTableManager) |
| 29 self.mock_issue_tbl = self.mox.CreateMock(sql.SQLTableManager) |
| 30 self.cnxn = self.mox.CreateMock(sql.MonorailConnection) |
| 31 self.issue_service = fake.IssueService() |
| 32 self.spam_service = spam_svc.SpamService() |
| 33 self.spam_service.report_tbl = self.mock_report_tbl |
| 34 self.spam_service.verdict_tbl = self.mock_verdict_tbl |
| 35 self.spam_service.issue_tbl = self.mock_issue_tbl |
| 36 |
| 37 def tearDown(self): |
| 38 self.testbed.deactivate() |
| 39 self.mox.UnsetStubs() |
| 40 self.mox.ResetAll() |
| 41 |
| 42 def testLookupFlaggers(self): |
| 43 self.mock_report_tbl.Select( |
| 44 self.cnxn, cols=['user_id', 'comment_id'], |
| 45 issue_id=234).AndReturn([[111L, None], [222L, 1]]) |
| 46 self.mox.ReplayAll() |
| 47 |
| 48 issue_reporters, comment_reporters = ( |
| 49 self.spam_service.LookupFlaggers(self.cnxn, 234)) |
| 50 self.mox.VerifyAll() |
| 51 self.assertItemsEqual([111L], issue_reporters) |
| 52 self.assertEqual({1: [222L]}, comment_reporters) |
| 53 |
| 54 def testFlagIssues_overThresh(self): |
| 55 issue = fake.MakeTestIssue( |
| 56 project_id=789, local_id=1, reporter_id=111L, owner_id=456, |
| 57 summary='sum', status='Live', issue_id=78901) |
| 58 |
| 59 self.mock_report_tbl.InsertRows(self.cnxn, |
| 60 ['issue_id', 'reported_user_id', 'user_id'], |
| 61 [(78901, 111L, 111L)], ignore=True) |
| 62 |
| 63 self.mock_report_tbl.Select(self.cnxn, |
| 64 cols=['issue_id', 'COUNT(*)'], group_by=['issue_id'], |
| 65 issue_id=[78901]).AndReturn([(78901, settings.spam_flag_thresh)]) |
| 66 self.mock_verdict_tbl.Select( |
| 67 self.cnxn, cols=['issue_id', 'reason', 'MAX(created)'], |
| 68 group_by=['issue_id'], issue_id=[78901]).AndReturn([]) |
| 69 self.mock_verdict_tbl.InsertRows( |
| 70 self.cnxn, ['issue_id', 'is_spam', 'reason', 'project_id'], |
| 71 [(78901, True, 'threshold', 789)], ignore=True) |
| 72 |
| 73 self.mox.ReplayAll() |
| 74 self.spam_service.FlagIssues( |
| 75 self.cnxn, self.issue_service, [issue], 111L, True) |
| 76 self.mox.VerifyAll() |
| 77 self.assertIn(issue, self.issue_service.updated_issues) |
| 78 |
| 79 def testFlagIssues_underThresh(self): |
| 80 issue = fake.MakeTestIssue( |
| 81 project_id=789, local_id=1, reporter_id=111L, owner_id=456, |
| 82 summary='sum', status='Live', issue_id=78901) |
| 83 |
| 84 self.mock_report_tbl.InsertRows(self.cnxn, |
| 85 ['issue_id', 'reported_user_id', 'user_id'], |
| 86 [(78901, 111L, 111L)], ignore=True) |
| 87 |
| 88 self.mock_report_tbl.Select(self.cnxn, |
| 89 cols=['issue_id', 'COUNT(*)'], group_by=['issue_id'], |
| 90 issue_id=[78901]).AndReturn([(78901, settings.spam_flag_thresh - 1)]) |
| 91 |
| 92 self.mock_verdict_tbl.Select( |
| 93 self.cnxn, cols=['issue_id', 'reason', 'MAX(created)'], |
| 94 group_by=['issue_id'], issue_id=[78901]).AndReturn([]) |
| 95 |
| 96 self.mox.ReplayAll() |
| 97 self.spam_service.FlagIssues( |
| 98 self.cnxn, self.issue_service, [issue], 111L, True) |
| 99 self.mox.VerifyAll() |
| 100 |
| 101 self.assertNotIn(issue, self.issue_service.updated_issues) |
| 102 |
| 103 def testUnflagIssue_overThresh(self): |
| 104 issue = fake.MakeTestIssue( |
| 105 project_id=789, local_id=1, reporter_id=111L, owner_id=456, |
| 106 summary='sum', status='Live', issue_id=78901, is_spam=True) |
| 107 self.mock_report_tbl.Delete(self.cnxn, issue_id=[issue.issue_id], |
| 108 comment_id=None, user_id=111L) |
| 109 self.mock_report_tbl.Select(self.cnxn, |
| 110 cols=['issue_id', 'COUNT(*)'], group_by=['issue_id'], |
| 111 issue_id=[78901]).AndReturn([(78901, settings.spam_flag_thresh)]) |
| 112 |
| 113 self.mock_verdict_tbl.Select( |
| 114 self.cnxn, cols=['issue_id', 'reason', 'MAX(created)'], |
| 115 group_by=['issue_id'], issue_id=[78901]).AndReturn([]) |
| 116 |
| 117 self.mox.ReplayAll() |
| 118 self.spam_service.FlagIssues( |
| 119 self.cnxn, self.issue_service, [issue], 111L, False) |
| 120 self.mox.VerifyAll() |
| 121 |
| 122 self.assertNotIn(issue, self.issue_service.updated_issues) |
| 123 self.assertEqual(True, issue.is_spam) |
| 124 |
| 125 def testUnflagIssue_underThresh(self): |
| 126 issue = fake.MakeTestIssue( |
| 127 project_id=789, local_id=1, reporter_id=111L, owner_id=456, |
| 128 summary='sum', status='Live', issue_id=78901, is_spam=True) |
| 129 self.mock_report_tbl.Delete(self.cnxn, issue_id=[issue.issue_id], |
| 130 comment_id=None, user_id=111L) |
| 131 self.mock_report_tbl.Select(self.cnxn, |
| 132 cols=['issue_id', 'COUNT(*)'], group_by=['issue_id'], |
| 133 issue_id=[78901]).AndReturn([(78901, settings.spam_flag_thresh - 1)]) |
| 134 |
| 135 self.mock_verdict_tbl.Select( |
| 136 self.cnxn, cols=['issue_id', 'reason', 'MAX(created)'], |
| 137 group_by=['issue_id'], issue_id=[78901]).AndReturn([]) |
| 138 self.mock_verdict_tbl.InsertRows( |
| 139 self.cnxn, ['issue_id', 'is_spam', 'reason', 'project_id'], |
| 140 [(78901, False, 'threshold', 789)], ignore=True) |
| 141 |
| 142 self.mox.ReplayAll() |
| 143 self.spam_service.FlagIssues( |
| 144 self.cnxn, self.issue_service, [issue], 111L, False) |
| 145 self.mox.VerifyAll() |
| 146 |
| 147 self.assertIn(issue, self.issue_service.updated_issues) |
| 148 self.assertEqual(False, issue.is_spam) |
| 149 |
| 150 def testUnflagIssue_underThreshNoManualOerride(self): |
| 151 issue = fake.MakeTestIssue( |
| 152 project_id=789, local_id=1, reporter_id=111L, owner_id=456, |
| 153 summary='sum', status='Live', issue_id=78901, is_spam=True) |
| 154 self.mock_report_tbl.Delete(self.cnxn, issue_id=[issue.issue_id], |
| 155 comment_id=None, user_id=111L) |
| 156 self.mock_report_tbl.Select(self.cnxn, |
| 157 cols=['issue_id', 'COUNT(*)'], group_by=['issue_id'], |
| 158 issue_id=[78901]).AndReturn([(78901, settings.spam_flag_thresh - 1)]) |
| 159 |
| 160 self.mock_verdict_tbl.Select( |
| 161 self.cnxn, cols=['issue_id', 'reason', 'MAX(created)'], |
| 162 group_by=['issue_id'], |
| 163 issue_id=[78901]).AndReturn([(78901, 'manual', '')]) |
| 164 |
| 165 self.mox.ReplayAll() |
| 166 self.spam_service.FlagIssues( |
| 167 self.cnxn, self.issue_service, [issue], 111L, False) |
| 168 self.mox.VerifyAll() |
| 169 |
| 170 self.assertNotIn(issue, self.issue_service.updated_issues) |
| 171 self.assertEqual(True, issue.is_spam) |
| 172 |
| 173 def testGetModerationQueue_noVerdicts(self): |
| 174 self.mock_verdict_tbl.Select(self.cnxn, |
| 175 cols=['issue_id', 'is_spam', 'reason', 'classifier_confidence', |
| 176 'created'], |
| 177 where=[ |
| 178 ('project_id = %s', [789]), |
| 179 ('classifier_confidence <= %s', |
| 180 [settings.classifier_moderation_thresh]), |
| 181 ('overruled = %s', [False]), |
| 182 ('issue_id IS NOT NULL', []), |
| 183 ], |
| 184 order_by=[ |
| 185 ('classifier_confidence ASC', []), |
| 186 ('created ASC', []) |
| 187 ], |
| 188 group_by=['issue_id'], |
| 189 offset=0, |
| 190 limit=10, |
| 191 ).AndReturn([]) |
| 192 |
| 193 self.mock_verdict_tbl.SelectValue(self.cnxn, |
| 194 col='COUNT(*)', |
| 195 where=[ |
| 196 ('project_id = %s', [789]), |
| 197 ('classifier_confidence <= %s', |
| 198 [settings.classifier_moderation_thresh]), |
| 199 ('overruled = %s', [False]), |
| 200 ('issue_id IS NOT NULL', []), |
| 201 ]).AndReturn(0) |
| 202 |
| 203 self.mox.ReplayAll() |
| 204 res, count = self.spam_service.GetModerationQueue( |
| 205 self.cnxn, self.issue_service, 789) |
| 206 self.mox.VerifyAll() |
| 207 |
| 208 self.assertEqual([], res) |
| 209 self.assertEqual(0, count) |
| 210 |
| 211 def testGetModerationQueue_someVerdicts(self): |
| 212 self.mock_verdict_tbl.Select(self.cnxn, |
| 213 cols=['issue_id', 'is_spam', 'reason', 'classifier_confidence', |
| 214 'created'], |
| 215 where=[ |
| 216 ('project_id = %s', [789]), |
| 217 ('classifier_confidence <= %s', |
| 218 [settings.classifier_moderation_thresh]), |
| 219 ('overruled = %s', [False]), |
| 220 ('issue_id IS NOT NULL', []), |
| 221 ], |
| 222 order_by=[ |
| 223 ('classifier_confidence ASC', []), |
| 224 ('created ASC', []) |
| 225 ], |
| 226 group_by=['issue_id'], |
| 227 offset=0, |
| 228 limit=10, |
| 229 ).AndReturn([[78901, 0, "classifier", 0.9, "2015-12-10 11:06:24"]]) |
| 230 |
| 231 self.mock_verdict_tbl.SelectValue(self.cnxn, |
| 232 col='COUNT(*)', |
| 233 where=[ |
| 234 ('project_id = %s', [789]), |
| 235 ('classifier_confidence <= %s', |
| 236 [settings.classifier_moderation_thresh]), |
| 237 ('overruled = %s', [False]), |
| 238 ('issue_id IS NOT NULL', []), |
| 239 ]).AndReturn(10) |
| 240 |
| 241 self.mox.ReplayAll() |
| 242 res, count = self.spam_service.GetModerationQueue( |
| 243 self.cnxn, self.issue_service, 789) |
| 244 self.mox.VerifyAll() |
| 245 self.assertEqual(1, len(res)) |
| 246 self.assertEqual(10, count) |
| 247 self.assertEqual(78901, res[0].issue_id) |
| 248 self.assertEqual(False, res[0].is_spam) |
| 249 self.assertEqual("classifier", res[0].reason) |
| 250 self.assertEqual(0.9, res[0].classifier_confidence) |
| 251 self.assertEqual("2015-12-10 11:06:24", res[0].verdict_time) |
| 252 |
| 253 if __name__ == '__main__': |
| 254 unittest.main() |
OLD | NEW |