1
1
from unittest import mock
2
2
from unittest .mock import MagicMock
3
3
4
- from django .core .exceptions import PermissionDenied
5
4
from django .test import RequestFactory , TestCase , override_settings
6
5
7
6
from baipw .middleware import BasicAuthIPWhitelistMiddleware
@@ -18,8 +17,9 @@ def setUp(self):
18
17
19
18
class TestMiddleware (TestCaseMixin , TestCase ):
20
19
def test_no_settings_returns_permission_denied (self ):
21
- with self .assertRaises (PermissionDenied ):
22
- self .middleware (self .request )
20
+ response = self .middleware (self .request )
21
+ self .assertEqual (response .status_code , 401 )
22
+ self .assertEqual (response .headers ["X-Robots-Tag" ], "noindex" )
23
23
24
24
@override_settings (
25
25
BASIC_AUTH_LOGIN = "testlogin" ,
@@ -28,6 +28,7 @@ def test_no_settings_returns_permission_denied(self):
28
28
def test_basic_auth_returns_401 (self ):
29
29
response = self .middleware (self .request )
30
30
self .assertEqual (response .status_code , 401 )
31
+ self .assertEqual (response .headers ["X-Robots-Tag" ], "noindex" )
31
32
32
33
@override_settings (
33
34
BASIC_AUTH_LOGIN = "testlogin" ,
@@ -236,17 +237,17 @@ def test_http_host_whitelist_fails_check_when_configured_(self):
236
237
237
238
@override_settings (BASIC_AUTH_WHITELISTED_HTTP_HOSTS = ["kernel.org" , "dgg.gg" ])
238
239
def test_http_host_whitelist_fails_check_with_no_host (self ):
239
- with self .assertRaises ( PermissionDenied ):
240
- self .middleware ( self . request )
240
+ response = self .middleware ( self . request )
241
+ self .assertEqual ( response . status_code , 401 )
241
242
242
243
@override_settings (
243
244
ALLOWED_HOSTS = ["www.example.com" ],
244
245
BASIC_AUTH_WHITELISTED_HTTP_HOSTS = ["kernel.org" , "dgg.gg" ],
245
246
)
246
247
def test_http_host_whitelist_fails_check_with_wrong_host (self ):
247
248
self .request .META ["HTTP_HOST" ] = "www.example.com"
248
- with self .assertRaises ( PermissionDenied ):
249
- self .middleware ( self . request )
249
+ response = self .middleware ( self . request )
250
+ self .assertEqual ( response . status_code , 401 )
250
251
251
252
@override_settings (
252
253
BASIC_AUTH_LOGIN = "somelogin" ,
@@ -300,14 +301,14 @@ def test_path_whitelist_fails_check_when_configured(self):
300
301
301
302
@override_settings (BASIC_AUTH_WHITELISTED_PATHS = ["ham/" , "eggs/" ])
302
303
def test_path_whitelist_fails_check_for_parent_path (self ):
303
- with self .assertRaises ( PermissionDenied ):
304
- self .middleware ( self . request )
304
+ response = self .middleware ( self . request )
305
+ self .assertEqual ( response . status_code , 401 )
305
306
306
307
@override_settings (BASIC_AUTH_WHITELISTED_PATHS = ["ham/" , "eggs/" ])
307
308
def test_path_whitelist_fails_check_with_wrong_path (self ):
308
309
self .request .path = "spam/"
309
- with self .assertRaises ( PermissionDenied ):
310
- self .middleware ( self . request )
310
+ response = self .middleware ( self . request )
311
+ self .assertEqual ( response . status_code , 401 )
311
312
312
313
@override_settings (
313
314
BASIC_AUTH_LOGIN = "somelogin" ,
@@ -330,11 +331,15 @@ def test_path_whitelist_check_when_settings_empty(self):
330
331
331
332
class TestResponseClass (TestCaseMixin , TestCase ):
332
333
def test_get_response_class_when_none_set (self ):
333
- self .assertIs (self .middleware .get_response_class (), HttpUnauthorizedResponse )
334
+ self .assertIsInstance (
335
+ self .middleware .get_error_response (self .request ), HttpUnauthorizedResponse
336
+ )
334
337
335
338
@override_settings (BASIC_AUTH_RESPONSE_CLASS = "baipw.tests.response.TestResponse" )
336
339
def test_get_response_class_when_set (self ):
337
- self .assertIs (self .middleware .get_response_class (), TestResponse )
340
+ self .assertIsInstance (
341
+ self .middleware .get_error_response (self .request ), TestResponse
342
+ )
338
343
339
344
@override_settings (
340
345
BASIC_AUTH_LOGIN = "testlogin" ,
@@ -343,5 +348,5 @@ def test_get_response_class_when_set(self):
343
348
)
344
349
def test_middleware_when_custom_response_set (self ):
345
350
response = self .middleware (self .request )
346
- self .assertIs (response . __class__ , TestResponse )
351
+ self .assertIsInstance (response , TestResponse )
347
352
self .assertEqual (response .content , b"Test message. :P" )
0 commit comments