from django.test import TestCase from django.urls import reverse from django.contrib.auth.models import User from audio.models import Feed, Episode from datetime import date, timedelta from audio.rss_utils import get_rss_item_desc from bs4 import BeautifulSoup from tp.settings import IMAGES_URL class TestGetRssItemDescTestCase(TestCase): def test_get_rss_item_desc(self): episode_a = Episode.objects.get(title="Caesar-Pompey Civil War Book I") desc = get_rss_item_desc(episode_a) soup = BeautifulSoup(desc, features="lxml") body = soup.find('body') bc = body.findChildren(recursive=False) self.assertEquals(len(bc), 6) self.assertEquals(str(bc[0]), f'

{episode_a.title}

') self.assertEquals(str(bc[1]), f'') self.assertEquals(str(bc[2]), f'

{episode_a.description}

') bc3a = f'

Photo ' bc3b = f'{episode_a.feed.image_title} by {episode_a.feed.image_attribution}' bc3c = f' is licensed ' bc3d = f'{episode_a.feed.image_license_name} {episode_a.feed.image_license_jurisdiction}.

' self.assertEquals(str(bc[3]), f'{bc3a}{bc3b}{bc3c}{bc3d}') bc4a = f'

Photo ' bc4b = f'{episode_a.image_title} by {episode_a.image_attribution}' bc4c = f' is licensed ' bc4d = f'{episode_a.image_license_name} {episode_a.image_license_jurisdiction}.

' self.assertEquals(str(bc[4]), f'{bc4a}{bc4b}{bc4c}{bc4d}') bc5a = f'

{episode_a.feed.ebook_title}'.replace('&', '&') bc5b = f' by {episode_a.feed.author}' bc5c = f' is licensed ' bc5d = f'{episode_a.feed.license_name} {episode_a.feed.license_jurisdiction}.

' self.assertEquals(str(bc[5]), f'{bc5a}{bc5b}{bc5c}{bc5d}') def setUp(self): user_a = User.objects.create(username='user_a') user_a.set_password('password_user_a') user_a.save() self.client.login(username='user_a', password='password_user_a') with open('bicycle.jpg', 'rb') as image_f: self.client.post(reverse('audio:new_feed'), { 'title': "Caesar-Pompey Civil War", 'author': "Gaius Julius Caesar", 'ebook_title': "Caesar's De Bello Gallico & Other Commentaries", 'ebook_url': "https://gutenberg.org/ebooks/10657", 'author_url': "https://gutenberg.org/ebooks/author/3621", 'license': 1, 'license_jurisdiction': 'in the USA', 'description': "Civil War between Pompey and Caesar.", 'image_title': 'A Penny Farthing', 'image_attribution': 'Agnieszka Kwiecień', 'original_image_url': 'https://commons.wikimedia.org/wiki/File:Ordinary_bicycle01.jpg', 'image_license': 1, 'image_license_jurisdiction': 'in the USA', 'image': image_f, }) feed_a = Feed.objects.get(title='Caesar-Pompey Civil War') kw_args = {'feed_pk': feed_a.pk, 'feed_title_slug': feed_a.slug} with ( open('bicycle.jpg', 'rb') as image_f, open('Mark Twain Taming The Bicycle.mp3', 'rb') as mp3_f ): self.client.post(reverse('audio:new_episode', kwargs=kw_args), { 'title': "Caesar-Pompey Civil War Book I", 'author': "Gaius Julius Caesar", 'pub_date': str(date.today() - timedelta(days=8)), 'episode_number': 1, 'description': 'Caesar confronts Afranius and Petreius', 'mp3': mp3_f, 'image_title': 'A Penny Farthing', 'image_attribution': 'Agnieszka Kwiecień', 'original_image_url': 'https://commons.wikimedia.org/wiki/File:Ordinary_bicycle01.jpg', 'image_license': 1, 'image_license_jurisdiction': 'in the USA', 'image': image_f, }) with ( open('bicycle.jpg', 'rb') as image_f, open('Mark Twain Taming The Bicycle.mp3', 'rb') as mp3_f ): self.client.post(reverse('audio:new_episode', kwargs=kw_args), { 'title': "Caesar-Pompey Civil War Book II", 'author': "Gaius Julius Caesar", 'pub_date': str(date.today() - timedelta(days=7)), 'episode_number': 2, 'description': 'Trebonius confronts Domitius', 'mp3': mp3_f, 'image_title': 'A Penny Farthing', 'image_attribution': 'Agnieszka Kwiecień', 'image_attribution_url': 'https://commons.wikimedia.org/wiki/User:Nova', 'original_image_url': 'https://commons.wikimedia.org/wiki/File:Ordinary_bicycle01.jpg', 'image_license': 1, 'image_license_jurisdiction': 'in the USA', 'image': image_f, })