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