Skip to content

Commit

Permalink
fixed broken test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
bashu committed Oct 9, 2020
1 parent 7e46b58 commit 25df5d8
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 75 deletions.
72 changes: 42 additions & 30 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,36 +1,48 @@
# Config file for automatic testing at travis-ci.org
dist: xenial
language: python

cache: pip
sudo: false
python:
- "3.7"
- "3.6"
- "3.5"
- "3.4"
- "2.7"
env:
- DJANGO="Django>=2.1,<2.2"
- DJANGO="Django>=2.0,<2.1"
- DJANGO="Django>=1.11,<1.12"
- DJANGO="https://github.com/django/django/archive/master.tar.gz"
before_install:
- pip install -q $DJANGO coveralls

matrix:
include:
- env: TOXENV=py34-dj111
python: 3.4
- env: TOXENV=py34-dj2
python: 3.4
- env: TOXENV=py35-dj111
python: 3.5
- env: TOXENV=py35-dj2
python: 3.5
- env: TOXENV=py35-dj21
python: 3.5
- env: TOXENV=py35-dj22
python: 3.5
- env: TOXENV=py36-dj111
python: 3.6
- env: TOXENV=py36-dj2
python: 3.6
- env: TOXENV=py36-dj21
python: 3.6
- env: TOXENV=py36-dj22
python: 3.6
- env: TOXENV=py36-dj3
python: 3.6
- env: TOXENV=py37-dj2
python: 3.7
- env: TOXENV=py37-dj21
python: 3.7
- env: TOXENV=py37-dj22
python: 3.7
- env: TOXENV=py37-dj3
python: 3.7
- env: TOXENV=py38-dj22
python: 3.8
- env: TOXENV=py38-dj3
python: 3.8

install:
- python setup.py develop
- pip install tox

script:
- python setup.py test
after_success:
- coveralls
matrix:
exclude:
- python: "3.4"
env: DJANGO="Django>=2.1,<2.2"
- python: "2.7"
env: DJANGO="Django>=2.1,<2.2"
- python: "2.7"
env: DJANGO="Django>=2.0,<2.1"
- python: "2.7"
env: DJANGO="https://github.com/django/django/archive/master.tar.gz"
allow_failures:
- env: DJANGO="https://github.com/django/django/archive/master.tar.gz"
- tox
51 changes: 13 additions & 38 deletions maintenancemode/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,20 @@
import os.path

from django.core import management
from django.http import HttpResponse
from django.utils.six import StringIO
try:
from django.utils.six import StringIO
except ImportError:
from six import StringIO
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 django.conf.urls import url

from maintenancemode import utils

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))

TEMPLATE_DIRS = [
os.path.join(PROJECT_ROOT, 'test_templates'),
]

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(PROJECT_ROOT, 'test_templates'),
],
'APP_DIRS': True,
}
]

urlpatterns = [
url('^$', lambda r: HttpResponse('Rendered response page'), name='test'),
url('^ignored/$', lambda r: HttpResponse('Rendered response page'), name='test'),
]


@override_settings(INTERNAL_IPS=[])
@override_settings(MAINTENANCE_MODE=False)
@override_settings(TEMPLATE_DIRS=[], TEMPLATES=[])
@override_settings(ROOT_URLCONF='maintenancemode.tests')
class MaintenanceModeMiddlewareTestCase(TestCase):

def setUp(self):
Expand All @@ -66,13 +41,13 @@ def test_disabled_middleware(self):
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, TEMPLATE_DIRS=[], TEMPLATES=[]):
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, TEMPLATE_DIRS=TEMPLATE_DIRS, TEMPLATES=TEMPLATES):
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)
Expand All @@ -81,7 +56,7 @@ 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, TEMPLATE_DIRS=TEMPLATE_DIRS, TEMPLATES=TEMPLATES):
with self.settings(MAINTENANCE_MODE=True):
response = self.client.get('/')
self.assertContains(response, text='Temporary unavailable', count=1, status_code=503)

Expand All @@ -92,7 +67,7 @@ def test_middleware_with_staff_user(self):

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

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

Expand All @@ -103,7 +78,7 @@ def test_middleware_with_staff_user_denied(self):

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

with self.settings(MAINTENANCE_MODE=True, MAINTENANCE_ALLOW_STAFF=False, TEMPLATE_DIRS=TEMPLATE_DIRS, TEMPLATES=TEMPLATES):
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)

Expand All @@ -114,7 +89,7 @@ def test_middleware_with_superuser_user_denied(self):

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

