|
25 | 25 | from django.contrib.auth import SESSION_KEY, get_user_model
|
26 | 26 | from django.contrib.auth.models import AnonymousUser
|
27 | 27 | from django.contrib.sessions.middleware import SessionMiddleware
|
| 28 | +from django.http.request import HttpRequest |
28 | 29 | from django.template import Context, Template
|
29 | 30 | from django.test import TestCase
|
30 | 31 | from django.test.client import RequestFactory
|
31 | 32 | from djangosaml2 import views
|
32 | 33 | from djangosaml2.cache import OutstandingQueriesCache
|
33 | 34 | from djangosaml2.conf import get_config
|
34 |
| -from djangosaml2.signals import post_authenticated |
| 35 | +from djangosaml2.signals import post_authenticated, pre_user_save |
35 | 36 | from djangosaml2.tests import conf
|
36 | 37 | from djangosaml2.tests.utils import SAMLPostFormParser
|
37 | 38 | from djangosaml2.tests.auth_response import auth_response
|
@@ -496,16 +497,51 @@ def _test_metadata(self):
|
496 | 497 | self.assertEqual(response.content, expected_metadata)
|
497 | 498 |
|
498 | 499 | def test_post_authenticated_signal(self):
|
| 500 | + self.called = [] |
499 | 501 |
|
500 |
| - def signal_handler(signal, user, session_info): |
501 |
| - self.assertEqual(isinstance(user, User), True) |
| 502 | + def signal_handler(sender, instance, session_info, request, **kwargs): |
| 503 | + self.called.append({'sender': sender, 'instance': instance, 'request': request, 'session_info': session_info}) |
502 | 504 |
|
503 | 505 | post_authenticated.connect(signal_handler, dispatch_uid='test_signal')
|
504 | 506 |
|
505 | 507 | self.do_login()
|
506 | 508 |
|
| 509 | + # make sure the handler is only called once |
| 510 | + self.assertEqual(len(self.called), 1) |
| 511 | + # test 'sender', this should be User.__class__ |
| 512 | + self.assertEqual(self.called[0]['sender'], get_user_model(), 'post_authenticated signal sender is not a User') |
| 513 | + # test 'instance', this should be User |
| 514 | + self.assertIsInstance(self.called[0]['instance'], get_user_model(), 'post_authenticated signal did not send a User instance') |
| 515 | + # test the request |
| 516 | + self.assertIsInstance(self.called[0]['request'], HttpRequest, 'post_authenticated signal did not send a request') |
| 517 | + # test the session_info |
| 518 | + self.assertIsInstance(self.called[0]['session_info'], dict, 'post_authenticated signal did not send a session_info dict') |
| 519 | + |
507 | 520 | post_authenticated.disconnect(dispatch_uid='test_signal')
|
508 | 521 |
|
| 522 | + def test_pre_user_save_signal(self): |
| 523 | + self.called = [] |
| 524 | + |
| 525 | + def signal_handler(sender, instance, attributes, user_modified, **kwargs): |
| 526 | + self.called.append({'sender': sender, 'instance': instance, 'attributes': attributes, 'user_modified': user_modified}) |
| 527 | + |
| 528 | + pre_user_save.connect(signal_handler, dispatch_uid='test_signal') |
| 529 | + |
| 530 | + self.do_login() |
| 531 | + |
| 532 | + # make sure the handler is only called once |
| 533 | + self.assertEqual(len(self.called), 1) |
| 534 | + # test 'sender', this should be User.__class__ |
| 535 | + self.assertEqual(self.called[0]['sender'], get_user_model(), 'pre_user_save signal sender is not a User') |
| 536 | + # test 'instance', this should be User |
| 537 | + self.assertIsInstance(self.called[0]['instance'], get_user_model(), 'pre_user_save signal did not send a User instance') |
| 538 | + # test the attributes |
| 539 | + self.assertIsInstance(self.called[0]['attributes'], dict, 'pre_user_save signal did not send attributes') |
| 540 | + # test the user_modified |
| 541 | + self.assertIsInstance(self.called[0]['user_modified'], bool, 'pre_user_save signal did not send a user_modified bool') |
| 542 | + |
| 543 | + pre_user_save.disconnect(dispatch_uid='test_signal') |
| 544 | + |
509 | 545 | def test_idplist_templatetag(self):
|
510 | 546 | settings.SAML_CONFIG = conf.create_conf(
|
511 | 547 | sp_host='sp.example.com',
|
|
0 commit comments