Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(138)

Side by Side Diff: appengine/monorail/framework/test/actionlimit_test.py

Issue 1868553004: Open Source Monorail (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Rebase Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 actionlimit module."""
7
8 import time
9 import unittest
10
11 from framework import actionlimit
12 from proto import user_pb2
13
14
15 class ActionLimitTest(unittest.TestCase):
16
17 def testNeedCaptchaNoUser(self):
18 action = actionlimit.ISSUE_COMMENT
19 self.assertFalse(actionlimit.NeedCaptcha(None, action))
20
21 def testNeedCaptchaAuthUserNoPreviousActions(self):
22 action = actionlimit.ISSUE_COMMENT
23 user = user_pb2.User()
24 self.assertFalse(actionlimit.NeedCaptcha(user, action))
25
26 def testNeedCaptchaAuthUserLifetimeExcessiveActivityException(self):
27 action = actionlimit.ISSUE_COMMENT
28 user = user_pb2.User()
29 life_max = actionlimit.ACTION_LIMITS[action][3]
30
31 for _i in range(0, life_max):
32 actionlimit.CountAction(user, action)
33
34 self.assertRaises(
35 actionlimit.ExcessiveActivityException,
36 actionlimit.NeedCaptcha, user, action)
37
38 def testNeedCaptchaAuthUserLifetimeIgnoresTimeout(self):
39 action = actionlimit.ISSUE_COMMENT
40 user = user_pb2.User()
41 (period, _soft_limit, _hard_limit,
42 life_max) = actionlimit.ACTION_LIMITS[action]
43 now = int(time.time())
44 later = now + period + 1 # a future in which our timestamp is expired
45
46 for _i in range(0, life_max):
47 actionlimit.CountAction(user, action, now=now)
48
49 self.assertRaises(
50 actionlimit.ExcessiveActivityException,
51 actionlimit.NeedCaptcha, user, action, now=later)
52
53 # TODO(jrobbins): write a soft limit captcha test.
54
55 def testNeedCaptchaAuthUserHardLimitExcessiveActivityException(self):
56 action = actionlimit.ISSUE_COMMENT
57 user = user_pb2.User()
58 (_period, _soft_limit, hard_limit,
59 _life_max) = actionlimit.ACTION_LIMITS[action]
60
61 for _i in range(0, hard_limit):
62 actionlimit.CountAction(user, action)
63
64 self.assertRaises(
65 actionlimit.ExcessiveActivityException,
66 actionlimit.NeedCaptcha, user, action)
67
68 def testNeedCaptchaAuthUserHardLimitRespectsTimeout(self):
69 action = actionlimit.ISSUE_COMMENT
70 user = user_pb2.User()
71 (period, _soft_limit, hard_limit,
72 _life_max) = actionlimit.ACTION_LIMITS[action]
73 now = int(time.time())
74 later = now + period + 1 # a future in which our timestamp is expired
75
76 for _i in range(0, hard_limit):
77 actionlimit.CountAction(user, action, now=now)
78
79 # if we didn't pass later, we'd get an exception
80 self.assertFalse(actionlimit.NeedCaptcha(user, action, now=later))
81
82 def testNeedCaptchaNoLifetimeLimit(self):
83 action = actionlimit.ISSUE_COMMENT
84 user = user_pb2.User()
85 life_max = actionlimit.ACTION_LIMITS[action][3]
86 actionlimit.GetLimitPB(user, action).lifetime_count = life_max + 1
87
88 self.assertRaises(
89 actionlimit.ExcessiveActivityException,
90 actionlimit.NeedCaptcha, user, action, skip_lifetime_check=False)
91 self.assertFalse(
92 actionlimit.NeedCaptcha(user, action, skip_lifetime_check=True))
93
94 def testCountActionResetRecentActions(self):
95 action = actionlimit.ISSUE_COMMENT
96 user = user_pb2.User()
97 limit = actionlimit.GetLimitPB(user, action)
98 limit.recent_count = 10
99 limit.reset_timestamp = 11
100
101 limit = actionlimit.GetLimitPB(user, action)
102 self.assertEqual(10, limit.recent_count)
103 self.assertEqual(11, limit.reset_timestamp)
104
105 actionlimit.ResetRecentActions(user, action)
106
107 limit = actionlimit.GetLimitPB(user, action)
108 self.assertEqual(0, limit.recent_count)
109 self.assertEqual(0, limit.reset_timestamp)
110
111 def testCountActionIncrementsRecentCount(self):
112 action = actionlimit.ISSUE_COMMENT
113 user = user_pb2.User()
114 (_period, soft_limit, _hard_limit,
115 _life_max) = actionlimit.ACTION_LIMITS[action]
116
117 for i in range(1, soft_limit):
118 actionlimit.CountAction(user, action)
119 limit = actionlimit.GetLimitPB(user, action)
120 self.assertEqual(i, limit.recent_count)
121 self.assertEqual(i, limit.lifetime_count)
122
123 def testCountActionPeriodExpiration(self):
124 action = actionlimit.ISSUE_COMMENT
125 user = user_pb2.User()
126 (period, soft_limit, _hard_limit,
127 _life_max) = actionlimit.ACTION_LIMITS[action]
128 now = int(time.time())
129 later = now + period + 1 # a future in which our timestamp is expired
130
131 for i in range(1, soft_limit):
132 actionlimit.CountAction(user, action, now=now)
133 limit = actionlimit.GetLimitPB(user, action)
134 self.assertEqual(i, limit.recent_count)
135 self.assertEqual(i, limit.lifetime_count)
136
137 actionlimit.CountAction(user, action, now=now)
138 self.assertEqual(soft_limit, limit.recent_count)
139 self.assertEqual(soft_limit, limit.lifetime_count)
140
141 actionlimit.CountAction(user, action, now=later)
142 self.assertEqual(1, limit.recent_count)
143 self.assertEqual(soft_limit + 1, limit.lifetime_count)
144
145 def testCustomizeLifetimeLimit(self):
146 user = user_pb2.User()
147
148 self.assertIsNone(user.get_assigned_value('issue_comment_limit'))
149 actionlimit.CustomizeLimit(user, actionlimit.ISSUE_COMMENT, 10, 100, 500)
150 self.assertIsNotNone(user.get_assigned_value('issue_comment_limit'))
151 limit = user.issue_comment_limit
152
153 # sets the specified limit
154 self.assertIsNotNone(limit.get_assigned_value('lifetime_limit'))
155 self.assertEqual(500, limit.lifetime_limit)
156 self.assertEqual(10, limit.period_soft_limit)
157 self.assertEqual(100, limit.period_hard_limit)
158
159 # sets initial values to zero
160 self.assertEqual(0, limit.recent_count)
161 self.assertEqual(0, limit.reset_timestamp)
162
163
164 if __name__ == '__main__':
165 unittest.main()
OLDNEW
« no previous file with comments | « appengine/monorail/framework/test/__init__.py ('k') | appengine/monorail/framework/test/alerts_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698