Skip to content

Commit 4fc56b4

Browse files
authored
Merge pull request #11 from easy-as-python/lint-code
Use black code style and lint with pyflakes
2 parents 6709bda + db93582 commit 4fc56b4

File tree

17 files changed

+176
-163
lines changed

17 files changed

+176
-163
lines changed

.travis.yml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,26 @@ language: python
33
cache: pip
44

55
python:
6-
- "3.5"
7-
- "3.6"
86
- "3.7"
7+
- "3.6"
8+
- "3.5"
99
- "3.8-dev"
1010

1111
install:
1212
- pip install tox-travis
1313

1414
script:
1515
- tox
16+
17+
stages:
18+
- lint
19+
- test
20+
21+
jobs:
22+
include:
23+
- stage: lint
24+
install:
25+
- pip install black pyflakes
26+
script:
27+
- pyflakes webmention tests
28+
- black --check webmention tests

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[tool.black]
2+
line-length = 120
3+
target-version = ['py35', 'py36', 'py37', 'py38']

tests/test_middleware.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,35 @@ def middleware():
1919

2020
def test_process_request_creates_link_header(middleware):
2121
request = Mock()
22-
request.scheme = 'http'
23-
request.META = {'HTTP_HOST': 'example.com'}
22+
request.scheme = "http"
23+
request.META = {"HTTP_HOST": "example.com"}
2424

2525
response = HttpResponse()
2626
response = middleware.process_response(request, response)
2727

2828
expected_link_header = '<{scheme}://{host}{path}>; rel="webmention"'.format(
29-
scheme=request.scheme,
30-
host=request.META.get('HTTP_HOST'),
31-
path=reverse('webmention:receive')
29+
scheme=request.scheme, host=request.META.get("HTTP_HOST"), path=reverse("webmention:receive")
3230
)
3331

34-
assert 'Link' in response
35-
assert response['Link'] == expected_link_header
32+
assert "Link" in response
33+
assert response["Link"] == expected_link_header
34+
3635

3736
def test_process_request_appends_link_header(middleware):
3837
request = Mock()
39-
request.scheme = 'http'
40-
request.META = {'HTTP_HOST': 'example.com'}
38+
request.scheme = "http"
39+
request.META = {"HTTP_HOST": "example.com"}
4140

4241
response = HttpResponse()
4342
original_link_header = '<meta.rdf>; rel="meta"'
44-
response['Link'] = original_link_header
43+
response["Link"] = original_link_header
4544
response = middleware.process_response(request, response)
4645

4746
new_link_header = '<{scheme}://{host}{path}>; rel="webmention"'.format(
48-
scheme=request.scheme,
49-
host=request.META.get('HTTP_HOST'),
50-
path=reverse('webmention:receive')
47+
scheme=request.scheme, host=request.META.get("HTTP_HOST"), path=reverse("webmention:receive")
5148
)
5249

53-
expected_link_header = ', '.join((original_link_header, new_link_header))
50+
expected_link_header = ", ".join((original_link_header, new_link_header))
5451

55-
assert 'Link' in response
56-
assert response['Link'] == expected_link_header
52+
assert "Link" in response
53+
assert response["Link"] == expected_link_header

tests/test_models.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,61 @@
77

88
@pytest.fixture
99
def test_response_body():
10-
return 'foo'
10+
return "foo"
1111

1212

1313
@pytest.mark.django_db
1414
def test_str(test_source, test_target, test_response_body):
15-
webmention = WebMentionResponse.objects.create(source=test_source, response_to=test_target, response_body=test_response_body)
15+
webmention = WebMentionResponse.objects.create(
16+
source=test_source, response_to=test_target, response_body=test_response_body
17+
)
1618
webmention.save()
1719

1820
assert str(webmention) == webmention.source
1921

