Skip to content

Commit

Permalink
Add admin_dsar testing
Browse files Browse the repository at this point in the history
  • Loading branch information
robhudson committed Dec 30, 2024
1 parent f07a650 commit f8533b7
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 4 deletions.
34 changes: 34 additions & 0 deletions basket/base/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from django.core.exceptions import ValidationError

import pytest

from basket.base.forms import EmailListField


class TestEmailListField:
def setup_method(self):
self.field = EmailListField()

def test_to_python_empty(self):
assert self.field.to_python("") == []

def test_to_python_single_email(self):
assert self.field.to_python("[email protected]") == ["[email protected]"]

def test_to_python_multiple_emails(self):
value = "[email protected]\n[email protected]\n"
assert self.field.to_python(value) == ["[email protected]", "[email protected]"]

def test_to_python_with_whitespace(self):
value = " [email protected] \n [email protected] \n"
assert self.field.to_python(value) == ["[email protected]", "[email protected]"]

def test_to_python_with_empty(self):
value = " [email protected] \n\n [email protected] \n"
assert self.field.to_python(value) == ["[email protected]", "[email protected]"]

def test_validate_invalid_emails(self):
value = ["[email protected]", "invalid-email"]
with pytest.raises(ValidationError) as excinfo:
self.field.validate(value)
assert "Invalid email: invalid-email" in str(excinfo.value)
101 changes: 101 additions & 0 deletions basket/base/tests/test_view_admin_dsar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
from unittest.mock import patch

from django.conf import settings
from django.contrib.auth.models import Permission, User
from django.test import Client
from django.urls import reverse

import pytest

from basket.base.forms import EmailListForm
from basket.news.backends.ctms import CTMSNotFoundByEmailError


@pytest.mark.django_db
class TestAdminDSARView:
def setup_method(self, method):
self.client = Client()
self.url = reverse("admin.dsar")

def _create_admin_user(self, with_perm=True):
user = User.objects.create_user(username="admin", password="password")
user.is_staff = True
if with_perm:
user.user_permissions.add(Permission.objects.get(codename="dsar_access"))
user.save()
return user

def _login_admin_user(self):
self.client.login(username="admin", password="password")

def test_get_requires_login(self):
self._create_admin_user()
response = self.client.get(self.url)
assert response.status_code == 302
assert response.url.startswith(settings.LOGIN_URL)

def test_get_requires_perm(self):
self._create_admin_user(with_perm=False)
self._login_admin_user()
response = self.client.get(self.url)
assert response.status_code == 302
assert response.url.startswith(settings.LOGIN_URL)

def test_get(self):
self._create_admin_user()
self._login_admin_user()
response = self.client.get(self.url)
assert response.status_code == 200
assert isinstance(response.context["form"], EmailListForm)
assert response.context["output"] is None

def test_post_valid_emails(self):
self._create_admin_user()
self._login_admin_user()
with patch("basket.base.views.ctms", spec_set=["delete"]) as mock_ctms:
mock_ctms.delete.side_effect = [
[{"email_id": "123", "fxa_id": "", "mofo_contact_id": ""}],
[{"email_id": "456", "fxa_id": "string", "mofo_contact_id": ""}],
[{"email_id": "789", "fxa_id": "string", "mofo_contact_id": "string"}],
]
response = self.client.post(self.url, {"emails": "[email protected]\n[email protected]\n[email protected]"}, follow=True)

assert response.status_code == 200
assert mock_ctms.delete.call_count == 3
assert "DELETED [email protected] (ctms id: 123)." in response.context["output"]
assert "DELETED [email protected] (ctms id: 456). fxa: YES." in response.context["output"]
assert "DELETED [email protected] (ctms id: 789). fxa: YES. mofo: YES." in response.context["output"]

def test_post_valid_email(self):
self._create_admin_user()
self._login_admin_user()
with patch("basket.base.views.ctms", spec_set=["delete"]) as mock_ctms:
mock_ctms.delete.return_value = [{"email_id": "123", "fxa_id": "", "mofo_contact_id": ""}]
response = self.client.post(self.url, {"emails": "[email protected]"}, follow=True)

assert response.status_code == 200
assert mock_ctms.delete.called
assert "DELETED [email protected] (ctms id: 123)." in response.context["output"]

def test_post_unknown_ctms_user(self, mocker):
self._create_admin_user()
self._login_admin_user()
with patch("basket.base.views.ctms", spec_set=["delete"]) as mock_ctms:
mock_ctms.delete.side_effect = CTMSNotFoundByEmailError("[email protected]")
response = self.client.post(self.url, {"emails": "[email protected]"}, follow=True)

assert response.status_code == 200
assert mock_ctms.delete.called
assert "[email protected] not found in CTMS" in response.context["output"]

def test_post_invalid_email(self, mocker):
self._create_admin_user()
self._login_admin_user()
with patch("basket.base.views.ctms", spec_set=["delete"]) as mock_ctms:
mock_ctms.delete.side_effect = CTMSNotFoundByEmailError
response = self.client.post(self.url, {"emails": "invalid@email"}, follow=True)

assert response.status_code == 200
assert not mock_ctms.delete.called
assert response.context["output"] is None
assert response.context["form"].errors == {"emails": ["Invalid email: invalid@email"]}
10 changes: 6 additions & 4 deletions basket/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ def path(*args):
"BACKEND": "django.core.files.storage.FileSystemStorage",
},
"staticfiles": {
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage"
if (DEBUG or UNITTEST)
else "whitenoise.storage.CompressedManifestStaticFilesStorage",
},
}
if DEBUG:
STORAGES["staticfiles"]["BACKEND"] = "django.contrib.staticfiles.storage.StaticFilesStorage"

try:
# Make this unique, and don't share it with anybody.
Expand Down Expand Up @@ -448,6 +448,9 @@ def before_send(event, hint):

COMMON_VOICE_NEWSLETTER = config("COMMON_VOICE_NEWSLETTER", default="common-voice")

LOGIN_URL = "/admin/"
LOGIN_REDIRECT_URL = "/admin/"

OIDC_ENABLE = config("OIDC_ENABLE", parser=bool, default="false")
if OIDC_ENABLE:
AUTHENTICATION_BACKENDS = ("basket.base.authentication.OIDCModelBackend",)
Expand All @@ -458,7 +461,6 @@ def before_send(event, hint):
OIDC_RP_CLIENT_ID = config("OIDC_RP_CLIENT_ID", default="")
OIDC_RP_CLIENT_SECRET = config("OIDC_RP_CLIENT_SECRET", default="")
OIDC_CREATE_USER = config("OIDC_CREATE_USER", parser=bool, default="false")
LOGIN_REDIRECT_URL = "/admin/"
OIDC_EXEMPT_URLS = [
"/",
"/fxa/",
Expand Down

0 comments on commit f8533b7

Please sign in to comment.