core -- Apply black + isort to all files
This commit is contained in:
parent
104e71dcf6
commit
fdd2b35289
196 changed files with 10727 additions and 8365 deletions
|
@ -2,6 +2,7 @@ import csv
|
|||
from unittest import mock
|
||||
from urllib.parse import parse_qs, urlparse
|
||||
|
||||
import icalendar
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.http import QueryDict
|
||||
|
@ -9,13 +10,10 @@ from django.test import Client
|
|||
from django.utils import timezone
|
||||
from django.utils.functional import cached_property
|
||||
|
||||
import icalendar
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
class TestCaseMixin:
|
||||
|
||||
def assertForbidden(self, response):
|
||||
"""
|
||||
Test that the response (retrieved with a Client) is a denial of access.
|
||||
|
@ -40,31 +38,30 @@ class TestCaseMixin:
|
|||
full_path = request.get_full_path()
|
||||
|
||||
querystring = QueryDict(mutable=True)
|
||||
querystring['next'] = full_path
|
||||
querystring["next"] = full_path
|
||||
|
||||
login_url = '{}?{}'.format(
|
||||
reverse('cof-login'), querystring.urlencode(safe='/'))
|
||||
login_url = "{}?{}".format(
|
||||
reverse("cof-login"), querystring.urlencode(safe="/")
|
||||
)
|
||||
|
||||
# We don't focus on what the login view does.
|
||||
# So don't fetch the redirect.
|
||||
self.assertRedirects(
|
||||
response, login_url,
|
||||
fetch_redirect_response=False,
|
||||
)
|
||||
self.assertRedirects(response, login_url, fetch_redirect_response=False)
|
||||
except AssertionError:
|
||||
raise AssertionError(
|
||||
"%(http_method)s request at %(path)s should be forbidden for "
|
||||
"%(username)s user.\n"
|
||||
"Response isn't 403, nor a redirect to login view. Instead, "
|
||||
"response code is %(code)d." % {
|
||||
'http_method': request.method,
|
||||
'path': request.get_full_path(),
|
||||
'username': (
|
||||
"response code is %(code)d."
|
||||
% {
|
||||
"http_method": request.method,
|
||||
"path": request.get_full_path(),
|
||||
"username": (
|
||||
"'{}'".format(request.user)
|
||||
if request.user.is_authenticated()
|
||||
else 'anonymous'
|
||||
else "anonymous"
|
||||
),
|
||||
'code': response.status_code,
|
||||
"code": response.status_code,
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -85,10 +82,9 @@ class TestCaseMixin:
|
|||
if type(expected) == dict:
|
||||
parsed = urlparse(actual)
|
||||
for part, expected_part in expected.items():
|
||||
if part == 'query':
|
||||
if part == "query":
|
||||
self.assertDictEqual(
|
||||
parse_qs(parsed.query),
|
||||
expected.get('query', {}),
|
||||
parse_qs(parsed.query), expected.get("query", {})
|
||||
)
|
||||
else:
|
||||
self.assertEqual(getattr(parsed, part), expected_part)
|
||||
|
@ -105,22 +101,17 @@ class TestCaseMixin:
|
|||
for k, v in kwargs.items():
|
||||
setattr(self, k, Elt(v))
|
||||
|
||||
results_as_ldap = [
|
||||
Entry(uid=uid, cn=name) for uid, name in results
|
||||
]
|
||||
results_as_ldap = [Entry(uid=uid, cn=name) for uid, name in results]
|
||||
|
||||
mock_connection = mock.MagicMock()
|
||||
mock_connection.entries = results_as_ldap
|
||||
|
||||
# Connection is used as a context manager.
|
||||
mock_context_manager = mock.MagicMock()
|
||||
mock_context_manager.return_value.__enter__.return_value = (
|
||||
mock_connection
|
||||
)
|
||||
mock_context_manager.return_value.__enter__.return_value = mock_connection
|
||||
|
||||
patcher = mock.patch(
|
||||
'gestioncof.autocomplete.Connection',
|
||||
new=mock_context_manager,
|
||||
"gestioncof.autocomplete.Connection", new=mock_context_manager
|
||||
)
|
||||
patcher.start()
|
||||
self.addCleanup(patcher.stop)
|
||||
|
@ -128,8 +119,8 @@ class TestCaseMixin:
|
|||
return mock_connection
|
||||
|
||||
def load_from_csv_response(self, r):
|
||||
decoded = r.content.decode('utf-8')
|
||||
return list(csv.reader(decoded.split('\n')[:-1]))
|
||||
decoded = r.content.decode("utf-8")
|
||||
return list(csv.reader(decoded.split("\n")[:-1]))
|
||||
|
||||
def _test_event_equal(self, event, exp):
|
||||
for k, v_desc in exp.items():
|
||||
|
@ -155,7 +146,7 @@ class TestCaseMixin:
|
|||
|
||||
cal = icalendar.Calendar.from_ical(ical_content)
|
||||
|
||||
for ev in cal.walk('vevent'):
|
||||
for ev in cal.walk("vevent"):
|
||||
found, i_found = self._find_event(ev, remaining)
|
||||
if found:
|
||||
remaining.pop(i_found)
|
||||
|
@ -235,10 +226,11 @@ class ViewTestCaseMixin(TestCaseMixin):
|
|||
can be given by defining an attribute '<method(lowercase)>_data'.
|
||||
|
||||
"""
|
||||
|
||||
url_name = None
|
||||
url_expected = None
|
||||
|
||||
http_methods = ['GET']
|
||||
http_methods = ["GET"]
|
||||
|
||||
auth_user = None
|
||||
auth_forbidden = []
|
||||
|
@ -250,7 +242,7 @@ class ViewTestCaseMixin(TestCaseMixin):
|
|||
# Signals handlers on login/logout send messages.
|
||||
# Due to the way the Django' test Client performs login, this raise an
|
||||
# error. As workaround, we mock the Django' messages module.
|
||||
patcher_messages = mock.patch('gestioncof.signals.messages')
|
||||
patcher_messages = mock.patch("gestioncof.signals.messages")
|
||||
patcher_messages.start()
|
||||
self.addCleanup(patcher_messages.stop)
|
||||
|
||||
|
@ -268,10 +260,7 @@ class ViewTestCaseMixin(TestCaseMixin):
|
|||
if self.auth_user:
|
||||
# The wrapper is a sanity check.
|
||||
self.assertTrue(
|
||||
self.client.login(
|
||||
username=self.auth_user,
|
||||
password=self.auth_user,
|
||||
)
|
||||
self.client.login(username=self.auth_user, password=self.auth_user)
|
||||
)
|
||||
|
||||
def tearDown(self):
|
||||
|
@ -289,8 +278,8 @@ class ViewTestCaseMixin(TestCaseMixin):
|
|||
|
||||
"""
|
||||
return {
|
||||
'user': User.objects.create_user('user', '', 'user'),
|
||||
'root': User.objects.create_superuser('root', '', 'root'),
|
||||
"user": User.objects.create_user("user", "", "user"),
|
||||
"root": User.objects.create_superuser("root", "", "root"),
|
||||
}
|
||||
|
||||
@cached_property
|
||||
|
@ -323,22 +312,25 @@ class ViewTestCaseMixin(TestCaseMixin):
|
|||
|
||||
@property
|
||||
def urls_conf(self):
|
||||
return [{
|
||||
'name': self.url_name,
|
||||
'args': getattr(self, 'url_args', []),
|
||||
'kwargs': getattr(self, 'url_kwargs', {}),
|
||||
'expected': self.url_expected,
|
||||
}]
|
||||
return [
|
||||
{
|
||||
"name": self.url_name,
|
||||
"args": getattr(self, "url_args", []),
|
||||
"kwargs": getattr(self, "url_kwargs", {}),
|
||||
"expected": self.url_expected,
|
||||
}
|
||||
]
|
||||
|
||||
@property
|
||||
def t_urls(self):
|
||||
return [
|
||||
reverse(
|
||||
url_conf['name'],
|
||||
args=url_conf.get('args', []),
|
||||
kwargs=url_conf.get('kwargs', {}),
|
||||
url_conf["name"],
|
||||
args=url_conf.get("args", []),
|
||||
kwargs=url_conf.get("kwargs", {}),
|
||||
)
|
||||
for url_conf in self.urls_conf]
|
||||
for url_conf in self.urls_conf
|
||||
]
|
||||
|
||||
@property
|
||||
def url(self):
|
||||
|
@ -346,7 +338,7 @@ class ViewTestCaseMixin(TestCaseMixin):
|
|||
|
||||
def test_urls(self):
|
||||
for url, conf in zip(self.t_urls, self.urls_conf):
|
||||
self.assertEqual(url, conf['expected'])
|
||||
self.assertEqual(url, conf["expected"])
|
||||
|
||||
def test_forbidden(self):
|
||||
for method in self.http_methods:
|
||||
|
@ -361,7 +353,7 @@ class ViewTestCaseMixin(TestCaseMixin):
|
|||
client.login(username=user, password=user)
|
||||
|
||||
send_request = getattr(client, method)
|
||||
data = getattr(self, '{}_data'.format(method), {})
|
||||
data = getattr(self, "{}_data".format(method), {})
|
||||
|
||||
r = send_request(url, data)
|
||||
self.assertForbidden(r)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue