From a36f341adfcb929d539b9274398e92fa6d273b3a Mon Sep 17 00:00:00 2001 From: Bartosz Cierocki Date: Thu, 21 Jun 2018 21:51:26 +0200 Subject: [PATCH] Make accept-invite viewname configurable in django settings --- invitations/app_settings.py | 7 +++++++ invitations/models.py | 2 +- tests/allauth/test_allauth.py | 8 +++++--- tests/basic/tests.py | 26 +++++++++++++------------- 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/invitations/app_settings.py b/invitations/app_settings.py index d2b59d1..aad6ce8 100644 --- a/invitations/app_settings.py +++ b/invitations/app_settings.py @@ -98,5 +98,12 @@ def ADMIN_CHANGE_FORM(self): "invitations.forms.InvitationAdminChangeForm" ) + @property + def COMFIRMATION_VIEWNAME(self): + return self._setting( + "COMFIRMATION_VIEWNAME", + "invitations:accept-invite" + ) + app_settings = AppSettings('INVITATIONS_') diff --git a/invitations/models.py b/invitations/models.py index 650b60a..0a2ba56 100644 --- a/invitations/models.py +++ b/invitations/models.py @@ -43,7 +43,7 @@ def key_expired(self): def send_invitation(self, request, **kwargs): current_site = kwargs.pop('site', Site.objects.get_current()) - invite_url = reverse('invitations:accept-invite', + invite_url = reverse(app_settings.COMFIRMATION_VIEWNAME, args=[self.key]) invite_url = request.build_absolute_uri(invite_url) ctx = kwargs diff --git a/tests/allauth/test_allauth.py b/tests/allauth/test_allauth.py index 8bf7209..bc1d676 100644 --- a/tests/allauth/test_allauth.py +++ b/tests/allauth/test_allauth.py @@ -1,3 +1,5 @@ +from invitations.app_settings import app_settings + try: from django.urls import reverse except ImportError: @@ -28,7 +30,7 @@ def test_accept_invite_accepted_invitation_after_signup( settings.INVITATIONS_ACCEPT_INVITE_AFTER_SIGNUP = True client_with_method = getattr(self.client, method) resp = client_with_method( - reverse('invitations:accept-invite', + reverse(app_settings.COMFIRMATION_VIEWNAME, kwargs={'key': sent_invitation_by_user_a.key} ), follow=True) assert resp.status_code == 200 @@ -59,7 +61,7 @@ def test_invite_accepted_after_signup_with_altered_case_email( settings.INVITATIONS_ACCEPT_INVITE_AFTER_SIGNUP = True client_with_method = getattr(self.client, method) resp = client_with_method( - reverse('invitations:accept-invite', + reverse(app_settings.COMFIRMATION_VIEWNAME, kwargs={'key': sent_invitation_by_user_a.key} ), follow=True) @@ -92,7 +94,7 @@ def test_accept_invite_allauth( client_with_method = getattr(self.client, method) resp = client_with_method( reverse( - 'invitations:accept-invite', + app_settings.COMFIRMATION_VIEWNAME, kwargs={'key': sent_invitation_by_user_a.key} ), follow=True) invite = Invitation.objects.get(email='email@example.com') diff --git a/tests/basic/tests.py b/tests/basic/tests.py index 252ceb4..1e9200b 100644 --- a/tests/basic/tests.py +++ b/tests/basic/tests.py @@ -110,7 +110,7 @@ def test_valid_form_submission(self, user_a): url = re.search( "(?P/invitations/[^\s]+)", mail.outbox[0].body).group("url") assert url == reverse( - 'invitations:accept-invite', kwargs={'key': invitation.key}) + app_settings.COMFIRMATION_VIEWNAME, kwargs={'key': invitation.key}) @override_settings( INVITATION_MODEL='ExampleSwappableInvitation' @@ -132,7 +132,7 @@ def test_valid_form_submission_with_swapped_model(self, user_a): url = re.search( "(?P/invitations/[^\s]+)", mail.outbox[0].body).group("url") assert url == reverse( - 'invitations:accept-invite', kwargs={'key': invitation.key}) + app_settings.COMFIRMATION_VIEWNAME, kwargs={'key': invitation.key}) @pytest.mark.django_db @@ -143,7 +143,7 @@ def test_accept_invite_get_is_404(self, settings, invitation_b): settings.INVITATIONS_CONFIRM_INVITE_ON_GET = False resp = self.client.get( reverse( - 'invitations:accept-invite', + app_settings.COMFIRMATION_VIEWNAME, kwargs={'key': invitation_b.key}), follow=True) assert resp.status_code == 404 @@ -155,7 +155,7 @@ def test_accept_invite_get_is_404(self, settings, invitation_b): def test_accept_invite_invalid_key(self, method): client_with_method = getattr(self.client, method) resp = client_with_method( - reverse('invitations:accept-invite', kwargs={'key': 'invalidKey'}), + reverse(app_settings.COMFIRMATION_VIEWNAME, kwargs={'key': 'invalidKey'}), follow=True) assert resp.status_code == 410 @@ -168,7 +168,7 @@ def test_accept_invite_invalid_key_error_disabled(self, settings, method): settings.INVITATIONS_LOGIN_REDIRECT = '/login-url/' client_with_method = getattr(self.client, method) resp = client_with_method( - reverse('invitations:accept-invite', kwargs={'key': 'invalidKey'}), + reverse(app_settings.COMFIRMATION_VIEWNAME, kwargs={'key': 'invalidKey'}), follow=True) assert resp.request['PATH_INFO'] == '/login-url/' @@ -179,7 +179,7 @@ def test_accept_invite_invalid_key_error_disabled(self, settings, method): def test_accept_invite_accepted_key(self, accepted_invitation, method): client_with_method = getattr(self.client, method) resp = client_with_method( - reverse('invitations:accept-invite', + reverse(app_settings.COMFIRMATION_VIEWNAME, kwargs={'key': accepted_invitation.key}), follow=True) assert resp.status_code == 410 @@ -193,7 +193,7 @@ def test_accept_invite_accepted_key_error_disabled( settings.INVITATIONS_LOGIN_REDIRECT = '/login-url/' client_with_method = getattr(self.client, method) resp = client_with_method( - reverse('invitations:accept-invite', + reverse(app_settings.COMFIRMATION_VIEWNAME, kwargs={'key': accepted_invitation.key}), follow=True) assert resp.request['PATH_INFO'] == '/login-url/' @@ -206,7 +206,7 @@ def test_accept_invite_expired_key( settings.INVITATIONS_INVITATION_EXPIRY = 0 client_with_method = getattr(self.client, method) resp = client_with_method( - reverse('invitations:accept-invite', + reverse(app_settings.COMFIRMATION_VIEWNAME, kwargs={'key': sent_invitation_by_user_a.key} ), follow=True) assert resp.status_code == 410 @@ -222,7 +222,7 @@ def test_accept_invite_expired_key_error_disabled( settings.INVITATIONS_SIGNUP_REDIRECT = '/signup-url/' client_with_method = getattr(self.client, method) resp = client_with_method( - reverse('invitations:accept-invite', + reverse(app_settings.COMFIRMATION_VIEWNAME, kwargs={'key': sent_invitation_by_user_a.key} ), follow=True) assert resp.request['PATH_INFO'] == '/signup-url/' @@ -236,7 +236,7 @@ def test_accept_invite( settings.INVITATIONS_SIGNUP_REDIRECT = '/non-existent-url/' client_with_method = getattr(self.client, method) resp = client_with_method( - reverse('invitations:accept-invite', + reverse(app_settings.COMFIRMATION_VIEWNAME, kwargs={'key': sent_invitation_by_user_a.key} ), follow=True) invite = Invitation.objects.get(email='email@example.com') @@ -247,7 +247,7 @@ def test_accept_invite( def test_signup_redirect(self, settings, sent_invitation_by_user_a): settings.INVITATIONS_SIGNUP_REDIRECT = '/non-existent-url/' resp = self.client.post( - reverse('invitations:accept-invite', + reverse(app_settings.COMFIRMATION_VIEWNAME, kwargs={'key': sent_invitation_by_user_a.key} ), follow=True) invite = Invitation.objects.get(email='email@example.com') @@ -261,7 +261,7 @@ class TestInvitationSignals: @patch('invitations.signals.invite_url_sent.send') def test_invite_url_sent_triggered_correctly( self, mock_signal, sent_invitation_by_user_a, user_a): - invite_url = reverse('invitations:accept-invite', + invite_url = reverse(app_settings.COMFIRMATION_VIEWNAME, args=[sent_invitation_by_user_a.key]) request = RequestFactory().get('/') invite_url = request.build_absolute_uri(invite_url) @@ -288,7 +288,7 @@ def test_invite_invite_accepted_triggered_correctly( sent_invitation_by_user_a.send_invitation(request) self.client.post( - reverse('invitations:accept-invite', + reverse(app_settings.COMFIRMATION_VIEWNAME, kwargs={'key': sent_invitation_by_user_a.key} ), follow=True) assert mock_signal.called