|
1 | 1 | """ Tests for pipelines. """ |
2 | 2 |
|
| 3 | +from unittest.mock import patch, MagicMock |
3 | 4 | from django.contrib.auth import get_user_model |
4 | 5 | from django.test import TestCase |
5 | | -from social_django.utils import load_strategy |
6 | 6 |
|
7 | 7 | from auth_backends.pipeline import get_user_if_exists, update_email |
8 | 8 |
|
@@ -40,20 +40,67 @@ class UpdateEmailPipelineTests(TestCase): |
40 | 40 |
|
41 | 41 | def setUp(self): |
42 | 42 | super().setUp() |
43 | | - self.user = User.objects.create() |
| 43 | + self.user = User.objects.create(username='test_user') |
| 44 | + self.strategy = MagicMock() |
44 | 45 |
|
45 | | - def test_update_email(self): |
46 | | - """ Verify that user email is updated upon changing email. """ |
| 46 | + # Make strategy.storage.user.changed actually save the user |
| 47 | + def save_user(user): |
| 48 | + user.save() |
| 49 | + |
| 50 | + self.strategy.storage.user.changed.side_effect = save_user |
| 51 | + |
| 52 | + @patch('auth_backends.pipeline.set_custom_attribute') |
| 53 | + def test_update_email(self, mock_set_attribute): |
| 54 | + """ Verify that user email is updated upon changing email when usernames match. """ |
47 | 55 | updated_email = 'updated@example.com' |
48 | 56 | self.assertNotEqual(self.user.email, updated_email) |
49 | 57 |
|
50 | | - update_email(load_strategy(), {'email': updated_email}, user=self.user) |
| 58 | + # Provide matching username in details |
| 59 | + update_email(self.strategy, {'email': updated_email, 'username': 'test_user'}, user=self.user) |
| 60 | + |
| 61 | + # Verify email was updated |
51 | 62 | self.user = User.objects.get(username=self.user.username) |
52 | 63 | self.assertEqual(self.user.email, updated_email) |
| 64 | + self.strategy.storage.user.changed.assert_called_once_with(self.user) |
| 65 | + |
| 66 | + # Verify custom attribute was set correctly |
| 67 | + mock_set_attribute.assert_called_with('update_email.username_mismatch', False) |
53 | 68 |
|
54 | | - def test_update_email_with_none(self): |
| 69 | + @patch('auth_backends.pipeline.set_custom_attribute') |
| 70 | + def test_update_email_with_none(self, mock_set_attribute): |
55 | 71 | """ Verify that user email is not updated if email value is None. """ |
56 | 72 | old_email = self.user.email |
57 | | - update_email(load_strategy(), {'email': None}, user=self.user) |
| 73 | + |
| 74 | + # Provide matching username in details |
| 75 | + update_email(self.strategy, {'email': None, 'username': 'test_user'}, user=self.user) |
| 76 | + |
| 77 | + # Verify email was not updated |
| 78 | + self.user = User.objects.get(username=self.user.username) |
| 79 | + self.assertEqual(self.user.email, old_email) |
| 80 | + self.strategy.storage.user.changed.assert_not_called() |
| 81 | + |
| 82 | + # Verify custom attribute was still set |
| 83 | + mock_set_attribute.assert_called_with('update_email.username_mismatch', False) |
| 84 | + |
| 85 | + @patch('auth_backends.pipeline.logger') |
| 86 | + @patch('auth_backends.pipeline.set_custom_attribute') |
| 87 | + def test_username_mismatch_no_update(self, mock_set_attribute, mock_logger): |
| 88 | + """ Verify that email is not updated when usernames don't match. """ |
| 89 | + old_email = self.user.email |
| 90 | + updated_email = 'updated@example.com' |
| 91 | + |
| 92 | + # Provide mismatched username in details |
| 93 | + update_email(self.strategy, {'email': updated_email, 'username': 'different_user'}, user=self.user) |
| 94 | + |
| 95 | + # Verify email was not updated |
58 | 96 | self.user = User.objects.get(username=self.user.username) |
59 | 97 | self.assertEqual(self.user.email, old_email) |
| 98 | + self.strategy.storage.user.changed.assert_not_called() |
| 99 | + |
| 100 | + # Verify logger was called with warning |
| 101 | + mock_logger.warning.assert_called_once() |
| 102 | + |
| 103 | + # Verify all custom attributes were set correctly |
| 104 | + mock_set_attribute.assert_any_call('update_email.username_mismatch', True) |
| 105 | + mock_set_attribute.assert_any_call('update_email.details_username', 'different_user') |
| 106 | + mock_set_attribute.assert_any_call('update_email.user_username', 'test_user') |
0 commit comments