add API v2
This commit is contained in:
parent
f809c3a8ff
commit
2df0189a90
71 changed files with 2740 additions and 315 deletions
|
@ -1,5 +1,5 @@
|
|||
import unittest
|
||||
from api_clients.api_client import ApcUrl, UrlBadParam, IncompleteUrl
|
||||
from api_clients.utils import ApcUrl, UrlBadParam, IncompleteUrl
|
||||
|
||||
class TestApcUrl(unittest.TestCase):
|
||||
def test_init(self):
|
||||
|
@ -8,16 +8,16 @@ class TestApcUrl(unittest.TestCase):
|
|||
self.assertEqual(u.base_url, url)
|
||||
|
||||
def test_params_1(self):
|
||||
u = ApcUrl("/testing/%%key%%")
|
||||
u = ApcUrl("/testing/{key}")
|
||||
self.assertEqual(u.params(key='val').url(), '/testing/val')
|
||||
|
||||
def test_params_2(self):
|
||||
u = ApcUrl('/testing/%%key%%/%%api%%/more_testing')
|
||||
u = ApcUrl('/testing/{key}/{api}/more_testing')
|
||||
full_url = u.params(key="AAA",api="BBB").url()
|
||||
self.assertEqual(full_url, '/testing/AAA/BBB/more_testing')
|
||||
|
||||
def test_params_ex(self):
|
||||
u = ApcUrl("/testing/%%key%%")
|
||||
u = ApcUrl("/testing/{key}")
|
||||
with self.assertRaises(UrlBadParam):
|
||||
u.params(bad_key='testing')
|
||||
|
||||
|
@ -26,5 +26,5 @@ class TestApcUrl(unittest.TestCase):
|
|||
self.assertEqual( ApcUrl(u).url(), u )
|
||||
|
||||
def test_url_ex(self):
|
||||
u = ApcUrl('/%%one%%/%%two%%/three').params(two='testing')
|
||||
u = ApcUrl('/{one}/{two}/three').params(two='testing')
|
||||
with self.assertRaises(IncompleteUrl): u.url()
|
||||
|
|
|
@ -1,26 +1,41 @@
|
|||
import unittest
|
||||
import json
|
||||
from mock import MagicMock, patch
|
||||
from api_clients.api_client import ApcUrl, ApiRequest
|
||||
from api_clients.utils import ApcUrl, ApiRequest
|
||||
|
||||
class ResponseInfo:
|
||||
def get_content_type(self):
|
||||
return 'application/json'
|
||||
@property
|
||||
def headers(self):
|
||||
return {'content-type': 'application/json'}
|
||||
|
||||
def json(self):
|
||||
return {'ok', 'ok'}
|
||||
|
||||
class TestApiRequest(unittest.TestCase):
|
||||
def test_init(self):
|
||||
u = ApiRequest('request_name', ApcUrl('/test/ing'))
|
||||
self.assertEqual(u.name, "request_name")
|
||||
|
||||
def test_call(self):
|
||||
ret = json.dumps( {'ok':'ok'} )
|
||||
def test_call_json(self):
|
||||
ret = {'ok':'ok'}
|
||||
read = MagicMock()
|
||||
read.read = MagicMock(return_value=ret)
|
||||
read.info = MagicMock(return_value=ResponseInfo())
|
||||
read.headers = {'content-type': 'application/json'}
|
||||
read.json = MagicMock(return_value=ret)
|
||||
u = 'http://localhost/testing'
|
||||
with patch('urllib.request.urlopen') as mock_method:
|
||||
with patch('requests.get') as mock_method:
|
||||
mock_method.return_value = read
|
||||
request = ApiRequest('mm', ApcUrl(u))()
|
||||
self.assertEqual(request, json.loads(ret))
|
||||
self.assertEqual(request, ret)
|
||||
|
||||
def test_call_html(self):
|
||||
ret = '<html><head></head><body></body></html>'
|
||||
read = MagicMock()
|
||||
read.headers = {'content-type': 'application/html'}
|
||||
read.text = MagicMock(return_value=ret)
|
||||
u = 'http://localhost/testing'
|
||||
with patch('requests.get') as mock_method:
|
||||
mock_method.return_value = read
|
||||
request = ApiRequest('mm', ApcUrl(u))()
|
||||
self.assertEqual(request.text(), ret)
|
||||
|
||||
if __name__ == '__main__': unittest.main()
|
||||
|
|
|
@ -2,7 +2,8 @@ import unittest
|
|||
import json
|
||||
from mock import patch, MagicMock
|
||||
from configobj import ConfigObj
|
||||
from api_clients.api_client import RequestProvider, api_config
|
||||
from api_clients.version1 import api_config
|
||||
from api_clients.utils import RequestProvider
|
||||
|
||||
class TestRequestProvider(unittest.TestCase):
|
||||
def setUp(self):
|
||||
|
@ -18,13 +19,17 @@ class TestRequestProvider(unittest.TestCase):
|
|||
self.assertTrue('general' in self.cfg)
|
||||
|
||||
def test_init(self):
|
||||
rp = RequestProvider(self.cfg)
|
||||
self.assertTrue( len( rp.available_requests() ) > 0 )
|
||||
rp = RequestProvider(self.cfg, {})
|
||||
self.assertEqual(len(rp.available_requests()), 0)
|
||||
|
||||
def test_contains(self):
|
||||
rp = RequestProvider(self.cfg)
|
||||
methods = ['upload_recorded', 'update_media_url', 'list_all_db_files']
|
||||
methods = {
|
||||
'upload_recorded': '/1/',
|
||||
'update_media_url': '/2/',
|
||||
'list_all_db_files': '/3/',
|
||||
}
|
||||
rp = RequestProvider(self.cfg, methods)
|
||||
for meth in methods:
|
||||
self.assertTrue( meth in rp.requests )
|
||||
self.assertTrue(meth in rp.requests)
|
||||
|
||||
if __name__ == '__main__': unittest.main()
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import unittest
|
||||
import datetime
|
||||
import configparser
|
||||
from api_clients.api_client import get_protocol
|
||||
import unittest
|
||||
from api_clients import utils
|
||||
|
||||
def get_force_ssl(value, useConfigParser):
|
||||
config = {}
|
||||
|
@ -10,12 +11,23 @@ def get_force_ssl(value, useConfigParser):
|
|||
'base_port': 80,
|
||||
'force_ssl': value,
|
||||
}
|
||||
return get_protocol(config)
|
||||
return utils.get_protocol(config)
|
||||
|
||||
|
||||
class TestTime(unittest.TestCase):
|
||||
def test_time_in_seconds(self):
|
||||
time = datetime.time(hour=0, minute=3, second=34, microsecond=649600)
|
||||
self.assertTrue(abs(utils.time_in_seconds(time) - 214.65) < 0.009)
|
||||
|
||||
def test_time_in_milliseconds(self):
|
||||
time = datetime.time(hour=0, minute=0, second=0, microsecond=500000)
|
||||
self.assertEqual(utils.time_in_milliseconds(time), 500)
|
||||
|
||||
|
||||
class TestGetProtocol(unittest.TestCase):
|
||||
def test_dict_config_empty_http(self):
|
||||
config = {'general': {}}
|
||||
protocol = get_protocol(config)
|
||||
protocol = utils.get_protocol(config)
|
||||
self.assertEqual(protocol, 'http')
|
||||
|
||||
def test_dict_config_http(self):
|
||||
|
@ -24,7 +36,7 @@ class TestGetProtocol(unittest.TestCase):
|
|||
'base_port': 80,
|
||||
},
|
||||
}
|
||||
protocol = get_protocol(config)
|
||||
protocol = utils.get_protocol(config)
|
||||
self.assertEqual(protocol, 'http')
|
||||
|
||||
def test_dict_config_https(self):
|
||||
|
@ -33,7 +45,7 @@ class TestGetProtocol(unittest.TestCase):
|
|||
'base_port': 443,
|
||||
},
|
||||
}
|
||||
protocol = get_protocol(config)
|
||||
protocol = utils.get_protocol(config)
|
||||
self.assertEqual(protocol, 'https')
|
||||
|
||||
def test_dict_config_force_https(self):
|
||||
|
@ -47,7 +59,7 @@ class TestGetProtocol(unittest.TestCase):
|
|||
def test_configparser_config_empty_http(self):
|
||||
config = configparser.ConfigParser()
|
||||
config['general'] = {}
|
||||
protocol = get_protocol(config)
|
||||
protocol = utils.get_protocol(config)
|
||||
self.assertEqual(protocol, 'http')
|
||||
|
||||
def test_configparser_config_http(self):
|
||||
|
@ -55,7 +67,7 @@ class TestGetProtocol(unittest.TestCase):
|
|||
config['general'] = {
|
||||
'base_port': 80,
|
||||
}
|
||||
protocol = get_protocol(config)
|
||||
protocol = utils.get_protocol(config)
|
||||
self.assertEqual(protocol, 'http')
|
||||
|
||||
def test_configparser_config_https(self):
|
||||
|
@ -63,7 +75,7 @@ class TestGetProtocol(unittest.TestCase):
|
|||
config['general'] = {
|
||||
'base_port': 443,
|
||||
}
|
||||
protocol = get_protocol(config)
|
||||
protocol = utils.get_protocol(config)
|
||||
self.assertEqual(protocol, 'https')
|
||||
|
||||
def test_configparser_config_force_https(self):
|
||||
|
@ -73,3 +85,5 @@ class TestGetProtocol(unittest.TestCase):
|
|||
self.assertEqual(get_force_ssl(value, True), 'https')
|
||||
for value in negative_values:
|
||||
self.assertEqual(get_force_ssl(value, True), 'http')
|
||||
|
||||
if __name__ == '__main__': unittest.main()
|
Loading…
Add table
Add a link
Reference in a new issue