|
17 | 17 | import base64 |
18 | 18 | import datetime |
19 | 19 | import re |
| 20 | +try: |
| 21 | + # Prefer PyPI mock over unittest.mock to benefit from backported mock |
| 22 | + # features (such as assert_called_once). Valid until Python 3.6. |
| 23 | + import mock |
| 24 | +except ImportError: |
| 25 | + from unittest import mock |
20 | 26 | from unittest import skip |
21 | 27 |
|
22 | 28 | from django.conf import settings |
@@ -504,6 +510,43 @@ def test_idplist_templatetag(self): |
504 | 510 |
|
505 | 511 | self.assertEqual(rendered, expected) |
506 | 512 |
|
| 513 | + def test_sigalg_not_passed_when_not_signing_request(self): |
| 514 | + # monkey patch SAML configuration |
| 515 | + settings.SAML_CONFIG = conf.create_conf( |
| 516 | + sp_host='sp.example.com', |
| 517 | + idp_hosts=['idp.example.com'], |
| 518 | + metadata_file='remote_metadata_one_idp.xml', |
| 519 | + ) |
| 520 | + |
| 521 | + with mock.patch( |
| 522 | + 'djangosaml2.views.Saml2Client.prepare_for_authenticate', |
| 523 | + return_value=('session_id', {'url': 'fake'}), |
| 524 | + |
| 525 | + ) as prepare_for_auth_mock: |
| 526 | + self.client.get(reverse('saml2_login')) |
| 527 | + prepare_for_auth_mock.assert_called_once() |
| 528 | + _args, kwargs = prepare_for_auth_mock.call_args |
| 529 | + self.assertNotIn('sigalg', kwargs) |
| 530 | + |
| 531 | + def test_sigalg_passed_when_signing_request(self): |
| 532 | + # monkey patch SAML configuration |
| 533 | + settings.SAML_CONFIG = conf.create_conf( |
| 534 | + sp_host='sp.example.com', |
| 535 | + idp_hosts=['idp.example.com'], |
| 536 | + metadata_file='remote_metadata_one_idp.xml', |
| 537 | + ) |
| 538 | + |
| 539 | + settings.SAML_CONFIG['service']['sp']['authn_requests_signed'] = True |
| 540 | + with mock.patch( |
| 541 | + 'djangosaml2.views.Saml2Client.prepare_for_authenticate', |
| 542 | + return_value=('session_id', {'url': 'fake'}), |
| 543 | + |
| 544 | + ) as prepare_for_auth_mock: |
| 545 | + self.client.get(reverse('saml2_login')) |
| 546 | + prepare_for_auth_mock.assert_called_once() |
| 547 | + _args, kwargs = prepare_for_auth_mock.call_args |
| 548 | + self.assertIn('sigalg', kwargs) |
| 549 | + |
507 | 550 |
|
508 | 551 | def test_config_loader(request): |
509 | 552 | config = SPConfig() |
|
0 commit comments