3 from django
.core
.urlresolvers
import reverse
4 from django
.test
.client
import Client
5 from django
.test
import TestCase
7 from mygpo
.test
import create_auth_string
, create_user
8 from mygpo
.api
.advanced
import lists
as views
9 from mygpo
.podcastlists
.models
import PodcastList
13 class TestAPI(TestCase
):
14 """ Tests the Podcast List API """
17 self
.user
, pwd
= create_user()
18 self
.client
= Client()
20 'HTTP_AUTHORIZATION': create_auth_string(self
.user
.username
, pwd
)
26 def test_create_missing_title(self
):
27 """ verify error response when creating podcast list w/o title """
28 url
= reverse(views
.create
, kwargs
={
29 'username': self
.user
.username
,
33 urls
= ['http://example.com/podcast.rss',
34 'http://example.com/asdf.xml']
36 resp
= self
.client
.post(url
, '\n'.join(urls
),
37 content_type
="text/plain", **self
.extra
)
38 self
.assertEqual(resp
.status_code
, 400, resp
.content
)
40 def test_create(self
):
41 """ Create a podcast list and verify """
42 title
= 'My Podcast List'
43 url
= get_create_url(self
.user
.username
, 'txt', title
)
45 urls
= ['http://example.com/podcast.rss',
46 'http://example.com/asdf.xml']
48 resp
= self
.client
.post(url
, '\n'.join(urls
),
49 content_type
="text/plain", **self
.extra
)
50 self
.assertEqual(resp
.status_code
, 201, resp
.content
)
52 # assert that the list has actually been created
53 lists
= PodcastList
.objects
.filter(user
=self
.user
)
54 self
.assertEqual(1, len(lists
))
56 self
.assertEqual(title
, pl
.title
)
57 self
.assertEqual(len(urls
), pl
.entries
.count())
60 def test_replace(self
):
61 """ Create, replace and delete a podcast list """
62 title
= 'My Podcast List'
63 url
= get_create_url(self
.user
.username
, 'txt', title
)
65 urls1
= ['http://example.com/podcast.rss',
66 'http://example.com/asdf.xml',
67 'http://example.com/test.rss']
69 resp
= self
.client
.post(url
, '\n'.join(urls1
),
70 content_type
="text/plain", **self
.extra
)
71 self
.assertEqual(resp
.status_code
, 201, resp
.content
)
73 # assert that the list has actually been created
74 lists
= PodcastList
.objects
.filter(user
=self
.user
)
75 self
.assertEqual(1, len(lists
))
76 self
.assertEqual(title
, lists
[0].title
)
78 # replace existing list; the lists's URL is returned
79 # in the Location header
80 url
= resp
['Location']
81 urls2
= ['http://example.com/test.rss', # reordered
82 'http://example.com/asdf.xml',
83 'http://example.com/new.rss'] # new
85 resp
= self
.client
.put(url
, '\n'.join(urls2
),
86 content_type
="text/plain", **self
.extra
)
87 self
.assertEqual(resp
.status_code
, 204, resp
.content
)
89 # assert that the list has actually been updated
90 resp
= self
.client
.get(url
, content_type
="text/plain", **self
.extra
)
91 resp_urls
= [_f
for _f
in resp
.content
.split('\n') if _f
]
92 self
.assertEqual(urls2
, resp_urls
)
95 self
.client
.delete(url
)
98 def get_create_url(username
, fmt
, title
):
99 return '{url}?title={title}'.format(
100 url
=reverse(views
.create
, kwargs
={
101 'username': username
,