Skip to content

Commit

Permalink
Test attachment select widget
Browse files Browse the repository at this point in the history
  • Loading branch information
frcroth committed Oct 15, 2023
1 parent 64f92db commit 6f04249
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
64 changes: 63 additions & 1 deletion myhpi/tests/core/setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.contrib.auth.models import Group, User
from django.contrib.auth.models import Group, User, Permission
from wagtail.models import Site

from myhpi.core.models import (
Expand All @@ -10,6 +10,8 @@
RootPage,
SecondLevelMenuItem,
)
from wagtail.models import Collection, GroupCollectionPermission
from wagtail.documents.models import Document


def create_users():
Expand Down Expand Up @@ -228,12 +230,70 @@ def setup_minutes(group, students_group, parent, user):
return minutes, minutes_list


def create_collections(groups):
root_collection = Collection.get_first_root_node()
for group in groups:
group_collection = root_collection.add_child(name=f"{group.name} collection")
group_collection.save()
GroupCollectionPermission.objects.create(
group=group,
collection=group_collection,
permission=Permission.objects.get(
content_type__app_label="wagtaildocs", codename="add_document"
),
)
GroupCollectionPermission.objects.create(
group=group,
collection=group_collection,
permission=Permission.objects.get(
content_type__app_label="wagtaildocs", codename="change_document"
),
)
GroupCollectionPermission.objects.create(
group=group,
collection=group_collection,
permission=Permission.objects.get(
content_type__app_label="wagtaildocs", codename="choose_document"
),
)
yield group_collection


def get_test_image_file():
from django.core.files.uploadedfile import SimpleUploadedFile

image_file = SimpleUploadedFile(
name="test_image.jpg",
content=open("myhpi/tests/files/test_image.jpg", "rb").read(),
content_type="image/jpeg",
)
return image_file


def create_documents(collections):
documents = [
Document.objects.create(
title="First document",
file=get_test_image_file(),
collection=collections[0],
),
Document.objects.create(
title="Second document",
file=get_test_image_file(),
collection=collections[1],
),
]
return documents


def setup_data():
users = create_users()
groups = create_groups(users)
basic_pages = create_basic_page_structure()
information_pages = create_information_pages(groups, basic_pages["information_menu"])
minutes, minutes_list = setup_minutes(groups[1], groups[0], basic_pages["fsr_menu"], users[2])
collections = list(create_collections(groups))
documents = create_documents(collections)

return {
"basic_pages": basic_pages,
Expand All @@ -242,4 +302,6 @@ def setup_data():
"pages": information_pages,
"minutes": minutes,
"minutes_list": minutes_list,
"collections": collections,
"documents": documents,
}
27 changes: 27 additions & 0 deletions myhpi/tests/core/test_widgets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from dataclasses import dataclass
from myhpi.core.widgets import AttachmentSelectWidget
from myhpi.tests.core.utils import MyHPIPageTestCase

from django.forms.models import ModelChoiceIteratorValue


class WidgetTests(MyHPIPageTestCase):
def setUp(self):
super().setUp()

def test_attachment_select_widget(self):
self.sign_in_as_student()
# student has access to document 1, not document 2
choices = list(
map(
lambda doc: (ModelChoiceIteratorValue(doc.id, doc), doc.title),
self.test_data["documents"],
)
)
widget = AttachmentSelectWidget(user=self.student, choices=choices)
optgroups = widget.optgroups("attachments", [])
self.assertEqual(len(optgroups), 1)

widget = AttachmentSelectWidget(user=self.student_representative, choices=choices)
optgroups = widget.optgroups("attachments", [])
self.assertEqual(len(optgroups), 2)
3 changes: 3 additions & 0 deletions myhpi/tests/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def setUp(self):
self.student = self.test_data["users"][1]
self.student_representative = self.test_data["users"][2]

self.first_document = self.test_data["documents"][0]
self.second_document = self.test_data["documents"][1]

def sign_in_as_student(self):
self.client.force_login(self.student)

Expand Down

0 comments on commit 6f04249

Please sign in to comment.