#!/usr/bin/env python3 """Tests for fetch_lambda_response_permissions.py, on the Service Reference and pages in the AWS Lambda API reference's markup.""" from __future__ import annotations import pytest import fetch_lambda_response_permissions as f def operation(name: str, *actions: str) -> dict: return {"Name": name, "AuthorizedActions": [{"Service": action.split(":")[0], "Name": action.split(":")[1]} for action in (f"lambda:{name}", *actions)]} REFERENCE = {"Operations": [ operation("GetFunction", "lambda:ListTags"), # An operation authorized by its own action alone has no page read, nor one the API reference does not list. operation("GetAlias"), operation("RunMicrovm", "lambda:GetMicrovmImage"), ]} INDEX = ('
') TAGS = ('
Tags
\n' '

The function\'s tags. Lambda\n returns tag data only if you have explicit allow ' 'permissions for lambda:ListTags.

Type: String to string map

') CONFIGURATION = ('
Configuration' '

The configuration of the function or version.

') def page(*elements: str, operation: str = "GetFunction") -> str: section = (f"
{''.join(elements)}
" if elements else "

If the action is successful, the service sends back an HTTP 204 response with an empty HTTP " "body.

") return (f'

{operation}

Response ' f'Elements

{section}

Errors

Errors.

' f'
') def test_a_response_element_returned_only_with_a_permission_is_read_with_its_action(): built = f.build(REFERENCE, INDEX, {"GetFunction": page(CONFIGURATION, TAGS)}) assert built["returnedOnlyIf"] == [{ "operation": "GetFunction", "element": "Tags", "sentence": "Lambda returns tag data only if you have explicit allow permissions for lambda:ListTags.", "action": "lambda:ListTags"}] assert f.operations(REFERENCE, INDEX) == ["GetFunction"] def test_a_page_whose_layout_or_wording_changed_is_refused(): with pytest.raises(ValueError, match="links to no operation"): f.operations(REFERENCE, "
") # A sentence AWS rewords is refused rather than dropping its action, however it says it. for reworded in (TAGS.replace("Lambda\n returns tag data", "Tags are returned"), TAGS.replace("only if you have", "only when you have"), TAGS.replace("Lambda\n returns tag data only if you have explicit allow permissions for", "Reading tags requires the permission")): with pytest.raises(ValueError, match="in no wording it used"): f.build(REFERENCE, INDEX, {"GetFunction": page(reworded)}) # A page whose response elements cannot be found is refused, not read as returning everything. with pytest.raises(ValueError, match="no response elements section"): f.build(REFERENCE, INDEX, {"GetFunction": page(TAGS).replace("_ResponseElements", "_Output")}) with pytest.raises(ValueError, match="lists no response element and does not say its response is empty"): f.build(REFERENCE, INDEX, {"GetFunction": page(TAGS).replace('
', "
")}) def test_a_page_saying_its_response_is_empty_returns_nothing_only_with_a_permission(): assert f.read_returned("GetFunction", page()) == [] def test_the_file_is_stale_when_the_reference_or_a_page_says_something_else(): pages = {"GetFunction": page(TAGS)} vendored = f.build(REFERENCE, INDEX, pages) assert not f.stale(vendored, REFERENCE, INDEX, pages) assert f.stale(vendored, REFERENCE, INDEX, {"GetFunction": page(TAGS.replace("lambda:ListTags", "lambda:GetTags"))})