File: test_middlewares.py

package info (click to toggle)
django-mailman3 1.3.15-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,544 kB
  • sloc: python: 2,186; makefile: 18; javascript: 14; sh: 7
file content (97 lines) | stat: -rw-r--r-- 3,417 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017-2023 by the Free Software Foundation, Inc.
#
# This file is part of Django-Mailman.
#
# Django-Mailman is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# Django-Mailman is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# Django-Mailman.  If not, see <http://www.gnu.org/licenses/>.
#


from unittest.mock import Mock

from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.test import TestCase
from django.utils import timezone

from django_mailman3.middleware import PaginationMiddleware, TimezoneMiddleware


class TestPaginatorMiddleware(TestCase):

    def setUp(self):
        self.get_response = Mock()
        self.middleware = PaginationMiddleware(self.get_response)
        self.request = Mock()

    def test_request_with_page_value(self):
        data = {'page': 100}
        self.request.REQUEST = data
        self.middleware(self.request)
        self.assertEqual(self.request.page, 100)
        self.get_response.assert_called_once()

    def test_request_without_page_value(self):
        self.middleware(self.request)
        self.assertEqual(self.request.page, 1)


class TestTimezoneMiddleware(TestCase):

    def setUp(self):
        self.get_response = Mock()
        self.middleware = TimezoneMiddleware(self.get_response)
        self.request = Mock()

    def tearDown(self):
        timezone.deactivate()

    def test_non_logged_in_user(self):
        self.request.user.is_authenticated = False
        self.middleware(self.request)
        # If the user is not logged in, his timezone is the default timezone.
        self.assertEqual(settings.TIME_ZONE,
                         timezone.get_current_timezone_name())
        self.get_response.assert_called_once()

    def test_logged_in_user_without_mailman_profile(self):

        class MockUser:
            @property
            def mailman_profile(self):
                raise ObjectDoesNotExist

            @property
            def is_authenticated(self):
                return True

        self.request.user = MockUser()
        # If the mailman profile does not exist for the user, it still does not
        # have a personalized timezone.
        self.middleware(self.request)
        self.assertEqual(settings.TIME_ZONE,
                         timezone.get_current_timezone_name())
        self.get_response.assert_called_once()

    def test_logged_in_user_with_mailman_profile(self):
        self.request.user.is_authenticated = lambda: True
        self.request.user.mailman_profile = Mock(timezone='America/Chicago')
        # self.assertTrue(self.request.user.mailman_profile.called)
        # If the user has a timezone set in their profile, it should be set as
        # current timezone.
        self.middleware(self.request)
        self.assertEqual('America/Chicago',
                         timezone.get_current_timezone_name())
        self.get_response.assert_called_once()