1
|
import os
|
2
|
import pytest
|
3
|
import mock
|
4
|
import feedparser
|
5
|
|
6
|
from django.core.files.storage import DefaultStorage
|
7
|
from django.utils import timezone
|
8
|
from django.core.urlresolvers import reverse
|
9
|
|
10
|
from corbo.models import Category, Announce
|
11
|
|
12
|
pytestmark = pytest.mark.django_db
|
13
|
|
14
|
CATEGORIES = ('Alerts',)
|
15
|
|
16
|
ATOM_FEED = """
|
17
|
<?xml version="1.0" encoding="UTF-8"?>
|
18
|
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom" xmlns:wfw="http://wellformedweb.org/CommentAPI/">
|
19
|
<id>tag:linuxfr.org,2005:/news</id>
|
20
|
<title>Sample RSS Feeds</title>
|
21
|
<updated>2016-09-16T10:29:46+02:00</updated>
|
22
|
<entry>
|
23
|
<id>tag:linuxfr.org,2005:News/37537</id>
|
24
|
<published>2016-09-16T10:29:46+02:00</published>
|
25
|
<updated>2016-09-16T11:27:00+02:00</updated>
|
26
|
<title>Feed entry sample</title>
|
27
|
<content type="html">
|
28
|
<img src="http://example.com/logo.png">
|
29
|
Feed entry content
|
30
|
</content>
|
31
|
<author>
|
32
|
<name>Foo Bar</name>
|
33
|
</author>
|
34
|
</entry>
|
35
|
</feed>
|
36
|
"""
|
37
|
|
38
|
|
39
|
def mocked_request_get(*args, **kwargs):
|
40
|
storage = DefaultStorage()
|
41
|
|
42
|
class MockResponse:
|
43
|
|
44
|
def __init__(self, content):
|
45
|
self.ok = True
|
46
|
self.content = content
|
47
|
|
48
|
if args[0] == 'http://example.com/atom':
|
49
|
return MockResponse(ATOM_FEED)
|
50
|
else:
|
51
|
logo_path = os.path.join(os.path.dirname(__file__), 'media', 'logo.png')
|
52
|
return MockResponse(file(logo_path).read())
|
53
|
|
54
|
|
55
|
@mock.patch('corbo.models.requests.get', side_effect=mocked_request_get)
|
56
|
def test_announces_from_feed(mocked_get):
|
57
|
storage = DefaultStorage()
|
58
|
feed_content = feedparser.parse(ATOM_FEED)
|
59
|
for category in CATEGORIES:
|
60
|
c = Category.objects.create(name=category, rss_feed_url='http://example.com/atom')
|
61
|
assert c.announce_set.count() == len(feed_content['entries'])
|
62
|
for announce in c.announce_set.all():
|
63
|
assert announce.title in [feed['title'] for feed in feed_content['entries']]
|
64
|
assert storage.url('logo.png') in announce.text
|
65
|
# cleanup uploaded images
|
66
|
os.unlink(storage.path('logo.png'))
|
67
|
|
68
|
|
69
|
def test_announces_publishing(app):
|
70
|
c = Category.objects.create(name='Test announces')
|
71
|
a = Announce.objects.create(category=c, title='Test 1',
|
72
|
text='text')
|
73
|
feed_content = feedparser.parse(app.get(reverse('atom')).text)
|
74
|
assert len(feed_content['entries']) == 0
|
75
|
a.publication_time = timezone.now()
|
76
|
a.save()
|
77
|
feed_content = feedparser.parse(app.get(reverse('atom')).text)
|
78
|
assert len(feed_content['entries']) == 1
|
79
|
a.publication_time = timezone.now() + timezone.timedelta(days=1)
|
80
|
a.save()
|
81
|
feed_content = feedparser.parse(app.get(reverse('atom')).text)
|
82
|
assert len(feed_content['entries']) == 0
|
83
|
a.publication_time = timezone.now()
|
84
|
a.expiration_time = timezone.now()
|
85
|
a.save()
|
86
|
feed_content = feedparser.parse(app.get(reverse('atom')).text)
|
87
|
assert len(feed_content['entries']) == 0
|