Skip to content

Commit

Permalink
Test for region deletion
Browse files Browse the repository at this point in the history
Co-authored-by: MizukiTemma <82345046+MizukiTemma@users.noreply.github.com>
  • Loading branch information
JoeyStk and MizukiTemma committed Dec 16, 2024
1 parent f394887 commit 711175d
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 2 deletions.
Binary file added celerybeat-schedule.db
Binary file not shown.
44 changes: 44 additions & 0 deletions integreat_cms/cms/fixtures/test_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -3427,6 +3427,29 @@
"embedded_offers": []
}
},
{
"model": "cms.page",
"pk": 30,
"fields": {
"created_date": "2024-11-04T15:19:48.247Z",
"lft": 1,
"rgt": 2,
"tree_id": 10,
"depth": 1,
"parent": null,
"region": 2,
"explicitly_archived": false,
"icon": null,
"mirrored_page": 6,
"mirrored_page_first": false,
"organization": null,
"api_token": "",
"hix_ignore": false,
"authors": [],
"editors": [],
"embedded_offers": []
}
},
{ "model": "cms.regionfeedback", "pk": 1, "fields": { "feedback_ptr": 1 } },
{ "model": "cms.regionfeedback", "pk": 4, "fields": { "feedback_ptr": 4 } },
{ "model": "cms.regionfeedback", "pk": 7, "fields": { "feedback_ptr": 7 } },
Expand Down Expand Up @@ -5513,6 +5536,27 @@
"hix_feedback": null
}
},
{
"model": "cms.pagetranslation",
"pk": 100,
"fields": {
"title": "Gespiegelte Seite von Augsburg",
"slug": "gespiegelte-seite",
"status": "PUBLIC",
"content": "<p>Gespiegelte Seite von Augsburg</p>",
"language": 1,
"currently_in_translation": false,
"machine_translated": false,
"version": 1,
"minor_edit": false,
"last_updated": "2024-11-04T07:57:42.456Z",
"creator": ["root"],
"automatic_translation": false,
"page": 30,
"hix_score": null,
"hix_feedback": null
}
},
{
"model": "cms.imprintpagetranslation",
"pk": 1,
Expand Down
122 changes: 122 additions & 0 deletions tests/cms/views/regions/delete_region.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import pytest
from django.conf import settings
from django.contrib.auth import get_user_model
from django.test.client import Client
from django.urls import reverse

from integreat_cms.cms.models.feedback.page_feedback import PageFeedback
from integreat_cms.cms.models.feedback.poi_feedback import POIFeedback
from integreat_cms.cms.models.media.media_file import MediaFile
from integreat_cms.cms.models.pages.page import Page
from integreat_cms.cms.models.push_notifications.push_notification import (
PushNotification,
)
from integreat_cms.cms.models.regions.region import Region
from tests.conftest import ANONYMOUS, CMS_TEAM, ROOT, SERVICE_TEAM

CLONED_PAGE = 30
NESTED_MEDIA_OBJECT_ID = 1
POI_FEEDBACK_AUGSBURG_ID = 6
PAGE_FEEDBACK_AUGSBURG_ID = 2


@pytest.mark.django_db
def test_delete_all_regions_is_successful(
load_test_data: None,
login_role_user: tuple[Client, str],
) -> None:
current_number_of_regions = Region.objects.count()
assert current_number_of_regions > 0

client, role = login_role_user

# We want to test regions can be deleted even if they have feedback and/or nested media object
# See #2462 for details: IntegrityError when there are 2 feedback with different endpoint
assert POIFeedback.objects.filter(id=POI_FEEDBACK_AUGSBURG_ID).first()
assert PageFeedback.objects.filter(id=PAGE_FEEDBACK_AUGSBURG_ID).first()
# See 1749 for details: ProtectedError when a subfolder in a media library has contents
nested_media_object = MediaFile.objects.filter(id=NESTED_MEDIA_OBJECT_ID).first()
assert nested_media_object
assert nested_media_object.parent_directory.id

# Users that will be deleted after all the regions are deleted
assert (
get_user_model().objects.filter(is_superuser=False, is_staff=False).count() > 0
)

# Deletion of Augsburg will not be possible due to a cloned page in Nurnberg.
# We want to test this is in the test below. In this test we want to make sure this is the only reason why deleting is not possible.
cloned_page = Page.objects.filter(id=CLONED_PAGE)
cloned_page.delete()

# We want to test with existing data, as we want to make sure as many dependencies as possible are covered
for region in Region.objects.all():
delete_region = reverse("delete_region", kwargs={"slug": region.slug})
response = client.post(delete_region, data={})

if role == ANONYMOUS:
assert response.status_code == 302
assert (
response.headers.get("location")
== f"{settings.LOGIN_URL}?next={delete_region}"
)
return

if role not in [CMS_TEAM, SERVICE_TEAM, ROOT] and not ANONYMOUS:
assert response.status_code == 403
continue

if role in [CMS_TEAM, SERVICE_TEAM, ROOT]:
assert response.status_code == 302
redirect = response.headers.get("location")
response = client.get(redirect)
assert "Region wurde erfolgreich gelöscht" in response.content.decode(
"utf-8"
)

if role in [CMS_TEAM, SERVICE_TEAM, ROOT]:
assert Region.objects.count() == 0
# No users without region
assert (
get_user_model()
.objects.filter(is_superuser=False, is_staff=False, regions=None)
.count()
== 0
)
# No push notification without region
assert PushNotification.objects.filter(regions=None).count() == 0


@pytest.mark.django_db
def test_deleting_mirrored_region_is_unsucessful(
load_test_data: None,
login_role_user: tuple[Client, str],
) -> None:
client, role = login_role_user

augsburg = Region.objects.get(slug="augsburg")
cloned_page = Page.objects.filter(id=CLONED_PAGE)
assert cloned_page.exists()

delete_region = reverse("delete_region", kwargs={"slug": augsburg.slug})
response = client.post(delete_region, data={})

if role == ANONYMOUS:
assert response.status_code == 302
assert (
response.headers.get("location")
== f"{settings.LOGIN_URL}?next={delete_region}"
)

if role not in [CMS_TEAM, SERVICE_TEAM, ROOT] and not ANONYMOUS:
assert response.status_code == 403

if role in [CMS_TEAM, SERVICE_TEAM, ROOT]:
assert response.status_code == 302
redirect = response.headers.get("location")
response = client.get(redirect)
assert (
"Die Region konnte nicht gelöscht werden, weil die folgenden Seiten in anderen Region gespiegelt werden:"
in response.content.decode("utf-8")
)
assert Region.objects.filter(slug="augsburg").exists()
2 changes: 1 addition & 1 deletion tests/sitemap/expected-sitemaps/sitemap-nurnberg-de.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url><loc>https://integreat.app/nurnberg/de/willkommen/</loc><lastmod>2019-08-12</lastmod><changefreq>monthly</changefreq><priority>1.0</priority><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/en/welcome/"/><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/ar/الترحيب/"/><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/fa/خوش-آمدید/"/></url><url><loc>https://integreat.app/nurnberg/de/willkommen/willkommen-in-nurnberg/</loc><lastmod>2019-08-12</lastmod><changefreq>monthly</changefreq><priority>1.0</priority><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/ar/الترحيب/مرحبا-بكم-في-نورنبيرغ/"/></url><url><loc>https://integreat.app/nurnberg/de/willkommen/zusammenleben-in-deutschland/</loc><lastmod>2019-08-12</lastmod><changefreq>monthly</changefreq><priority>1.0</priority><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/en/welcome/community-life-in-germany/"/></url><url><loc>https://integreat.app/nurnberg/de/willkommen/wissenswertes-uber-nurnberg/</loc><lastmod>2019-08-12</lastmod><changefreq>monthly</changefreq><priority>1.0</priority><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/en/welcome/trivia-about-nuremberg/"/><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/ar/الترحيب/معلومات-قيمة-عن-نورنبيرغ/"/></url><url><loc>https://integreat.app/nurnberg/de/willkommen/stadtplan/</loc><lastmod>2019-08-12</lastmod><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://integreat.app/nurnberg/de/willkommen/uber-integreat/</loc><lastmod>2019-08-12</lastmod><changefreq>monthly</changefreq><priority>1.0</priority><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/en/welcome/about-integreat/"/><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/fa/خوش-آمدید/درباره-integreat/"/></url><url><loc>https://integreat.app/nurnberg/de/willkommen/aufenthaltstitel/</loc><lastmod>2019-08-12</lastmod><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://integreat.app/nurnberg/de/test_links/</loc><lastmod>2022-12-30</lastmod><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://integreat.app/nurnberg/de/locations/test-ort/</loc><lastmod>2020-01-22</lastmod><changefreq>monthly</changefreq><priority>0.5</priority><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/en/locations/test-location/"/></url><url><loc>https://integreat.app/nurnberg/de/offers/ihk-lehrstellenboerse</loc><lastmod>2019-10-09</lastmod><changefreq>monthly</changefreq><priority>1.0</priority><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/en/offers/ihk-lehrstellenboerse"/><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/ar/offers/ihk-lehrstellenboerse"/><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/fa/offers/ihk-lehrstellenboerse"/></url><url><loc>https://integreat.app/nurnberg/de/offers/ihk-praktikumsboerse</loc><lastmod>2019-10-09</lastmod><changefreq>monthly</changefreq><priority>1.0</priority><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/en/offers/ihk-praktikumsboerse"/><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/ar/offers/ihk-praktikumsboerse"/><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/fa/offers/ihk-praktikumsboerse"/></url><url><loc>https://integreat.app/nurnberg/de/offers/lehrstellen-radar</loc><lastmod>2019-10-09</lastmod><changefreq>monthly</changefreq><priority>1.0</priority><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/en/offers/lehrstellen-radar"/><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/ar/offers/lehrstellen-radar"/><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/fa/offers/lehrstellen-radar"/></url><url><loc>https://integreat.app/nurnberg/de/events/test-veranstaltung/</loc><lastmod>2020-01-22</lastmod><changefreq>daily</changefreq><priority>0.5</priority><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/en/events/test-event/"/></url>
<url><loc>https://integreat.app/nurnberg/de/willkommen/</loc><lastmod>2019-08-12</lastmod><changefreq>monthly</changefreq><priority>1.0</priority><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/en/welcome/"/><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/ar/الترحيب/"/><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/fa/خوش-آمدید/"/></url><url><loc>https://integreat.app/nurnberg/de/willkommen/willkommen-in-nurnberg/</loc><lastmod>2019-08-12</lastmod><changefreq>monthly</changefreq><priority>1.0</priority><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/ar/الترحيب/مرحبا-بكم-في-نورنبيرغ/"/></url><url><loc>https://integreat.app/nurnberg/de/willkommen/zusammenleben-in-deutschland/</loc><lastmod>2019-08-12</lastmod><changefreq>monthly</changefreq><priority>1.0</priority><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/en/welcome/community-life-in-germany/"/></url><url><loc>https://integreat.app/nurnberg/de/willkommen/wissenswertes-uber-nurnberg/</loc><lastmod>2019-08-12</lastmod><changefreq>monthly</changefreq><priority>1.0</priority><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/en/welcome/trivia-about-nuremberg/"/><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/ar/الترحيب/معلومات-قيمة-عن-نورنبيرغ/"/></url><url><loc>https://integreat.app/nurnberg/de/willkommen/stadtplan/</loc><lastmod>2019-08-12</lastmod><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://integreat.app/nurnberg/de/willkommen/uber-integreat/</loc><lastmod>2019-08-12</lastmod><changefreq>monthly</changefreq><priority>1.0</priority><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/en/welcome/about-integreat/"/><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/fa/خوش-آمدید/درباره-integreat/"/></url><url><loc>https://integreat.app/nurnberg/de/willkommen/aufenthaltstitel/</loc><lastmod>2019-08-12</lastmod><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://integreat.app/nurnberg/de/test_links/</loc><lastmod>2022-12-30</lastmod><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://integreat.app/nurnberg/de/gespiegelte-seite/</loc><lastmod>2024-11-04</lastmod><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://integreat.app/nurnberg/de/locations/test-ort/</loc><lastmod>2020-01-22</lastmod><changefreq>monthly</changefreq><priority>0.5</priority><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/en/locations/test-location/"/></url><url><loc>https://integreat.app/nurnberg/de/offers/ihk-lehrstellenboerse</loc><lastmod>2019-10-09</lastmod><changefreq>monthly</changefreq><priority>1.0</priority><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/en/offers/ihk-lehrstellenboerse"/><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/ar/offers/ihk-lehrstellenboerse"/><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/fa/offers/ihk-lehrstellenboerse"/></url><url><loc>https://integreat.app/nurnberg/de/offers/ihk-praktikumsboerse</loc><lastmod>2019-10-09</lastmod><changefreq>monthly</changefreq><priority>1.0</priority><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/en/offers/ihk-praktikumsboerse"/><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/ar/offers/ihk-praktikumsboerse"/><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/fa/offers/ihk-praktikumsboerse"/></url><url><loc>https://integreat.app/nurnberg/de/offers/lehrstellen-radar</loc><lastmod>2019-10-09</lastmod><changefreq>monthly</changefreq><priority>1.0</priority><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/en/offers/lehrstellen-radar"/><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/ar/offers/lehrstellen-radar"/><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/fa/offers/lehrstellen-radar"/></url><url><loc>https://integreat.app/nurnberg/de/events/test-veranstaltung/</loc><lastmod>2020-01-22</lastmod><changefreq>daily</changefreq><priority>0.5</priority><xhtml:link rel="alternate" hreflang="" href="https://integreat.app/nurnberg/en/events/test-event/"/></url>
</urlset>
2 changes: 1 addition & 1 deletion tests/sitemap/sitemap_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
(
"/nurnberg/de/sitemap.xml",
"tests/sitemap/expected-sitemaps/sitemap-nurnberg-de.xml",
78,
83,
),
(
"/nurnberg/en/sitemap.xml",
Expand Down

0 comments on commit 711175d

Please sign in to comment.