Debian Codegraph @

django_mailman3/tests/test_lib_user.py

raw
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2020-2023 by the Free Software Foundation, Inc.
3 #
4 # This file is part of Django-Mailman.
5 #
6 # Django-Mailman3 is free software: you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by the Free
8 # Software Foundation, either version 3 of the License, or (at your option) any
9 # later version.
10 #
11 # Django-Mailman3 is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 # more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Django-Mailman. If not, see <http://www.gnu.org/licenses/>.
18
19 from unittest.mock import Mock
20
21 from django.contrib.auth.models import User
22
23 from allauth.account.models import EmailAddress
24
25 from django_mailman3.lib.user import get_django_user
26 from django_mailman3.tests.utils import TestCase
27
28
29 class TestGetDjangUser(TestCase):
30
31 def setUp(self):
32 self.user = User.objects.create_user(
33 'testuser', '[email protected]', 'testPass')
34 EmailAddress.objects.create(
35 user=self.user, email=self.user.email, verified=True)
36 EmailAddress.objects.create(
37 user=self.user, email='[email protected]', verified=True)
38 EmailAddress.objects.create(
39 user=self.user, email='[email protected]', verified=True)
40
41 def test_get_user_with_email(self):
42 # Test we can get the user from Member objects.
43 for email in (
44 '[email protected]', '[email protected]',
45 '[email protected]'):
46 member = Mock()
47 member.address = Mock(email=email)
48 user = get_django_user(member)
49 self.assertEqual(user, self.user)
50
51 def test_missing_user(self):
52 # test that we don't raise exception when the user doesn't exist.
53 member = Mock()
54 member.address = Mock(email='[email protected]')
55 self.assertIsNone(get_django_user(member))