|
| 1 | +import os |
| 2 | +import unittest |
| 3 | +import pytest |
| 4 | +from mock import patch, MagicMock |
| 5 | +from connect.exceptions import ServerError |
| 6 | +from .common import Response, load_str |
| 7 | + |
| 8 | +from connect.config import Config |
| 9 | +from connect.resources.fulfillment import FulfillmentResource |
| 10 | + |
| 11 | +create_tier_account_request_body = load_str( |
| 12 | + os.path.join(os.path.dirname(__file__), 'data', 'create_tier_account_request_body.json')) |
| 13 | +create_tier_account_request_response = load_str( |
| 14 | + os.path.join(os.path.dirname(__file__), 'data', 'create_tier_account_request_response.json')) |
| 15 | +create_purchase_request_body = load_str( |
| 16 | + os.path.join(os.path.dirname(__file__), 'data', 'create_purchase_request_body.json')) |
| 17 | +create_purchase_request_response = load_str( |
| 18 | + os.path.join(os.path.dirname(__file__), 'data', 'create_purchase_request_response.json')) |
| 19 | + |
| 20 | + |
| 21 | +class FulfillmentRequest(unittest.TestCase): |
| 22 | + def setUp(self): |
| 23 | + self.config = Config(file='tests/config.json') |
| 24 | + |
| 25 | + def _get_bad_response(): |
| 26 | + return Response( |
| 27 | + ok=False, |
| 28 | + text='{}', |
| 29 | + status_code=404 |
| 30 | + ) |
| 31 | + |
| 32 | + @patch('requests.put') |
| 33 | + def test_update_param_asset_request(self, put_mock): |
| 34 | + request = FulfillmentResource(config=self.config) |
| 35 | + pr_id = 'PR-4405-9454-9305-001' |
| 36 | + test_data = { |
| 37 | + "params": [{ |
| 38 | + "id": "PM-9861-7949-8492-0001", |
| 39 | + "value": "32323323" |
| 40 | + }] |
| 41 | + } |
| 42 | + request.update_param_asset_request(pr_id, test_data, "i'm a note!") |
| 43 | + assert put_mock.call_count == 1 |
| 44 | + put_mock.assert_called_with( |
| 45 | + headers={'Content-Type': 'application/json', 'Authorization': 'ApiKey XXXX:YYYYY'}, |
| 46 | + timeout=300, |
| 47 | + url=('http://localhost:8080/api/public/v1/' |
| 48 | + 'requests/PR-4405-9454-9305-001/'), |
| 49 | + json={ |
| 50 | + 'asset': test_data, |
| 51 | + 'note': "i'm a note!", |
| 52 | + }) |
| 53 | + |
| 54 | + @patch('requests.post') |
| 55 | + def test_create_tier_account_request(self, post_mock): |
| 56 | + post_mock.return_value = Response(True, create_tier_account_request_response, 200) |
| 57 | + body = create_tier_account_request_body |
| 58 | + request = FulfillmentResource(config=self.config) |
| 59 | + tier_account_request = request.create_tier_account_request(body) |
| 60 | + post_mock.assert_called_with( |
| 61 | + headers={'Content-Type': 'application/json', 'Authorization': 'ApiKey XXXX:YYYYY'}, |
| 62 | + json=body, |
| 63 | + timeout=300, |
| 64 | + url='http://localhost:8080/api/public/v1/tier/account-requests') |
| 65 | + assert tier_account_request[0].id == 'TAR-6458-9737-0065-004-001' |
| 66 | + |
| 67 | + @patch('requests.get') |
| 68 | + def test_get_pending_tier_account(self, get_mock): |
| 69 | + get_mock.return_value = Response(True, create_tier_account_request_response, 200) |
| 70 | + request = FulfillmentResource(config=self.config) |
| 71 | + request.get_pending_tier_account_requests() |
| 72 | + get_mock.assert_called_with( |
| 73 | + headers={'Content-Type': 'application/json', 'Authorization': 'ApiKey XXXX:YYYYY'}, |
| 74 | + params={'status': 'pending'}, |
| 75 | + timeout=300, |
| 76 | + url='http://localhost:8080/api/public/v1/tier/account-requests') |
| 77 | + |
| 78 | + @patch('requests.get', MagicMock(return_value=_get_bad_response())) |
| 79 | + def test_search_pending_tier_account_bad(self): |
| 80 | + with pytest.raises(ServerError): |
| 81 | + request = FulfillmentResource(config=self.config) |
| 82 | + request.search_tier_account_requests(dict(status='pending')) |
| 83 | + |
| 84 | + @patch('requests.post') |
| 85 | + def test_create_purchase_request(self, post_mock): |
| 86 | + post_mock.return_value = Response(True, create_purchase_request_response, 200) |
| 87 | + body = create_purchase_request_body |
| 88 | + request = FulfillmentResource(config=self.config) |
| 89 | + purchase_request = request.create_purchase_request(body) |
| 90 | + post_mock.assert_called_with( |
| 91 | + headers={'Content-Type': 'application/json', 'Authorization': 'ApiKey XXXX:YYYYY'}, |
| 92 | + json=body, |
| 93 | + timeout=300, |
| 94 | + url='http://localhost:8080/api/public/v1/requests') |
| 95 | + assert purchase_request.id == 'PR-9301-9893-8624-001' |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + unittest.main() |
0 commit comments