remove deprecation warning for request.raw_post_data
[mygpo.git] / mygpo / api / tests.py
blob8b105b3c04c49ef221e4478401c7a4ddef9afb6a
2 # This file is part of my.gpodder.org.
4 # my.gpodder.org is free software: you can redistribute it and/or modify it
5 # under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or (at your
7 # option) any later version.
9 # my.gpodder.org is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
12 # License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with my.gpodder.org. If not, see <http://www.gnu.org/licenses/>.
19 import unittest
20 import doctest
21 from copy import deepcopy
23 from django.test.client import Client
24 from django.core.urlresolvers import reverse
26 from mygpo.api.advanced import episodes
27 from mygpo.users.models import User
28 from mygpo.test import create_auth_string
29 from mygpo.core.json import json
32 class LegacyAPITests(unittest.TestCase):
33 pass
36 class AdvancedAPITests(unittest.TestCase):
38 def setUp(self):
39 self.password = 'asdf'
40 self.username = 'user'
41 self.user = User(username=self.username, email='user@example.com')
42 self.user.set_password(self.password)
43 self.user.save()
44 self.user.is_active = True
45 self.client = Client()
47 self.extra = {
48 'HTTP_AUTHORIZATION': create_auth_string(self.username, self.password)
51 self.action_data = [
53 "podcast": "http://example.com/feed.rss",
54 "episode": "http://example.com/files/s01e20.mp3",
55 "device": "gpodder_abcdef123",
56 "action": "download",
57 "timestamp": "2009-12-12T09:00:00"
60 "podcast": "http://example.org/podcast.php",
61 "episode": "http://ftp.example.org/foo.ogg",
62 "action": "play",
63 "started": 15,
64 "position": 120,
65 "total": 500
71 def tearDown(self):
72 self.user.delete()
75 def test_episode_actions(self):
76 url = reverse(episodes, kwargs=dict(version='2', username=self.user.username))
78 # upload actions
79 response = self.client.post(url, json.dumps(self.action_data),
80 content_type="application/json", **self.extra)
81 self.assertEqual(response.status_code, 200, response.content)
84 # get all
85 extra = deepcopy(self.extra)
86 extra['since'] = 0
88 response = self.client.get(url, **extra)
89 self.assertEqual(response.status_code, 200, response.content)
90 response_obj = json.loads(response.content)
91 actions = response_obj['actions']
92 self.assertTrue(self.compare_action_list(self.action_data, actions))
95 def compare_action_list(self, as1, as2):
96 for a1 in as1:
97 found = False
98 for a2 in as2:
99 if self.compare_actions(a1, a2):
100 found = True
102 if not found:
103 raise ValueError('%s not found in %s' % (a1, as2))
104 return False
106 return True
109 def compare_actions(self, a1, a2):
110 for key, val in a1.items():
111 if a2.get(key, None) != val:
112 return False
113 return True
115 def suite():
116 suite = unittest.TestSuite()
117 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(AdvancedAPITests))
118 return suite