From 8dc69f2a9e30b493cc5051f1fb98b544aed7597d Mon Sep 17 00:00:00 2001 From: Hugh Rundle Date: Mon, 3 Feb 2025 16:26:06 +1100 Subject: [PATCH] fix blog announcements and update tests Blog announcements were using the string "None" as the author, now they should not. Updated tests accordingly. Fixed email test being captured by bot trap. --- blogs/admin.py | 1 + .../management/commands/send_weekly_email.py | 8 ++--- blogs/models/blog.py | 3 +- blogs/tests/test_models.py | 29 +++++++++++++++++-- blogs/tests/test_utilities.py | 9 ++++-- blogs/tests/test_views.py | 19 ++++++++++++ blogs/utilities.py | 13 ++++++--- glamr-dev | 2 +- 8 files changed, 66 insertions(+), 18 deletions(-) diff --git a/blogs/admin.py b/blogs/admin.py index 3ad6c43..aa8bfc5 100644 --- a/blogs/admin.py +++ b/blogs/admin.py @@ -155,6 +155,7 @@ class Tag(admin.ModelAdmin): list_display = ("name",) search_fields = ["name"] + @admin.register(models.Event) class Event(admin.ModelAdmin): """display settings for conferences""" diff --git a/blogs/management/commands/send_weekly_email.py b/blogs/management/commands/send_weekly_email.py index 4b69565..e720786 100644 --- a/blogs/management/commands/send_weekly_email.py +++ b/blogs/management/commands/send_weekly_email.py @@ -132,13 +132,9 @@ class Command(BaseCommand): open_cfps = "" for instance in cfps: c_date = instance.closing_date - title_string = ( - f"

{instance.event.name} - {instance.name}

" - ) + title_string = f"

{instance.event.name} - {instance.name}

" dates_string = f"

Closes:{c_date:%a} {c_date.day} {c_date:%B}

" - description_string = ( - f"

{instance.details}

{instance.event.description}

" - ) + description_string = f"

{instance.details}

{instance.event.description}

" string_list = [title_string, dates_string, description_string] string = "".join(string_list) diff --git a/blogs/models/blog.py b/blogs/models/blog.py index 8e7e89f..d43e915 100644 --- a/blogs/models/blog.py +++ b/blogs/models/blog.py @@ -79,8 +79,7 @@ class Blog(BlogData): author = "" category = Category(self.category).label - status = f"{self.title}{author} has been added to Aus GLAMR! \ - \n\nIt's about {category}\n\n{self.url}" + status = f"{self.title}{author} has been added to Aus GLAMR! \n\nIt's about {category}\n\n{self.url}" Announcement.objects.create(status=status) self.announced = True diff --git a/blogs/tests/test_models.py b/blogs/tests/test_models.py index 5f15597..3fdc235 100644 --- a/blogs/tests/test_models.py +++ b/blogs/tests/test_models.py @@ -15,10 +15,19 @@ class BlogTestCase(TestCase): def setUp(self): """set up test blog""" self.blog = models.Blog.objects.create( - title="mya awesome blog", + title="my awesome blog", url="https://test.com", feed="https://test.com/feed.xml", category="LIB", + author_name="Hugh", + ) + + self.blog_no_author = models.Blog.objects.create( + title="my awesome archives", + url="https://test2.com", + feed="https://test2.com/feed.xml", + category="ARC", + author_name="", ) def test_get_absolute_url(self): @@ -46,6 +55,22 @@ class BlogTestCase(TestCase): self.blog.set_failing() self.assertEqual(self.blog.failing, True) + def test_announce_blog(self): + """test announcing the blog""" + + self.blog.announce() + status = f"my awesome blog by Hugh has been added to Aus GLAMR! \n\nIt's about Libraries\n\nhttps://test.com" + announcement = models.Announcement.objects.first() + self.assertEqual(status, announcement.status) + + def test_announce_blog_no_author(self): + """test announcing the blog with a blank blog author name""" + + self.blog_no_author.announce() + status = f"my awesome archives has been added to Aus GLAMR! \n\nIt's about Archives\n\nhttps://test2.com" + announcement = models.Announcement.objects.first() + self.assertEqual(status, announcement.status) + def test_announce_article(self): """announcing a blog article""" @@ -59,7 +84,7 @@ class BlogTestCase(TestCase): ) article.announce() - status = f"My article (Hugh on mya awesome blog)\n\nhttps://example.blog/1" + status = f"My article (Hugh on my awesome blog)\n\nhttps://example.blog/1" self.assertTrue(models.Announcement.objects.filter(status=status).exists()) diff --git a/blogs/tests/test_utilities.py b/blogs/tests/test_utilities.py index eb9b4ca..a1bbdb2 100644 --- a/blogs/tests/test_utilities.py +++ b/blogs/tests/test_utilities.py @@ -70,7 +70,10 @@ class UtilityTests(TestCase): self.assertEqual(data["description"], "A short summary of my blog") def test_get_blog_info_no_feed(self): - """test get blog info""" + """ + test get blog info + note get_blog_info only checks , not rss feed + """ with open( pathlib.Path(__file__).parent.joinpath("data/example.html"), @@ -152,7 +155,7 @@ class UtilityTests(TestCase): data = utilities.get_blog_info("http://test.test") self.assertEqual(data["title"], "My test website with an RSS feed") - self.assertEqual(data["author_name"], "Hugh Rundle") + self.assertEqual(data["author_name"], "") self.assertEqual(data["description"], "A short summary of my blog") def test_get_blog_info_with_incomplete_head_and_partial_feed(self): @@ -171,7 +174,7 @@ class UtilityTests(TestCase): data = utilities.get_blog_info("http://test.test") self.assertEqual(data["title"], "My test website with an RSS feed") - self.assertEqual(data["author_name"], None) + self.assertEqual(data["author_name"], "") self.assertEqual(data["description"], None) def test_get_webfinger_subscribe_uri(self): diff --git a/blogs/tests/test_views.py b/blogs/tests/test_views.py index 57c9059..0c3dee8 100644 --- a/blogs/tests/test_views.py +++ b/blogs/tests/test_views.py @@ -222,6 +222,7 @@ class PublicTests(TestCase): form.data["from_email"] = "example@example.mail" form.data["subject"] = "Hello" form.data["message"] = "Hi there" + form.data["bot_check"] = "books" request = self.factory.post("contact/", form.data) request.user = AnonymousUser() @@ -234,6 +235,24 @@ class PublicTests(TestCase): # Verify that the subject of the first message is correct. self.assertEqual(mail.outbox[0].subject, "Message via Aus GLAMR: Hello") + def test_contact_from_bot(self): + """post message""" + + view = views.Contact.as_view() + form = forms.ContactForm() + form.data["from_email"] = "example@example.mail" + form.data["subject"] = "Hello" + form.data["message"] = "Hi there" + form.data["bot_check"] = "i am a stupid bot" + + request = self.factory.post("contact/", form.data) + request.user = AnonymousUser() + + view(request) + + # Test that one message has been sent. + self.assertEqual(len(mail.outbox), 0) + def test_search(self): """post search query""" diff --git a/blogs/utilities.py b/blogs/utilities.py index ff49a78..80734c4 100644 --- a/blogs/utilities.py +++ b/blogs/utilities.py @@ -4,6 +4,7 @@ import re from bs4 import BeautifulSoup import feedparser +import logging import requests from django.conf import settings @@ -65,17 +66,21 @@ def get_blog_info(url): ) # use the title from the feed blog_info["description"] = description or blog_info.get("description", "") + blog_info["author_name"] = blog_info.get("author", author) else: return False # if there is no feed info we need to put the onus back on the user to fill in the data - normalised_author = "" - if author: - normalised_author = author.replace("(noreply@blogger.com)", "") + normalised_author = blog_info["author_name"] or None + if normalised_author: + normalised_author = normalised_author.replace("(noreply@blogger.com)", "") if normalised_author.strip() == "Unknown": normalised_author = "" - blog_info["author_name"] = normalised_author + else: + normalised_author = "" + + blog_info["author_name"] = normalised_author return blog_info diff --git a/glamr-dev b/glamr-dev index 78ff9d8..f215db1 100755 --- a/glamr-dev +++ b/glamr-dev @@ -77,7 +77,7 @@ case "$CMD" in runweb python manage.py send_weekly_email ;; test) - runweb python manage.py test "$@" + runweb python manage.py test blogs/tests "$@" ;; *) set +x -- 2.39.5