|
| 1 | +from django.contrib.auth.models import Group, Permission |
| 2 | +from django.contrib.contenttypes.models import ContentType |
| 3 | +from django.test import TestCase |
| 4 | +from testapp.models import MathPage |
| 5 | +from wagtail.blocks.stream_block import StreamValue |
| 6 | +from wagtail.models import Page |
| 7 | +from wagtail.test.utils import WagtailTestUtils |
| 8 | + |
| 9 | + |
| 10 | +class TestStreamField(WagtailTestUtils, TestCase): |
| 11 | + def setUp(self): |
| 12 | + self.user = self.login() |
| 13 | + |
| 14 | + # Convert the user into an moderator |
| 15 | + self.moderators_group = Group.objects.get(name="Moderators") |
| 16 | + for permission in Permission.objects.filter( |
| 17 | + content_type=ContentType.objects.get_for_model(MathPage) |
| 18 | + ): |
| 19 | + self.moderators_group.permissions.add(permission) |
| 20 | + self.user.is_superuser = False |
| 21 | + self.user.groups.add(self.moderators_group) |
| 22 | + self.user.save() |
| 23 | + |
| 24 | + # Create test page with a draft revision |
| 25 | + self.page = Page.objects.get(id=1).add_child( |
| 26 | + instance=MathPage(title="Test", slug="test") |
| 27 | + ) |
| 28 | + |
| 29 | + self.page.body = StreamValue( |
| 30 | + stream_block=self.page.body.stream_block, |
| 31 | + stream_data=[ |
| 32 | + { |
| 33 | + "type": "equation", |
| 34 | + "value": "$ y = ax + b $", |
| 35 | + } |
| 36 | + ], |
| 37 | + is_lazy=True, |
| 38 | + ) |
| 39 | + |
| 40 | + self.page.save_revision().publish() |
| 41 | + |
| 42 | + def test_streamfield_value(self): |
| 43 | + page = MathPage.objects.first() |
| 44 | + self.assertTrue(page is not None) |
| 45 | + |
| 46 | + response = self.client.get(f"/admin/pages/{page.id}/edit/") |
| 47 | + self.assertEqual(response.status_code, 200) |
| 48 | + |
| 49 | + self.assertRegex( |
| 50 | + response.content.decode(), |
| 51 | + r""value": "\$ y = ax \+ b \$"", |
| 52 | + ) |
0 commit comments