Skip to content

Commit

Permalink
Bugfix for query-endpoints output formatting, add tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Caroline Russell <caroline@appthreat.dev>
  • Loading branch information
cerrussell committed Apr 4, 2024
1 parent 08d3894 commit 136433f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 17 deletions.
2 changes: 1 addition & 1 deletion atom_tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
A cli, classes and functions for converting an atom slice to a different format
"""
__version__ = '0.5.1'
__version__ = '0.5.2'
3 changes: 2 additions & 1 deletion atom_tools/cli/commands/query_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,5 @@ def handle(self):
line_filter = ()
if self.option('filter-lines'):
line_filter = get_ln_range(self.option('filter-lines'))
output_endpoints(result, self.option('sparse'), line_filter)
output = output_endpoints(result, self.option('sparse'), line_filter)
print(output)
32 changes: 19 additions & 13 deletions atom_tools/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,28 @@ def export_json(data: Dict, outfile: str, indent: int | None = None) -> None:
json.dump(data, f, indent=indent, sort_keys=True)


def output_endpoints(data: Dict, names_only: bool, line_range: Tuple[int, int] | Tuple) -> None:
def output_endpoints(data: Dict, sparse: bool, line_range: Tuple[int, int] | Tuple) -> str:
"""Outputs endpoints"""
to_print = ''
for endpoint, values in data.get('paths', {}).items():
to_print += f'{endpoint}'
usages = values.get("x-atom-usages", {}).get('call', {})
if names_only:
to_print += '\n'
continue
for k, v in usages.items():
for i in v:
if not line_range or line_range[0] <= i <= line_range[1]:
to_print += f':{k}:{i}'
break
to_print += '\n'
print(to_print)
if result := filter_endpoint_ln(endpoint, values, sparse, line_range):
to_print += f'{result}\n'
return to_print


def filter_endpoint_ln(ep: str, values: Dict, sparse: bool, ln_range: Tuple[int, int]) -> str:
"""Filters endpoint line numbers"""
to_print = ''
usages = values.get("x-atom-usages", {}).get('call', {})
for k, v in usages.items():
for i in v:
if not ln_range or ln_range[0] <= i <= ln_range[1]:
if sparse:
return f'{ep}'
to_print += f':{k}:{i}'
if to_print:
to_print = f'{ep}{to_print}'
return to_print


def remove_duplicates_list(obj: List[Dict]) -> List[Dict]:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "atom-tools"
version = "0.5.1"
version = "0.5.2"
description = "Collection of tools for use with AppThreat/atom."
authors = [
{ name = "Caroline Russell", email = "caroline@appthreat.dev" },
Expand Down
22 changes: 21 additions & 1 deletion test/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from atom_tools.lib.utils import add_params_to_cmd, remove_duplicates_list
from atom_tools.lib.utils import add_params_to_cmd, output_endpoints, remove_duplicates_list


def test_add_params_to_cmd():
Expand All @@ -18,3 +18,23 @@ def test_remove_duplicates_list():
{'code': None, 'function_name': ':program', 'line_number': 1},
{'code': 'app.ts::program:require', 'function_name': 'require', 'line_number': 6}
]


def test_output_endpoints():
data = {'openapi': '3.1.0', 'info': {'title': 'OpenAPI Specification for data', 'version': '1.0.0'}, 'paths': {'/uaa/users': {'post': {'responses': {}}, 'x-atom-usages': {'call': {'account-service/src/main/java/com/piggymetrics/account/client/AuthServiceClient.java': [12]}}}, '/statistics/{accountName}': {'parameters': [{'name': 'accountName', 'in': 'path', 'required': True}], 'put': {'responses': {}}, 'x-atom-usages': {'call': {'account-service/src/main/java/com/piggymetrics/account/client/StatisticsServiceClient.java': [13]}}}, '/current': {'put': {'responses': {}}, 'x-atom-usages': {'call': {'account-service/src/main/java/com/piggymetrics/account/controller/AccountController.java': [30, 25], 'auth-service/src/main/java/com/piggymetrics/auth/controller/UserController.java': [22], 'notification-service/src/main/java/com/piggymetrics/notification/controller/RecipientController.java': [26, 21], 'statistics-service/src/main/java/com/piggymetrics/statistics/controller/StatisticsController.java': [20]}, 'target': {'account-service/src/main/java/com/piggymetrics/account/controller/AccountController.java': [30], 'auth-service/src/main/java/com/piggymetrics/auth/controller/UserController.java': [22], 'notification-service/src/main/java/com/piggymetrics/notification/controller/RecipientController.java': [26]}}, 'get': {'responses': {}}}, '/': {'post': {'responses': {}}, 'x-atom-usages': {'call': {'account-service/src/main/java/com/piggymetrics/account/controller/AccountController.java': [35]}}}, '/{name}': {'parameters': [{'name': 'name', 'in': 'path', 'required': True}], 'get': {'responses': {}}, 'x-atom-usages': {'call': {'account-service/src/main/java/com/piggymetrics/account/controller/AccountController.java': [20]}}}, '/accounts/{accountName}': {'parameters': [{'name': 'accountName', 'in': 'path', 'required': True}], 'get': {'responses': {}}, 'x-atom-usages': {'call': {'notification-service/src/main/java/com/piggymetrics/notification/client/AccountServiceClient.java': [12]}}}, '/latest': {'get': {'responses': {}}, 'x-atom-usages': {'call': {'statistics-service/src/main/java/com/piggymetrics/statistics/client/ExchangeRatesClient.java': [13]}}}, '/{accountName}': {'parameters': [{'name': 'accountName', 'in': 'path', 'required': True}], 'put': {'responses': {}}, 'x-atom-usages': {'call': {'statistics-service/src/main/java/com/piggymetrics/statistics/controller/StatisticsController.java': [32, 26]}, 'target': {'statistics-service/src/main/java/com/piggymetrics/statistics/controller/StatisticsController.java': [32]}}, 'get': {'responses': {}}}}}
assert output_endpoints(data, False, ()) == ('/uaa/users:account-service/src/main/java/com/piggymetrics/account/client/AuthServiceClient.java:12\n'
'/statistics/{accountName}:account-service/src/main/java/com/piggymetrics/account/client/StatisticsServiceClient.java:13\n'
'/current:account-service/src/main/java/com/piggymetrics/account/controller/AccountController.java:30:account-service/src/main/java/com/piggymetrics/account/controller/AccountController.java:25:auth-service/src/main/java/com/piggymetrics/auth/controller/UserController.java:22:notification-service/src/main/java/com/piggymetrics/notification/controller/RecipientController.java:26:notification-service/src/main/java/com/piggymetrics/notification/controller/RecipientController.java:21:statistics-service/src/main/java/com/piggymetrics/statistics/controller/StatisticsController.java:20\n'
'/:account-service/src/main/java/com/piggymetrics/account/controller/AccountController.java:35\n'
'/{name}:account-service/src/main/java/com/piggymetrics/account/controller/AccountController.java:20\n'
'/accounts/{accountName}:notification-service/src/main/java/com/piggymetrics/notification/client/AccountServiceClient.java:12\n'
'/latest:statistics-service/src/main/java/com/piggymetrics/statistics/client/ExchangeRatesClient.java:13\n'
'/{accountName}:statistics-service/src/main/java/com/piggymetrics/statistics/controller/StatisticsController.java:32:statistics-service/src/main/java/com/piggymetrics/statistics/controller/StatisticsController.java:26\n')
assert output_endpoints(data, True, ()) == ('/uaa/users\n/statistics/{'
'accountName}\n/current\n/\n/{name}\n/accounts/{'
'accountName}\n/latest\n/{accountName}\n')
assert output_endpoints(data, False, (1, 14)) == ('/uaa/users:account-service/src/main/java/com/piggymetrics/account/client/AuthServiceClient.java:12\n'
'/statistics/{accountName}:account-service/src/main/java/com/piggymetrics/account/client/StatisticsServiceClient.java:13\n'
'/accounts/{accountName}:notification-service/src/main/java/com/piggymetrics/notification/client/AccountServiceClient.java:12\n'
'/latest:statistics-service/src/main/java/com/piggymetrics/statistics/client/ExchangeRatesClient.java:13\n')
assert output_endpoints(data, True, (30,30)) == '/current\n'

0 comments on commit 136433f

Please sign in to comment.