Debian Codegraph @

django_mailman3/tests/test_view_profile.py

raw
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
25
26 from django.contrib.auth.models import User
27 from django.urls import reverse
28
29 from django_mailman3.models import Profile
30 from django_mailman3.tests.utils import TestCase
31
32
33 class ProfileViewTestCase(TestCase):
34
35 def setUp(self):
36 self.mm_user = Mock()
37 self.mm_user.user_id = "dummy"
38 self.mm_user.addresses = [Mock()]
39 self.mailman_client.get_user.side_effect = lambda e: self.mm_user
40 self.user = User.objects.create_user(
41 'testuser', '[email protected]', 'testPass',
42 first_name="firstname", last_name="lastname",
43 )
44 self.client.login(username='testuser', password='testPass')
45
46 def test_get_page(self):
47 response = self.client.get(reverse('mm_user_profile'))
48 self.assertEqual(response.status_code, 200)
49 self.assertEqual(list(response.context['other_addresses']), [])
50
51 def test_change_display_name(self):
52 # We create a Mailman user, from the django user object.
53 response = self.client.post(
54 reverse('mm_user_profile'), {
55 'username': self.user.username,
56 'first_name': 'test-first-name',
57 'last_name': 'test-last-name',
58 'timezone': 'Europe/Paris'})
59 self.assertRedirects(response, reverse('mm_user_profile'))
60 # The user's first and last name must be updated.
61 user = User.objects.get(username='testuser')
62 self.assertEqual(user.first_name, 'test-first-name')
63 self.assertEqual(user.last_name, 'test-last-name')
64 # The profile must have been created and populated.
65 profile = Profile.objects.get(user=user)
66 self.assertIsNotNone(profile)
67 self.assertEqual(profile.timezone, 'Europe/Paris')
68 # The Mailman user's display name must have been updated.
69 self.assertEqual(
70 self.mm_user.display_name, 'test-first-name test-last-name')
71
72 def test_username_unique(self):
73 # Usernames should be unique.
74 User.objects.create_user(
75 'otheruser', '[email protected]', 'testPass',
76 )
77 response = self.client.post(
78 reverse('mm_user_profile'), {
79 'username': 'otheruser',
80 'first_name': 'first-name',
81 'last_name': 'last-name',
82 'timezone': 'Europe/Paris'})
83 self.assertEqual(response.status_code, 200)
84 self.assertIn('username', response.context["form"].errors)