def test_parse_invalid_http_response(self): """ should fail to parse invalid http response """ http_response = HttpResponse() response = comm.parse_http_response(http_response) self.assert_has_error_code(response, 'INVALID_REMOTE_RESPONSE') self.assertEqual(http_response, response.http_response)
def test_send_request_valid(self, request: MagicMock, parse_http_response: MagicMock): """Should successfully send request""" request.return_value = HttpResponse() parse_http_response.return_value = environ.Response('test') response = comm.send_request(endpoint='/fake', method='post', data=dict(a=1, b=2)) self.assertEqual(response.identifier, 'test')
def test_parse_valid_http_response(self): """Should fail to send request""" source_response = environ.Response().update(test='hello_world') def json_mock(*args, **kwargs): return source_response.serialize() http_response = HttpResponse() http_response.json = json_mock response = comm.parse_http_response(http_response) self.assertEqual(source_response.data['test'], response.data['test'])
def test_download(self, requests_get: MagicMock): """ should successfully download saved cauldron file """ def mock_iter_content(*args, **kwargs): yield from [b'a', b'b', b'', None, b'c'] http_response = HttpResponse() http_response.iter_content = mock_iter_content requests_get.return_value = http_response path = self.get_temp_path('failed_download', 'fake.filename') response = comm.download_file('fake.filename', path) self.assertTrue(response.success) self.assertTrue(os.path.exists(path)) with open(path, 'rb') as f: contents = f.read() self.assertEqual(contents, b'abc')