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.
This commit is contained in:
Hugh Rundle 2025-02-03 16:26:06 +11:00
parent cda4b61b4b
commit 8dc69f2a9e
Signed by: hugh
GPG key ID: A7E35779918253F9
8 changed files with 66 additions and 18 deletions

View file

@ -155,6 +155,7 @@ class Tag(admin.ModelAdmin):
list_display = ("name",) list_display = ("name",)
search_fields = ["name"] search_fields = ["name"]
@admin.register(models.Event) @admin.register(models.Event)
class Event(admin.ModelAdmin): class Event(admin.ModelAdmin):
"""display settings for conferences""" """display settings for conferences"""

View file

@ -132,13 +132,9 @@ class Command(BaseCommand):
open_cfps = "" open_cfps = ""
for instance in cfps: for instance in cfps:
c_date = instance.closing_date c_date = instance.closing_date
title_string = ( title_string = f"<h4><a href='{instance.event.url}'>{instance.event.name} - {instance.name}</a></h4>"
f"<h4><a href='{instance.event.url}'>{instance.event.name} - {instance.name}</a></h4>"
)
dates_string = f"<p><strong>Closes:</strong><em>{c_date:%a} {c_date.day} {c_date:%B}</em></p>" dates_string = f"<p><strong>Closes:</strong><em>{c_date:%a} {c_date.day} {c_date:%B}</em></p>"
description_string = ( description_string = f"<p>{instance.details}</p><p style='margin-bottom:24px;'>{instance.event.description}</p>"
f"<p>{instance.details}</p><p style='margin-bottom:24px;'>{instance.event.description}</p>"
)
string_list = [title_string, dates_string, description_string] string_list = [title_string, dates_string, description_string]
string = "".join(string_list) string = "".join(string_list)

View file

@ -79,8 +79,7 @@ class Blog(BlogData):
author = "" author = ""
category = Category(self.category).label category = Category(self.category).label
status = f"{self.title}{author} has been added to Aus GLAMR! \ status = f"{self.title}{author} has been added to Aus GLAMR! \n\nIt's about {category}\n\n{self.url}"
\n\nIt's about {category}\n\n{self.url}"
Announcement.objects.create(status=status) Announcement.objects.create(status=status)
self.announced = True self.announced = True

View file

@ -15,10 +15,19 @@ class BlogTestCase(TestCase):
def setUp(self): def setUp(self):
"""set up test blog""" """set up test blog"""
self.blog = models.Blog.objects.create( self.blog = models.Blog.objects.create(
title="mya awesome blog", title="my awesome blog",
url="https://test.com", url="https://test.com",
feed="https://test.com/feed.xml", feed="https://test.com/feed.xml",
category="LIB", 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): def test_get_absolute_url(self):
@ -46,6 +55,22 @@ class BlogTestCase(TestCase):
self.blog.set_failing() self.blog.set_failing()
self.assertEqual(self.blog.failing, True) 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): def test_announce_article(self):
"""announcing a blog article""" """announcing a blog article"""
@ -59,7 +84,7 @@ class BlogTestCase(TestCase):
) )
article.announce() 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()) self.assertTrue(models.Announcement.objects.filter(status=status).exists())

View file

@ -70,7 +70,10 @@ class UtilityTests(TestCase):
self.assertEqual(data["description"], "A short summary of my blog") self.assertEqual(data["description"], "A short summary of my blog")
def test_get_blog_info_no_feed(self): def test_get_blog_info_no_feed(self):
"""test get blog info""" """
test get blog info
note get_blog_info only checks <head>, not rss feed
"""
with open( with open(
pathlib.Path(__file__).parent.joinpath("data/example.html"), pathlib.Path(__file__).parent.joinpath("data/example.html"),
@ -152,7 +155,7 @@ class UtilityTests(TestCase):
data = utilities.get_blog_info("http://test.test") data = utilities.get_blog_info("http://test.test")
self.assertEqual(data["title"], "My test website with an RSS feed") 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") self.assertEqual(data["description"], "A short summary of my blog")
def test_get_blog_info_with_incomplete_head_and_partial_feed(self): 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") data = utilities.get_blog_info("http://test.test")
self.assertEqual(data["title"], "My test website with an RSS feed") 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) self.assertEqual(data["description"], None)
def test_get_webfinger_subscribe_uri(self): def test_get_webfinger_subscribe_uri(self):

View file

@ -222,6 +222,7 @@ class PublicTests(TestCase):
form.data["from_email"] = "example@example.mail" form.data["from_email"] = "example@example.mail"
form.data["subject"] = "Hello" form.data["subject"] = "Hello"
form.data["message"] = "Hi there" form.data["message"] = "Hi there"
form.data["bot_check"] = "books"
request = self.factory.post("contact/", form.data) request = self.factory.post("contact/", form.data)
request.user = AnonymousUser() request.user = AnonymousUser()
@ -234,6 +235,24 @@ class PublicTests(TestCase):
# Verify that the subject of the first message is correct. # Verify that the subject of the first message is correct.
self.assertEqual(mail.outbox[0].subject, "Message via Aus GLAMR: Hello") 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): def test_search(self):
"""post search query""" """post search query"""

View file

@ -4,6 +4,7 @@ import re
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import feedparser import feedparser
import logging
import requests import requests
from django.conf import settings from django.conf import settings
@ -65,16 +66,20 @@ def get_blog_info(url):
) # use the title from the feed ) # use the title from the feed
blog_info["description"] = description or blog_info.get("description", "") blog_info["description"] = description or blog_info.get("description", "")
blog_info["author_name"] = blog_info.get("author", author)
else: else:
return False # if there is no feed info we need to put the onus back on the user to fill in the data 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 = "" normalised_author = blog_info["author_name"] or None
if author: if normalised_author:
normalised_author = author.replace("(noreply@blogger.com)", "") normalised_author = normalised_author.replace("(noreply@blogger.com)", "")
if normalised_author.strip() == "Unknown": if normalised_author.strip() == "Unknown":
normalised_author = "" normalised_author = ""
else:
normalised_author = ""
blog_info["author_name"] = normalised_author blog_info["author_name"] = normalised_author
return blog_info return blog_info

View file

@ -77,7 +77,7 @@ case "$CMD" in
runweb python manage.py send_weekly_email runweb python manage.py send_weekly_email
;; ;;
test) test)
runweb python manage.py test "$@" runweb python manage.py test blogs/tests "$@"
;; ;;
*) *)
set +x set +x