Skip to content

Feature: Eventbridge v2: Add connection for api endpoint #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions .test_durations
Original file line number Diff line number Diff line change
Expand Up @@ -843,21 +843,21 @@
"tests/aws/services/events/test_events_inputs.py::TestInputTransformer::test_put_events_with_input_transformer_input_template_string[\"{[/Check with special starting characters for event of <detail-type> type\"]": 0.11936591799985763,
"tests/aws/services/events/test_events_inputs.py::TestInputTransformer::test_put_events_with_input_transformer_missing_keys": 0.0006529859997499443,
"tests/aws/services/events/test_events_inputs.py::test_put_event_input_path_and_input_transfomer": 0.0006545790001837304,
"tests/aws/services/events/test_events_integrations.py::test_put_events_with_target_firehose": 0.07406386900083817,
"tests/aws/services/events/test_events_integrations.py::test_put_events_with_target_kinesis": 0.9582597469998291,
"tests/aws/services/events/test_events_integrations.py::test_put_events_with_target_lambda": 4.074284712000008,
"tests/aws/services/events/test_events_integrations.py::test_put_events_with_target_lambda_list_entries_partial_match": 4.080728459999591,
"tests/aws/services/events/test_events_integrations.py::test_put_events_with_target_lambda_list_entry": 4.072307003000333,
"tests/aws/services/events/test_events_integrations.py::test_put_events_with_target_sns[domain]": 0.05766030499989938,
"tests/aws/services/events/test_events_integrations.py::test_put_events_with_target_sns[path]": 0.058427959999335144,
"tests/aws/services/events/test_events_integrations.py::test_put_events_with_target_sns[standard]": 0.06193175200041878,
"tests/aws/services/events/test_events_integrations.py::test_put_events_with_target_sqs": 0.05069734699964101,
"tests/aws/services/events/test_events_integrations.py::test_put_events_with_target_sqs_event_detail_match": 5.064694446999965,
"tests/aws/services/events/test_events_integrations.py::test_put_events_with_target_sqs_new_region": 0.021846191999884468,
"tests/aws/services/events/test_events_integrations.py::test_should_ignore_schedules_for_put_event": 11.069308752000325,
"tests/aws/services/events/test_events_integrations.py::test_trigger_event_on_ssm_change[domain]": 0.038843789999646106,
"tests/aws/services/events/test_events_integrations.py::test_trigger_event_on_ssm_change[path]": 0.040668131999609614,
"tests/aws/services/events/test_events_integrations.py::test_trigger_event_on_ssm_change[standard]": 0.041024010999535676,
"tests/aws/services/events/test_events_targets.py::test_put_events_with_target_firehose": 0.07406386900083817,
"tests/aws/services/events/test_events_targets.py::test_put_events_with_target_kinesis": 0.9582597469998291,
"tests/aws/services/events/test_events_targets.py::test_put_events_with_target_lambda": 4.074284712000008,
"tests/aws/services/events/test_events_targets.py::test_put_events_with_target_lambda_list_entries_partial_match": 4.080728459999591,
"tests/aws/services/events/test_events_targets.py::test_put_events_with_target_lambda_list_entry": 4.072307003000333,
"tests/aws/services/events/test_events_targets.py::test_put_events_with_target_sns[domain]": 0.05766030499989938,
"tests/aws/services/events/test_events_targets.py::test_put_events_with_target_sns[path]": 0.058427959999335144,
"tests/aws/services/events/test_events_targets.py::test_put_events_with_target_sns[standard]": 0.06193175200041878,
"tests/aws/services/events/test_events_targets.py::test_put_events_with_target_sqs": 0.05069734699964101,
"tests/aws/services/events/test_events_targets.py::test_put_events_with_target_sqs_event_detail_match": 5.064694446999965,
"tests/aws/services/events/test_events_targets.py::test_put_events_with_target_sqs_new_region": 0.021846191999884468,
"tests/aws/services/events/test_events_targets.py::test_should_ignore_schedules_for_put_event": 11.069308752000325,
"tests/aws/services/events/test_events_targets.py::test_trigger_event_on_ssm_change[domain]": 0.038843789999646106,
"tests/aws/services/events/test_events_targets.py::test_trigger_event_on_ssm_change[path]": 0.040668131999609614,
"tests/aws/services/events/test_events_targets.py::test_trigger_event_on_ssm_change[standard]": 0.041024010999535676,
"tests/aws/services/events/test_events_rules.py::test_put_event_with_content_base_rule_in_pattern": 3.05817666799976,
"tests/aws/services/events/test_events_rules.py::test_put_events_with_rule_anything_but_to_sqs": 5.094898402000126,
"tests/aws/services/events/test_events_rules.py::test_put_events_with_rule_exists_false_to_sqs": 5.073232123999787,
Expand Down
18 changes: 18 additions & 0 deletions tests/aws/services/events/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,21 @@ def _add_resource_policy_logs_events_access(log_group_arn: str):

for policy_name in policies:
aws_client.logs.delete_resource_policy(policyName=policy_name)


@pytest.fixture
def events_create_connection(aws_client):
connections = []

