Skip to content

Commit

Permalink
Make accept-invite viewname configurable in django settings
Browse files Browse the repository at this point in the history
  • Loading branch information
BartoszCki committed Jun 21, 2018
1 parent 22f9701 commit a36f341
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
7 changes: 7 additions & 0 deletions invitations/app_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_')
2 changes: 1 addition & 1 deletion invitations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions tests/allauth/test_allauth.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from invitations.app_settings import app_settings

try:
from django.urls import reverse
except ImportError:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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')
Expand Down
26 changes: 13 additions & 13 deletions tests/basic/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def test_valid_form_submission(self, user_a):
url = re.search(
"(?P<url>/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'
Expand All @@ -132,7 +132,7 @@ def test_valid_form_submission_with_swapped_model(self, user_a):
url = re.search(
"(?P<url>/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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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/'

Expand All @@ -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

Expand All @@ -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/'

Expand All @@ -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
Expand All @@ -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/'
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit a36f341

Please sign in to comment.