10
10
from django .test .utils import (setup_test_template_loader ,
11
11
restore_template_loaders )
12
12
from django .core .urlresolvers import reverse
13
- from django .template import TemplateSyntaxError
14
13
from django .views .debug import ExceptionReporter
15
14
from django .core import mail
16
15
17
16
from .. import BrokenException , except_args
18
17
from ..views import (sensitive_view , non_sensitive_view , paranoid_view ,
19
- custom_exception_reporter_filter_view )
18
+ custom_exception_reporter_filter_view , sensitive_method_view )
20
19
21
20
22
21
class DebugViewTests (TestCase ):
@@ -238,7 +237,8 @@ class ExceptionReportTestMixin(object):
238
237
'hash-brown-key' : 'hash-brown-value' ,
239
238
'bacon-key' : 'bacon-value' ,}
240
239
241
- def verify_unsafe_response (self , view , check_for_vars = True ):
240
+ def verify_unsafe_response (self , view , check_for_vars = True ,
241
+ check_for_POST_params = True ):
242
242
"""
243
243
Asserts that potentially sensitive info are displayed in the response.
244
244
"""
@@ -250,13 +250,14 @@ def verify_unsafe_response(self, view, check_for_vars=True):
250
250
self .assertContains (response , 'scrambled' , status_code = 500 )
251
251
self .assertContains (response , 'sauce' , status_code = 500 )
252
252
self .assertContains (response , 'worcestershire' , status_code = 500 )
253
+ if check_for_POST_params :
254
+ for k , v in self .breakfast_data .items ():
255
+ # All POST parameters are shown.
256
+ self .assertContains (response , k , status_code = 500 )
257
+ self .assertContains (response , v , status_code = 500 )
253
258
254
- for k , v in self .breakfast_data .items ():
255
- # All POST parameters are shown.
256
- self .assertContains (response , k , status_code = 500 )
257
- self .assertContains (response , v , status_code = 500 )
258
-
259
- def verify_safe_response (self , view , check_for_vars = True ):
259
+ def verify_safe_response (self , view , check_for_vars = True ,
260
+ check_for_POST_params = True ):
260
261
"""
261
262
Asserts that certain sensitive info are not displayed in the response.
262
263
"""
@@ -269,18 +270,19 @@ def verify_safe_response(self, view, check_for_vars=True):
269
270
# Sensitive variable's name is shown but not its value.
270
271
self .assertContains (response , 'sauce' , status_code = 500 )
271
272
self .assertNotContains (response , 'worcestershire' , status_code = 500 )
273
+ if check_for_POST_params :
274
+ for k , v in self .breakfast_data .items ():
275
+ # All POST parameters' names are shown.
276
+ self .assertContains (response , k , status_code = 500 )
277
+ # Non-sensitive POST parameters' values are shown.
278
+ self .assertContains (response , 'baked-beans-value' , status_code = 500 )
279
+ self .assertContains (response , 'hash-brown-value' , status_code = 500 )
280
+ # Sensitive POST parameters' values are not shown.
281
+ self .assertNotContains (response , 'sausage-value' , status_code = 500 )
282
+ self .assertNotContains (response , 'bacon-value' , status_code = 500 )
272
283
273
- for k , v in self .breakfast_data .items ():
274
- # All POST parameters' names are shown.
275
- self .assertContains (response , k , status_code = 500 )
276
- # Non-sensitive POST parameters' values are shown.
277
- self .assertContains (response , 'baked-beans-value' , status_code = 500 )
278
- self .assertContains (response , 'hash-brown-value' , status_code = 500 )
279
- # Sensitive POST parameters' values are not shown.
280
- self .assertNotContains (response , 'sausage-value' , status_code = 500 )
281
- self .assertNotContains (response , 'bacon-value' , status_code = 500 )
282
-
283
- def verify_paranoid_response (self , view , check_for_vars = True ):
284
+ def verify_paranoid_response (self , view , check_for_vars = True ,
285
+ check_for_POST_params = True ):
284
286
"""
285
287
Asserts that no variables or POST parameters are displayed in the response.
286
288
"""
@@ -292,14 +294,14 @@ def verify_paranoid_response(self, view, check_for_vars=True):
292
294
self .assertNotContains (response , 'scrambled' , status_code = 500 )
293
295
self .assertContains (response , 'sauce' , status_code = 500 )
294
296
self .assertNotContains (response , 'worcestershire' , status_code = 500 )
297
+ if check_for_POST_params :
298
+ for k , v in self .breakfast_data .items ():
299
+ # All POST parameters' names are shown.
300
+ self .assertContains (response , k , status_code = 500 )
301
+ # No POST parameters' values are shown.
302
+ self .assertNotContains (response , v , status_code = 500 )
295
303
296
- for k , v in self .breakfast_data .items ():
297
- # All POST parameters' names are shown.
298
- self .assertContains (response , k , status_code = 500 )
299
- # No POST parameters' values are shown.
300
- self .assertNotContains (response , v , status_code = 500 )
301
-
302
- def verify_unsafe_email (self , view ):
304
+ def verify_unsafe_email (self , view , check_for_POST_params = True ):
303
305
"""
304
306
Asserts that potentially sensitive info are displayed in the email report.
305
307
"""
@@ -314,12 +316,13 @@ def verify_unsafe_email(self, view):
314
316
self .assertNotIn ('scrambled' , email .body )
315
317
self .assertNotIn ('sauce' , email .body )
316
318
self .assertNotIn ('worcestershire' , email .body )
317
- for k , v in self .breakfast_data .items ():
318
- # All POST parameters are shown.
319
- self .assertIn (k , email .body )
320
- self .assertIn (v , email .body )
319
+ if check_for_POST_params :
320
+ for k , v in self .breakfast_data .items ():
321
+ # All POST parameters are shown.
322
+ self .assertIn (k , email .body )
323
+ self .assertIn (v , email .body )
321
324
322
- def verify_safe_email (self , view ):
325
+ def verify_safe_email (self , view , check_for_POST_params = True ):
323
326
"""
324
327
Asserts that certain sensitive info are not displayed in the email report.
325
328
"""
@@ -334,15 +337,16 @@ def verify_safe_email(self, view):
334
337
self .assertNotIn ('scrambled' , email .body )
335
338
self .assertNotIn ('sauce' , email .body )
336
339
self .assertNotIn ('worcestershire' , email .body )
337
- for k , v in self .breakfast_data .items ():
338
- # All POST parameters' names are shown.
339
- self .assertIn (k , email .body )
340
- # Non-sensitive POST parameters' values are shown.
341
- self .assertIn ('baked-beans-value' , email .body )
342
- self .assertIn ('hash-brown-value' , email .body )
343
- # Sensitive POST parameters' values are not shown.
344
- self .assertNotIn ('sausage-value' , email .body )
345
- self .assertNotIn ('bacon-value' , email .body )
340
+ if check_for_POST_params :
341
+ for k , v in self .breakfast_data .items ():
342
+ # All POST parameters' names are shown.
343
+ self .assertIn (k , email .body )
344
+ # Non-sensitive POST parameters' values are shown.
345
+ self .assertIn ('baked-beans-value' , email .body )
346
+ self .assertIn ('hash-brown-value' , email .body )
347
+ # Sensitive POST parameters' values are not shown.
348
+ self .assertNotIn ('sausage-value' , email .body )
349
+ self .assertNotIn ('bacon-value' , email .body )
346
350
347
351
def verify_paranoid_email (self , view ):
348
352
"""
@@ -425,6 +429,24 @@ def test_custom_exception_reporter_filter(self):
425
429
self .verify_unsafe_response (custom_exception_reporter_filter_view )
426
430
self .verify_unsafe_email (custom_exception_reporter_filter_view )
427
431
432
+ def test_sensitive_method (self ):
433
+ """
434
+ Ensure that the sensitive_variables decorator works with object
435
+ methods.
436
+ Refs #18379.
437
+ """
438
+ with self .settings (DEBUG = True ):
439
+ self .verify_unsafe_response (sensitive_method_view ,
440
+ check_for_POST_params = False )
441
+ self .verify_unsafe_email (sensitive_method_view ,
442
+ check_for_POST_params = False )
443
+
444
+ with self .settings (DEBUG = False ):
445
+ self .verify_safe_response (sensitive_method_view ,
446
+ check_for_POST_params = False )
447
+ self .verify_safe_email (sensitive_method_view ,
448
+ check_for_POST_params = False )
449
+
428
450
429
451
class AjaxResponseExceptionReporterFilter (TestCase , ExceptionReportTestMixin ):
430
452
"""
0 commit comments