def script_setup(parser): args = parser.parse_args() if any( getattr(args, name) != parser.get_default(name) for name in ('protocol', 'domain', 'version')) and args.url: parser.error('--url can not be used with ' '--protocol, --domain or --version') logging.basicConfig(level=args.log_level) if args.unsafe_certs: ssl_context = ssl._create_stdlib_context() else: ssl_context = None if args.url: session = Session(None, url_template=args.url, ssl_context=ssl_context) else: session = Session(protocol=args.protocol, domain=args.domain, api_version=args.version, ssl_context=ssl_context) return args, session
def test_request_with_api_key(self): session = Session('test', api_key='xyz') self.server.add(url='/ISSUE', params='method=GET&apiKey=xyz', response='{"data": "foo"}') actual = session.request('GET', '/ISSUE') compare(actual, expected='foo') self.server.assert_called(times=1)
def test_get_api_key(self): session = Session('test') self.server.add( url='/user', params='method=PUT&action=getApiKey&username=u&password=p', response='{"data": {"result": "xyz"}}') compare(session.get_api_key('u', 'p'), 'xyz') self.server.assert_called(times=1)
def test_delete(self): session = Session('test', api_key='xyz') self.server.add(url='/ISSUE', params='method=DELETE&apiKey=xyz', response='{"data": "foo"}') actual = session.delete('/ISSUE') compare(actual, expected='foo') self.server.assert_called(times=1)
def test_other_error(self): session = Session('test') self.server.add(url='/', params='method=GET', response=Exception('boom!')) with ShouldRaise(Exception('boom!')): session.get('/') self.server.assert_called(times=1)
def test_insecure_context(self): context = ssl._create_stdlib_context() session = Session('test', ssl_context=context) self.server.add(url='/login', params='method=GET', response='{"data": "foo"}', ssl_context=context) compare(session.get('/login'), expected='foo') self.server.assert_called(times=1)
def test_different_protocol_and_version(self): self.server.base_url = '' session = Session('test', protocol='http', api_version='v4.0') self.server.add( url='http://test.attask-ondemand.com/attask/api/v4.0/login', params='method=GET', response='{"data": "foo"}') compare(session.get('/login'), expected='foo') self.server.assert_called(times=1)
def test_get_api_key(self): session = Session('test') self.server.add( url='/user', params='method=PUT&action=getApiKey&username=u&password=p', response='{"data": {"result": "xyz"}}' ) compare(session.get_api_key('u', 'p'), 'xyz') self.server.assert_called(times=1)
def test_request_with_api_key(self): session = Session('test', api_key='xyz') self.server.add( url='/ISSUE', params='method=GET&apiKey=xyz', response='{"data": "foo"}' ) actual = session.request('GET', '/ISSUE') compare(actual, expected='foo') self.server.assert_called(times=1)
def test_different_url_template(self): self.server.base_url = '' session = Session('test', url_template=SANDBOX_TEMPLATE) self.server.add( url='https://test.attasksandbox.com/attask/api/unsupported/login', params='method=GET', response='{"data": "foo"}' ) compare(session.get('/login'), expected='foo') self.server.assert_called(times=1)
def test_other_error(self): session = Session('test') self.server.add( url='/', params='method=GET', response=Exception('boom!') ) with ShouldRaise(Exception('boom!')): session.get('/') self.server.assert_called(times=1)
def test_different_protocol_and_version(self): self.server.base_url = '' session = Session('test', protocol='http', api_version='v4.0') self.server.add( url='http://test.attask-ondemand.com/attask/api/v4.0/login', params='method=GET', response='{"data": "foo"}' ) compare(session.get('/login'), expected='foo') self.server.assert_called(times=1)
def test_delete(self): session = Session('test', api_key='xyz') self.server.add( url='/ISSUE', params='method=DELETE&apiKey=xyz', response='{"data": "foo"}' ) actual = session.delete('/ISSUE') compare(actual, expected='foo') self.server.assert_called(times=1)
def test_different_url_template(self): self.server.base_url = '' session = Session('test', url_template=SANDBOX_TEMPLATE) self.server.add( url= 'https://test.preview.workfront.com/attask/api/unsupported/login', params='method=GET', response='{"data": "foo"}') compare(session.get('/login'), expected='foo') self.server.assert_called(times=1)
def test_http_error(self): # somewhat hypothetical, error is usually in the return json session = Session('test') self.server.add( url='/login', params='method=GET', response=MockHTTPError('{"data": "foo"}', 500), ) with ShouldRaise(WorkfrontAPIError('Unknown error, check log', 500)): session.get('/login') self.server.assert_called(times=1)
def test_request_with_dodgy_absolute_url(self): session = Session('test') with ShouldRaise(TypeError( 'url not for this session: ' 'https://bad.example.com/attask/api/unsupported/some/url' )): session.request( 'GET', 'https://bad.example.com/attask/api/unsupported/some/url' )
def test_login(self): session = Session('test') self.server.add( url='/login', params='method=GET&username=u&password=p', response='{"data": {"sessionID": "x", "userID": "uid"}}') session.login('u', 'p') compare(session.session_id, 'x') compare(session.user_id, 'uid') self.server.assert_called(times=1) return session
def test_request_with_dodgy_absolute_url(self): session = Session('test') with ShouldRaise( TypeError( 'url not for this session: ' 'https://bad.example.com/attask/api/unsupported/some/url') ): session.request( 'GET', 'https://bad.example.com/attask/api/unsupported/some/url')
def test_insecure_context(self): context = ssl._create_stdlib_context() session = Session('test', ssl_context=context) self.server.add( url='/login', params='method=GET', response='{"data": "foo"}', ssl_context=context ) compare(session.get('/login'), expected='foo') self.server.assert_called(times=1)
def test_request_absolute_url(self): session = Session('test') self.server.add(url='/some/url', params='method=GET', response='{"data": "foo"}') actual = session.request( 'GET', 'https://test.attask-ondemand.com/attask/api/unsupported/some/url') compare(actual, expected='foo') self.server.assert_called(times=1)
def setUp(self): super(TestObjectHelper, self).setUp() self.replace('workfront.session.Session.version_registry', {}) test_api = APIVersion('test') class TestObject(Object): code='TEST' field_one = Field('fieldOne') field_two = Field('fieldTwo') test_api.register(TestObject) Session.register(test_api) self.session = Session('test', api_version='test') self.api = self.session.api
def test_login(self): session = Session('test') self.server.add( url='/login', params='method=GET&username=u&password=p', response='{"data": {"sessionID": "x", "userID": "uid"}}' ) session.login('u', 'p') compare(session.session_id, 'x') compare(session.user_id, 'uid') self.server.assert_called(times=1) return session
def test_basic_request(self): session = Session('test') self.server.add(url='/login', params='method=GET', response='{"data": "foo"}') compare(session.get('/login'), expected='foo') self.server.assert_called(times=1) self.log.check( ('workfront', 'INFO', "url: https://test.attask-ondemand.com/attask/api/unsupported" "/login " "params: {'method': 'GET'}"), ('workfront', 'DEBUG', 'returned: {\n "data": "foo"\n}'))
def setUp(self): super(TestObjectHelper, self).setUp() self.replace('workfront.session.Session.version_registry', {}) test_api = APIVersion('test') class TestObject(Object): code = 'TEST' field_one = Field('fieldOne') field_two = Field('fieldTwo') test_api.register(TestObject) Session.register(test_api) self.session = Session('test', api_version='test') self.api = self.session.api
def test_request_absolute_url(self): session = Session('test') self.server.add( url='/some/url', params='method=GET', response='{"data": "foo"}' ) actual = session.request( 'GET', 'https://test.attask-ondemand.com/attask/api/unsupported/some/url' ) compare(actual, expected='foo') self.server.assert_called(times=1)
def test_api_error(self): session = Session('test') self.server.add(url='/', params='method=GET', response='{"error":{' '"class":"java.lang.UnsupportedOperationException",' '"message":"you must specify an action"}}') with ShouldRaise( WorkfrontAPIError( { u'message': u'you must specify an action', u'class': u'java.lang.UnsupportedOperationException' }, 200)): session.get('/') self.server.assert_called(times=1)
def test_bad_json(self): session = Session('test') self.server.add(url='/', params='method=GET', response="{'oops': 'not json'}") with ShouldRaise( WorkfrontAPIError( { 'exception': 'Expecting property name enclosed in double ' 'quotes: line 1 column 2 (char 1)', 'response': u"{'oops': 'not json'}" }, 200)): session.get('/') self.server.assert_called(times=1)
def test_bad_json(self): session = Session('test') self.server.add( url='/', params='method=GET', response="{'oops': 'not json'}" ) with ShouldRaise(WorkfrontAPIError( {'exception': 'Expecting property name enclosed in double ' 'quotes: line 1 column 2 (char 1)', 'response': u"{'oops': 'not json'}"}, 200 )): session.get('/') self.server.assert_called(times=1)
def setUp(self): self.dir = TempDirectory() self.addCleanup(self.dir.cleanup) replace = Replacer() replace('workfront.generate.TARGET_ROOT', self.dir.path) self.addCleanup(replace.restore) self.session = Session('test')
def test_api_error(self): session = Session('test') self.server.add( url='/', params='method=GET', response='{"error":{' '"class":"java.lang.UnsupportedOperationException",' '"message":"you must specify an action"}}' ) with ShouldRaise(WorkfrontAPIError( {u'message': u'you must specify an action', u'class': u'java.lang.UnsupportedOperationException'}, 200 )): session.get('/') self.server.assert_called(times=1)
def test_basic_request(self): session = Session('test') self.server.add( url='/login', params='method=GET', response='{"data": "foo"}' ) compare(session.get('/login'), expected='foo') self.server.assert_called(times=1) self.log.check( ('workfront', 'INFO', "url: https://test.attask-ondemand.com/attask/api/unsupported" "/login " "params: {'method': 'GET'}"), ('workfront', 'DEBUG', 'returned: {\n "data": "foo"\n}') )
def test_request_with_params(self): session = Session('test') self.server.add( url='/endpoint', params='method=GET&str=svalue&unicode=uvalue&int=1&float=1.0&' 'dict={"key": "value"}', response='{"data": "foo"}' ) actual = session.request('GET', '/endpoint', params={ 'str': 'svalue', 'unicode': u'uvalue', 'int': 1, 'float': 1.0, 'dict': {'key': 'value'}, }) compare(actual, expected='foo') self.server.assert_called(times=1)
def test_save_no_dirty(self): TestObject = self.make_test_object() obj = TestObject(Session('test'), ID='x') # no post! obj.save() compare(obj.id, 'x') compare(obj.fields.dirty(), {})
def test_delete(self): TestObject = self.make_test_object() obj = TestObject(Session('test'), ID='x') self.server.add(url='/TEST/x', params='method=DELETE', response='{"data": null}') obj.delete() self.server.assert_called(times=1)
def test_request_with_params(self): session = Session('test') self.server.add( url='/endpoint', params='method=GET&str=svalue&unicode=uvalue&int=1&float=1.0&' 'dict={"key": "value"}', response='{"data": "foo"}') actual = session.request('GET', '/endpoint', params={ 'str': 'svalue', 'unicode': u'uvalue', 'int': 1, 'float': 1.0, 'dict': { 'key': 'value' }, }) compare(actual, expected='foo') self.server.assert_called(times=1)
def test_save_create(self): TestObject = self.make_test_object() obj = TestObject(Session('test'), field_one=1, field_two=2) self.server.add(url='/TEST', params='method=POST&fieldOne=1&fieldTwo=2', response='{"data":{"ID": "y"}}') obj.save() compare(obj.id, 'y') compare(obj.fields.dirty(), {})
def test_save_update(self): # some dirty, some not, check clean results TestObject = self.make_test_object() obj = TestObject(Session('test'), ID='x') obj.field_one = 1 self.server.add(url='/TEST/x', params='method=PUT&fieldOne=1', response='{"data": null}') obj.save() compare(obj.id, 'x') compare(obj.fields.dirty(), {})
def test_name_override(self): base_url = 'https://test.attask-ondemand.com/attask/api/unsupported' session = Session('test') self.server.add( url=base_url + '/metadata', params='method=GET', response=json.dumps( dict(data=dict(objects=dict( SomeThing=dict(objCode='OPTASK', name='SomeThing')))))) expected = dict(objCode='SMTHING', name='SomeThing', stuff='a value') self.server.add(url=base_url + '/optask/metadata', params='method=GET', response=json.dumps(dict(data=expected))) compare(decorated_object_types(session, None), expected=[('Issue', 'OPTASK', expected)])
def test_normal(self): base_url = 'https://test.attask-ondemand.com/attask/api/v4.0' session = Session('test', api_version='v4.0') self.server.add( url=base_url + '/metadata', params='method=GET', response=json.dumps( dict(data=dict(objects=dict( SomeThing=dict(objCode='SMTHING', name='SomeThing')))))) expected = dict(objCode='SMTHING', name='SomeThing', stuff='a value') self.server.add(url=base_url + '/smthing/metadata', params='method=GET', response=json.dumps(dict(data=expected))) compare(decorated_object_types(session, None), expected=[('SomeThing', 'SMTHING', expected)])
def test_load(self): TestObject = self.make_test_object() obj = TestObject(Session('test'), ID='x') obj.field_one = 1 obj.field_two = 2 compare(obj.fields.dirty(), {'fieldOne': 1, 'fieldTwo': 2}) self.server.add(url='/TEST/x', params='fields=fieldOne&method=GET', response='{"data":{"fieldOne": 3}}') obj.load('field_one') compare(obj.field_one, expected=3) compare(obj.fields.dirty(), {'fieldTwo': 2})
def test_cache_read(self): expected = dict(objCode='SMTHING', name='SomeThing', stuff='a value') self.dir.write( 'v4.0_metadata.json', json.dumps( dict(objects=dict( SomeThing=dict(objCode='SMTHING', name='SomeThing')))), encoding='ascii') self.dir.write('v4.0_smthing_metadata.json', json.dumps(expected), encoding='ascii') session = Session('test', api_version='v4.0') compare(decorated_object_types(session, self.dir.path), expected=[('SomeThing', 'SMTHING', expected)])
def test_cache_write(self): base_url = 'https://test.attask-ondemand.com/attask/api/v4.0' session = Session('test', api_version='v4.0') self.server.add( url=base_url + '/metadata', params='method=GET', response=json.dumps( dict(data=dict(objects=dict( SomeThing=dict(objCode='SMTHING', name='SomeThing')))))) expected = dict(objCode='SMTHING', name='SomeThing', stuff='a value') self.server.add(url=base_url + '/smthing/metadata', params='method=GET', response=json.dumps(dict(data=expected))) compare(decorated_object_types(session, self.dir.path), expected=[('SomeThing', 'SMTHING', expected)]) self.dir.compare( expected=['v4.0_metadata.json', 'v4.0_smthing_metadata.json']) compare(json.loads( self.dir.read('v4.0_metadata.json').decode('ascii')), expected=dict(objects=dict( SomeThing=dict(objCode='SMTHING', name='SomeThing')))) compare(json.loads( self.dir.read('v4.0_smthing_metadata.json').decode('ascii')), expected=expected)
def test_dots_in_version(self): path = prepare_target(Session('test', api_version='v4.0')) compare(path, expected=self.dir.getpath('v40.py')) self.dir.compare(expected=[])
def test_request(self): session = Session('test', url_template='http://127.0.0.1:8000') compare(session.request('GET', '/uri', {'foo': 'bar'}), expected='foo')
def test_warn_on_unknown_api(self): with ShouldWarn( UserWarning( 'No APIVersion for silly, only basic requests possible')): Session('test', api_version='silly')