|
| 1 | +# This file is a part of globus-registered-api. |
| 2 | +# https://github.com/globusonline/globus-registered-api |
| 3 | +# Copyright 2025 Globus <support@globus.org> |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +from unittest.mock import patch |
| 7 | + |
| 8 | +import responses |
| 9 | + |
| 10 | +import globus_registered_api.cli |
| 11 | +from globus_registered_api.extended_flows_client import ExtendedFlowsClient |
| 12 | + |
| 13 | +from conftest import GET_REGISTERED_API_URL |
| 14 | + |
| 15 | + |
| 16 | +@patch("globus_registered_api.cli._create_flows_client") |
| 17 | +def test_get_registered_api_text_format(mock_create_client, cli_runner): |
| 18 | + mock_create_client.return_value = ExtendedFlowsClient() |
| 19 | + responses.add( |
| 20 | + responses.GET, |
| 21 | + GET_REGISTERED_API_URL, |
| 22 | + json={ |
| 23 | + "id": "abc-123-def-456", |
| 24 | + "name": "Test API", |
| 25 | + "description": "A test description", |
| 26 | + "created_timestamp": "2025-01-01T00:00:00+00:00", |
| 27 | + "updated_timestamp": "2025-01-02T00:00:00+00:00", |
| 28 | + }, |
| 29 | + ) |
| 30 | + |
| 31 | + result = cli_runner.invoke( |
| 32 | + globus_registered_api.cli.cli, ["get", "abc-123-def-456"] |
| 33 | + ) |
| 34 | + |
| 35 | + assert result.exit_code == 0 |
| 36 | + assert "ID:" in result.output |
| 37 | + assert "abc-123-def-456" in result.output |
| 38 | + assert "Name:" in result.output |
| 39 | + assert "Test API" in result.output |
| 40 | + assert "Description:" in result.output |
| 41 | + assert "A test description" in result.output |
| 42 | + assert "Created:" in result.output |
| 43 | + assert "Updated:" in result.output |
| 44 | + |
| 45 | + |
| 46 | +@patch("globus_registered_api.cli._create_flows_client") |
| 47 | +def test_get_registered_api_json_format(mock_create_client, cli_runner): |
| 48 | + mock_create_client.return_value = ExtendedFlowsClient() |
| 49 | + responses.add( |
| 50 | + responses.GET, |
| 51 | + GET_REGISTERED_API_URL, |
| 52 | + json={ |
| 53 | + "id": "abc-123-def-456", |
| 54 | + "name": "Test API", |
| 55 | + "description": "A test description", |
| 56 | + "created_timestamp": "2025-01-01T00:00:00+00:00", |
| 57 | + "updated_timestamp": "2025-01-02T00:00:00+00:00", |
| 58 | + }, |
| 59 | + ) |
| 60 | + |
| 61 | + result = cli_runner.invoke( |
| 62 | + globus_registered_api.cli.cli, ["get", "abc-123-def-456", "--format", "json"] |
| 63 | + ) |
| 64 | + |
| 65 | + assert result.exit_code == 0 |
| 66 | + assert '"id": "abc-123-def-456"' in result.output |
| 67 | + assert '"name": "Test API"' in result.output |
| 68 | + assert '"description": "A test description"' in result.output |
| 69 | + |
| 70 | + |
| 71 | +@patch("globus_registered_api.cli._create_flows_client") |
| 72 | +def test_get_registered_api_empty_description(mock_create_client, cli_runner): |
| 73 | + mock_create_client.return_value = ExtendedFlowsClient() |
| 74 | + responses.add( |
| 75 | + responses.GET, |
| 76 | + GET_REGISTERED_API_URL, |
| 77 | + json={ |
| 78 | + "id": "abc-123-def-456", |
| 79 | + "name": "Minimal API", |
| 80 | + "description": "", |
| 81 | + "created_timestamp": "2025-01-01T00:00:00+00:00", |
| 82 | + "updated_timestamp": "2025-01-01T00:00:00+00:00", |
| 83 | + }, |
| 84 | + ) |
| 85 | + |
| 86 | + result = cli_runner.invoke( |
| 87 | + globus_registered_api.cli.cli, ["get", "abc-123-def-456"] |
| 88 | + ) |
| 89 | + |
| 90 | + assert result.exit_code == 0 |
| 91 | + assert "Minimal API" in result.output |
| 92 | + assert "Description:" in result.output |
| 93 | + |
| 94 | + |
| 95 | +@patch("globus_registered_api.cli._create_flows_client") |
| 96 | +def test_get_registered_api_calls_correct_endpoint(mock_create_client, cli_runner): |
| 97 | + mock_create_client.return_value = ExtendedFlowsClient() |
| 98 | + api_id = "12345678-1234-1234-1234-123456789abc" |
| 99 | + responses.add( |
| 100 | + responses.GET, |
| 101 | + GET_REGISTERED_API_URL, |
| 102 | + json={ |
| 103 | + "id": api_id, |
| 104 | + "name": "Test", |
| 105 | + "description": "", |
| 106 | + "created_timestamp": "2025-01-01T00:00:00+00:00", |
| 107 | + "updated_timestamp": "2025-01-01T00:00:00+00:00", |
| 108 | + }, |
| 109 | + ) |
| 110 | + |
| 111 | + cli_runner.invoke(globus_registered_api.cli.cli, ["get", api_id]) |
| 112 | + |
| 113 | + assert f"/registered_apis/{api_id}" in responses.calls[0].request.url |
| 114 | + |
| 115 | + |
| 116 | +@patch("globus_registered_api.cli._create_flows_client") |
| 117 | +def test_get_registered_api_not_found(mock_create_client, cli_runner): |
| 118 | + mock_create_client.return_value = ExtendedFlowsClient() |
| 119 | + api_id = "12345678-1234-1234-1234-123456789abc" |
| 120 | + responses.add( |
| 121 | + responses.GET, |
| 122 | + GET_REGISTERED_API_URL, |
| 123 | + status=404, |
| 124 | + json={ |
| 125 | + "error": { |
| 126 | + "code": "NOT_FOUND", |
| 127 | + "detail": f"No Registered API exists with id value {api_id}", |
| 128 | + } |
| 129 | + }, |
| 130 | + ) |
| 131 | + |
| 132 | + result = cli_runner.invoke(globus_registered_api.cli.cli, ["get", api_id]) |
| 133 | + |
| 134 | + assert result.exit_code != 0 |
| 135 | + assert "No Registered API exists" in str(result.exception) |
0 commit comments