Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
response.reset_mock()
response.ok = False
do_http_things.__name__ = 'rput' # just for instance
self.assertRaises(pdpyras.PDClientError,
pdpyras.resource_envelope(do_http_things), my_self, '/services')
reset_mocks()
# GET /: use a different envelope name
response.ok = True
users_array = [{"type":"user","email":"user@example.com",
"summary":"User McUserson"}]
response.json.return_value = {'users': users_array}
do_http_things.__name__ = 'rget'
dummy_session.url = 'https://api.pagerduty.com'
self.assertEqual(users_array,
pdpyras.resource_envelope(do_http_things)(dummy_session, '/users',
query='user'))
reset_mocks()
# Test request body JSON envelope stuff in post/put
# Response body validation
do_http_things.__name__ = 'rpost'
user_payload = {'email':'user@example.com', 'name':'User McUserson'}
self.assertRaises(
pdpyras.PDClientError,
pdpyras.resource_envelope(do_http_things),
dummy_session, '/users', json=user_payload
)
reset_mocks()
# Add type property; should work now and automatically pack the user
# object into a JSON object inside the envelope.
user_payload['type'] = 'user'
"summary":"User McUserson"}]
response.json.return_value = {'users': users_array}
do_http_things.__name__ = 'rget'
dummy_session.url = 'https://api.pagerduty.com'
self.assertEqual(users_array,
pdpyras.resource_envelope(do_http_things)(dummy_session, '/users',
query='user'))
reset_mocks()
# Test request body JSON envelope stuff in post/put
# Response body validation
do_http_things.__name__ = 'rpost'
user_payload = {'email':'user@example.com', 'name':'User McUserson'}
self.assertRaises(
pdpyras.PDClientError,
pdpyras.resource_envelope(do_http_things),
dummy_session, '/users', json=user_payload
)
reset_mocks()
# Add type property; should work now and automatically pack the user
# object into a JSON object inside the envelope.
user_payload['type'] = 'user'
do_http_things.__name__ = 'rpost'
response.ok = True
created_user = user_payload.copy()
created_user['id'] = 'P456XYZ'
response.json.return_value = {'user':created_user}
self.assertEqual(
created_user,
pdpyras.resource_envelope(do_http_things)(dummy_session, '/users',
json=user_payload)
)
pdpyras.PDClientError,
pdpyras.resource_envelope(do_http_things),
dummy_session, '/users', json=user_payload
)
reset_mocks()
# Add type property; should work now and automatically pack the user
# object into a JSON object inside the envelope.
user_payload['type'] = 'user'
do_http_things.__name__ = 'rpost'
response.ok = True
created_user = user_payload.copy()
created_user['id'] = 'P456XYZ'
response.json.return_value = {'user':created_user}
self.assertEqual(
created_user,
pdpyras.resource_envelope(do_http_things)(dummy_session, '/users',
json=user_payload)
)
do_http_things.assert_called_with(dummy_session, '/users',
json={'user':user_payload})
reset_mocks()
# Test auto-envelope functionality for multi-update
# TODO: This test is loosely coupled but somewhat naive. Tighten if need
# be.
incidents = [{'id':'PABC123'}, {'id':'PDEF456'}]
do_http_things.__name__ = 'rput'
response.ok = True
updated_incidents = copy.deepcopy(incidents)
response.json.return_value = {'incidents': updated_incidents}
self.assertEqual(
updated_incidents,
do_http_things.return_value = response
my_self = pdpyras.APISession('some_key')
self.debug(my_self)
dummy_session = MagicMock()
def reset_mocks():
do_http_things.reset_mock()
response.reset_mock()
do_http_things.return_value = response
dummy_session.reset_mock()
# OK response, good JSON: JSON-decode and unpack response
response.ok = True
response.json.return_value = {'service': {'name': 'value'}}
do_http_things.__name__ = 'rput' # just for instance
self.assertEqual(
pdpyras.resource_envelope(do_http_things)(my_self,
'/services/PTHINGY'),
{'name': 'value'}
)
reset_mocks()
# OK response, bad JSON: raise exception.
response.ok = True
do_http_things.__name__ = 'rput' # just for instance
response.json.side_effect = [ValueError('Bad JSON!')]
self.assertRaises(pdpyras.PDClientError,
pdpyras.resource_envelope(do_http_things), my_self, '/services')
reset_mocks()
# OK response, but ruh-roh we hit an anti-pattern (probably won't exist
# except maybe in beta/reverse-engineered endpoints; this design is thus
# anticipatory rather than practical). Raise exception.
)
do_http_things.assert_called_with(dummy_session, '/users',
json={'user':user_payload})
reset_mocks()
# Test auto-envelope functionality for multi-update
# TODO: This test is loosely coupled but somewhat naive. Tighten if need
# be.
incidents = [{'id':'PABC123'}, {'id':'PDEF456'}]
do_http_things.__name__ = 'rput'
response.ok = True
updated_incidents = copy.deepcopy(incidents)
response.json.return_value = {'incidents': updated_incidents}
self.assertEqual(
updated_incidents,
pdpyras.resource_envelope(do_http_things)(dummy_session,
'/incidents', json=incidents)
)