Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
sudo: false
language: python
python:
- '3.6'
python: 3.7
dist: xenial
sudo: true
env:
matrix:
- TOXENV=py27-dj111
- TOXENV=py36-dj111
- TOXENV=py36-dj20
- TOXENV=py37-dj21
- TOXENV=qa
install:
- pip install -U pip
- pip install tox codecov
- pip install codecov
script:
- tox
after_success:
Expand Down
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ itself and to make it reusable for other apps.

It's pretty much the same as django-dbdiff, except this is for HTTP response.

.. note:: v0.7 supports up to Django < 2.1

Response state assertion
========================

Expand Down
1 change: 1 addition & 0 deletions responsediff/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
"""Basic HTTP response diff-ing for test purposes."""
from .response import Response # noqa
17 changes: 16 additions & 1 deletion responsediff/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import inspect
import json
import os
import re
import subprocess
import tempfile

Expand Down Expand Up @@ -79,8 +80,22 @@ def __init__(self, path):
"""
self.path = path

def assertNoDiff(self, response, selector=None): # noqa
def assertNoDiff(self, response, selector=None, replace=None): # noqa
"""Backward compatibility method for pre-assertWebsiteSame versions."""
if not replace:
replace = []

replace += [
('<[^<]*csrfmiddlewaretoken[^>]*>', '{% csrf_token %}'),
]

for pattern in replace:
response.content = re.sub(
pattern[0],
pattern[1],
response.content.decode('utf8')
).encode('utf8')

diffs, created = self.make_diff(response, selector=selector)

if created or diffs:
Expand Down
7 changes: 3 additions & 4 deletions responsediff/tests/project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,15 @@
'django.contrib.staticfiles',
)

MIDDLEWARE_CLASSES = (
MIDDLEWARE = [ # django 2
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
]

ROOT_URLCONF = 'responsediff.tests.project.urls'

Expand Down
7 changes: 6 additions & 1 deletion responsediff/tests/project/urls.py
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
urlpatterns = []
from django.contrib import admin
from django.urls import path

urlpatterns = [
path('admin/', admin.site.urls),
]
Original file line number Diff line number Diff line change
@@ -1 +1,88 @@
<h1>Not Found</h1><p>The requested URL /admin/ was not found on this server.</p>
<!DOCTYPE html>

<html lang="en-us" >
<head>
<title>Log in | Django site admin</title>
<link rel="stylesheet" type="text/css" href="/static/admin/css/base.css">
<link rel="stylesheet" type="text/css" href="/static/admin/css/login.css">





<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0">
<link rel="stylesheet" type="text/css" href="/static/admin/css/responsive.css">


<meta name="robots" content="NONE,NOARCHIVE">
</head>


<body class=" login"
data-admin-utc-offset="0">

<!-- Container -->
<div id="container">


<!-- Header -->
<div id="header">
<div id="branding">

<h1 id="site-name"><a href="/admin/">Django administration</a></h1>

</div>


</div>
<!-- END Header -->







<!-- Content -->
<div id="content" class="colM">







<div id="content-main">



<form action="/admin/login/" method="post" id="login-form">{% csrf_token %}
<div class="form-row">

<label class="required" for="id_username">Username:</label> <input type="text" name="username" autofocus required id="id_username">
</div>
<div class="form-row">

<label class="required" for="id_password">Password:</label> <input type="password" name="password" required id="id_password">
<input type="hidden" name="next" value="/admin/">
</div>


<div class="submit-row">
<label>&nbsp;</label><input type="submit" value="Log in">
</div>
</form>

</div>


<br class="clear">
</div>
<!-- END Content -->

<div id="footer"></div>
</div>
<!-- END Container -->

</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"status_code": 404
"status_code": 200
}
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
<h1>Not Found</h1>
---
<p>The requested URL /admin/ was not found on this server.</p>
<h1 id="site-name"><a href="/admin/">Django administration</a></h1>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"status_code": 404
"status_code": 200
}
88 changes: 88 additions & 0 deletions responsediff/tests/responses/test_response.content
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<!DOCTYPE html>

<html lang="en-us" >
<head>
<title>Log in | Django site admin</title>
<link rel="stylesheet" type="text/css" href="/static/admin/css/base.css">
<link rel="stylesheet" type="text/css" href="/static/admin/css/login.css">





<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0">
<link rel="stylesheet" type="text/css" href="/static/admin/css/responsive.css">


<meta name="robots" content="NONE,NOARCHIVE">
</head>


<body class=" login"
data-admin-utc-offset="0">

<!-- Container -->
<div id="container">


<!-- Header -->
<div id="header">
<div id="branding">

<h1 id="site-name"><a href="/admin/">Django administration</a></h1>

</div>


</div>
<!-- END Header -->







<!-- Content -->
<div id="content" class="colM">







<div id="content-main">



<form action="/admin/login/" method="post" id="login-form">{% csrf_token %}
<div class="form-row">

<label class="required" for="id_username">Username:</label> <input type="text" name="username" autofocus required id="id_username">
</div>
<div class="form-row">

<label class="required" for="id_password">Password:</label> <input type="password" name="password" required id="id_password">
<input type="hidden" name="next" value="/admin/">
</div>


<div class="submit-row">
<label>&nbsp;</label><input type="submit" value="Log in">
</div>
</form>

</div>


<br class="clear">
</div>
<!-- END Content -->

<div id="footer"></div>
</div>
<!-- END Container -->

</body>
</html>
3 changes: 3 additions & 0 deletions responsediff/tests/responses/test_response.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"status_code": 200
}
7 changes: 5 additions & 2 deletions responsediff/tests/test_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ def get_client(self, fixtures):
return client

def test_assertNoDiff(self): # noqa
self.assertResponseDiffEmpty(test.Client().get('/admin/'))
self.assertResponseDiffEmpty(test.Client().get('/admin/login/'))

def test_assertNoDiffSelector(self): # noqa
self.assertResponseDiffEmpty(test.Client().get('/admin/'), 'h1, p')
self.assertResponseDiffEmpty(
test.Client().get('/admin/login/'),
'h1, p'
)

def test_assertNoDiffSelector_non_ascii(self): # noqa
response = test.Client().get('/admin/')
Expand Down
7 changes: 7 additions & 0 deletions responsediff/tests/test_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from responsediff import Response


def test_response(client):
"""Check that csrf_token was replaced and that the thing works."""
res = client.get('/admin/login/')
Response('responsediff/tests/responses/test_response').assertNoDiff(res)
4 changes: 2 additions & 2 deletions responsediff/tests/test_responsediff.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_path(self):
assert Response.for_test(self).path == expected

def test_story(self):
result = test.Client().get('/admin/')
result = test.Client().get('/adminfoo/')
expected = Response.for_test(self)

if os.path.exists(expected.content_path): # pragma: no cover
Expand Down Expand Up @@ -71,7 +71,7 @@ def test_story(self):
@@ -1 +1 @@
-bla
\ No newline at end of file
+<h1>Not Found</h1><p>The requested URL /admin/ was not found on this server.</p>
+<h1>Not Found</h1><p>The requested URL /adminfoo/ was not found on this server.</p>
\ No newline at end of file
'''.lstrip()

Expand Down
1 change: 1 addition & 0 deletions responses/test_response.content
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Not Found</h1><p>The requested resource was not found on this server.</p>
3 changes: 3 additions & 0 deletions responses/test_response.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"status_code": 404
}
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def read(fname):

setup(
name='django-responsediff',
version='0.7.11',
version='0.8.0',
description='HTTP response diffing against fixtures for testing',
author='James Pic',
author_email='jamespic@gmail.com',
Expand Down
7 changes: 3 additions & 4 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py{27,36,py}-dj{111,20}
envlist = py{37,py}-dj{21}

[testenv]
usedevelop = true
Expand All @@ -21,12 +21,11 @@ deps =
pytest-cov
mock
coverage
dj111: Django>=1.11,<2.0
dj20: Django>=2.0,<2.1
dj21: Django>=2.1,<2.2
setenv =
PIP_ALLOW_EXTERNAL=true
DJANGO_SETTINGS_MODULE=responsediff.tests.project.settings
passenv = TEST_* DBDIFF_*
passenv = TEST_* DBDIFF_* FIXTURE_*

[testenv:qa]
basepython = python2.7
Expand Down