-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
141 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters