|
| 1 | +''' Tests for Billing API client. ''' |
| 2 | + |
| 3 | +# pylint: disable=E1120,W0621 |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from livechat.billing.base import BillingApi |
| 8 | +from livechat.config import CONFIG |
| 9 | + |
| 10 | +billing_url = CONFIG.get('billing_url') |
| 11 | +billing_version = CONFIG.get('billing_version') |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def billing_api_client(): |
| 16 | + ''' Fixture returning Billing API client. ''' |
| 17 | + return BillingApi.get_client(token='test') |
| 18 | + |
| 19 | + |
| 20 | +def test_get_client_without_args(): |
| 21 | + ''' Test if TypeError raised without args. ''' |
| 22 | + with pytest.raises(TypeError) as exception: |
| 23 | + BillingApi.get_client() |
| 24 | + assert str( |
| 25 | + exception.value |
| 26 | + ) == "get_client() missing 1 required positional argument: 'token'" |
| 27 | + |
| 28 | + |
| 29 | +def test_get_client_without_access_token(): |
| 30 | + ''' Test if TypeError raised without access_token. ''' |
| 31 | + with pytest.raises(TypeError) as exception: |
| 32 | + BillingApi.get_client(version='test') |
| 33 | + assert str( |
| 34 | + exception.value |
| 35 | + ) == "get_client() missing 1 required positional argument: 'token'" |
| 36 | + |
| 37 | + |
| 38 | +def test_get_client_with_non_existing_version(): |
| 39 | + ''' Test if ValueError raised for non-existing version. ''' |
| 40 | + with pytest.raises(ValueError) as exception: |
| 41 | + BillingApi.get_client(token='test', version='test') |
| 42 | + assert str(exception.value) == 'Provided version does not exist.' |
| 43 | + |
| 44 | + |
| 45 | +def test_get_client_with_valid_args(billing_api_client): |
| 46 | + ''' Test if production API URL is used and token is added to headers for valid args. ''' |
| 47 | + assert billing_api_client.api_url == f'https://{billing_url}/v{billing_version}' |
| 48 | + assert billing_api_client.session.headers.get('Authorization') == 'test' |
| 49 | + |
| 50 | + |
| 51 | +def test_send_request(billing_api_client): |
| 52 | + ''' Test if it's possible to send a basic request via Billing API |
| 53 | + client with arbitrary chosen method. ''' |
| 54 | + assert billing_api_client.create_direct_charge().json() == { |
| 55 | + 'error': |
| 56 | + 'invalid_request', |
| 57 | + 'error_description': |
| 58 | + 'The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed.' |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | +def test_modify_header(billing_api_client): |
| 63 | + ''' Test if Billing API object header can be updated with custom value. ''' |
| 64 | + assert 'test' not in billing_api_client.get_headers() |
| 65 | + billing_api_client.modify_header({'test': '1234'}) |
| 66 | + assert 'test' in billing_api_client.get_headers() |
| 67 | + |
| 68 | + |
| 69 | +def test_remove_header(billing_api_client): |
| 70 | + ''' Test if header can be removed from Billing API object. ''' |
| 71 | + billing_api_client.modify_header({'test2': '1234'}) |
| 72 | + assert 'test2' in billing_api_client.get_headers() |
| 73 | + billing_api_client.remove_header('test2') |
| 74 | + assert 'test2' not in billing_api_client.get_headers() |
| 75 | + |
| 76 | + |
| 77 | +def test_custom_headers_within_the_request(billing_api_client): |
| 78 | + ''' Test if custom headers can be added to the session headers |
| 79 | + only within the particular request. ''' |
| 80 | + headers = {'x-test': 'enabled'} |
| 81 | + response = billing_api_client.create_direct_charge(headers=headers) |
| 82 | + assert headers.items() <= response.request.headers.items() |
| 83 | + assert 'x-test' not in billing_api_client.get_headers() |
| 84 | + |
| 85 | + |
| 86 | +def test_client_supports_http_1(): |
| 87 | + ''' Test if client supports HTTP/1.1 protocol. ''' |
| 88 | + client = BillingApi.get_client(token='test') |
| 89 | + assert client.create_direct_charge().http_version == 'HTTP/1.1' |
0 commit comments