with self.settings(MAINTENANCE_MODE=True, MAINTENANCE_ALLOW_SUPERUSER=False, TEMPLATE_DIRS=TEMPLATE_DIRS, TEMPLATES=TEMPLATES):
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)

Expand All @@ -125,7 +100,7 @@ def test_middleware_with_superuser_user_allowed(self):

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

with self.settings(MAINTENANCE_MODE=True, MAINTENANCE_ALLOW_STAFF=False, TEMPLATE_DIRS=TEMPLATE_DIRS, TEMPLATES=TEMPLATES):
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)

Expand All @@ -150,15 +125,15 @@ def test_middleware_with_internal_ips_range(self):
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, TEMPLATE_DIRS=TEMPLATE_DIRS, TEMPLATES=TEMPLATES):
with self.settings(MAINTENANCE_MODE=True):
with self.settings(IGNORE_URLS=(re.compile(r'^/ignored.*'),)):
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, TEMPLATE_DIRS=TEMPLATE_DIRS, TEMPLATES=TEMPLATES):
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)

Expand Down
17 changes: 17 additions & 0 deletions runtests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!test/usr/bin/env python
import os
import sys

from django.core.management import execute_from_command_line

os.environ['DJANGO_SETTINGS_MODULE'] = 'test_settings'


def runtests():

argv = [sys.argv[0], 'test']
return execute_from_command_line(argv)


if __name__ == '__main__':
sys.exit(runtests())
6 changes: 1 addition & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def find_version(*parts):

install_requires=[
'django-appconf',
'six>=1.9.0',
'ipy',
],

Expand All @@ -46,11 +47,6 @@ def find_version(*parts):
packages=find_packages(exclude=('example*', '*.tests*')),
include_package_data=True,

tests_require=[
'django-setuptest',
],
test_suite='setuptest.setuptest.SetupTestSuite',

zip_safe=False,
classifiers=[
'Development Status :: 5 - Production/Stable',
Expand Down
33 changes: 31 additions & 2 deletions test_settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
import os, re

SECRET_KEY = 'DUMMY_SECRET_KEY'

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))

INTERNAL_IPS = []

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(PROJECT_ROOT, 'test_templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
],
},
},
]

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
Expand All @@ -12,6 +39,7 @@
'django.contrib.admin',
'django.contrib.sessions',
'django.contrib.contenttypes',
'django.contrib.messages',
'django.contrib.sites',

'maintenancemode',
Expand All @@ -20,15 +48,16 @@
MIDDLEWARE = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',

'maintenancemode.middleware.MaintenanceModeMiddleware',
)

ROOT_URLCONF = 'maintenancemode.tests'
ROOT_URLCONF = 'test_urls'

SITE_ID = 1

MAINTENANCE_MODE = True # or ``False`` and use ``maintenance`` command
MAINTENANCE_MODE = False # or ``True`` and use ``maintenance`` command
MAINTENANCE_IGNORE_URLS = (
re.compile(r'^/ignored.*'),
)
File renamed without changes.
15 changes: 15 additions & 0 deletions test_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
try:
from django.conf.urls import include, url
except ImportError:
from django.conf.urls.defaults import include, url

from django.http import HttpResponse

# URL test patterns for popularity. Use this file to ensure a consistent
# set of URL patterns are used when running unit tests. This test_urls
# module should be referred to by your test class.

urlpatterns = [
url('^$', lambda r: HttpResponse('Rendered response page'), name='test'),
url('^ignored/$', lambda r: HttpResponse('Rendered response page'), name='test'),
]
34 changes: 34 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[tox]
skipsdist = True
usedevelop = True
toxworkdir = ../toxworkdir/django-maintenance/.tox

envlist =
; py34
py34-dj{111,2}
; py35
py35-dj{111,2,21,22}
; py36
py36-dj{111,2,21,22,3}
; py37
py37-dj{111,2,21,22,3}
; py38
py38-dj{22,3}

[testenv]
install_command = pip install -e . -U {opts} {packages}
commands = python runtests.py

basepython =
py34: python3.4
py35: python3.5
py36: python3.6
py37: python3.7
py38: python3.8

deps =
dj111: Django>=1.11,<2.0
dj2: Django>=2.0,<2.1
dj21: Django>=2.1,<2.2
dj22: Django>=2.2,<3.0
dj3: Django>=3.0,<3.1

0 comments on commit 25df5d8

Please sign in to comment.