22+
2023
@pytest.mark.django_db
2124
def test_source_for_admin(test_source, test_target, test_response_body):
22-
webmention = WebMentionResponse.objects.create(source=test_source, response_to=test_target, response_body=test_response_body)
25+
webmention = WebMentionResponse.objects.create(
26+
source=test_source, response_to=test_target, response_body=test_response_body
27+
)
2328
webmention.save()
2429

2530
assert webmention.source_for_admin() == '<a href="{href}">{href}</a>'.format(href=webmention.source)
2631

32+
2733
@pytest.mark.django_db
2834
def test_response_to_for_admin(test_source, test_target, test_response_body):
29-
webmention = WebMentionResponse.objects.create(source=test_source, response_to=test_target, response_body=test_response_body)
35+
webmention = WebMentionResponse.objects.create(
36+
source=test_source, response_to=test_target, response_body=test_response_body
37+
)
3038
webmention.save()
3139

3240
assert webmention.response_to_for_admin() == '<a href="{href}">{href}</a>'.format(href=webmention.response_to)
3341

34-
@patch('webmention.models.WebMentionResponse.save')
42+
43+
@patch("webmention.models.WebMentionResponse.save")
3544
def test_invalidate_when_not_previously_saved(mock_save):
3645
webmention = WebMentionResponse()
3746
webmention.invalidate()
3847

3948
assert not mock_save.called
4049

50+
4151
@pytest.mark.django_db
4252
def test_invalidate_when_previously_saved(test_source, test_target, test_response_body):
43-
webmention = WebMentionResponse.objects.create(source=test_source, response_to=test_target, response_body=test_response_body)
53+
webmention = WebMentionResponse.objects.create(
54+
source=test_source, response_to=test_target, response_body=test_response_body
55+
)
4456
webmention.save()
4557
webmention.invalidate()
4658

4759
assert not webmention.current
4860

49-
@patch('webmention.models.WebMentionResponse.save')
61+
62+
@patch("webmention.models.WebMentionResponse.save")
5063
def test_update_when_previously_invalid(mock_save, test_source, test_target, test_response_body):
51-
webmention = WebMentionResponse.objects.create(source='foo', response_to='bar', response_body='baz', current=False)
64+
webmention = WebMentionResponse.objects.create(source="foo", response_to="bar", response_body="baz", current=False)
5265
assert mock_save.call_count == 1
5366
webmention.update(test_source, test_target, test_response_body)
5467

tests/test_resolution.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@
77
except ImportError:
88
from django.urls import Resolver404
99

10-
from django.test import TestCase
11-
1210
from webmention.resolution import url_resolves, fetch_and_validate_source, SourceFetchError, TargetNotFoundError
1311

1412

15-
@patch('webmention.resolution.resolve')
13+
@patch("webmention.resolution.resolve")
1614
def test_url_resolves_when_resolves(mock_resolve, test_source, test_target):
17-
mock_resolve.return_value = 'foo'
15+
mock_resolve.return_value = "foo"
1816
assert url_resolves(test_target)
1917

20-
@patch('webmention.resolution.resolve')
18+
19+
@patch("webmention.resolution.resolve")
2120
def test_url_resolves_when_does_not_resolve(mock_resolve):
2221
mock_resolve.side_effect = Resolver404
23-
assert not url_resolves('http://example.com/page')
22+
assert not url_resolves("http://example.com/page")
23+
2424