def _create_connection(**kwargs):
response = aws_client.events.create_connection(**kwargs)
connections.append(kwargs["Name"])
return response

yield _create_connection

for connection in connections:
try:
aws_client.events.delete_connection(Name=connection)
except Exception as e:
LOG.warning(f"Failed to delete connection {connection}: {e}")
11 changes: 11 additions & 0 deletions tests/aws/services/events/helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,14 @@ def collect_events() -> None:
retry(collect_events, retries=retries, sleep=0.01)

return events


def events_connection_wait_for_deleted(aws_client, connection_name: str) -> None:
def _wait_for_deleted():
try:
aws_client.events.describe_connection(Name=connection_name)
except aws_client.events.exceptions.ResourceNotFoundException:
return
raise AssertionError(f"Connection {connection_name} was not deleted")

retry(_wait_for_deleted, retries=3, sleep=1)
250 changes: 249 additions & 1 deletion tests/aws/services/events/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from localstack.utils.sync import poll_condition, retry
from tests.aws.services.events.conftest import assert_valid_event
from tests.aws.services.events.helper_functions import (
events_connection_wait_for_deleted,
is_old_provider,
is_v2_provider,
sqs_collect_messages,
Expand Down Expand Up @@ -83,6 +84,14 @@
}
}

API_CONNECTION_TEST_PASSWORD = "test_pw_1984%&"
API_CONNECTION_TEST_USERNAME = "test_user_1984"

API_CONNECTION_TEST_KEY_NAME = "test_key_1984"
API_CONNECTION_TEST_KEY_VALUE = "test_value_1984"

OAUTH_PLAYGROUND_URL = "https://oauth.pstmn.io/v1/callback"


class TestEvents:
@markers.aws.validated
Expand Down Expand Up @@ -592,7 +601,7 @@ def test_list_event_buses_with_prefix(self, create_event_bus, aws_client, snapsh
bus_name = f"unique-prefix-1234567890-{short_uid()}"
snapshot.add_transformer(snapshot.transform.regex(bus_name, "<bus-name>"))

bus_name_not_match = "no-prefix-match"
bus_name_not_match = f"no-prefix-match-{short_uid()}"
snapshot.add_transformer(snapshot.transform.regex(bus_name_not_match, "<bus-name>"))

create_event_bus(Name=bus_name)
Expand Down Expand Up @@ -1347,3 +1356,242 @@ def test_put_target_id_validation(
{"Id": target_id, "Arn": queue_arn, "InputPath": "$.detail"},
],
)


class TestConnection:
@markers.aws.validated
@pytest.mark.parametrize("auth_type", ["BASIC", "OAUTH_CLIENT_CREDENTIALS", "API_KEY"])
@pytest.mark.parametrize("invocation_parameters", [True, False])
def test_create_list_describe_update_delete_connection(
self,
auth_type,
invocation_parameters,
events_create_connection,
aws_client,
snapshot,
):
# Specify auth parameters
if auth_type == "BASIC":
auth_parameters = {
"BasicAuthParameters": {
"Username": API_CONNECTION_TEST_USERNAME,
"Password": API_CONNECTION_TEST_PASSWORD,
}
}
if auth_type == "OAUTH_CLIENT_CREDENTIALS":
auth_parameters = {
"OAuthParameters": {
"ClientParameters": {"ClientID": "string", "ClientSecret": "string"},
"AuthorizationEndpoint": OAUTH_PLAYGROUND_URL,
"HttpMethod": "PUT", # TODO test "GET" | "POST"
"OAuthHttpParameters": {
"HeaderParameters": [
{
"Key": "Content_Type",
"Value": "'application/x-www-form-urlencoded'",
"IsValueSecret": False,
},
],
"QueryStringParameters": [
{
"Key": "some_test_key",
"Value": "some_test_value",
"IsValueSecret": False,
},
],
"BodyParameters": [
{"Key": "grant_type", "Value": "string", "IsValueSecret": True},
{
"Key": "user",
"Value": API_CONNECTION_TEST_USERNAME,
"IsValueSecret": True,
},
{
"Key": "password",
"Value": API_CONNECTION_TEST_PASSWORD,
"IsValueSecret": True,
},
],
},
}
}
if auth_type == "API_KEY":
auth_parameters = {
"ApiKeyAuthParameters": {
"ApiKeyName": API_CONNECTION_TEST_KEY_NAME,
"ApiKeyValue": API_CONNECTION_TEST_KEY_VALUE,
}
}

# Specify invocation parameters
if invocation_parameters:
invocation_http_parameters = {
"HeaderParameters": [
{"Key": "string", "Value": "string", "IsValueSecret": True},
],
"QueryStringParameters": [
{"Key": "string", "Value": "string", "IsValueSecret": False},
],
"BodyParameters": [
{"Key": "string", "Value": "string", "IsValueSecret": True},
],
}
auth_parameters["InvocationHttpParameters"] = invocation_http_parameters

# Test create connection
connection_name = f"test-connection-{short_uid()}"
response = events_create_connection(
Name=connection_name,
Description="test description",
AuthorizationType=auth_type,
AuthParameters=auth_parameters,
)
connection_arn = response["ConnectionArn"]
connection_arn_id = connection_arn.split("/")[-1]

