move mygpo.json => mygpo.core.jsony to avoid import confusion
[mygpo.git] / mygpo / api / tests.py
blobde5146e4f405def2b8a41bb2eae3c07ce7caf6d4
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 response_obj = json.loads(response.content)
90 actions = response_obj['actions']
91 self.assertTrue(self.compare_action_list(self.action_data, actions))
94 def compare_action_list(self, as1, as2):
95 for a1 in as1:
96 found = False
97 for a2 in as2:
98 if self.compare_actions(a1, a2):
99 found = True
101 if not found:
102 return False
104 return True
107 def compare_actions(self, a1, a2):
108 for key, val in a1.items():
109 if a2[key] != val:
110 return False
111 return True
113 def suite():
114 suite = unittest.TestSuite()
115 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(AdvancedAPITests))
116 return suite