Skip to content

Commit

Permalink
Add tests for contact actions
Browse files Browse the repository at this point in the history
  • Loading branch information
MizukiTemma committed Dec 9, 2024
1 parent c391eaa commit f0c4481
Show file tree
Hide file tree
Showing 2 changed files with 246 additions and 36 deletions.
56 changes: 56 additions & 0 deletions tests/cms/models/contacts/test_contacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,59 @@ def test_contact_string(
str(contact_4)
== "Draft location with email: generalcontactinformation@example.com"
)


@pytest.mark.django_db
def test_copying_contact_works(
load_test_data: None,
login_role_user: tuple[Client, str],
) -> None:
assert Contact.objects.all().count() == 4

contact = Contact.objects.get(id=1)
contact.copy()

assert Contact.objects.all().count() == 5


@pytest.mark.django_db
def test_deleting_contact_works(
load_test_data: None,
login_role_user: tuple[Client, str],
) -> None:
assert Contact.objects.all().count() == 4

contact = Contact.objects.get(id=1)
contact.delete()

assert Contact.objects.all().count() == 3


@pytest.mark.django_db
def test_archiving_contact_works(
load_test_data: None,
login_role_user: tuple[Client, str],
) -> None:
assert Contact.objects.all().count() == 4

contact = Contact.objects.get(id=1)
assert contact.archived is False
contact.archive()

assert Contact.objects.all().count() == 4
assert contact.archived is True


@pytest.mark.django_db
def test_restoring_contact_works(
load_test_data: None,
login_role_user: tuple[Client, str],
) -> None:
assert Contact.objects.all().count() == 4

contact = Contact.objects.get(id=2)
assert contact.archived is True
contact.restore()

assert Contact.objects.all().count() == 4
assert contact.archived is False
226 changes: 190 additions & 36 deletions tests/cms/views/contacts/test_contact_actions.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,214 @@
from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from _pytest.logging import LogCaptureFixture
from django.test.client import Client
from pytest_django.fixtures import SettingsWrapper

import pytest
from django.test.client import Client
from django.urls import reverse

from integreat_cms.cms.models.contact.contact import Contact
from integreat_cms.cms.models import Contact
from tests.conftest import ANONYMOUS, HIGH_PRIV_STAFF_ROLES
from tests.utils import assert_message_in_log

# Use the region Augsburg, as it has some contacts in the test data
REGION_SLUG = "augsburg"

@pytest.mark.django_db
def test_copying_contact_works(
load_test_data: None,
login_role_user: tuple[Client, str],
) -> None:
assert Contact.objects.all().count() == 4
NOT_USED_CONTACT_ID = 3

contact = Contact.objects.get(id=1)
contact.copy()

assert Contact.objects.all().count() == 5

test_archive_parameters = [
(NOT_USED_CONTACT_ID, True)
]

