1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-23 01:16:18 +00:00
supysonic/tests/api/test_response_helper.py

235 lines
7.3 KiB
Python
Raw Normal View History

2017-10-28 19:18:34 +00:00
#!/usr/bin/env python
2018-03-04 20:49:56 +00:00
# coding: utf-8
2017-10-28 19:18:34 +00:00
#
# This file is part of Supysonic.
# Supysonic is a Python implementation of the Subsonic server API.
#
# Copyright (C) 2017-2018 Alban 'spl0k' Féron
2017-10-28 19:18:34 +00:00
#
# Distributed under terms of the GNU AGPLv3 license.
2017-11-27 21:30:13 +00:00
import unittest
2018-01-21 21:02:32 +00:00
import flask.json
2017-10-28 19:18:34 +00:00
from xml.etree import ElementTree
2018-02-11 11:40:10 +00:00
from supysonic.api.formatters import JSONFormatter, JSONPFormatter, XMLFormatter
2018-01-08 22:13:50 +00:00
from supysonic.py23 import strtype
2017-11-27 21:30:13 +00:00
from ..testbase import TestBase
2017-10-28 19:18:34 +00:00
2018-02-11 11:40:10 +00:00
class UnwrapperMixin(object):
def make_response(self, elem, data):
with self.request_context():
rv = super(UnwrapperMixin, self).make_response(elem, data)
return rv.get_data(as_text = True)
2017-11-27 21:30:13 +00:00
2018-02-11 11:40:10 +00:00
@staticmethod
def create_from(cls):
class Unwrapper(UnwrapperMixin, cls):
pass
return Unwrapper
2018-02-11 11:40:10 +00:00
class ResponseHelperJsonTestCase(TestBase, UnwrapperMixin.create_from(JSONFormatter)):
def make_response(self, elem, data):
rv = super(ResponseHelperJsonTestCase, self).make_response(elem, data)
return flask.json.loads(rv)
2018-02-11 11:40:10 +00:00
def process_and_extract(self, d):
return self.make_response('tag', d)['subsonic-response']['tag']
2017-10-28 19:18:34 +00:00
def test_basic(self):
2018-02-11 11:40:10 +00:00
empty = self.empty
2017-10-28 19:18:34 +00:00
self.assertEqual(len(empty), 1)
2018-01-08 22:13:50 +00:00
self.assertIn('subsonic-response', empty)
self.assertIsInstance(empty['subsonic-response'], dict)
2017-10-28 19:18:34 +00:00
2018-01-08 22:13:50 +00:00
resp = empty['subsonic-response']
2017-10-28 19:18:34 +00:00
self.assertEqual(len(resp), 2)
2018-01-08 22:13:50 +00:00
self.assertIn('status', resp)
self.assertIn('version', resp)
self.assertEqual(resp['status'], 'ok')
2017-10-28 19:18:34 +00:00
2018-02-11 11:40:10 +00:00
resp = self.error(0, 'message')['subsonic-response']
2018-01-08 22:13:50 +00:00
self.assertEqual(resp['status'], 'failed')
2017-10-28 19:18:34 +00:00
some_dict = {
2018-01-08 22:13:50 +00:00
'intValue': 2,
'someString': 'Hello world!'
2017-10-28 19:18:34 +00:00
}
resp = self.process_and_extract(some_dict)
2018-01-08 22:13:50 +00:00
self.assertIn('intValue', resp)
self.assertIn('someString', resp)
2017-10-28 19:18:34 +00:00
def test_lists(self):
resp = self.process_and_extract({
2018-01-08 22:13:50 +00:00
'someList': [ 2, 4, 8, 16 ],
'emptyList': []
2017-10-28 19:18:34 +00:00
})
2018-01-08 22:13:50 +00:00
self.assertIn('someList', resp)
self.assertNotIn('emptyList', resp)
self.assertListEqual(resp['someList'], [ 2, 4, 8, 16 ])
2017-10-28 19:18:34 +00:00
def test_dicts(self):
resp = self.process_and_extract({
2018-01-08 22:13:50 +00:00
'dict': { 's': 'Blah', 'i': 20 },
'empty': {}
2017-10-28 19:18:34 +00:00
})
2018-01-08 22:13:50 +00:00
self.assertIn('dict', resp)
self.assertIn('empty', resp)
self.assertDictEqual(resp['dict'], { 's': 'Blah', 'i': 20 })
self.assertDictEqual(resp['empty'], {})
2017-10-28 19:18:34 +00:00
def test_nesting(self):
resp = self.process_and_extract({
2018-01-08 22:13:50 +00:00
'dict': {
'value': 'hey look! a string',
'list': [ 1, 2, 3 ],
'emptyList': [],
'subdict': { 'a': 'A' }
2017-10-28 19:18:34 +00:00
},
2018-01-08 22:13:50 +00:00
'list': [
{ 'b': 'B' },
{ 'c': 'C' },
2017-10-28 19:18:34 +00:00
[ 4, 5, 6 ],
2018-01-08 22:13:50 +00:00
'final string'
2017-10-28 19:18:34 +00:00
]
})
2018-02-11 11:40:10 +00:00
self.assertEqual(len(resp), 2)
2018-01-08 22:13:50 +00:00
self.assertIn('dict', resp)
self.assertIn('list', resp)
2017-10-28 19:18:34 +00:00
2018-01-08 22:13:50 +00:00
d = resp['dict']
l = resp['list']
2017-10-28 19:18:34 +00:00
2018-01-08 22:13:50 +00:00
self.assertIn('value', d)
self.assertIn('list', d)
2017-10-28 19:18:34 +00:00
self.assertNotIn('emptyList', d)
2018-01-08 22:13:50 +00:00
self.assertIn('subdict', d)
self.assertIsInstance(d['value'], strtype)
self.assertIsInstance(d['list'], list)
self.assertIsInstance(d['subdict'], dict)
2017-10-28 19:18:34 +00:00
self.assertEqual(l, [
2018-01-08 22:13:50 +00:00
{ 'b': 'B' },
{ 'c': 'C' },
2017-10-28 19:18:34 +00:00
[ 4, 5, 6 ],
2018-01-08 22:13:50 +00:00
'final string'
2017-10-28 19:18:34 +00:00
])
2018-02-11 11:40:10 +00:00
class ResponseHelperJsonpTestCase(TestBase, UnwrapperMixin.create_from(JSONPFormatter)):
2017-10-28 19:18:34 +00:00
def test_basic(self):
2018-02-11 11:40:10 +00:00
self._JSONPFormatter__callback = 'callback' # hacky
result = self.empty
2018-01-08 22:13:50 +00:00
self.assertTrue(result.startswith('callback({'))
self.assertTrue(result.endswith('})'))
2017-10-28 19:18:34 +00:00
2018-01-21 21:02:32 +00:00
json = flask.json.loads(result[9:-1])
2018-01-08 22:13:50 +00:00
self.assertIn('subsonic-response', json)
2017-10-28 19:18:34 +00:00
2018-02-11 11:40:10 +00:00
class ResponseHelperXMLTestCase(TestBase, UnwrapperMixin.create_from(XMLFormatter)):
def make_response(self, elem, data):
xml = super(ResponseHelperXMLTestCase, self).make_response(elem, data)
2018-01-08 22:13:50 +00:00
xml = xml.replace('xmlns="http://subsonic.org/restapi"', '')
2017-10-28 19:18:34 +00:00
root = ElementTree.fromstring(xml)
return root
2018-02-11 11:40:10 +00:00
def process_and_extract(self, d):
rv = self.make_response('tag', d)
return rv.find('tag')
2017-10-28 19:18:34 +00:00
def assertAttributesMatchDict(self, elem, d):
d = { k: str(v) for k, v in d.items() }
2017-10-28 19:18:34 +00:00
self.assertDictEqual(elem.attrib, d)
def test_root(self):
2018-02-11 11:40:10 +00:00
xml = super(ResponseHelperXMLTestCase, self).make_response('tag', {})
2018-01-08 22:13:50 +00:00
self.assertIn('<subsonic-response ', xml)
self.assertIn('xmlns="http://subsonic.org/restapi"', xml)
self.assertTrue(xml.strip().endswith('</subsonic-response>'))
2017-10-28 19:18:34 +00:00
def test_basic(self):
2018-02-11 11:40:10 +00:00
empty = self.empty
2018-01-08 22:13:50 +00:00
self.assertIsNotNone(empty.find('.[@version]'))
self.assertIsNotNone(empty.find(".[@status='ok']"))
2017-10-28 19:18:34 +00:00
2018-02-11 11:40:10 +00:00
resp = self.error(0, 'message')
2018-01-08 22:13:50 +00:00
self.assertIsNotNone(resp.find(".[@status='failed']"))
2017-10-28 19:18:34 +00:00
some_dict = {
2018-01-08 22:13:50 +00:00
'intValue': 2,
'someString': 'Hello world!'
2017-10-28 19:18:34 +00:00
}
2018-02-11 11:40:10 +00:00
resp = self.process_and_extract(some_dict)
2018-01-08 22:13:50 +00:00
self.assertIsNotNone(resp.find('.[@intValue]'))
self.assertIsNotNone(resp.find('.[@someString]'))
2017-10-28 19:18:34 +00:00
def test_lists(self):
2018-02-11 11:40:10 +00:00
resp = self.process_and_extract({
2018-01-08 22:13:50 +00:00
'someList': [ 2, 4, 8, 16 ],
'emptyList': []
2017-10-28 19:18:34 +00:00
})
2018-01-08 22:13:50 +00:00
elems = resp.findall('./someList')
2017-10-28 19:18:34 +00:00
self.assertEqual(len(elems), 4)
2018-01-08 22:13:50 +00:00
self.assertIsNone(resp.find('./emptyList'))
2017-10-28 19:18:34 +00:00
for e, i in zip(elems, [ 2, 4, 8, 16 ]):
self.assertEqual(int(e.text), i)
def test_dicts(self):
2018-02-11 11:40:10 +00:00
resp = self.process_and_extract({
2018-01-08 22:13:50 +00:00
'dict': { 's': 'Blah', 'i': 20 },
'empty': {}
2017-10-28 19:18:34 +00:00
})
2018-01-08 22:13:50 +00:00
d = resp.find('./dict')
2017-10-28 19:18:34 +00:00
self.assertIsNotNone(d)
2018-01-08 22:13:50 +00:00
self.assertIsNotNone(resp.find('./empty'))
self.assertAttributesMatchDict(d, { 's': 'Blah', 'i': 20 })
2017-10-28 19:18:34 +00:00
def test_nesting(self):
2018-02-11 11:40:10 +00:00
resp = self.process_and_extract({
2018-01-08 22:13:50 +00:00
'dict': {
'value': 'hey look! a string',
'list': [ 1, 2, 3 ],
'emptyList': [],
'subdict': { 'a': 'A' }
2017-10-28 19:18:34 +00:00
},
2018-01-08 22:13:50 +00:00
'list': [
{ 'b': 'B' },
{ 'c': 'C' },
'final string'
2017-10-28 19:18:34 +00:00
]
})
self.assertEqual(len(resp), 4) # 'dict' and 3 'list's
2018-01-08 22:13:50 +00:00
d = resp.find('./dict')
lists = resp.findall('./list')
2017-10-28 19:18:34 +00:00
self.assertIsNotNone(d)
2018-01-08 22:13:50 +00:00
self.assertAttributesMatchDict(d, { 'value': 'hey look! a string' })
self.assertEqual(len(d.findall('./list')), 3)
self.assertEqual(len(d.findall('./emptyList')), 0)
self.assertIsNotNone(d.find('./subdict'))
2017-10-28 19:18:34 +00:00
self.assertEqual(len(lists), 3)
2018-01-08 22:13:50 +00:00
self.assertAttributesMatchDict(lists[0], { 'b': 'B' })
self.assertAttributesMatchDict(lists[1], { 'c': 'C' })
self.assertEqual(lists[2].text, 'final string')
2017-10-28 19:18:34 +00:00
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(ResponseHelperJsonTestCase))
suite.addTest(unittest.makeSuite(ResponseHelperJsonpTestCase))
suite.addTest(unittest.makeSuite(ResponseHelperXMLTestCase))
return suite
if __name__ == '__main__':
unittest.main()