25-
@patch('requests.get')
25+
@patch("requests.get")
2626
def test_fetch_and_validate_source_happy_path(mock_get, test_source, test_target):
2727
mock_response = Mock()
2828
mock_response.status_code = 200
@@ -31,7 +31,8 @@ def test_fetch_and_validate_source_happy_path(mock_get, test_source, test_target
3131

3232
assert fetch_and_validate_source(test_source, test_target) == mock_response.content
3333

34-
@patch('requests.get')
34+
35+
@patch("requests.get")
3536
def test_fetch_and_validate_source_when_source_unavailable(mock_get, test_source, test_target):
3637
mock_response = Mock()
3738
mock_response.status_code = 404
@@ -40,11 +41,12 @@ def test_fetch_and_validate_source_when_source_unavailable(mock_get, test_source
4041
with pytest.raises(SourceFetchError):
4142
fetch_and_validate_source(test_source, test_target)
4243

43-
@patch('requests.get')
44+
45+
@patch("requests.get")
4446
def test_fetch_and_validate_source_when_source_does_not_contain_target(mock_get, test_source, test_target):
4547
mock_response = Mock()
4648
mock_response.status_code = 200
47-
mock_response.content = 'foo'
49+
mock_response.content = "foo"
4850
mock_get.return_value = mock_response
4951

5052
with pytest.raises(TargetNotFoundError):

tests/test_settings.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
SECRET_KEY = 'key-for-testing'
2-
INSTALLED_APPS = [
3-
'webmention',
4-
]
1+
SECRET_KEY = "key-for-testing"
2+
INSTALLED_APPS = ["webmention"]
53

6-
DATABASES = {
7-
'default': {
8-
'ENGINE': 'django.db.backends.sqlite3',
9-
'NAME': 'tests.sqlite3',
10-
}
11-
}
4+
DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": "tests.sqlite3"}}
125

13-
ROOT_URLCONF = 'tests.test_urls'
6+
ROOT_URLCONF = "tests.test_urls"

tests/test_urls.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
from django.conf.urls import url, include
22

3-
urlpatterns = [
4-
url(r'^webmention', include('webmention.urls', namespace='webmention')),
5-
]
3+
urlpatterns = [url(r"^webmention", include("webmention.urls", namespace="webmention"))]

tests/test_views.py

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,60 +10,66 @@
1010

1111
def test_receive_when_source_not_in_post_data(test_target):
1212
request = Mock()
13-
request.method = 'POST'
14-
request.POST = {'target': test_target}
13+
request.method = "POST"
14+
request.POST = {"target": test_target}
1515

1616
response = receive(request)
1717

1818
assert isinstance(response, HttpResponseBadRequest)
1919

20+
2021
def test_receive_when_target_not_in_post_data(test_source):
2122
request = Mock()
22-
request.method = 'POST'
23-
request.POST = {'source': test_source}
23+
request.method = "POST"
24+
request.POST = {"source": test_source}
2425

2526
response = receive(request)
2627

2728
assert isinstance(response, HttpResponseBadRequest)
2829

29-
@patch('webmention.views.url_resolves')
30+
31+
@patch("webmention.views.url_resolves")
3032
def test_receive_when_target_does_not_resolve(mock_url_resolves, test_source, test_target):
3133
request = Mock()
32-
request.method = 'POST'
33-
request.POST = {'source': test_source, 'target': test_target}
34+
request.method = "POST"
35+
request.POST = {"source": test_source, "target": test_target}
3436

3537
mock_url_resolves.return_value = False
3638
response = receive(request)
3739

3840
mock_url_resolves.assert_called_once_with(test_target)
3941
assert isinstance(response, HttpResponseBadRequest)
4042

43+
4144
@pytest.mark.django_db
42-
@patch('webmention.views.WebMentionResponse.update')
43-
@patch('webmention.views.fetch_and_validate_source')
44-
@patch('webmention.views.url_resolves')
45+
@patch("webmention.views.WebMentionResponse.update")
46+
@patch("webmention.views.fetch_and_validate_source")
47+
@patch("webmention.views.url_resolves")
4548
def test_receive_happy_path(mock_url_resolves, mock_fetch_and_validate_source, mock_update, test_source, test_target):
4649
request = Mock()
47-
request.method = 'POST'
48-
request.POST = {'source': test_source, 'target': test_target}
50+
request.method = "POST"
51+
request.POST = {"source": test_source, "target": test_target}
4952

5053
mock_url_resolves.return_value = True
51-
mock_fetch_and_validate_source.return_value = 'foo'
54+
mock_fetch_and_validate_source.return_value = "foo"
5255
response = receive(request)
5356

5457
mock_fetch_and_validate_source.assert_called_once_with(test_source, test_target)
5558
mock_update.assert_called_once_with(test_source, test_target, mock_fetch_and_validate_source.return_value)
5659
mock_url_resolves.assert_called_once_with(test_target)
5760
assert isinstance(response, HttpResponse)
5861

62+
5963
@pytest.mark.django_db
60-
@patch('webmention.views.WebMentionResponse.invalidate')
61-
@patch('webmention.views.fetch_and_validate_source')
62-
@patch('webmention.views.url_resolves')
63-
def test_receive_when_source_unavailable(mock_url_resolves, mock_fetch_and_validate_source, mock_invalidate, test_source, test_target):
64+
@patch("webmention.views.WebMentionResponse.invalidate")
65+
@patch("webmention.views.fetch_and_validate_source")
66+
@patch("webmention.views.url_resolves")
67+
def test_receive_when_source_unavailable(
68+
mock_url_resolves, mock_fetch_and_validate_source, mock_invalidate, test_source, test_target
69+
):
6470
request = Mock()
65-
request.method = 'POST'
66-
request.POST = {'source': test_source, 'target': test_target}
71+
request.method = "POST"
72+
request.POST = {"source": test_source, "target": test_target}
6773

6874
mock_url_resolves.return_value = True
6975
mock_fetch_and_validate_source.side_effect = SourceFetchError
@@ -74,14 +80,17 @@ def test_receive_when_source_unavailable(mock_url_resolves, mock_fetch_and_valid
7480
assert mock_invalidate.call_count == 1
7581
assert isinstance(response, HttpResponseBadRequest)
7682

83+
7784
@pytest.mark.django_db
78-
@patch('webmention.views.WebMentionResponse.invalidate')
79-
@patch('webmention.views.fetch_and_validate_source')
80-
@patch('webmention.views.url_resolves')
81-
def test_receive_when_source_does_not_contain_target(mock_url_resolves, mock_fetch_and_validate_source, mock_invalidate, test_source, test_target):
85+
@patch("webmention.views.WebMentionResponse.invalidate")
86+
@patch("webmention.views.fetch_and_validate_source")
87+
@patch("webmention.views.url_resolves")
88+
def test_receive_when_source_does_not_contain_target(
89+
mock_url_resolves, mock_fetch_and_validate_source, mock_invalidate, test_source, test_target
90+
):
8291
request = Mock()
83-
request.method = 'POST'
84-
request.POST = {'source': test_source, 'target': test_target}
92+
request.method = "POST"
93+
request.POST = {"source": test_source, "target": test_target}
8594

8695
mock_url_resolves.return_value = True
8796
mock_fetch_and_validate_source.side_effect = TargetNotFoundError
@@ -92,13 +101,16 @@ def test_receive_when_source_does_not_contain_target(mock_url_resolves, mock_fet
92101
assert mock_invalidate.call_count == 1
93102
assert isinstance(response, HttpResponseBadRequest)
94103

104+
95105
@pytest.mark.django_db
96-
@patch('webmention.views.fetch_and_validate_source')
97-
@patch('webmention.views.url_resolves')
98-
def test_receive_when_general_exception_occurs(mock_url_resolves, mock_fetch_and_validate_source, test_source, test_target):
106+
@patch("webmention.views.fetch_and_validate_source")
107+
@patch("webmention.views.url_resolves")
108+
def test_receive_when_general_exception_occurs(
109+
mock_url_resolves, mock_fetch_and_validate_source, test_source, test_target
110+
):
99111
request = Mock()
100-
request.method = 'POST'
101-
request.POST = {'source': test_source, 'target': test_target}
112+
request.method = "POST"
113+
request.POST = {"source": test_source, "target": test_target}
102114

103115
mock_url_resolves.return_value = True
104116
mock_fetch_and_validate_source.side_effect = Exception

webmention/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
from . import checks
2+
3+
__all__ = ["checks"]

0 commit comments

Comments
 (0)