Skip to content

Commit

Permalink
Add tests for bulk actions
Browse files Browse the repository at this point in the history
  • Loading branch information
MizukiTemma committed Dec 9, 2024
1 parent f0c4481 commit 4a828b2
Show file tree
Hide file tree
Showing 2 changed files with 213 additions and 29 deletions.
6 changes: 1 addition & 5 deletions tests/cms/models/contacts/test_contacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def test_contact_string(
@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

Expand All @@ -52,7 +51,6 @@ def test_copying_contact_works(
@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

Expand All @@ -65,7 +63,6 @@ def test_deleting_contact_works(
@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

Expand All @@ -80,7 +77,6 @@ def test_archiving_contact_works(
@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

Expand All @@ -89,4 +85,4 @@ def test_restoring_contact_works(
contact.restore()

assert Contact.objects.all().count() == 4
assert contact.archived is False
assert contact.archived is False
236 changes: 212 additions & 24 deletions tests/cms/views/contacts/test_contact_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
REGION_SLUG = "augsburg"

NOT_USED_CONTACT_ID = 3
ARCHIVED_CONTACT_ID = 2


test_archive_parameters = [(NOT_USED_CONTACT_ID, True)]

test_archive_parameters = [
(NOT_USED_CONTACT_ID, True)
]

@pytest.mark.django_db
@pytest.mark.parametrize("parameter", test_archive_parameters)
Expand Down Expand Up @@ -52,7 +51,7 @@ def test_archive_contact(
kwargs={
"contact_id": contact_id,
"region_slug": REGION_SLUG,
}
},
)
response = client.post(archive_contact)

Expand All @@ -73,10 +72,9 @@ def test_archive_contact(
"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 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:
Expand All @@ -89,9 +87,8 @@ def test_archive_contact(
assert response.status_code == 403


test_delete_parameters = [
(NOT_USED_CONTACT_ID, True)
]
test_delete_parameters = [(NOT_USED_CONTACT_ID, True)]


@pytest.mark.django_db
@pytest.mark.parametrize("parameter", test_delete_parameters)
Expand Down Expand Up @@ -119,7 +116,7 @@ def test_delete_contact(
kwargs={
"contact_id": contact_id,
"region_slug": REGION_SLUG,
}
},
)
response = client.post(delete_contact)

Expand All @@ -140,10 +137,9 @@ def test_delete_contact(
"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 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:
Expand Down Expand Up @@ -171,18 +167,15 @@ def test_restore_contact(
# 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
archived_contact = Contact.objects.filter(id=ARCHIVED_CONTACT_ID).first()
assert archived_contact.archived

contact_string = str(archived_contact)
archived_contact_id = archived_contact.id

restore_contact = reverse(
"restore_contact",
kwargs={
"contact_id": archived_contact_id,
"contact_id": ARCHIVED_CONTACT_ID,
"region_slug": REGION_SLUG,
},
)
Expand All @@ -198,17 +191,212 @@ def test_restore_contact(
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


BULK_ARCHIVE_SELECTED_IDS = [NOT_USED_CONTACT_ID]


@pytest.mark.django_db
def test_bulk_archive_contacts(
load_test_data: None,
login_role_user: tuple[Client, str],
settings: SettingsWrapper,
caplog: LogCaptureFixture,
) -> None:
"""
Test whether bulk archiving of contacts 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"

not_used_contact_string = str(
Contact.objects.filter(id=NOT_USED_CONTACT_ID).first()
)

bulk_archive_contacts = reverse(
"bulk_archive_contacts",
kwargs={
"region_slug": REGION_SLUG,
},
)
response = client.post(
bulk_archive_contacts,
data={"selected_ids[]": BULK_ARCHIVE_SELECTED_IDS},
)

if role in HIGH_PRIV_STAFF_ROLES:
response.status_code == 302
redirect_url = response.headers.get("location")
redirect_page = client.get(redirect_url).content.decode("utf-8")

"""
assert_message_in_log(
"ERROR could not be archived",
caplog,
)
assert (
" could not be archived"
in redirect_page
)
assert (
not Contact.objects.filter(id=archived_contact_id)
not Contact.objects.filter(id=USED_CONTACT_ID)
.first()
.archived
)
"""
assert_message_in_log(
f'SUCCESS Contact "{not_used_contact_string}" was successfully archived.',
caplog,
)
assert (
f"Contact "{not_used_contact_string}" was successfully archived."
in redirect_page
)
assert Contact.objects.filter(id=NOT_USED_CONTACT_ID).first().archived
elif role == ANONYMOUS:
assert response.status_code == 302
assert (
response.headers.get("location")
== f"{settings.LOGIN_URL}?next={bulk_archive_contacts}"
)
else:
assert response.status_code == 403


BULK_DELETE_SELECTED_IDS = [NOT_USED_CONTACT_ID]


@pytest.mark.django_db
def test_bulk_delete_contacts(
load_test_data: None,
login_role_user: tuple[Client, str],
settings: SettingsWrapper,
caplog: LogCaptureFixture,
) -> None:
"""
Test whether bulk deleting of contacts 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"

not_used_contact_string = str(
Contact.objects.filter(id=NOT_USED_CONTACT_ID).first()
)

bulk_delete_contacts = reverse(
"bulk_delete_contacts",
kwargs={
"region_slug": REGION_SLUG,
},
)
response = client.post(
bulk_delete_contacts,
data={"selected_ids[]": BULK_DELETE_SELECTED_IDS},
)

if role in HIGH_PRIV_STAFF_ROLES:
response.status_code == 302
redirect_url = response.headers.get("location")
redirect_page = client.get(redirect_url).content.decode("utf-8")

"""
assert_message_in_log(
"ERROR could not be archived",
caplog,
)
assert (
" could not be archived"
in redirect_page
)
assert (
not Contact.objects.filter(id=USED_CONTACT_ID)
.first()
.archived
)
"""
assert_message_in_log(
f'SUCCESS Contact "{not_used_contact_string}" was successfully deleted.',
caplog,
)
assert (
f"Contact "{not_used_contact_string}" was successfully deleted."
in redirect_page
)
assert not Contact.objects.filter(id=NOT_USED_CONTACT_ID).first()
elif role == ANONYMOUS:
assert response.status_code == 302
assert (
response.headers.get("location")
== f"{settings.LOGIN_URL}?next={restore_contact}"
== f"{settings.LOGIN_URL}?next={bulk_delete_contacts}"
)
else:
assert response.status_code == 403


BULK_RESTORE_SELECTED_IDS = [ARCHIVED_CONTACT_ID]


@pytest.mark.django_db
def test_bulk_restore_contacts(
load_test_data: None,
login_role_user: tuple[Client, str],
settings: SettingsWrapper,
caplog: LogCaptureFixture,
) -> None:
"""
Test whether bulk restoring of contacts 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"

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

bulk_restore_contacts = reverse(
"bulk_restore_contacts",
kwargs={
"region_slug": REGION_SLUG,
},
)
response = client.post(
bulk_restore_contacts,
data={"selected_ids[]": BULK_RESTORE_SELECTED_IDS},
)

if role in HIGH_PRIV_STAFF_ROLES:
response.status_code == 302
redirect_url = response.headers.get("location")
redirect_page = client.get(redirect_url).content.decode("utf-8")

assert_message_in_log(
f'SUCCESS Contact "{contact_string}" was successfully restored.',
caplog,
)
assert (
f"Contact "{contact_string}" was successfully restored."
in redirect_page
)
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={bulk_restore_contacts}"
)
else:
assert response.status_code == 403

0 comments on commit 4a828b2

Please sign in to comment.