|
| 1 | +import pytest |
| 2 | + |
| 3 | +from marklogic.client import Client |
| 4 | + |
| 5 | +""" |
| 6 | +This module is intended for manual testing where the cloud_config fixture |
| 7 | +in conftest.py is modified to have a real API key and not "changeme" as a value. |
| 8 | +""" |
| 9 | + |
| 10 | +DEFAULT_BASE_PATH = "/ml/test/marklogic/manage" |
| 11 | + |
| 12 | + |
| 13 | +def test_base_path_doesnt_end_with_slash(cloud_config): |
| 14 | + if cloud_config["key"] == "changeme": |
| 15 | + return |
| 16 | + |
| 17 | + client = _new_client(cloud_config, DEFAULT_BASE_PATH) |
| 18 | + _verify_client_works(client) |
| 19 | + |
| 20 | + |
| 21 | +def test_base_path_ends_with_slash(cloud_config): |
| 22 | + if cloud_config["key"] == "changeme": |
| 23 | + return |
| 24 | + |
| 25 | + client = _new_client(cloud_config, DEFAULT_BASE_PATH + "/") |
| 26 | + _verify_client_works(client) |
| 27 | + |
| 28 | + |
| 29 | +def test_base_url_used_instead_of_host(cloud_config): |
| 30 | + if cloud_config["key"] == "changeme": |
| 31 | + return |
| 32 | + |
| 33 | + base_url = f"https://{cloud_config['host']}" |
| 34 | + client = Client( |
| 35 | + base_url, cloud_api_key=cloud_config["key"], base_path=DEFAULT_BASE_PATH |
| 36 | + ) |
| 37 | + _verify_client_works(client) |
| 38 | + |
| 39 | + |
| 40 | +def test_invalid_host(): |
| 41 | + with pytest.raises(ValueError) as err: |
| 42 | + Client( |
| 43 | + host="marklogic.com", |
| 44 | + cloud_api_key="doesnt-matter-for-this-test", |
| 45 | + base_path=DEFAULT_BASE_PATH, |
| 46 | + ) |
| 47 | + assert str(err.value).startswith( |
| 48 | + "Unable to generate token; status code: 403; cause: " |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def test_invalid_api_key(cloud_config): |
| 53 | + if cloud_config["key"] == "changeme": |
| 54 | + return |
| 55 | + |
| 56 | + with pytest.raises(ValueError) as err: |
| 57 | + Client( |
| 58 | + host=cloud_config["host"], |
| 59 | + cloud_api_key="invalid-api-key", |
| 60 | + base_path=DEFAULT_BASE_PATH, |
| 61 | + ) |
| 62 | + assert ( |
| 63 | + 'Unable to generate token; status code: 401; cause: {"statusCode":401,"errorMessage":"API Key is not valid."}' |
| 64 | + == str(err.value) |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +def _new_client(cloud_config, base_path: str) -> Client: |
| 69 | + return Client( |
| 70 | + host=cloud_config["host"], |
| 71 | + cloud_api_key=cloud_config["key"], |
| 72 | + base_path=base_path, |
| 73 | + ) |
| 74 | + |
| 75 | + |
| 76 | +def _verify_client_works(client): |
| 77 | + # Verify that the request works regardless of whether the path starts with a slash |
| 78 | + # or not. |
| 79 | + _verify_search_response(client.get("v1/search?format=json")) |
| 80 | + _verify_search_response(client.get("/v1/search?format=json")) |
| 81 | + |
| 82 | + |
| 83 | +def _verify_search_response(response): |
| 84 | + assert 200 == response.status_code |
| 85 | + assert 1 == response.json()["start"] |
0 commit comments