import html import json import pytest import fetch_ec2_request_samples as f def block(text): return '
' + html.escape(text) + '
' REQUEST = '''https://ec2.amazonaws.com/?Action=CreateVpcPeeringConnection &VpcId=vpc-1a2b3c4d &PeerVpcId=vpc-a1b2c3d4 &PeerOwnerId=123456789012 &AUTHPARAMS''' RESPONSE = ''' 7a62c49f-347e-4fc4-9331-6e8eEXAMPLE pcx-73a5401a 777788889999vpc-1a2b3c4d 123456789012vpc-a1b2c3d4 ''' def page(request=REQUEST, response=RESPONSE): return block(request) + block(response) def test_pairs_a_sample_request_with_the_response_that_follows_it(): assert f.read_samples("CreateVpcPeeringConnection", page()) == [{ "request": {"VpcId": "vpc-1a2b3c4d", "PeerVpcId": "vpc-a1b2c3d4", "PeerOwnerId": "123456789012"}, "response": [ {"path": ["requestId"], "value": "7a62c49f-347e-4fc4-9331-6e8eEXAMPLE"}, {"path": ["vpcPeeringConnection", "vpcPeeringConnectionId"], "value": "pcx-73a5401a"}, {"path": ["vpcPeeringConnection", "requesterVpcInfo", "ownerId"], "value": "777788889999"}, {"path": ["vpcPeeringConnection", "requesterVpcInfo", "vpcId"], "value": "vpc-1a2b3c4d"}, {"path": ["vpcPeeringConnection", "accepterVpcInfo", "ownerId"], "value": "123456789012"}, {"path": ["vpcPeeringConnection", "accepterVpcInfo", "vpcId"], "value": "vpc-a1b2c3d4"}]}] def test_a_request_without_its_response_and_other_operations_are_left_out(): other = REQUEST.replace("Action=CreateVpcPeeringConnection", "Action=OtherAction") lone = REQUEST.replace("PeerOwnerId=123456789012", "PeerRegion=us-west-2") source = block(other) + block(RESPONSE) + block(lone) + page() assert f.read_samples("CreateVpcPeeringConnection", source) == \ f.read_samples("CreateVpcPeeringConnection", page()) @pytest.mark.parametrize("source", ["", block(REQUEST), block(RESPONSE)]) def test_a_page_with_no_paired_sample_fails_closed(source): with pytest.raises(ValueError, match="no sample request"): f.read_samples("CreateVpcPeeringConnection", source) def test_malformed_response_is_not_silently_omitted(): with pytest.raises(ValueError, match="malformed XML"): f.read_samples("CreateVpcPeeringConnection", page(response=RESPONSE.replace("", ""))) def test_verification_detects_changed_samples_and_ignores_retrieval_date(tmp_path, monkeypatch): output = tmp_path / "samples.json" monkeypatch.setattr(f, "OUTPUT", output) fetch = lambda url: page() assert f.main([], fetch) == 0 source = json.loads(output.read_text()) source["_source"]["retrieved"] = "2000-01-01" output.write_text(json.dumps(source)) assert f.main(["--verify"], fetch) == 0 assert f.main(["--verify"], lambda url: page(response=RESPONSE.replace("vpc-a1b2c3d4", "vpc-ffffffff"))) == 1 def test_source_declares_namespace_and_pages_for_shared_verification(): source = f.build({url: page() for url in f.PAGES.values()}) assert source["namespace"] == "ec2" assert source["_source"]["pages"] == list(f.PAGES.values()) source["namespace"] = "other" assert f.stale(source, {url: page() for url in f.PAGES.values()})