-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨(backend) domain accesses delete API
- Loading branch information
Showing
4 changed files
with
207 additions
and
2 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
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
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
151 changes: 151 additions & 0 deletions
151
...nd/mailbox_manager/tests/api/mail_domain_accesses/test_api_mail_domain_accesses_delete.py
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,151 @@ | ||
""" | ||
Test for mail_domain accesses API endpoints in People's core app : delete | ||
""" | ||
|
||
import random | ||
|
||
import pytest | ||
from rest_framework import status | ||
from rest_framework.test import APIClient | ||
|
||
from core import factories as core_factories | ||
|
||
from mailbox_manager import enums, factories, models | ||
|
||
pytestmark = pytest.mark.django_db | ||
|
||
|
||
def test_api_mail_domain__accesses_delete_anonymous(): | ||
"""Anonymous users should not be allowed to destroy a mail domain access.""" | ||
access = factories.MailDomainAccessFactory() | ||
|
||
response = APIClient().delete( | ||
f"/api/v1.0/mail-domains/{access.domain.slug}/accesses/{access.id!s}/", | ||
) | ||
|
||
assert response.status_code == status.HTTP_401_UNAUTHORIZED | ||
assert models.MailDomainAccess.objects.count() == 1 | ||
|
||
|
||
def test_api_mail_domain__accesses_delete_authenticated(): | ||
""" | ||
Authenticated users should not be allowed to delete a mail domain access for a | ||
mail domain to which they are not related. | ||
""" | ||
authenticated_user = core_factories.UserFactory() | ||
access = factories.MailDomainAccessFactory() | ||
|
||
client = APIClient() | ||
client.force_login(authenticated_user) | ||
response = client.delete( | ||
f"/api/v1.0/mail-domains/{access.domain.slug}/accesses/{access.id!s}/", | ||
) | ||
|
||
assert response.status_code == status.HTTP_403_FORBIDDEN | ||
assert models.MailDomainAccess.objects.count() == 1 | ||
|
||
|
||
def test_api_mail_domain__accesses_delete_viewer(): | ||
""" | ||
Authenticated users should not be allowed to delete a mail domain access for a | ||
mail domain in which they are a simple viewer. | ||
""" | ||
authenticated_user = core_factories.UserFactory() | ||
mail_domain = factories.MailDomainFactory( | ||
users=[(authenticated_user, enums.MailDomainRoleChoices.VIEWER)] | ||
) | ||
access = factories.MailDomainAccessFactory(domain=mail_domain) | ||
|
||
assert models.MailDomainAccess.objects.count() == 2 | ||
assert models.MailDomainAccess.objects.filter(user=access.user).exists() | ||
|
||
client = APIClient() | ||
client.force_login(authenticated_user) | ||
response = client.delete( | ||
f"/api/v1.0/mail-domains/{mail_domain.slug}/accesses/{access.id!s}/", | ||
) | ||
|
||
assert response.status_code == status.HTTP_403_FORBIDDEN | ||
assert models.MailDomainAccess.objects.count() == 2 | ||
|
||
|
||
def test_api_mail_domain__accesses_delete_administrators(): | ||
""" | ||
Users who are administrators in a mail_domain should be allowed to delete an access | ||
from the mail_domain provided it is not ownership. | ||
""" | ||
authenticated_user = core_factories.UserFactory() | ||
mail_domain = factories.MailDomainFactory( | ||
users=[(authenticated_user, enums.MailDomainRoleChoices.ADMIN)] | ||
) | ||
access = factories.MailDomainAccessFactory( | ||
domain=mail_domain, | ||
role=random.choice( | ||
[enums.MailDomainRoleChoices.VIEWER, enums.MailDomainRoleChoices.ADMIN] | ||
), | ||
) | ||
|
||
assert models.MailDomainAccess.objects.count() == 2 | ||
assert models.MailDomainAccess.objects.filter(user=access.user).exists() | ||
|
||
client = APIClient() | ||
client.force_login(authenticated_user) | ||
response = client.delete( | ||
f"/api/v1.0/mail-domains/{mail_domain.slug}/accesses/{access.id!s}/", | ||
) | ||
|
||
assert response.status_code == status.HTTP_204_NO_CONTENT | ||
assert models.MailDomainAccess.objects.count() == 1 | ||
|
||
|
||
def test_api_mail_domain__accesses_delete_owners_except_owners(): | ||
""" | ||
Users should be able to delete the mail_domain access of another user | ||
for a mail_domain of which they are owner provided it is not an owner access. | ||
""" | ||
authenticated_user = core_factories.UserFactory() | ||
mail_domain = factories.MailDomainFactory( | ||
users=[(authenticated_user, enums.MailDomainRoleChoices.OWNER)] | ||
) | ||
access = factories.MailDomainAccessFactory( | ||
domain=mail_domain, | ||
role=random.choice( | ||
[enums.MailDomainRoleChoices.VIEWER, enums.MailDomainRoleChoices.ADMIN] | ||
), | ||
) | ||
|
||
assert models.MailDomainAccess.objects.count() == 2 | ||
assert models.MailDomainAccess.objects.filter(user=access.user).exists() | ||
|
||
client = APIClient() | ||
client.force_login(authenticated_user) | ||
response = client.delete( | ||
f"/api/v1.0/mail-domains/{mail_domain.slug}/accesses/{access.id!s}/", | ||
) | ||
|
||
assert response.status_code == status.HTTP_204_NO_CONTENT | ||
assert models.MailDomainAccess.objects.count() == 1 | ||
|
||
|
||
def test_api_mail_domain__accesses_delete_owners_last_owner(): | ||
""" | ||
It should not be possible to delete the last owner access from a mail_domain | ||
""" | ||
authenticated_user = core_factories.UserFactory() | ||
mail_domain = factories.MailDomainFactory() | ||
access = factories.MailDomainAccessFactory( | ||
domain=mail_domain, | ||
user=authenticated_user, | ||
role=enums.MailDomainRoleChoices.OWNER, | ||
) | ||
assert models.MailDomainAccess.objects.count() == 1 | ||
|
||
client = APIClient() | ||
client.force_login(authenticated_user) | ||
|
||
response = client.delete( | ||
f"/api/v1.0/mail-domains/{mail_domain.slug}/accesses/{access.id!s}/", | ||
) | ||
|
||
assert response.status_code == status.HTTP_403_FORBIDDEN | ||
assert models.MailDomainAccess.objects.count() == 1 |