@@ -196,3 +196,59 @@ def test_superuser_read_write_can_see_audit_logs(self):
196
196
197
197
self .add_user_permission (superuser , "superuser.write" )
198
198
self .get_success_response (self .organization .slug )
199
+
200
+ def test_filter_by_date (self ):
201
+ now = timezone .now ()
202
+
203
+ entry1 = AuditLogEntry .objects .create (
204
+ organization_id = self .organization .id ,
205
+ event = audit_log .get_event_id ("ORG_EDIT" ),
206
+ actor = self .user ,
207
+ datetime = now - timedelta (days = 1 ),
208
+ )
209
+ AuditLogEntry .objects .create (
210
+ organization_id = self .organization .id ,
211
+ event = audit_log .get_event_id ("ORG_ADD" ),
212
+ actor = self .user ,
213
+ datetime = now ,
214
+ )
215
+
216
+ start_time = now - timedelta (days = 1 )
217
+ end_time = now - timedelta (hours = 1 )
218
+
219
+ response = self .get_success_response (
220
+ self .organization .slug ,
221
+ qs_params = {
222
+ "event" : "org.edit" ,
223
+ "start" : start_time .isoformat (),
224
+ "end" : end_time .isoformat (),
225
+ },
226
+ )
227
+ assert len (response .data ["rows" ]) == 1
228
+ assert response .data ["rows" ][0 ]["id" ] == str (entry1 .id )
229
+
230
+ def test_filter_by_stats_period (self ):
231
+ now = timezone .now ()
232
+
233
+ # old entry
234
+ AuditLogEntry .objects .create (
235
+ organization_id = self .organization .id ,
236
+ event = audit_log .get_event_id ("ORG_EDIT" ),
237
+ actor = self .user ,
238
+ datetime = now - timedelta (days = 2 ),
239
+ )
240
+
241
+ recent_entry = AuditLogEntry .objects .create (
242
+ organization_id = self .organization .id ,
243
+ event = audit_log .get_event_id ("ORG_ADD" ),
244
+ actor = self .user ,
245
+ datetime = now - timedelta (hours = 12 ),
246
+ )
247
+
248
+ response = self .get_success_response (
249
+ self .organization .slug , qs_params = {"statsPeriod" : "1d" }
250
+ )
251
+
252
+ # Should only return the recent entry, not the old one
253
+ assert len (response .data ["rows" ]) == 1
254
+ assert response .data ["rows" ][0 ]["id" ] == str (recent_entry .id )
0 commit comments