File: test_middleware.py

package info (click to toggle)
django-maintenancemode 0.11.7%2Bgit221001-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 220 kB
  • sloc: python: 327; makefile: 7; sh: 5
file content (140 lines) | stat: -rw-r--r-- 6,430 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import re
from io import StringIO

from django.core import management
from django.contrib.auth.models import User
from django.template import TemplateDoesNotExist
from django.test import TestCase
from django.test.client import Client
from django.test.utils import override_settings

from maintenancemode import utils

@override_settings(ROOT_URLCONF="maintenancemode.tests.urls", MAINTENANCE_MODE=False)
class MaintenanceModeMiddlewareTestCase(TestCase):

    def setUp(self):
        utils.deactivate()  # make sure maintenance mode is off

        self.user = User.objects.create_user(
            username="maintenance", email="maintenance@example.org", password="password"
        )

    def tearDown(self):
        self.user.delete()

    def test_default_middleware(self):
        # Middleware should default to being disabled
        response = self.client.get("/")
        self.assertContains(response, text="Rendered response page", count=1, status_code=200)

    def test_disabled_middleware(self):
        # Explicitly disabling the ``MAINTENANCE_MODE`` should work
        with self.settings(MAINTENANCE_MODE=False):
            response = self.client.get("/")
        self.assertContains(response, text="Rendered response page", count=1, status_code=200)

    def test_enabled_middleware_without_template(self):
        # Enabling the middleware without a proper 503 template should
        # raise a template error
        with self.settings(MAINTENANCE_MODE=True, TEMPLATES=[]):
            self.assertRaises(TemplateDoesNotExist, self.client.get, "/")

    def test_enabled_middleware_with_template(self):
        # Enabling the middleware having a ``503.html`` in any of the
        # template locations should return the rendered template"
        with self.settings(MAINTENANCE_MODE=True):
            response = self.client.get("/")
        self.assertContains(response, text="Temporary unavailable", count=1, status_code=503)
        self.assertContains(response, text="You requested: /", count=1, status_code=503)

    def test_middleware_with_non_staff_user(self):
        # A logged in user that is not a staff user should see the 503 message
        self.client.login(username="maintenance", password="password")

        with self.settings(MAINTENANCE_MODE=True):
            response = self.client.get("/")
        self.assertContains(response, text="Temporary unavailable", count=1, status_code=503)

    def test_middleware_with_staff_user(self):
        # A logged in user that _is_ a staff user should be able to
        # use the site normally
        User.objects.filter(pk=self.user.pk).update(is_staff=True)

        self.client.login(username="maintenance", password="password")

        with self.settings(MAINTENANCE_MODE=True):
            response = self.client.get("/")
        self.assertContains(response, text="Rendered response page", count=1, status_code=200)

    def test_middleware_with_staff_user_denied(self):
        # A logged in user that _is_ a staff user should be able to
        # use the site normally
        User.objects.filter(pk=self.user.pk).update(is_staff=True)

        self.client.login(username="maintenance", password="password")

        with self.settings(MAINTENANCE_MODE=True, MAINTENANCE_ALLOW_STAFF=False):
            response = self.client.get("/")
        self.assertContains(response, text="Temporary unavailable", count=1, status_code=503)

    def test_middleware_with_superuser_user_denied(self):
        # A logged in user that _is_ a staff user should be able to
        # use the site normally
        User.objects.filter(pk=self.user.pk).update(is_superuser=True)

        self.client.login(username="maintenance", password="password")

        with self.settings(MAINTENANCE_MODE=True, MAINTENANCE_ALLOW_SUPERUSER=False):
            response = self.client.get("/")
        self.assertContains(response, text="Temporary unavailable", count=1, status_code=503)

    def test_middleware_with_superuser_user_allowed(self):
        # A logged in user that _is_ a staff user should be able to
        # use the site normally
        User.objects.filter(pk=self.user.pk).update(is_superuser=True)

        self.client.login(username="maintenance", password="password")

        with self.settings(MAINTENANCE_MODE=True, MAINTENANCE_ALLOW_STAFF=False):
            response = self.client.get("/")
        self.assertContains(response, text="Rendered response page", count=1, status_code=200)

    def test_middleware_with_internal_ips(self):
        # A user that visits the site from an IP in ``INTERNAL_IPS``
        # should be able to use the site normally

        # Use a new Client instance to be able to set the REMOTE_ADDR used by INTERNAL_IPS
        client = Client(REMOTE_ADDR="127.0.0.1")

        with self.settings(MAINTENANCE_MODE=True, INTERNAL_IPS=("127.0.0.1",)):
            response = client.get("/")
        self.assertContains(response, text="Rendered response page", count=1, status_code=200)

    def test_middleware_with_internal_ips_range(self):
        client = Client(REMOTE_ADDR="10.10.10.1")

        with self.settings(MAINTENANCE_MODE=True, INTERNAL_IPS=("10.10.10.0/24",)):
            response = client.get("/")
        self.assertContains(response, text="Rendered response page", count=1, status_code=200)

    def test_ignored_path(self):
        # A path is ignored when applying the maintanance mode and
        # should be reachable normally
        with self.settings(MAINTENANCE_MODE=True):
            # Note that we cannot override the settings here, since they are
            # ONLY used when the middleware starts up.
            # For this reason, MAINTENANCE_IGNORE_URLS is set in the base
            # settings file.
            response = self.client.get("/ignored/")
        self.assertContains(response, text="Rendered response page", count=1, status_code=200)

    def test_management_command(self):
        out = StringIO()
        # Explicitly disabling the ``MAINTENANCE_MODE``
        with self.settings(MAINTENANCE_MODE=False):
            management.call_command("maintenance", "on", stdout=out)
            self.assertContains(self.client.get("/"), text="Temporary unavailable", count=1, status_code=503)

            management.call_command("maintenance", "off", stdout=out)
            self.assertContains(self.client.get("/"), text="Rendered response page", count=1, status_code=200)