4 import urllib
.request
, urllib
.parse
, urllib
.error
7 from django
.urls
import reverse
8 from django
.test
.client
import Client
as TestClient
9 from django
.test
import TestCase
11 from mygpo
.test
import create_auth_string
, create_user
12 from mygpo
.api
.advanced
import settings
as views
13 from mygpo
.usersettings
.models
import UserSettings
14 from mygpo
.podcasts
.models
import Podcast
, Episode
15 from mygpo
.users
.models
import Client
18 class TestAPI(TestCase
):
21 self
.user
, pwd
= create_user()
22 self
.podcast_url
= 'http://example.com/podcast.rss'
23 self
.episode_url
= 'http://example.com/podcast/episode-1.mp3'
24 self
.uid
= 'client-uid'
25 self
.podcast
= Podcast
.objects
.get_or_create_for_url(self
.podcast_url
)
26 self
.episode
= Episode
.objects
.get_or_create_for_url(
30 self
.user_client
= Client
.objects
.create(
35 self
.client
= TestClient()
37 'HTTP_AUTHORIZATION': create_auth_string(self
.user
.username
, pwd
)
45 def test_user_settings(self
):
46 """ Create, update and verify settings for the user """
47 url
= self
.get_url(self
.user
.username
, 'account')
48 self
._do
_test
_url
(url
)
50 def test_podcast_settings(self
):
51 url
= self
.get_url(self
.user
.username
, 'podcast', {
52 'podcast': self
.podcast_url
,
54 self
._do
_test
_url
(url
)
56 def test_episode_settings(self
):
57 url
= self
.get_url(self
.user
.username
, 'episode', {
58 'podcast': self
.podcast_url
,
59 'episode': self
.episode_url
,
61 self
._do
_test
_url
(url
)
63 def test_client_settings(self
):
64 url
= self
.get_url(self
.user
.username
, 'device', {
67 self
._do
_test
_url
(url
)
69 def _do_test_url(self
, url
):
71 settings
= {'set': {'a': 'b', 'c': 'd'}}
72 resp
= self
.client
.post(url
, json
.dumps(settings
),
73 content_type
='application/octet-stream',
75 self
.assertEqual(resp
.status_code
, 200, resp
.content
)
78 settings
= {'set': {'a': 'x'}, 'remove': ['c']}
79 resp
= self
.client
.post(url
, json
.dumps(settings
),
80 content_type
='application/octet-stream',
82 self
.assertEqual(resp
.status_code
, 200, resp
.content
)
85 resp
= self
.client
.get(url
, **self
.extra
)
86 self
.assertEqual(resp
.status_code
, 200, resp
.content
)
87 self
.assertEqual(json
.loads(resp
.content
.decode('utf-8')), {'a': 'x'})
89 def get_url(self
, username
, scope
, params
={}):
90 url
= reverse('settings-api', kwargs
={
94 return '{url}?{params}'.format(url
=url
,
95 params
=urllib
.parse
.urlencode(params
))