@pytest.mark.django_db
def test_deleting_contact_works(
@pytest.mark.parametrize("parameter", test_archive_parameters)
@pytest.mark.django_db
def test_archive_contact(
load_test_data: None,
login_role_user: tuple[Client, str],
settings: SettingsWrapper,
caplog: LogCaptureFixture,
parameter: tuple[int, bool],
) -> None:
assert Contact.objects.all().count() == 4

contact = Contact.objects.get(id=1)
contact.delete()

assert Contact.objects.all().count() == 3

"""
Test that archiving a contact works as expected
"""
client, role = login_role_user
contact_id, should_be_archived = parameter

# Set the language setting to English so assertion does not fail because of corresponding German sentence appearing instead the english one.
settings.LANGUAGE_CODE = "en"

contact_string = str(Contact.objects.filter(id=contact_id).first())

archive_contact = reverse(
"archive_contact",
kwargs={
"contact_id": contact_id,
"region_slug": REGION_SLUG,
}
)
response = client.post(archive_contact)

if role in HIGH_PRIV_STAFF_ROLES:
assert response.status_code == 302
redirect_url = response.headers.get("location")
if should_be_archived:
assert_message_in_log(
f"SUCCESS Contact {contact_string} was successfully archived",
caplog,
)
assert f"Contact {contact_string} was successfully archived" in client.get(
redirect_url
).content.decode("utf-8")
assert Contact.objects.filter(id=contact_id).first().archived
else:
assert_message_in_log(
"ERROR Contact couldn't be archived as it's used in a content",
caplog,
)
assert "Contact couldn't be archived as it's used in a content" in client.get(
redirect_url
).content.decode(
"utf-8"
)
assert not Contact.objects.filter(id=contact_id).first().archived
elif role == ANONYMOUS:
assert response.status_code == 302
assert (
response.headers.get("location")
== f"{settings.LOGIN_URL}?next={archive_contact}"
)
else:
assert response.status_code == 403


test_delete_parameters = [
(NOT_USED_CONTACT_ID, True)
]

@pytest.mark.django_db
def test_archiving_contact_works(
@pytest.mark.parametrize("parameter", test_delete_parameters)
@pytest.mark.django_db
def test_delete_contact(
load_test_data: None,
login_role_user: tuple[Client, str],
settings: SettingsWrapper,
caplog: LogCaptureFixture,
parameter: tuple[int, bool],
) -> None:
assert Contact.objects.all().count() == 4

contact = Contact.objects.get(id=1)
assert contact.archived is False
contact.archive()

assert Contact.objects.all().count() == 4
assert contact.archived is True
"""
Test that deleting a contact works as expected
"""
client, role = login_role_user
contact_id, should_be_deleted = parameter

# Set the language setting to English so assertion does not fail because of corresponding German sentence appearing instead the english one.
settings.LANGUAGE_CODE = "en"

contact_string = str(Contact.objects.filter(id=contact_id).first())

delete_contact = reverse(
"delete_contact",
kwargs={
"contact_id": contact_id,
"region_slug": REGION_SLUG,
}
)
response = client.post(delete_contact)

if role in HIGH_PRIV_STAFF_ROLES:
assert response.status_code == 302
redirect_url = response.headers.get("location")
if should_be_deleted:
assert_message_in_log(
f"SUCCESS Contact {contact_string} was successfully deleted",
caplog,
)
assert f"Contact {contact_string} was successfully deleted" in client.get(
redirect_url
).content.decode("utf-8")
assert not Contact.objects.filter(id=contact_id).first()
else:
assert_message_in_log(
"ERROR Contact couldn't be archived as it's used in a content",
caplog,
)
assert "Contact couldn't be archived as it's used in a content" in client.get(
redirect_url
).content.decode(
"utf-8"
)
assert Contact.objects.filter(id=contact_id).first()
elif role == ANONYMOUS:
assert response.status_code == 302
assert (
response.headers.get("location")
== f"{settings.LOGIN_URL}?next={delete_contact}"
)
else:
assert response.status_code == 403


@pytest.mark.django_db
def test_restoring_contact_works(
def test_restore_contact(
load_test_data: None,
login_role_user: tuple[Client, str],
settings: SettingsWrapper,
caplog: LogCaptureFixture,
) -> None:
assert Contact.objects.all().count() == 4

contact = Contact.objects.get(id=2)
assert contact.archived is True
contact.restore()

assert Contact.objects.all().count() == 4
assert contact.archived is False
"""
Test whether restoring a contact works as expected
"""
client, role = login_role_user

# Set the language setting to English so assertion does not fail because of corresponding German sentence appearing instead the english one.
settings.LANGUAGE_CODE = "en"

archived_contact = Contact.objects.filter(
location__region__slug=REGION_SLUG, archived=True
).first()
assert archived_contact

contact_string = str(archived_contact)
archived_contact_id = archived_contact.id

restore_contact = reverse(
"restore_contact",
kwargs={
"contact_id": archived_contact_id,
"region_slug": REGION_SLUG,
},
)
response = client.post(restore_contact)

if role in HIGH_PRIV_STAFF_ROLES:
response.status_code == 302
redirect_url = response.headers.get("location")
assert_message_in_log(
f"SUCCESS Contact {contact_string} was successfully restored",
caplog,
)
assert f"Contact {contact_string} was successfully restored" in client.get(
redirect_url
).content.decode("utf-8")
assert (
not Contact.objects.filter(id=archived_contact_id)
.first()
.archived
)

elif role == ANONYMOUS:
assert response.status_code == 302
assert (
response.headers.get("location")
== f"{settings.LOGIN_URL}?next={restore_contact}"
)
else:
assert response.status_code == 403

0 comments on commit f0c4481

Please sign in to comment.