snapshot.add_transformers_list(
[
snapshot.transform.regex(connection_name, "<connection-name>"),
snapshot.transform.regex(connection_arn_id, "<connection-arn-id>"),
]
)
snapshot.match("create-connection", response)

response = aws_client.events.list_connections(NamePrefix=connection_name)
snapshot.match("list-connections", response)

response = aws_client.events.describe_connection(
Name=connection_name,
)
secret_arn = response["SecretArn"]
secret_arn_id = secret_arn.split("/")[-1]

snapshot.add_transformer(snapshot.transform.regex(secret_arn_id, "<secret-arn-id>"))
snapshot.match("describe-connection", response)

response = aws_client.events.delete_connection(Name=connection_name)
snapshot.match("delete-connection", response)

events_connection_wait_for_deleted(aws_client, connection_name)

response = aws_client.events.list_connections(NamePrefix=connection_name)
snapshot.match("list-connections-after-delete", response)

@markers.aws.validated
def test_list_connections_with_prefix(self, events_create_connection, aws_client, snapshot):
events = aws_client.events
connection_name = f"unique-prefix-1234567890-{short_uid()}"
snapshot.add_transformer(snapshot.transform.regex(connection_name, "<connection-name-a>"))

connection_name_not_match = f"no-prefix-match-{short_uid()}"
snapshot.add_transformer(
snapshot.transform.regex(connection_name_not_match, "<connection-name-b>")
)

response = events_create_connection(
Name=connection_name,
Description="test description",
AuthorizationType="BASIC",
AuthParameters={
"BasicAuthParameters": {
"Username": API_CONNECTION_TEST_USERNAME,
"Password": API_CONNECTION_TEST_PASSWORD,
}
},
)
connection_arn = response["ConnectionArn"]
connection_arn_id = connection_arn.split("/")[-1]
snapshot.add_transformer(snapshot.transform.regex(connection_arn_id, "<connection-arn-id>"))

events_create_connection(
Name=connection_name_not_match,
Description="test description",
AuthorizationType="BASIC",
AuthParameters={
"BasicAuthParameters": {
"Username": API_CONNECTION_TEST_USERNAME,
"Password": API_CONNECTION_TEST_PASSWORD,
}
},
)

response = events.list_connections(NamePrefix=connection_name)
snapshot.match("list-connections-prefix-complete-name", response)

response = events.list_connections(NamePrefix=connection_name.split("-")[0])
snapshot.match("list-connections-prefix", response)

@markers.aws.validated
def test_list_connections_with_limit(self, events_create_connection, aws_client, snapshot):
snapshot.add_transformer(snapshot.transform.jsonpath("$..NextToken", "next_token"))
connection_name_prefix = f"test-connection-{short_uid()}"
snapshot.add_transformer(
snapshot.transform.regex(connection_name_prefix, "<connection-name-prefix>")
)
count = 6

for i in range(count):
connection_name = f"{connection_name_prefix}-{i}"
response = events_create_connection(
Name=connection_name,
Description="test description",
AuthorizationType="BASIC",
AuthParameters={
"BasicAuthParameters": {
"Username": API_CONNECTION_TEST_USERNAME,
"Password": API_CONNECTION_TEST_PASSWORD,
}
},
)
connection_arn = response["ConnectionArn"]
connection_arn_id = connection_arn.split("/")[-1]
snapshot.add_transformer(
snapshot.transform.regex(connection_arn_id, "<connection-arn-id>")
)

response = aws_client.events.list_connections(
Limit=int(count / 2), NamePrefix=connection_name_prefix
)
snapshot.match("list-connections-limit", response)

response = aws_client.events.list_connections(
Limit=int(count / 2) + 2,
NextToken=response["NextToken"],
NamePrefix=connection_name_prefix,
)
snapshot.match("list-connections-limit-next-token", response)

@markers.aws.validated
def test_list_connection_with_state(self, events_create_connection, aws_client, snapshot):
connection_name = f"test-connection-{short_uid()}"
response = events_create_connection(
Name=connection_name,
Description="test description",
AuthorizationType="BASIC",
AuthParameters={
"BasicAuthParameters": {
"Username": API_CONNECTION_TEST_USERNAME,
"Password": API_CONNECTION_TEST_PASSWORD,
}
},
)
connection_arn = response["ConnectionArn"]
connection_arn_id = connection_arn.split("/")[-1]
snapshot.add_transformers_list(
[
snapshot.transform.regex(connection_arn_id, "<connection-arn-id>"),
snapshot.transform.regex(connection_name, "<connection-name>"),
]
)

def _wait_for_active():
response = aws_client.events.describe_connection(Name=connection_name)
return response["ConnectionState"] == "AUTHORIZED"

retry(_wait_for_active, retries=5, sleep=1)

response = aws_client.events.list_connections(ConnectionState="AUTHORIZED")
snapshot.match("list-connections-state", response)

response = aws_client.events.list_connections(ConnectionState="CREATING")
snapshot.match("list-connections-state-empty", response)
Loading