def test_gracefully_handles_errors(self, m: Mocker): m.post('/service', text=RESPONSE_ERROR_GENERIC, status_code=500) with self.assertRaises(piazza.ServerError): piazza.register_service( contract_url='test-contract-url', description='test-description', name='test-name', url='test-url', )
def test_throws_when_credentials_are_rejected(self, m: Mocker): m.post('/service', text=RESPONSE_AUTH_REJECTED, status_code=401) with self.assertRaises(piazza.Unauthorized): piazza.register_service( contract_url='test-contract-url', description='test-description', name='test-name', url='test-url', )
def test_throws_when_piazza_is_unreachable(self, _): with unittest.mock.patch('requests.post') as mock: mock.side_effect = ConnectionError(request=unittest.mock.Mock()) with self.assertRaises(piazza.Unreachable): piazza.register_service( contract_url='test-contract-url', description='test-description', name='test-name', url='test-url', )
def test_calls_correct_url(self, m: Mocker): m.post('/service', text=RESPONSE_SERVICE_REGISTERED, status_code=201) piazza.register_service( contract_url='test-contract-url', description='test-description', name='test-name', url='test-url', ) self.assertEqual('https://test-piazza-host.localdomain/service', m.request_history[0].url)
def test_throws_when_data_is_missing(self, m: Mocker): mangled_response = json.loads(RESPONSE_SERVICE_REGISTERED) mangled_response.pop('data') m.post('/service', json=mangled_response, status_code=201) with self.assertRaisesRegex(piazza.InvalidResponse, 'missing `data`'): piazza.register_service( contract_url='test-contract-url', description='test-description', name='test-name', url='test-url', )
def test_throws_when_response_is_malformed(self, m: Mocker): mangled_response = json.loads(RESPONSE_SERVICE_REGISTERED) mangled_response['data'] = 'lolwut' m.post('/service', json=mangled_response, status_code=201) with self.assertRaisesRegex(piazza.InvalidResponse, '`data` is of the wrong type'): piazza.register_service( contract_url='test-contract-url', description='test-description', name='test-name', url='test-url', )
def test_returns_service_id(self, m: Mocker): m.post('/service', text=RESPONSE_SERVICE_REGISTERED, status_code=201) service_id = piazza.register_service( contract_url='test-contract-url', description='test-description', name='test-name', url='test-url', ) self.assertEqual('test-service-id', service_id)
def test_sends_correct_payload(self, m: Mocker): m.post('/service', text=RESPONSE_SERVICE_REGISTERED, status_code=201) piazza.register_service( contract_url='test-contract-url', description='test-description', name='test-name', url='test-url', ) self.assertEqual( { 'url': 'test-url', 'contractUrl': 'test-contract-url', 'method': 'POST', 'timeout': 60, 'resourceMetadata': { 'name': 'test-name', 'description': 'test-description', 'version': '0.0', 'classType': { 'classification': 'Unclassified', }, }, }, m.request_history[0].json())