| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# Copyright (C) 2016-2023 by the Free Software Foundation, Inc. |
| 4 |
# |
| 5 |
# This file is part of Django-Mailman. |
| 6 |
# |
| 7 |
# Django-Mailman is free software: you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by the Free |
| 9 |
# Software Foundation, either version 3 of the License, or (at your option) |
| 10 |
# any later version. |
| 11 |
# |
| 12 |
# Django-Mailman is distributed in the hope that it will be useful, but WITHOUT |
| 13 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 15 |
# more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License along with |
| 18 |
# Django-Mailman. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
# |
| 20 |
# Author: Aurelien Bompard <[email protected]> |
| 21 |
# |
| 22 |
|
| 23 |
|
| 24 |
from unittest.mock import Mock, call, patch |
| 25 |
|
| 26 |
from django.contrib.auth.models import User |
| 27 |
|
| 28 |
from allauth.account.models import EmailAddress |
| 29 |
from allauth.account.signals import ( |
| 30 |
email_confirmed, email_removed, user_logged_in, user_signed_up) |
| 31 |
from allauth.socialaccount.models import SocialLogin |
| 32 |
from allauth.socialaccount.signals import social_account_added |
| 33 |
|
| 34 |
from django_mailman3.models import Profile |
| 35 |
from django_mailman3.signals import user_subscribed, user_unsubscribed |
| 36 |
from django_mailman3.tests.utils import TestCase |
| 37 |
|
| 38 |
|
| 39 |
class SignalsTestCase(TestCase): |
| 40 |
|
| 41 |
def setUp(self): |
| 42 |
self.user = User.objects.create_user( |
| 43 |
'testuser', '[email protected]', 'testPass') |
| 44 |
|
| 45 |
def test_user_logged_in(self): |
| 46 |
Profile.objects.get(user=self.user).delete() |
| 47 |
with patch('django_mailman3.signals.sync_email_addresses') as sea: |
| 48 |
user_logged_in.send(sender=User, user=self.user) |
| 49 |
self.assertEqual(sea.call_args_list, [call(self.user)]) |
| 50 |
self.assertTrue(Profile.objects.filter(user=self.user).exists()) |
| 51 |
|
| 52 |
def test_user_signed_up(self): |
| 53 |
Profile.objects.get(user=self.user).delete() |
| 54 |
EmailAddress.objects.create( |
| 55 |
user=self.user, email=self.user.email, verified=True) |
| 56 |
EmailAddress.objects.create( |
| 57 |
user=self.user, email='[email protected]', verified=False) |
| 58 |
with patch('django_mailman3.signals.add_address_to_mailman_user') \ |
| 59 |
as aatmu: |
| 60 |
user_signed_up.send(sender=User, user=self.user) |
| 61 |
self.assertEqual( |
| 62 |
aatmu.call_args_list, [call(self.user, self.user.email)]) |
| 63 |
self.assertTrue(Profile.objects.filter(user=self.user).exists()) |
| 64 |
|
| 65 |
def test_email_removed(self): |
| 66 |
address = EmailAddress.objects.create( |
| 67 |
user=self.user, email=self.user.email, verified=True) |
| 68 |
mm_user = Mock() |
| 69 |
with patch('django_mailman3.signals.get_mailman_user') as gmu: |
| 70 |
gmu.return_value = mm_user |
| 71 |
email_removed.send( |
| 72 |
sender=User, user=self.user, email_address=address) |
| 73 |
self.assertEqual(mm_user.addresses.remove.call_args_list, |
| 74 |
[call(self.user.email)]) |
| 75 |
|
| 76 |
def test_email_removed_no_mailman_user(self): |
| 77 |
address = EmailAddress.objects.create( |
| 78 |
user=self.user, email=self.user.email, verified=True) |
| 79 |
with patch('django_mailman3.signals.get_mailman_user') as gmu: |
| 80 |
gmu.return_value = None |
| 81 |
email_removed.send( |
| 82 |
sender=User, user=self.user, email_address=address) |
| 83 |
|
| 84 |
def test_email_confirmed(self): |
| 85 |
address = EmailAddress.objects.create( |
| 86 |
user=self.user, email=self.user.email, verified=True) |
| 87 |
with patch('django_mailman3.signals.add_address_to_mailman_user') \ |
| 88 |
as aatmu: |
| 89 |
email_confirmed.send(sender=EmailAddress, email_address=address) |
| 90 |
self.assertEqual( |
| 91 |
aatmu.call_args_list, [call(self.user, self.user.email)]) |
| 92 |
|
| 93 |
def test_social_account_added(self): |
| 94 |
verified = EmailAddress( |
| 95 |
email='[email protected]', verified=True) |
| 96 |
unverified = EmailAddress( |
| 97 |
email='[email protected]', verified=False) |
| 98 |
sociallogin = SocialLogin( |
| 99 |
user=self.user, email_addresses=[verified, unverified]) |
| 100 |
with patch('django_mailman3.signals.add_address_to_mailman_user') \ |
| 101 |
as aatmu: |
| 102 |
social_account_added.send(sender=User, sociallogin=sociallogin) |
| 103 |
self.assertEqual( |
| 104 |
aatmu.call_args_list, [call(self.user, '[email protected]')]) |
| 105 |
self.assertEqual( |
| 106 |
[e.user for e in EmailAddress.objects.all()], |
| 107 |
[self.user, self.user]) |
| 108 |
|
| 109 |
def test_user_subscribed(self): |
| 110 |
with patch('django_mailman3.signals.get_subscriptions') as gs: |
| 111 |
user_subscribed.send( |
| 112 |
sender="Postorius", user_email="[email protected]") |
| 113 |
self.assertEqual(gs.call_count, 1) |
| 114 |
user_unsubscribed.send( |
| 115 |
sender="Postorius", user_email="[email protected]") |
| 116 |
self.assertEqual(gs.call_count, 2) |
| 117 |
|
| 118 |
def test_user_subscribed_secondary_address(self): |
| 119 |
EmailAddress.objects.create( |
| 120 |
user=self.user, email="[email protected]", verified=True) |
| 121 |
with patch('django_mailman3.signals.get_subscriptions') as gs: |
| 122 |
user_subscribed.send( |
| 123 |
sender="Postorius", user_email="[email protected]") |
| 124 |
self.assertEqual(gs.call_count, 1) |
| 125 |
user_unsubscribed.send( |
| 126 |
sender="Postorius", user_email="[email protected]") |
| 127 |
self.assertEqual(gs.call_count, 2) |
| 128 |
|
| 129 |
def test_create_profile(self): |
| 130 |
# Test that when a user is saved, we create a Profile if one doesn't |
| 131 |
# exist for the User. Also, we update the display name for the user and |
| 132 |
# their all addresses. |
| 133 |
mm_user = Mock() |
| 134 |
mm_user.display_name = None |
| 135 |
# Mock user has two addresses. |
| 136 |
mm_user.addresses = [Mock(), Mock()] |
| 137 |
|
| 138 |
with patch('django_mailman3.signals.get_mailman_user') as gmu: |
| 139 |
gmu.return_value = mm_user |
| 140 |
user = User.objects.create_user( |
| 141 |
'myuser', '[email protected]', 'testPass') |
| 142 |
# Initially, the name of user is null, so we don't update the |
| 143 |
# display_name in Core. |
| 144 |
gmu.assert_not_called() |
| 145 |
# Now lets update the name of the user and see if it is reflected. |
| 146 |
user.first_name = 'Anne' |
| 147 |
user.last_name = 'Person' |
| 148 |
user.save() |
| 149 |
# Assert that the mock user was fetched and that the display name |
| 150 |
# was set. |
| 151 |
gmu.assert_called_once_with(user) |
| 152 |
mm_user.save.assert_called_once() |
| 153 |
self.assertEqual(mm_user.display_name, 'Anne Person') |
| 154 |
for addr in mm_user.addresses: |
| 155 |
self.assertEqual(addr.display_name, 'Anne Person') |
| 156 |
addr.save.assert_called_once() |
| 157 |
|
| 158 |
def test_create_profile_one_name(self): |
| 159 |
# Same as test_create_profile but tests that first_name only has no |
| 160 |
# trailing space. |
| 161 |
mm_user = Mock() |
| 162 |
mm_user.display_name = None |
| 163 |
# Mock user has two addresses. |
| 164 |
mm_user.addresses = [Mock(), Mock()] |
| 165 |
|
| 166 |
with patch('django_mailman3.signals.get_mailman_user') as gmu: |
| 167 |
gmu.return_value = mm_user |
| 168 |
user = User.objects.create_user( |
| 169 |
'myuser', '[email protected]', 'testPass') |
| 170 |
# Initially, the name of user is null, so we don't update the |
| 171 |
# display_name in Core. |
| 172 |
gmu.assert_not_called() |
| 173 |
# Now lets update the name of the user and see if it is reflected. |
| 174 |
user.first_name = 'Anne' |
| 175 |
# user.last_name = 'Person' |
| 176 |
user.save() |
| 177 |
# Assert that the mock user was fetched and that the display name |
| 178 |
# was set. |
| 179 |
gmu.assert_called_once_with(user) |
| 180 |
mm_user.save.assert_called_once() |
| 181 |
self.assertEqual(mm_user.display_name, 'Anne') |
| 182 |
for addr in mm_user.addresses: |
| 183 |
self.assertEqual(addr.display_name, 'Anne') |
| 184 |
addr.save.assert_called_once() |
| 185 |
|
| 186 |
def test_create_profile_one_address_not_updated(self): |
| 187 |
# Similar to test_create_profile but tests that address.display_name |
| 188 |
# not updated if not changed. |
| 189 |
mm_user = Mock() |
| 190 |
mm_user.display_name = None |
| 191 |
# Mock user has two addresses. |
| 192 |
mm_user.addresses = [Mock(), Mock()] |
| 193 |
|
| 194 |
with patch('django_mailman3.signals.get_mailman_user') as gmu: |
| 195 |
gmu.return_value = mm_user |
| 196 |
user = User.objects.create_user( |
| 197 |
'myuser', '[email protected]', 'testPass') |
| 198 |
# Initially, the name of user is null, so we don't update the |
| 199 |
# display_name in Core. |
| 200 |
gmu.assert_not_called() |
| 201 |
# Now lets update the name of the user and see if it is reflected. |
| 202 |
user.first_name = 'Anne' |
| 203 |
user.last_name = 'Person' |
| 204 |
user.save() |
| 205 |
# Assert that the mock user was fetched and that the display name |
| 206 |
# was set. |
| 207 |
gmu.assert_called_once_with(user) |
| 208 |
mm_user.save.assert_called_once() |
| 209 |
self.assertEqual(mm_user.display_name, 'Anne Person') |
| 210 |
for addr in mm_user.addresses: |
| 211 |
self.assertEqual(addr.display_name, 'Anne Person') |
| 212 |
addr.save.assert_called_once() |
| 213 |
# Set second address display_name to new name. |
| 214 |
mm_user.addresses[1].display_name = 'Ann Person' |
| 215 |
# Reset the mocks and update the user. |
| 216 |
for addr in mm_user.addresses: |
| 217 |
addr.reset_mock() |
| 218 |
user.first_name = 'Ann' |
| 219 |
user.save() |
| 220 |
self.assertEqual(mm_user.display_name, 'Ann Person') |
| 221 |
self.assertEqual(mm_user.addresses[0].display_name, 'Ann Person') |
| 222 |
mm_user.addresses[0].save.assert_called_once() |
| 223 |
self.assertEqual(mm_user.addresses[1].display_name, 'Ann Person') |
| 224 |
mm_user.addresses[1].save.assert_not_called() |