You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by ma...@apache.org on 2020/03/04 17:51:58 UTC
[airavata-django-portal] branch develop updated (8994ee3 -> 5292091)
This is an automated email from the ASF dual-hosted git repository.
machristie pushed a change to branch develop
in repository https://gitbox.apache.org/repos/asf/airavata-django-portal.git.
from 8994ee3 Merge branch 'AIRAVATA-3290--Ansible--automatically-set-wagtail-site-record' into develop
new 5b09284 AIRAVATA-3088 Add Create Account link to login page
new 5943eda AIRAVATA-3088 Change tests to directory module
new 88d2488 AIRAVATA-3088 Pass through next from login to create account
new 5292091 Merge branch 'AIRAVATA-3088--Have-'Create-Account'-link-in-gateway-Login-page' into develop
The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
django_airavata/apps/auth/forms.py | 2 +
.../auth/migrations/0006_emailverification_next.py | 20 +++
django_airavata/apps/auth/models.py | 1 +
.../django_airavata_auth/partials/form.html | 6 +-
.../partials/username_password_login_form.html | 7 +-
.../management => apps/auth/tests}/__init__.py | 0
.../apps/auth/{tests.py => tests/test_signals.py} | 3 +-
django_airavata/apps/auth/tests/test_views.py | 183 +++++++++++++++++++++
django_airavata/apps/auth/views.py | 23 ++-
9 files changed, 235 insertions(+), 10 deletions(-)
create mode 100644 django_airavata/apps/auth/migrations/0006_emailverification_next.py
copy django_airavata/{wagtailapps/base/tests/management => apps/auth/tests}/__init__.py (100%)
rename django_airavata/apps/auth/{tests.py => tests/test_signals.py} (98%)
create mode 100644 django_airavata/apps/auth/tests/test_views.py
[airavata-django-portal] 03/04: AIRAVATA-3088 Pass through next
from login to create account
Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
machristie pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/airavata-django-portal.git
commit 88d2488ae4b0719a2b6ba3970531d2e87dcdab71
Author: Marcus Christie <ma...@apache.org>
AuthorDate: Wed Mar 4 12:46:47 2020 -0500
AIRAVATA-3088 Pass through next from login to create account
---
django_airavata/apps/auth/forms.py | 2 +
.../auth/migrations/0006_emailverification_next.py | 20 +++
django_airavata/apps/auth/models.py | 1 +
.../django_airavata_auth/partials/form.html | 6 +-
.../partials/username_password_login_form.html | 2 +-
django_airavata/apps/auth/tests/test_views.py | 183 +++++++++++++++++++++
django_airavata/apps/auth/views.py | 23 ++-
7 files changed, 229 insertions(+), 8 deletions(-)
diff --git a/django_airavata/apps/auth/forms.py b/django_airavata/apps/auth/forms.py
index 374c098..edd551a 100644
--- a/django_airavata/apps/auth/forms.py
+++ b/django_airavata/apps/auth/forms.py
@@ -60,6 +60,8 @@ class CreateAccountForm(forms.Form):
widget=forms.TextInput(attrs={'class': 'form-control',
'placeholder': 'Last Name'}))
+ next = forms.CharField(widget=forms.HiddenInput(), required=False)
+
def clean(self):
cleaned_data = super().clean()
password = cleaned_data.get('password')
diff --git a/django_airavata/apps/auth/migrations/0006_emailverification_next.py b/django_airavata/apps/auth/migrations/0006_emailverification_next.py
new file mode 100644
index 0000000..b68cd5c
--- /dev/null
+++ b/django_airavata/apps/auth/migrations/0006_emailverification_next.py
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.28 on 2020-02-26 21:13
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('django_airavata_auth', '0005_auto_20191211_2011'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='emailverification',
+ name='next',
+ field=models.CharField(blank=True, max_length=255),
+ ),
+ ]
diff --git a/django_airavata/apps/auth/models.py b/django_airavata/apps/auth/models.py
index cfea8c1..2e2f212 100644
--- a/django_airavata/apps/auth/models.py
+++ b/django_airavata/apps/auth/models.py
@@ -14,6 +14,7 @@ class EmailVerification(models.Model):
max_length=36, unique=True, default=uuid.uuid4)
created_date = models.DateTimeField(auto_now_add=True)
verified = models.BooleanField(default=False)
+ next = models.CharField(max_length=255, blank=True)
class EmailTemplate(models.Model):
diff --git a/django_airavata/apps/auth/templates/django_airavata_auth/partials/form.html b/django_airavata/apps/auth/templates/django_airavata_auth/partials/form.html
index ce558f7..aeed7b8 100644
--- a/django_airavata/apps/auth/templates/django_airavata_auth/partials/form.html
+++ b/django_airavata/apps/auth/templates/django_airavata_auth/partials/form.html
@@ -4,7 +4,11 @@
{% csrf_token %}
{% for field in form %}
- {% include './form_field.html' %}
+ {% if field.is_hidden %}
+ {{ field }}
+ {% else %}
+ {% include './form_field.html' %}
+ {% endif %}
{% endfor %}
<button type="submit" class="btn btn-primary btn-block">
diff --git a/django_airavata/apps/auth/templates/django_airavata_auth/partials/username_password_login_form.html b/django_airavata/apps/auth/templates/django_airavata_auth/partials/username_password_login_form.html
index adc82af..a0799b6 100644
--- a/django_airavata/apps/auth/templates/django_airavata_auth/partials/username_password_login_form.html
+++ b/django_airavata/apps/auth/templates/django_airavata_auth/partials/username_password_login_form.html
@@ -7,7 +7,7 @@
<h5 class="card-title">
Log in with {{ options.password.name|default:"a username and password" }}
<small class="ml-2 text-muted">
- (Not registered? <a href="{% url 'django_airavata_auth:create_account' %}">Create Account</a>)
+ (Not registered? <a href="{{ create_account_url }}">Create Account</a>)
</small>
</h5>
{% include "./messages.html" %}
diff --git a/django_airavata/apps/auth/tests/test_views.py b/django_airavata/apps/auth/tests/test_views.py
new file mode 100644
index 0000000..dc7e671
--- /dev/null
+++ b/django_airavata/apps/auth/tests/test_views.py
@@ -0,0 +1,183 @@
+from unittest.mock import patch
+from urllib.parse import urlencode
+
+from django.contrib.auth.models import AnonymousUser
+from django.contrib.messages.middleware import MessageMiddleware
+from django.contrib.sessions.middleware import SessionMiddleware
+from django.core import mail
+from django.http import HttpResponseRedirect
+from django.shortcuts import reverse
+from django.test import RequestFactory, TestCase
+
+from django_airavata.apps.auth import models, views
+
+
+class LoginViewTestCase(TestCase):
+
+ def test_login_with_next_param(self):
+
+ response = self.client.get('/auth/login', data={'next': '/some/url'})
+ self.assertTrue('next' in response.context)
+ self.assertEqual(response.context['next'], '/some/url')
+ self.assertContains(
+ response,
+ '<input type="hidden" name="next" value="/some/url"/>')
+ # Create account url should pass along the next param
+ create_account_url = (reverse('django_airavata_auth:create_account') +
+ "?" +
+ urlencode({'next': '/some/url'}))
+ self.assertContains(response, f'<a href="{create_account_url}">')
+
+
+class CreateAccountViewTestCase(TestCase):
+
+ def setUp(self):
+ self.factory = RequestFactory()
+
+ def test_create_account_with_next_param(self):
+ create_account_url = (reverse('django_airavata_auth:create_account') +
+ "?" +
+ urlencode({'next': '/some/url'}))
+ response = self.client.get(create_account_url)
+ self.assertTrue('form' in response.context)
+ form = response.context['form']
+ self.assertEqual(form.initial['next'], "/some/url")
+
+ # Need to mock the iam_admin_client twice because it is imported in .forms
+ # and in .views
+ @patch('django_airavata.apps.auth.forms.iam_admin_client')
+ @patch('django_airavata.apps.auth.views.iam_admin_client')
+ def test_submit_create_account_with_next(self, forms_iam_admin_client,
+ views_iam_admin_client):
+
+ # make sure there are no EmailVerification records at the beginning
+ self.assertEqual(0, models.EmailVerification.objects.all().count())
+ create_account_url = reverse('django_airavata_auth:create_account')
+ data = {
+ 'username': 'testuser',
+ 'email': 'test@example.com',
+ 'email_again': 'test@example.com',
+ 'first_name': 'Test',
+ 'last_name': 'User',
+ 'password': 'passworD1#',
+ 'password_again': 'passworD1#',
+ 'next': '/next/path'
+ }
+ request = self.factory.post(create_account_url, data)
+ request.user = AnonymousUser()
+ forms_iam_admin_client.is_username_available.return_value = True
+ views_iam_admin_client.register_user.return_value = True
+ # RequestFactory doesn't load middleware so have to manually call
+ # SessionMiddleware and MessageMiddleware since create_account uses
+ # 'messages' framework
+ response = SessionMiddleware(
+ MessageMiddleware(
+ views.create_account))(request)
+ self.assertEqual(302, response.status_code)
+ self.assertIsInstance(response, HttpResponseRedirect)
+ self.assertEqual(reverse('django_airavata_auth:create_account'),
+ response.url)
+ self.assertEqual(len(mail.outbox), 1)
+ email_verification = models.EmailVerification.objects.get(
+ username=data['username'])
+ self.assertIsNotNone(email_verification)
+ self.assertEqual(data['next'], email_verification.next)
+
+ @patch('django_airavata.apps.auth.forms.iam_admin_client')
+ def test_submit_invalid_create_account_with_next(
+ self, forms_iam_admin_client):
+
+ # make sure there are no EmailVerification records at the beginning
+ self.assertEqual(0, models.EmailVerification.objects.all().count())
+ create_account_url = reverse('django_airavata_auth:create_account')
+ data = {
+ 'username': 'testuser',
+ 'email': 'test@example.com',
+ 'email_again': 'test@example.com',
+ 'first_name': 'Test',
+ 'last_name': 'User',
+ 'password': 'passworD1#',
+ # make invalid by having the password_again not match
+ 'password_again': 'DOESNT-MATCH',
+ 'next': '/next/path'
+ }
+ request = self.factory.post(create_account_url, data)
+ request.user = AnonymousUser()
+ forms_iam_admin_client.is_username_available.return_value = True
+ # RequestFactory doesn't load middleware so have to manually call
+ # SessionMiddleware and MessageMiddleware since create_account uses
+ # 'messages' framework
+ response = SessionMiddleware(
+ MessageMiddleware(
+ views.create_account))(request)
+ self.assertEqual(200, response.status_code)
+ self.assertNotIsInstance(response, HttpResponseRedirect)
+ # No email is sent
+ self.assertEqual(len(mail.outbox), 0)
+ # Make sure that the 'next' field was put back into the form
+ self.assertRegex(
+ response.content.decode("utf-8"),
+ '(?s)type="hidden"[^>]+name="next"[^>]+value="/next/path"')
+
+
+class VerifyEmailViewTestCase(TestCase):
+
+ def setUp(self):
+ self.factory = RequestFactory()
+
+ @patch('django_airavata.apps.auth.views.iam_admin_client')
+ def test_verify_email(self, views_iam_admin_client):
+
+ # create an EmailVerification record with a next url
+ email_verification = models.EmailVerification(username='testuser')
+ email_verification.save()
+
+ verify_email_url = reverse(
+ 'django_airavata_auth:verify_email', kwargs={
+ 'code': email_verification.verification_code})
+ request = self.factory.get(verify_email_url)
+ request.user = AnonymousUser()
+ # Skip enabling the user
+ views_iam_admin_client.is_user_enabled.return_value = True
+ # RequestFactory doesn't load middleware so have to manually call
+ # SessionMiddleware and MessageMiddleware since create_account uses
+ # 'messages' framework
+ response = SessionMiddleware(MessageMiddleware(
+ lambda r: views.verify_email(r,
+ email_verification.verification_code)
+ ))(request)
+ self.assertIsInstance(response, HttpResponseRedirect)
+ self.assertEqual(reverse('django_airavata_auth:login'), response.url)
+ email_verification = models.EmailVerification.objects.get(
+ username="testuser")
+ self.assertTrue(email_verification.verified)
+
+ @patch('django_airavata.apps.auth.views.iam_admin_client')
+ def test_verify_email_with_next(self, views_iam_admin_client):
+
+ # create an EmailVerification record with a next url
+ email_verification = models.EmailVerification(
+ username='testuser', next='/next/path')
+ email_verification.save()
+
+ verify_email_url = reverse(
+ 'django_airavata_auth:verify_email', kwargs={
+ 'code': email_verification.verification_code})
+ request = self.factory.get(verify_email_url)
+ request.user = AnonymousUser()
+ # Skip enabling the user
+ views_iam_admin_client.is_user_enabled.return_value = True
+ # RequestFactory doesn't load middleware so have to manually call
+ # SessionMiddleware and MessageMiddleware since create_account uses
+ # 'messages' framework
+ response = SessionMiddleware(MessageMiddleware(
+ lambda r: views.verify_email(r,
+ email_verification.verification_code)
+ ))(request)
+ self.assertIsInstance(response, HttpResponseRedirect)
+ self.assertEqual(reverse('django_airavata_auth:login') + "?" +
+ urlencode({'next': email_verification.next}),
+ response.url)
+ email_verification = models.EmailVerification.objects.get(
+ username="testuser")
+ self.assertTrue(email_verification.verified)
diff --git a/django_airavata/apps/auth/views.py b/django_airavata/apps/auth/views.py
index 4e8decd..f4949df 100644
--- a/django_airavata/apps/auth/views.py
+++ b/django_airavata/apps/auth/views.py
@@ -20,9 +20,16 @@ logger = logging.getLogger(__name__)
def start_login(request):
+ next_url = request.GET.get('next', None)
+ if next_url is not None:
+ create_account_url = (reverse('django_airavata_auth:create_account') +
+ "?" + urlencode({'next': next_url}))
+ else:
+ create_account_url = reverse('django_airavata_auth:create_account')
return render(request, 'django_airavata_auth/login.html', {
'next': request.GET.get('next', None),
'options': settings.AUTHENTICATION_OPTIONS,
+ 'create_account_url': create_account_url
})
@@ -166,8 +173,9 @@ def create_account(request):
form.add_error(None, ValidationError(
"Failed to register user with IAM service"))
else:
+ next = form.cleaned_data['next']
_create_and_send_email_verification_link(
- request, username, email, first_name, last_name)
+ request, username, email, first_name, last_name, next)
messages.success(
request,
"Account request processed successfully. Before you "
@@ -181,7 +189,7 @@ def create_account(request):
"Failed to create account for user", exc_info=e)
form.add_error(None, ValidationError(e.message))
else:
- form = forms.CreateAccountForm()
+ form = forms.CreateAccountForm(initial=request.GET)
return render(request, 'django_airavata_auth/create_account.html', {
'options': settings.AUTHENTICATION_OPTIONS,
'form': form
@@ -198,13 +206,16 @@ def verify_email(request, code):
# Check if user is enabled, if so redirect to login page
username = email_verification.username
logger.debug("Email address verified for {}".format(username))
+ login_url = reverse('django_airavata_auth:login')
+ if email_verification.next:
+ login_url += "?" + urlencode({'next': email_verification.next})
if iam_admin_client.is_user_enabled(username):
logger.debug("User {} is already enabled".format(username))
messages.success(
request,
"Your account has already been successfully created. "
"Please log in now.")
- return redirect(reverse('django_airavata_auth:login'))
+ return redirect(login_url)
else:
logger.debug("Enabling user {}".format(username))
# enable user and inform admins
@@ -222,7 +233,7 @@ def verify_email(request, code):
request,
"Your account has been successfully created. "
"Please log in now.")
- return redirect(reverse('django_airavata_auth:login'))
+ return redirect(login_url)
except ObjectDoesNotExist as e:
# if doesn't exist, give user a form where they can enter their
# username to resend verification code
@@ -283,10 +294,10 @@ def resend_email_link(request):
def _create_and_send_email_verification_link(
- request, username, email, first_name, last_name):
+ request, username, email, first_name, last_name, next):
email_verification = models.EmailVerification(
- username=username)
+ username=username, next=next)
email_verification.save()
verification_uri = request.build_absolute_uri(
[airavata-django-portal] 01/04: AIRAVATA-3088 Add Create Account
link to login page
Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
machristie pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/airavata-django-portal.git
commit 5b09284e52ff78066352e9a165198003b2c9af91
Author: Marcus Christie <ma...@apache.org>
AuthorDate: Tue Jan 28 16:21:14 2020 -0500
AIRAVATA-3088 Add Create Account link to login page
---
.../partials/username_password_login_form.html | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/django_airavata/apps/auth/templates/django_airavata_auth/partials/username_password_login_form.html b/django_airavata/apps/auth/templates/django_airavata_auth/partials/username_password_login_form.html
index 6cc3bcc..adc82af 100644
--- a/django_airavata/apps/auth/templates/django_airavata_auth/partials/username_password_login_form.html
+++ b/django_airavata/apps/auth/templates/django_airavata_auth/partials/username_password_login_form.html
@@ -4,7 +4,12 @@
<div class="col">
<div class="card">
<div class="card-body">
- <h5 class="card-title">Log in with {{ options.password.name|default:"a username and password" }}</h5>
+ <h5 class="card-title">
+ Log in with {{ options.password.name|default:"a username and password" }}
+ <small class="ml-2 text-muted">
+ (Not registered? <a href="{% url 'django_airavata_auth:create_account' %}">Create Account</a>)
+ </small>
+ </h5>
{% include "./messages.html" %}
<form action="{% url 'django_airavata_auth:handle_login' %}" method="post">
{% csrf_token %}
[airavata-django-portal] 02/04: AIRAVATA-3088 Change tests to
directory module
Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
machristie pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/airavata-django-portal.git
commit 5943edae77bb42556a21700434d0143e549c3648
Author: Marcus Christie <ma...@apache.org>
AuthorDate: Wed Jan 29 15:41:22 2020 -0500
AIRAVATA-3088 Change tests to directory module
---
django_airavata/apps/auth/tests/__init__.py | 0
django_airavata/apps/auth/{tests.py => tests/test_signals.py} | 3 +--
2 files changed, 1 insertion(+), 2 deletions(-)
diff --git a/django_airavata/apps/auth/tests/__init__.py b/django_airavata/apps/auth/tests/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/django_airavata/apps/auth/tests.py b/django_airavata/apps/auth/tests/test_signals.py
similarity index 98%
rename from django_airavata/apps/auth/tests.py
rename to django_airavata/apps/auth/tests/test_signals.py
index 2dbcc6a..274f362 100644
--- a/django_airavata/apps/auth/tests.py
+++ b/django_airavata/apps/auth/tests/test_signals.py
@@ -5,8 +5,7 @@ from django.test import RequestFactory, TestCase, override_settings
from airavata.model.group.ttypes import GroupModel
from airavata.model.user.ttypes import UserProfile
from django_airavata.apps.api.signals import user_added_to_group
-
-from . import signals # noqa
+from django_airavata.apps.auth import signals # noqa
GATEWAY_ID = "test-gateway"
SERVER_EMAIL = "admin@test-gateway.com"
[airavata-django-portal] 04/04: Merge branch
'AIRAVATA-3088--Have-'Create-Account'-link-in-gateway-Login-page' into
develop
Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
machristie pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/airavata-django-portal.git
commit 52920918bfc7a6cb1a0ad4d8a1c3fa158e1102dd
Merge: 8994ee3 88d2488
Author: Marcus Christie <ma...@apache.org>
AuthorDate: Wed Mar 4 12:49:57 2020 -0500
Merge branch 'AIRAVATA-3088--Have-'Create-Account'-link-in-gateway-Login-page' into develop
django_airavata/apps/auth/forms.py | 2 +
.../auth/migrations/0006_emailverification_next.py | 20 +++
django_airavata/apps/auth/models.py | 1 +
.../django_airavata_auth/partials/form.html | 6 +-
.../partials/username_password_login_form.html | 7 +-
django_airavata/apps/auth/tests/__init__.py | 0
.../apps/auth/{tests.py => tests/test_signals.py} | 3 +-
django_airavata/apps/auth/tests/test_views.py | 183 +++++++++++++++++++++
django_airavata/apps/auth/views.py | 23 ++-
9 files changed, 235 insertions(+), 10 deletions(-)