core -- Apply black + isort to all files

This commit is contained in:
Aurélien Delobelle 2018-10-06 12:35:49 +02:00
parent 104e71dcf6
commit fdd2b35289
196 changed files with 10727 additions and 8365 deletions

View file

@ -37,34 +37,32 @@ class TestCaseMixin:
full_path = request.get_full_path()
querystring = QueryDict(mutable=True)
querystring['next'] = full_path
querystring["next"] = full_path
login_url = '/login?' + querystring.urlencode(safe='/')
login_url = "/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,
}
)
def assertForbiddenKfet(self, response, form_ctx='form'):
def assertForbiddenKfet(self, response, form_ctx="form"):
"""
Test that a response (retrieved with a Client) contains error due to
lack of kfet permissions.
@ -83,7 +81,7 @@ class TestCaseMixin:
form = response.context[form_ctx]
self.assertIn("Permission refusée", form.non_field_errors())
except (AssertionError, AttributeError, KeyError):
messages = [str(msg) for msg in response.context['messages']]
messages = [str(msg) for msg in response.context["messages"]]
self.assertIn("Permission refusée", messages)
except AssertionError:
request = response.wsgi_request
@ -91,15 +89,16 @@ class TestCaseMixin:
"%(http_method)s request at %(path)s should raise an error "
"for %(username)s user.\n"
"Cannot find any errors in non-field errors of form "
"'%(form_ctx)s', nor in messages." % {
'http_method': request.method,
'path': request.get_full_path(),
'username': (
"'%(form_ctx)s', nor in messages."
% {
"http_method": request.method,
"path": request.get_full_path(),
"username": (
"'%s'" % request.user
if request.user.is_authenticated
else 'anonymous'
else "anonymous"
),
'form_ctx': form_ctx,
"form_ctx": form_ctx,
}
)
@ -131,10 +130,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)
@ -215,10 +213,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 = []
@ -232,7 +231,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,14 +267,14 @@ class ViewTestCaseMixin(TestCaseMixin):
# Format desc: username, password, trigramme
users_base = {
# user, user, 000
'user': create_user(),
"user": create_user(),
# team, team, 100
'team': create_team(),
"team": create_team(),
# root, root, 200
'root': create_root(),
"root": create_root(),
}
if self.with_liq:
users_base['liq'] = create_user('liq', 'LIQ')
users_base["liq"] = create_user("liq", "LIQ")
return users_base
@cached_property
@ -300,7 +299,7 @@ class ViewTestCaseMixin(TestCaseMixin):
def register_user(self, label, user):
self.users[label] = user
if hasattr(user.profile, 'account_kfet'):
if hasattr(user.profile, "account_kfet"):
self.accounts[label] = user.profile.account_kfet
def get_user(self, label):
@ -310,22 +309,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):
@ -333,7 +335,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:
@ -348,7 +350,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)