Skip to content

Commit

Permalink
✅ [#14] add admin tests for informatie categorie, and moved the admin…
Browse files Browse the repository at this point in the history
….py file
  • Loading branch information
bart-maykin committed Sep 18, 2024
1 parent 4d30fe9 commit e168a91
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 29 deletions.
1 change: 0 additions & 1 deletion src/woo_publications/api/admin/__init__.py

This file was deleted.

22 changes: 0 additions & 22 deletions src/woo_publications/api/admin/category.py

This file was deleted.

7 changes: 1 addition & 6 deletions src/woo_publications/fixtures/default_admin_index.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@
},
"name": "WOO publications",
"slug": "woo_publications",
"models": [
[
"api",
"informatiecategorie"
]
]
"models": []
}
},
{
Expand Down
29 changes: 29 additions & 0 deletions src/woo_publications/metadata/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from django.contrib import admin

from ordered_model.admin import OrderedModelAdmin

from .constants import InformatieCategorieOrigins
from .models import InformatieCategorie


@admin.register(InformatieCategorie)
class InformatieCategorieAdmin(OrderedModelAdmin):
list_display = ("identifier", "naam", "order", "move_up_down_links")
readonly_fields = ("oorsprong",)
search_fields = (
"identifier",
"naam",
)
list_filter = ("oorsprong",)

def has_change_permission(self, request, obj=None):
if obj and obj.oorsprong == InformatieCategorieOrigins.value_list:
return False
return True

def get_readonly_fields(self, request, obj=None):
readonly_fields = self.readonly_fields
if obj and obj.oorsprong == InformatieCategorieOrigins.custom_entry:
readonly_fields += ("identifier",)

return readonly_fields
Empty file.
10 changes: 10 additions & 0 deletions src/woo_publications/metadata/tests/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import factory

from ..models import InformatieCategorie


class InformatieCategorieFactory(factory.django.DjangoModelFactory):
naam = factory.Faker("word")

class Meta:

Check failure on line 9 in src/woo_publications/metadata/tests/factories.py

View workflow job for this annotation

GitHub Actions / Type checking (Pyright)

"Meta" overrides symbol of same name in class "DjangoModelFactory"   "woo_publications.metadata.tests.factories.InformatieCategorieFactory.Meta" is not assignable to "factory.django.DjangoModelFactory.Meta"   Type "type[woo_publications.metadata.tests.factories.InformatieCategorieFactory.Meta]" is not assignable to type "type[factory.django.DjangoModelFactory.Meta]" (reportIncompatibleVariableOverride)
model = InformatieCategorie
212 changes: 212 additions & 0 deletions src/woo_publications/metadata/tests/test_informatie_categorie_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
from django.urls import reverse

from django_webtest import WebTest
from maykin_2fa.test import disable_admin_mfa

from woo_publications.accounts.tests.factories import UserFactory
from woo_publications.metadata.constants import InformatieCategorieOrigins
from woo_publications.metadata.models import InformatieCategorie
from woo_publications.metadata.tests.factories import InformatieCategorieFactory


@disable_admin_mfa()
class TestInformatieCategorieAdmin(WebTest):
def setUp(self):
self.user = UserFactory.create(
is_staff=True,
is_superuser=True,
)
self.information_category_value_list = InformatieCategorieFactory.create(
identifier="https://www.example.com/waardenlijsten/1",
naam="first item",
order=0,
oorsprong=InformatieCategorieOrigins.value_list,
)
self.information_category_custom_entry = InformatieCategorieFactory.create(
identifier="https://www.example.com/waardenlijsten/2",
naam="second item",
order=1,
oorsprong=InformatieCategorieOrigins.custom_entry,
)
super().setUp()

def test_informatie_category_admin_show_items(self):
response = self.app.get(
reverse(
"admin:metadata_informatiecategorie_changelist",
),
user=self.user,
)

self.assertEqual(response.status_int, 200)
# first item
self.assertInHTML(
"https://www.example.com/waardenlijsten/1",
str(response.content),
)
self.assertInHTML(
'<td class="field-naam">first item</td>',
str(response.content),
)
self.assertInHTML('<td class="field-order">0</td>', str(response.content))

# second item
self.assertInHTML(
"https://www.example.com/waardenlijsten/2",
str(response.content),
)
self.assertInHTML(
'<td class="field-naam">second item</td>',
str(response.content),
)
self.assertInHTML('<td class="field-order">1</td>', str(response.content))

def test_informatie_category_admin_show_item_search(self):
url = reverse("admin:metadata_informatiecategorie_changelist")

with self.subTest("filter_on_naam"):
response = self.app.get(url, {"q": "first item"}, user=self.user)

self.assertEqual(response.status_int, 200)
# first item in list
self.assertInHTML(
'<td class="field-naam">first item</td>',
str(response.content),
)
# second item not in list
with self.assertRaises(AssertionError):
self.assertInHTML(
'<td class="field-naam">second item</td>',
str(response.content),
)

with self.subTest("filter_on_identifier"):
response = self.app.get(
url, {"q": "https://www.example.com/waardenlijsten/2"}, user=self.user
)

self.assertEqual(response.status_int, 200)
# first item in list
self.assertInHTML(
"https://www.example.com/waardenlijsten/2",
str(response.content),
)
# second item not in list
with self.assertRaises(AssertionError):
self.assertInHTML(
"https://www.example.com/waardenlijsten/1",
str(response.content),
)

with self.subTest("filter_on_oorsprong"):
response = self.app.get(
url,
{"oorsprong__exact": InformatieCategorieOrigins.value_list},
user=self.user,
)

self.assertEqual(response.status_int, 200)
# first item in list
self.assertInHTML(
"https://www.example.com/waardenlijsten/1",
str(response.content),
)
# second item not in list
with self.assertRaises(AssertionError):
self.assertInHTML(
"https://www.example.com/waardenlijsten/2",
str(response.content),
)

def test_informatie_category_admin_can_not_update_item_with_oorsprong_waardenlijst(
self,
):
url = reverse(
"admin:metadata_informatiecategorie_change",
kwargs={"object_id": self.information_category_value_list.id},
)

response = self.app.get(url, user=self.user)
form = response.forms["informatiecategorie_form"]

self.assertEqual(response.status_code, 200)
# page doesn't have save button.
with self.assertRaisesMessage(
AssertionError,
'Couldn\'t find \'<input class="default" name="_save" type="submit" value="Opslaan">\' in response',
):
self.assertInHTML(
'<input type="submit" value="Opslaan" class="default" name="_save">',
str(response.content),
)

with self.assertRaisesMessage(
AssertionError,
"No field by the name 'identifier' found (fields: 'csrfmiddlewaretoken')",
):
form["identifier"] = "https://www.example.com/waardenlijsten/12"

def test_informatie_category_admin_can_update_item_with_oorsprong_zelf_toegevoegd(
self,
):
url = reverse(
"admin:metadata_informatiecategorie_change",
kwargs={"object_id": self.information_category_custom_entry.id},
)

response = self.app.get(url, user=self.user)
form = response.forms["informatiecategorie_form"]

self.assertEqual(response.status_code, 200)
# page has save button.
self.assertInHTML(
'<input type="submit" value="Opslaan" class="default" name="_save">',
str(response.content),
)

# test if identifier isn't editable
with self.assertRaises(AssertionError):
form["identifier"] = "https://www.example.com/waardenlijsten/12"

form["naam"] = "changed"
form["naam_meervoud"] = "changed"
form["definitie"] = "changed"
response = form.submit("submit")

self.assertEqual(response.status_code, 302)
self.information_category_custom_entry.refresh_from_db()

self.assertEqual(self.information_category_custom_entry.naam, "changed")
self.assertEqual(
self.information_category_custom_entry.naam_meervoud, "changed"
)
self.assertEqual(self.information_category_custom_entry.definitie, "changed")

def test_informatie_category_admin_create_item(self):
response = self.app.get(
reverse("admin:metadata_informatiecategorie_add"), user=self.user
)
form = response.forms["informatiecategorie_form"]

form["identifier"] = "https://www.example.com/waardenlijsten/999"
form["naam"] = "new item"
form["naam_meervoud"] = "new items"
form["definitie"] = (
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris risus nibh, "
"iaculis eu cursus sit amet, accumsan ac urna. Mauris interdum eleifend eros sed consectetur."
)

form.submit("submit")
self.assertTrue(
InformatieCategorie.objects.filter(
identifier="https://www.example.com/waardenlijsten/999",
naam="new item",
naam_meervoud="new items",
definitie=(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris risus nibh, "
"iaculis eu cursus sit amet, accumsan ac urna. Mauris interdum eleifend eros sed consectetur."
),
oorsprong=InformatieCategorieOrigins.custom_entry,
order=2,
).exists()
)

0 comments on commit e168a91

Please sign in to comment.