Skip to content

Commit

Permalink
Add remaining tests for polls and markdown extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
frcroth committed Oct 15, 2023
1 parent bbdb5f3 commit 64f92db
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
5 changes: 4 additions & 1 deletion myhpi/core/markdown/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ def handleMatch(self, m, data=None):
def url(self, id):
return Image.objects.get(id=id).get_rendition("width-800").url

def default_pattern():
return r"!\[(?P<title>[^\[]+)\]\(image:(?P<id>\d+)\)"


class MinuteExtension(Extension):
def extendMarkdown(self, md):
Expand All @@ -193,7 +196,7 @@ def extendMarkdown(self, md):
200,
)
md.inlinePatterns.register(
ImagePattern(r"!\[(?P<title>[^\[]+)\]\(image:(?P<id>\d+)\)", md),
ImagePattern(ImagePattern.default_pattern(), md),
"ImagePattern",
200,
)
13 changes: 13 additions & 0 deletions myhpi/tests/core/test_polls.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def test_post_vote(self):
)
self.choice_good.refresh_from_db()
self.assertEqual(self.choice_good.votes, 1)
self.assertEqual(self.choice_good.percentage(), 100)
self.assertEqual(self.choice_bad.percentage(), 0)
self.assertFalse(self.poll.can_vote(self.student))

def test_post_vote_invalid_choice(self):
Expand Down Expand Up @@ -90,3 +92,14 @@ def test_post_vote_too_many_choices(self):
)
self.assertContains(response, "You can only select up to 1 options.", 1)
self.assertTrue(self.poll.can_vote(self.student))

def test_post_vote_before_start_date(self):
self.sign_in_as_student()
self.poll.start_date = datetime.now() + timedelta(days=1)
self.poll.save()
self.assertTrue(self.poll.can_vote(self.student))
response = self.client.post(
self.poll.url, data={"choice": [self.choice_good.id]}, follow=True
)
self.assertContains(response, "This poll has not yet started.")
self.assertTrue(self.poll.can_vote(self.student))
Binary file added myhpi/tests/files/test_image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 31 additions & 1 deletion myhpi/tests/test_markdown_extensions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import re
from typing import Collection

import django
from django.test import TestCase
from django.utils.translation import activate
from wagtail.core.models import Page
from wagtail.images.models import Image

from myhpi.core.markdown.extensions import ImagePattern

django.setup()

Expand Down Expand Up @@ -89,7 +95,6 @@ def test_heading_level_preprocessor(self):

def test_internal_link_preprocessor(self):
ilp = InternalLinkPattern(InternalLinkPattern.default_pattern())
import re

from myhpi.tests.core.setup import setup_data

Expand All @@ -98,3 +103,28 @@ def test_internal_link_preprocessor(self):
text = f"[Page title](page:{test_page.id})"
el, _, _ = ilp.handleMatch(re.match(ilp.pattern, text))
self.assertEqual(el.attrib["href"], test_page.localized.get_url())

def test_image_pattern(self):
activate("en")
from django.core.files.uploadedfile import SimpleUploadedFile

ip = ImagePattern(ImagePattern.default_pattern())

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

image = Image.objects.create(
title="Test image",
file=image_file,
)

text = f"![Alt text](image:{image.id})"
invalid_text = "![Alt text](image:1234567890)"
el, _, _ = ip.handleMatch(re.match(ip.pattern, text))
self.assertEqual(el.attrib["src"], image.get_rendition("width-800").url)

el, _, _ = ip.handleMatch(re.match(ip.pattern, invalid_text))
self.assertEqual(el.text, "[missing image]")

0 comments on commit 64f92db

Please sign in to comment.