Skip to content

Commit

Permalink
Ensure PROJECT_INVITATION_URL setting works for multipe domains
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankApiyo committed Jul 25, 2023
1 parent 7f0c5d8 commit 3ae3e5c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/projects.rst
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ adding the setting ``PROJECT_INVITATION_URL``

::

PROJECT_INVITATION_URL = 'https://example.com/register'
PROJECT_INVITATION_URL = {'*': 'https://example.com/register'}


Update a project invitation
Expand Down
39 changes: 35 additions & 4 deletions onadata/apps/api/tests/viewsets/test_project_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2841,7 +2841,13 @@ def test_only_admins_allowed(self, mock_send_mail):
else:
self.assertEqual(response.status_code, 403)

@override_settings(PROJECT_INVITATION_URL="https://example.com/register")
@override_settings(
PROJECT_INVITATION_URL={
"*": "https://example.com/register",
"onadata.com": "https://onadata.com/register",
}
)
@override_settings(ALLOWED_HOSTS=["*"])
def test_create_invitation(self, mock_send_mail):
"""Project invitation can be created"""
post_data = {
Expand Down Expand Up @@ -2882,6 +2888,31 @@ def test_create_invitation(self, mock_send_mail):
response = self.view(request, pk=self.project.pk)
self.assertEqual(response.status_code, 400)

# Project invitations are created for non-default host
request = self.factory.post(
"/",
data=json.dumps(post_data),
content_type="application/json",
**self.extra,
)
request.META["HTTP_HOST"] = "onadata.com"
response = self.view(request, pk=self.project.pk)
self.assertEqual(response.status_code, 200)
self.assertEqual(self.project.invitations.count(), 1)
invitation = self.project.invitations.first()
self.assertEqual(
response.data,
{
"id": invitation.pk,
"email": "[email protected]",
"role": "editor",
"status": 1,
},
)
mock_send_mail.assert_called_once_with(
invitation.pk, "https://example.com/register"
)

def test_email_required(self, mock_send_mail):
"""email is required"""
# blank string
Expand Down Expand Up @@ -3073,7 +3104,7 @@ def test_only_admins_allowed(self, mock_send_mail):
else:
self.assertEqual(response.status_code, 403)

@override_settings(PROJECT_INVITATION_URL="https://example.com/register")
@override_settings(PROJECT_INVITATION_URL={"*": "https://example.com/register"})
def test_update(self, mock_send_mail):
"""We can update an invitation"""
payload = {
Expand Down Expand Up @@ -3133,7 +3164,7 @@ def test_update_role_only(self, mock_send_mail):
)
mock_send_mail.assert_not_called()

@override_settings(PROJECT_INVITATION_URL="https://example.com/register")
@override_settings(PROJECT_INVITATION_URL={"*": "https://example.com/register"})
def test_update_email_only(self, mock_send_mail):
"""We can update email only"""
payload = {
Expand Down Expand Up @@ -3351,7 +3382,7 @@ def test_only_admins_allowed(self, mock_send_mail):

mock_send_mail.assert_not_called()

@override_settings(PROJECT_INVITATION_URL="https://example.com/register")
@override_settings(PROJECT_INVITATION_URL={"*": "https://example.com/register"})
def test_resend_invite(self, mock_send_mail):
"""Invitation is revoked"""
invitation = self.project.invitations.create(
Expand Down
22 changes: 19 additions & 3 deletions onadata/libs/tests/utils/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def setUp(self):

@override_settings(
VERIFICATION_URL={
"stage-testserver": 'https://stage-testserver/email-verification-confirmation',
"stage-testserver": "https://stage-testserver/email-verification-confirmation",
"*": None,
}
)
Expand Down Expand Up @@ -89,7 +89,10 @@ def test_get_verification_url(self):

self.assertEqual(
verification_url,
("https://stage-testserver/email-verification-confirmation?%s" % string_query_params)
(
"https://stage-testserver/email-verification-confirmation?%s"
% string_query_params
),
)

def _get_email_data(self, include_redirect_url=False):
Expand Down Expand Up @@ -215,12 +218,25 @@ def setUp(self):

self.custom_request = RequestFactory().get("/path", data={"name": "test"})

@override_settings(PROJECT_INVITATION_URL="https://example.com/register")
@override_settings(PROJECT_INVITATION_URL={"*": "https://example.com/register"})
def test_url_configured(self):
"""settings.PROJECT_INVITATION_URL is set"""
url = get_project_invitation_url(self.custom_request)
self.assertEqual(url, "https://example.com/register")

@override_settings(
PROJECT_INVITATION_URL={
"*": "https://example.com/register",
"new-domain.com": "https://new-domain.com/register",
}
)
@override_settings(ALLOWED_HOSTS=["*"])
def test_url_configured(self):
"""settings.PROJECT_INVITATION_URL is set"""
self.custom_request.META["HTTP_HOST"] = "new-domain.com"
url = get_project_invitation_url(self.custom_request)
self.assertEqual(url, "https://new-domain.com/register")

def test_url_not_configured(self):
"""settings.PROJECT_INVITATION_URL not set"""
url = get_project_invitation_url(self.custom_request)
Expand Down
7 changes: 6 additions & 1 deletion onadata/libs/utils/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ def send_generic_email(email, message_txt, subject):

def get_project_invitation_url(request: HttpRequest):
"""Get project invitation url"""
url: str = getattr(settings, "PROJECT_INVITATION_URL", "")
invitation_url_setting: dict = getattr(settings, "PROJECT_INVITATION_URL", {})

site_domain = request.get_host()
url = (
site_domain in invitation_url_setting and invitation_url_setting[site_domain]
) or ("*" in invitation_url_setting and invitation_url_setting["*"])

if not url:
url = reverse("userprofile-list", request=request)
Expand Down

0 comments on commit 3ae3e5c

Please sign in to comment.