Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
Refactored if statement and try/except, added more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jpprins1 committed Oct 17, 2017
1 parent af984e9 commit cca47c0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lizard_auth_server/tests/test_views_api_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def test_duplicate_username_http_response(self):
params = {'username': 'pietje',
'email': 'nietpietje@example.com',
'first_name': 'pietje',
'last_name': 'pietje', }
'last_name': 'pietje'}
response = client.post(
reverse('lizard_auth_server.api_v2.new_user'), params)
self.assertEquals(400, response.status_code)
Expand Down
22 changes: 15 additions & 7 deletions lizard_auth_server/views_api_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,11 @@ def form_valid(self, form):
"""
# The JWT message is validated; now check the message's contents.
if (('username' not in form.cleaned_data) or
('password' not in form.cleaned_data)):
if ((('username' not in form.cleaned_data) or
('password' not in form.cleaned_data))):
return HttpResponseBadRequest(
"username and/or password are missing from the JWT message")

portal = Portal.objects.get(sso_key=form.cleaned_data['iss'])
# Verify the username/password
user = django_authenticate(username=form.cleaned_data.get('username'),
Expand Down Expand Up @@ -451,9 +452,9 @@ def form_valid(self, form):
An error 400 when mandatory keys are missing from the decoded
JWT message or when the language is unknown.
An error 409 when a duplicate username is found.
An error 409 (conflict) when the username is already used.
"""

portal = Portal.objects.get(sso_key=form.cleaned_data['iss'])
# The JWT message is validated; now check the message's contents.
mandatory_keys = ['username', 'email', 'first_name', 'last_name']
Expand Down Expand Up @@ -481,19 +482,22 @@ def form_valid(self, form):
try:
user = User.objects.get(
username=form.cleaned_data['username'])
# Use statuscode 409 (conflict) when an user
# with 'username' is found.
status_code = 409
except User.DoesNotExist:
pass
else:
# If found, return 409 (conflict)
status_code = 409

if not user:
# No user found by email or username
language = form.cleaned_data.get('language', 'en')
visit_url = form.cleaned_data.get('visit_url')

if language not in AVAILABLE_LANGUAGES:
return HttpResponseBadRequest("Language %s is not in %s" % (
language,
AVAILABLE_LANGUAGES))

user = self.create_and_mail_user(
username=form.cleaned_data['username'],
first_name=form.cleaned_data['first_name'],
Expand All @@ -504,6 +508,10 @@ def form_valid(self, form):
visit_url=visit_url)
status_code = 201 # Created

# Return json dump of user data with one of the following status_codes:
# 200 => emailadres already in use (return first matching user)
# 201 => new user created (return new user)
# 409 => username already in use (return user with username)
user_data = construct_user_data(user=user)
return HttpResponse(json.dumps({'user': user_data}),
content_type='application/json',
Expand Down

0 comments on commit cca47c0

Please sign in to comment.