-
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.
✅ [#14] add admin tests for informatie categorie, and moved the admin…
….py file
- Loading branch information
1 parent
4d30fe9
commit e168a91
Showing
7 changed files
with
252 additions
and
29 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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.
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,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 GitHub Actions / Type checking (Pyright)
|
||
model = InformatieCategorie |
212 changes: 212 additions & 0 deletions
212
src/woo_publications/metadata/tests/test_informatie_categorie_admin.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,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() | ||
) |