class TestJOSEProcessJOSERequest(unittest.TestCase): def setUp(self): self._transport = JOSETransport() self._transport.verify_jwt_response = MagicMock() self._transport._build_jwt_signature = MagicMock() self._transport.content_hash_function = MagicMock() self._transport.decrypt_response = MagicMock() self._transport._encrypt_request = MagicMock() @patch('requests.get') def test_process_jose_request_success(self, requests_patch): requests_patch.return_value = MagicMock() self.assertIsInstance( self._transport._process_jose_request('GET', '/path', ANY), APIResponse) @patch('requests.post') @patch('launchkey.transports.jose_auth.json') def test_process_jose_request_data_success(self, requests_patch, json_patch): requests_patch.return_value = MagicMock() json_patch.return_value = MagicMock() self.assertIsInstance( self._transport._process_jose_request('POST', '/path', ANY, ANY), APIResponse) def test_process_jose_request_error_response(self): self._transport._http_client = MagicMock() self._transport._http_client.put.return_value = APIErrorResponse( ANY, ANY, ANY) with self.assertRaises(LaunchKeyAPIException): self.assertIsInstance( self._transport._process_jose_request('PUT', '/path', ANY), APIResponse)
class TestJOSEProcessJOSERequest(unittest.TestCase): def setUp(self): self._http_transport = MagicMock(spec=RequestsTransport) self._transport = JOSETransport(http_client=self._http_transport) self._transport.verify_jwt_response = MagicMock() self._transport._build_jwt_signature = MagicMock() self._transport.decrypt_response = MagicMock( return_value="Decrypted Response") self._transport._encrypt_request = MagicMock( return_value="Encrypted Response") @patch('requests.get') def test_process_jose_request_success_encrypted_response( self, requests_patch): requests_patch.return_value = MagicMock() response = self._transport._process_jose_request( 'GET', '/path', 'subject', 'body') self._transport.decrypt_response.assert_called_once() self.assertEqual(response.data, self._transport.decrypt_response.return_value) def test_process_jose_request_success_unencrypted_response(self): self._transport._http_client = MagicMock() self._transport._http_client.get.return_value = APIResponse( {"a": "response"}, {}, 200) response = self._transport._process_jose_request('GET', '/path', ANY) self._transport.decrypt_response.assert_not_called() self.assertIsInstance(response, APIResponse) self.assertEqual(response.data, {"a": "response"}) self.assertEqual(response.status_code, 200) def test_process_jose_request_success_empty_response(self): self._transport._http_client = MagicMock() self._transport._http_client.get.return_value = APIResponse( None, {}, 201) response = self._transport._process_jose_request( 'GET', '/path', 'subject') self._transport.decrypt_response.assert_not_called() self.assertIsInstance(response, APIResponse) self.assertIsNone(response.data) self.assertEqual(response.status_code, 201) @patch('launchkey.transports.jose_auth.json') def test_process_jose_request_data_json_success(self, json_patch): json_patch.return_value = "{\"json\": true}" response = self._transport._process_jose_request( 'POST', '/path', 'subject', 'data') json_patch.loads.assert_called_with( self._transport.decrypt_response.return_value) self.assertEqual(response.data, json_patch.loads.return_value) def test_process_jose_request_error_response(self): self._transport._http_client = MagicMock() self._transport._http_client.put.return_value = APIErrorResponse( ANY, ANY, ANY) with self.assertRaises(LaunchKeyAPIException): self._transport._process_jose_request('PUT', '/path', 'subject')
class TestJOSEProcessJOSERequest(unittest.TestCase): def setUp(self): self._http_transport = MagicMock(spec=RequestsTransport) self._transport = JOSETransport(http_client=self._http_transport) self._transport.verify_jwt_response = MagicMock() self._transport._build_jwt_signature = MagicMock() self._transport.decrypt_response = MagicMock(return_value="Decrypted Response") self._transport._encrypt_request = MagicMock(return_value="Encrypted Response") @patch('requests.get') def test_process_jose_request_success_encrypted_response(self, requests_patch): requests_patch.return_value = MagicMock() response = self._transport._process_jose_request('GET', '/path', 'subject', 'body') self._transport.decrypt_response.assert_called_once() self.assertEqual(response.data, self._transport.decrypt_response.return_value) def test_process_jose_request_success_unencrypted_response(self): self._transport._http_client = MagicMock() self._transport._http_client.get.return_value = APIResponse({"a": "response"}, {}, 200) response = self._transport._process_jose_request('GET', '/path', ANY) self._transport.decrypt_response.assert_not_called() self.assertIsInstance(response, APIResponse) self.assertEqual(response.data, {"a": "response"}) self.assertEqual(response.status_code, 200) def test_process_jose_request_success_empty_response(self): self._transport._http_client = MagicMock() self._transport._http_client.get.return_value = APIResponse(None, {}, 201) response = self._transport._process_jose_request('GET', '/path', 'subject') self._transport.decrypt_response.assert_not_called() self.assertIsInstance(response, APIResponse) self.assertIsNone(response.data) self.assertEqual(response.status_code, 201) @patch('launchkey.transports.jose_auth.json') def test_process_jose_request_data_json_success(self, json_patch): json_patch.return_value = "{\"json\": true}" response = self._transport._process_jose_request('POST', '/path', 'subject', 'data') json_patch.loads.assert_called_with(self._transport.decrypt_response.return_value) self.assertEqual(response.data, json_patch.loads.return_value) def test_process_jose_request_error_response(self): self._transport._http_client = MagicMock() self._transport._http_client.put.return_value = APIErrorResponse(ANY, ANY, ANY) with self.assertRaises(LaunchKeyAPIException): self._transport._process_jose_request('PUT', '/path', 'subject')