mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 14:10:49 -05:00
* fix(cognito): reject token validity outside AWS's limits CreateUserPoolClient and UpdateUserPoolClient accepted any positive token validity, so a client with one-minute access and ID tokens, or a 20-minute refresh token, worked locally and failed on deploy. AWS allows access and ID tokens between 5 minutes and 1 day and refresh tokens between 60 minutes and 3,650 days, in each token's TokenValidityUnits unit (hours for access and ID tokens, days for refresh tokens, by default). A client outside them now fails with InvalidParameterException "Invalid range for token validity.", the message AWS returns. floci.services.cognito.enforce-token-validity-limits (env FLOCI_SERVICES_COGNITO_ENFORCE_TOKEN_VALIDITY_LIMITS, default true) turns the check off for tests that want tokens shorter than AWS allows. * test(scheduler): start recurring test schedules in the future Since #5610 a rate schedule's first occurrence is its creation time, so a schedule is due the moment it exists. The application's own dispatcher keeps ticking every 10 seconds during @QuarkusTest and sees every schedule, so a tick that lands between CreateSchedule and the test's own dispatcher fires the schedule once more than the test expects. Four tests create an enabled rate(1 minute) schedule and count exact deliveries: recurringScheduleStartsOneExecutionPerOccurrence, stepFunctionsDeleteScheduleStopsFutureOccurrence, rateScheduleDeliversEachOccurrenceWithItsScheduledTime and executionIdsDifferBetweenOccurrences. They now create the schedule through SchedulerTestHarness.createRecurringSchedule, which sets a StartDate a day ahead, and tick past it on their scoped dispatcher. The application's dispatcher skips a schedule before its StartDate, and the test profile and its tick interval stay as they are. * test(compat): poll OpenSearch domain readiness every 250 ms against Floci OpenSearchTest waited for the domain to finish processing in its own loop that slept 5 s between checks and swallowed InterruptedException. Floci flips Processing to false on its own 3 s readiness poll, so the test could sit up to 5 s past the moment the domain was ready. The wait now goes through TestFixtures.await, polling every 250 ms against Floci and keeping the 5 s cadence against real AWS, with the same 3 minute timeout. * fix(opensearch): run domain containers under an init process so a stop ends them at once The OpenSearch image's entrypoint is a bash script that starts the server as its child instead of exec'ing it. Running as PID 1, that script ignores SIGTERM, so every DeleteDomain, and every shutdown for each domain, waited out the whole 5 s stop timeout before Docker sent SIGKILL. The server never saw the SIGTERM either way. Domain containers now run under Docker's init process, which takes the SIGTERM and ends the container at once. Stopping a ready 2.11.1 container took 5.27 s (exit 137) without it and 0.26 s (exit 143) with it, and DeleteDomain in the compatibility OpenSearchTest took 5.3 s. The two startDomain tests' shared setup moves into one helper, which the new init test uses too. * fix(cognito): keep a RefreshTokenValidity of 0 at 30 days in any unit AWS replaces a RefreshTokenValidity of 0 with "the default value of 30 days". Floci stored the bare number 30 in the client's refresh token unit, so a client with RefreshToken=minutes issued 30-minute refresh tokens. With the token validity limits, any later UpdateUserPoolClient that left the validity alone re-checked that stored 30 minutes and failed with "Invalid range for token validity.", for a client Floci had accepted. A 0 now becomes 30 days in the client's refresh token unit (2592000 seconds, 43200 minutes, 720 hours or 30 days), on create and on update, and the limits check runs on that normalized value. Also imports UUID on a test line that spelled it out in full. * fix(cognito): reset the settings UpdateUserPoolClient omits to their defaults AWS: "If you don't provide a value for an attribute, Amazon Cognito sets it to its default value." The developer guide adds that an update with one parameter "sets all others to a default value". Floci kept the stored value of every setting the request left out, so a client could end up with a combination AWS never produces: an AccessTokenValidity of 60 kept from a minutes client, read in the hours of a units-only update, was refused as out of range, where AWS resets the validity and accepts. CreateUserPoolClient and UpdateUserPoolClient now build their settings in one place, with a new client's default for each one the request omits, and the update copies them onto the stored client. ClientName is the exception: the API documents no default for it, so an update without one keeps the name. The id, the secrets, the dates and the branding are not settings and stay as they are. An explicitly empty list still clears the setting. The AuthSessionValidity tests and the boto3 compatibility test that asserted the old keep-on-omit behaviour now expect the 3-minute default. * test: share one self-signed and one user-cert TLS profile across the TLS integration tests CustomDomainTlsIntegrationTest, TlsEnsureHostIntegrationTest, TlsHttp2HostRoutingIntegrationTest and EcrTlsDataPlaneDockerIntegrationTest each declared a self-signed TLS profile whose static block wrote its own CA-issued leaf before boot, and under @QuarkusTest that leaf is the one served. They now share SelfSignedTlsProfile, which writes one leaf for all four: the SANs TlsConfigSource uses by default, without 127.0.0.1, plus the API Gateway custom domain the HTTP/2 test routes to. The custom domain and ensure-host tests connect by IP and expect a name the leaf doesn't cover yet to be refused, and the JDK falls back to the peer address when the SNI name doesn't match, so a 127.0.0.1 SAN would let those handshakes through. The leaf and its metadata are written fresh every run, which also drops hostnames an earlier run learned at runtime, and the container CA bundle the ECR test relied on is written beside them. TlsIntegrationTest and TlsUserCertIntegrationTest declared the same user-provided certificate setup apart from paths. They share UserCertTlsProfile, which keeps the certificate file carrying its private key, since TlsUserCertIntegrationTest checks /_floci/ca.pem never echoes it. Both profiles live in the config test package, beside every class that uses them and the package-private default SAN list, and expose their paths as compile-time constants, so a test can read them without initializing the profile class and rerunning its static block under the running application. Sharing an application also means no class may leave anything behind for the next. TlsHttp2HostRoutingIntegrationTest now removes the bucket, CloudFront public key, key group and distribution, and the REST API, domain name and base path mapping it creates. EcrTlsDataPlaneDockerIntegrationTest deletes the manifest it pushes, TlsIntegrationTest its queue and TlsUserCertIntegrationTest its parameter. CustomDomainTlsIntegrationTest and TlsEnsureHostIntegrationTest already end with a state reset, which keeps the configured names from the profile's metadata, so neither changes the leaf a later class is served. The ECR test's registry container also outlived every run: keep-running-on-shutdown defaults to true and the test config never overrides it, and the next application to start adopted the container by name, a developer's own Floci registry included. SelfSignedTlsProfile now names it in the run's sidecar namespace, which SidecarContainersProfile exposes for this, and stops it at shutdown. The test checks for Docker through SidecarContainersProfile.requireDockerAndImage, so a registry a killed run left behind is removed like any other abandoned sidecar. Part of #4542. --------- Co-authored-by: Priamo <pgermosen@gmail.com>
609 lines
25 KiB
Python
609 lines
25 KiB
Python
"""Cognito Identity Provider integration tests."""
|
|
|
|
import boto3
|
|
import pytest
|
|
from botocore.config import Config
|
|
|
|
|
|
class TestCognitoUserPool:
|
|
"""Test Cognito user pool operations."""
|
|
|
|
def test_create_user_pool(self, cognito_client, unique_name):
|
|
"""Test CreateUserPool creates a pool."""
|
|
pool_name = f"pytest-pool-{unique_name}"
|
|
|
|
try:
|
|
response = cognito_client.create_user_pool(PoolName=pool_name)
|
|
pool_id = response["UserPool"]["Id"]
|
|
assert pool_id
|
|
finally:
|
|
cognito_client.delete_user_pool(UserPoolId=response["UserPool"]["Id"])
|
|
|
|
def test_delete_user_pool(self, cognito_client, unique_name):
|
|
"""Test DeleteUserPool removes pool."""
|
|
pool_name = f"pytest-pool-{unique_name}"
|
|
|
|
response = cognito_client.create_user_pool(PoolName=pool_name)
|
|
pool_id = response["UserPool"]["Id"]
|
|
|
|
cognito_client.delete_user_pool(UserPoolId=pool_id)
|
|
# If no exception, test passes
|
|
|
|
|
|
class TestCognitoUserPoolClient:
|
|
"""Test Cognito user pool client operations."""
|
|
|
|
@pytest.mark.parametrize("minutes", [3, 10, 15])
|
|
def test_auth_session_validity_sdk_round_trip(self, cognito_client, unique_name, minutes):
|
|
"""The SDK must send and parse the configured session duration."""
|
|
pool_id = cognito_client.create_user_pool(
|
|
PoolName=f"session-validity-{unique_name}"
|
|
)["UserPool"]["Id"]
|
|
try:
|
|
created = cognito_client.create_user_pool_client(
|
|
UserPoolId=pool_id, ClientName="session-client", AuthSessionValidity=minutes
|
|
)["UserPoolClient"]
|
|
client_id = created["ClientId"]
|
|
assert created["AuthSessionValidity"] == minutes
|
|
assert cognito_client.describe_user_pool_client(
|
|
UserPoolId=pool_id, ClientId=client_id
|
|
)["UserPoolClient"]["AuthSessionValidity"] == minutes
|
|
|
|
updated_minutes = 15 if minutes != 15 else 3
|
|
updated = cognito_client.update_user_pool_client(
|
|
UserPoolId=pool_id, ClientId=client_id, AuthSessionValidity=updated_minutes
|
|
)["UserPoolClient"]
|
|
assert updated["AuthSessionValidity"] == updated_minutes
|
|
assert cognito_client.describe_user_pool_client(
|
|
UserPoolId=pool_id, ClientId=client_id
|
|
)["UserPoolClient"]["AuthSessionValidity"] == updated_minutes
|
|
|
|
omitted = cognito_client.update_user_pool_client(
|
|
UserPoolId=pool_id, ClientId=client_id, ClientName="renamed"
|
|
)["UserPoolClient"]
|
|
# As on AWS, an update that omits a setting resets it to its default.
|
|
assert omitted["AuthSessionValidity"] == 3
|
|
finally:
|
|
cognito_client.delete_user_pool(UserPoolId=pool_id)
|
|
|
|
def test_auth_session_validity_sdk_default(self, cognito_client, unique_name):
|
|
"""Omitting the duration creates a client with the 3-minute default."""
|
|
pool_id = cognito_client.create_user_pool(
|
|
PoolName=f"session-default-{unique_name}"
|
|
)["UserPool"]["Id"]
|
|
try:
|
|
created = cognito_client.create_user_pool_client(
|
|
UserPoolId=pool_id, ClientName="default-client"
|
|
)["UserPoolClient"]
|
|
assert created["AuthSessionValidity"] == 3
|
|
assert cognito_client.describe_user_pool_client(
|
|
UserPoolId=pool_id, ClientId=created["ClientId"]
|
|
)["UserPoolClient"]["AuthSessionValidity"] == 3
|
|
finally:
|
|
cognito_client.delete_user_pool(UserPoolId=pool_id)
|
|
|
|
@pytest.mark.parametrize(
|
|
"minutes,constraint",
|
|
[
|
|
(2, "Member must have value greater than or equal to 3"),
|
|
(16, "Member must have value less than or equal to 15"),
|
|
],
|
|
)
|
|
def test_auth_session_validity_sdk_constraint_errors(
|
|
self, aws_config, client_config, unique_name, minutes, constraint
|
|
):
|
|
"""Disable SDK validation so the server's constraint errors reach the SDK."""
|
|
client = boto3.client(
|
|
"cognito-idp",
|
|
config=client_config.merge(Config(parameter_validation=False)),
|
|
**aws_config,
|
|
)
|
|
pool_id = None
|
|
try:
|
|
message = (
|
|
f"1 validation error detected: Value '{minutes}' at 'authSessionValidity' "
|
|
f"failed to satisfy constraint: {constraint}"
|
|
)
|
|
with pytest.raises(client.exceptions.InvalidParameterException) as missing:
|
|
client.create_user_pool_client(
|
|
UserPoolId="us-east-1_missing",
|
|
ClientName="missing-pool-client",
|
|
AuthSessionValidity=minutes,
|
|
)
|
|
assert missing.value.response["Error"]["Message"] == message
|
|
|
|
pool_id = client.create_user_pool(
|
|
PoolName=f"session-constraint-{unique_name}"
|
|
)["UserPool"]["Id"]
|
|
created = client.create_user_pool_client(
|
|
UserPoolId=pool_id, ClientName="session-client", AuthSessionValidity=10
|
|
)["UserPoolClient"]
|
|
with pytest.raises(client.exceptions.InvalidParameterException) as rejected:
|
|
client.update_user_pool_client(
|
|
UserPoolId=pool_id,
|
|
ClientId=created["ClientId"],
|
|
ClientName="changed",
|
|
AuthSessionValidity=minutes,
|
|
)
|
|
assert rejected.value.response["Error"]["Message"] == message
|
|
stored = client.describe_user_pool_client(
|
|
UserPoolId=pool_id, ClientId=created["ClientId"]
|
|
)["UserPoolClient"]
|
|
assert stored["AuthSessionValidity"] == 10
|
|
assert stored["ClientName"] == "session-client"
|
|
finally:
|
|
try:
|
|
if pool_id is not None:
|
|
client.delete_user_pool(UserPoolId=pool_id)
|
|
finally:
|
|
client.close()
|
|
|
|
def test_create_user_pool_client(self, cognito_client, unique_name):
|
|
"""Test CreateUserPoolClient creates a client."""
|
|
pool_name = f"pytest-pool-{unique_name}"
|
|
client_name = f"pytest-client-{unique_name}"
|
|
|
|
response = cognito_client.create_user_pool(PoolName=pool_name)
|
|
pool_id = response["UserPool"]["Id"]
|
|
|
|
try:
|
|
response = cognito_client.create_user_pool_client(
|
|
UserPoolId=pool_id, ClientName=client_name
|
|
)
|
|
client_id = response["UserPoolClient"]["ClientId"]
|
|
assert client_id
|
|
finally:
|
|
cognito_client.delete_user_pool(UserPoolId=pool_id)
|
|
|
|
def test_delete_user_pool_client(self, cognito_client, unique_name):
|
|
"""Test DeleteUserPoolClient removes client."""
|
|
pool_name = f"pytest-pool-{unique_name}"
|
|
client_name = f"pytest-client-{unique_name}"
|
|
|
|
pool_response = cognito_client.create_user_pool(PoolName=pool_name)
|
|
pool_id = pool_response["UserPool"]["Id"]
|
|
|
|
client_response = cognito_client.create_user_pool_client(
|
|
UserPoolId=pool_id, ClientName=client_name
|
|
)
|
|
client_id = client_response["UserPoolClient"]["ClientId"]
|
|
|
|
try:
|
|
cognito_client.delete_user_pool_client(
|
|
UserPoolId=pool_id, ClientId=client_id
|
|
)
|
|
# If no exception, test passes
|
|
finally:
|
|
cognito_client.delete_user_pool(UserPoolId=pool_id)
|
|
|
|
|
|
class TestCognitoUser:
|
|
"""Test Cognito user operations."""
|
|
|
|
def test_admin_create_user(self, cognito_client, unique_name):
|
|
"""Test AdminCreateUser creates a user."""
|
|
pool_name = f"pytest-pool-{unique_name}"
|
|
username = f"pytest-user-{unique_name}"
|
|
|
|
pool_response = cognito_client.create_user_pool(PoolName=pool_name)
|
|
pool_id = pool_response["UserPool"]["Id"]
|
|
|
|
try:
|
|
response = cognito_client.admin_create_user(
|
|
UserPoolId=pool_id,
|
|
Username=username,
|
|
UserAttributes=[{"Name": "email", "Value": "pytest@example.com"}],
|
|
)
|
|
assert response["User"]["Username"] == username
|
|
finally:
|
|
cognito_client.admin_delete_user(UserPoolId=pool_id, Username=username)
|
|
cognito_client.delete_user_pool(UserPoolId=pool_id)
|
|
|
|
def test_admin_delete_user(self, cognito_client, unique_name):
|
|
"""Test AdminDeleteUser removes user."""
|
|
pool_name = f"pytest-pool-{unique_name}"
|
|
username = f"pytest-user-{unique_name}"
|
|
|
|
pool_response = cognito_client.create_user_pool(PoolName=pool_name)
|
|
pool_id = pool_response["UserPool"]["Id"]
|
|
|
|
cognito_client.admin_create_user(
|
|
UserPoolId=pool_id,
|
|
Username=username,
|
|
UserAttributes=[{"Name": "email", "Value": "pytest@example.com"}],
|
|
)
|
|
|
|
try:
|
|
cognito_client.admin_delete_user(UserPoolId=pool_id, Username=username)
|
|
# If no exception, test passes
|
|
finally:
|
|
cognito_client.delete_user_pool(UserPoolId=pool_id)
|
|
|
|
|
|
class TestCognitoAuth:
|
|
"""Test Cognito authentication operations."""
|
|
|
|
def test_admin_initiate_auth(self, cognito_client, unique_name):
|
|
"""Test AdminInitiateAuth returns tokens."""
|
|
pool_name = f"pytest-pool-{unique_name}"
|
|
client_name = f"pytest-client-{unique_name}"
|
|
username = f"pytest-user-{unique_name}"
|
|
|
|
pool_response = cognito_client.create_user_pool(PoolName=pool_name)
|
|
pool_id = pool_response["UserPool"]["Id"]
|
|
|
|
client_response = cognito_client.create_user_pool_client(
|
|
UserPoolId=pool_id,
|
|
ClientName=client_name,
|
|
ExplicitAuthFlows=["ALLOW_ADMIN_USER_PASSWORD_AUTH"],
|
|
)
|
|
client_id = client_response["UserPoolClient"]["ClientId"]
|
|
|
|
cognito_client.admin_create_user(
|
|
UserPoolId=pool_id,
|
|
Username=username,
|
|
UserAttributes=[{"Name": "email", "Value": "pytest@example.com"}],
|
|
)
|
|
cognito_client.admin_set_user_password(
|
|
UserPoolId=pool_id, Username=username, Password="Pytest#1234", Permanent=True
|
|
)
|
|
|
|
try:
|
|
response = cognito_client.admin_initiate_auth(
|
|
UserPoolId=pool_id,
|
|
ClientId=client_id,
|
|
AuthFlow="ADMIN_NO_SRP_AUTH",
|
|
AuthParameters={"USERNAME": username, "PASSWORD": "Pytest#1234"},
|
|
)
|
|
access_token = response["AuthenticationResult"]["AccessToken"]
|
|
assert access_token
|
|
finally:
|
|
cognito_client.admin_delete_user(UserPoolId=pool_id, Username=username)
|
|
cognito_client.delete_user_pool_client(
|
|
UserPoolId=pool_id, ClientId=client_id
|
|
)
|
|
cognito_client.delete_user_pool(UserPoolId=pool_id)
|
|
|
|
def test_get_user(self, cognito_client, unique_name):
|
|
"""Test GetUser returns user details from access token."""
|
|
pool_name = f"pytest-pool-{unique_name}"
|
|
client_name = f"pytest-client-{unique_name}"
|
|
username = f"pytest-user-{unique_name}"
|
|
|
|
pool_response = cognito_client.create_user_pool(PoolName=pool_name)
|
|
pool_id = pool_response["UserPool"]["Id"]
|
|
|
|
client_response = cognito_client.create_user_pool_client(
|
|
UserPoolId=pool_id,
|
|
ClientName=client_name,
|
|
ExplicitAuthFlows=["ALLOW_ADMIN_USER_PASSWORD_AUTH"],
|
|
)
|
|
client_id = client_response["UserPoolClient"]["ClientId"]
|
|
|
|
cognito_client.admin_create_user(
|
|
UserPoolId=pool_id,
|
|
Username=username,
|
|
UserAttributes=[{"Name": "email", "Value": "pytest@example.com"}],
|
|
)
|
|
cognito_client.admin_set_user_password(
|
|
UserPoolId=pool_id, Username=username, Password="Pytest#1234", Permanent=True
|
|
)
|
|
|
|
auth_response = cognito_client.admin_initiate_auth(
|
|
UserPoolId=pool_id,
|
|
ClientId=client_id,
|
|
AuthFlow="ADMIN_NO_SRP_AUTH",
|
|
AuthParameters={"USERNAME": username, "PASSWORD": "Pytest#1234"},
|
|
)
|
|
access_token = auth_response["AuthenticationResult"]["AccessToken"]
|
|
|
|
try:
|
|
response = cognito_client.get_user(AccessToken=access_token)
|
|
assert response["Username"] == username
|
|
finally:
|
|
cognito_client.admin_delete_user(UserPoolId=pool_id, Username=username)
|
|
cognito_client.delete_user_pool_client(
|
|
UserPoolId=pool_id, ClientId=client_id
|
|
)
|
|
cognito_client.delete_user_pool(UserPoolId=pool_id)
|
|
|
|
|
|
class TestCognitoLogDeliveryConfiguration:
|
|
"""Test Cognito log delivery configuration operations.
|
|
|
|
``LogConfigurations`` is always present on the response, as ``[]`` when nothing
|
|
is configured, and ``SetLogDeliveryConfiguration`` replaces the list rather than
|
|
merging into it.
|
|
"""
|
|
|
|
LOG_GROUP_ARN = "arn:aws:logs:us-east-1:000000000000:log-group:pytest-cognito-logs"
|
|
|
|
@pytest.fixture
|
|
def pool_id(self, cognito_client, unique_name):
|
|
response = cognito_client.create_user_pool(PoolName=f"pytest-log-pool-{unique_name}")
|
|
pool_id = response["UserPool"]["Id"]
|
|
yield pool_id
|
|
cognito_client.delete_user_pool(UserPoolId=pool_id)
|
|
|
|
def test_get_returns_an_empty_list_before_anything_is_configured(
|
|
self, cognito_client, pool_id
|
|
):
|
|
"""An unconfigured pool still returns the LogConfigurations member."""
|
|
config = cognito_client.get_log_delivery_configuration(UserPoolId=pool_id)[
|
|
"LogDeliveryConfiguration"
|
|
]
|
|
|
|
assert config["UserPoolId"] == pool_id
|
|
assert config["LogConfigurations"] == []
|
|
|
|
def test_set_round_trips_and_replaces(self, cognito_client, pool_id):
|
|
"""Set stores the configuration, and a second Set replaces rather than merges."""
|
|
cognito_client.set_log_delivery_configuration(
|
|
UserPoolId=pool_id,
|
|
LogConfigurations=[
|
|
{
|
|
"LogLevel": "ERROR",
|
|
"EventSource": "userNotification",
|
|
"CloudWatchLogsConfiguration": {"LogGroupArn": self.LOG_GROUP_ARN},
|
|
}
|
|
],
|
|
)
|
|
|
|
config = cognito_client.get_log_delivery_configuration(UserPoolId=pool_id)[
|
|
"LogDeliveryConfiguration"
|
|
]
|
|
assert len(config["LogConfigurations"]) == 1
|
|
assert config["LogConfigurations"][0]["EventSource"] == "userNotification"
|
|
assert (
|
|
config["LogConfigurations"][0]["CloudWatchLogsConfiguration"]["LogGroupArn"]
|
|
== self.LOG_GROUP_ARN
|
|
)
|
|
|
|
cognito_client.set_log_delivery_configuration(
|
|
UserPoolId=pool_id,
|
|
LogConfigurations=[
|
|
{
|
|
"LogLevel": "INFO",
|
|
"EventSource": "userAuthEvents",
|
|
"CloudWatchLogsConfiguration": {"LogGroupArn": self.LOG_GROUP_ARN},
|
|
}
|
|
],
|
|
)
|
|
replaced = cognito_client.get_log_delivery_configuration(UserPoolId=pool_id)[
|
|
"LogDeliveryConfiguration"
|
|
]
|
|
assert len(replaced["LogConfigurations"]) == 1
|
|
assert replaced["LogConfigurations"][0]["EventSource"] == "userAuthEvents"
|
|
|
|
def test_empty_list_clears_the_configuration(self, cognito_client, pool_id):
|
|
"""An empty LogConfigurations clears what was stored."""
|
|
cognito_client.set_log_delivery_configuration(
|
|
UserPoolId=pool_id,
|
|
LogConfigurations=[
|
|
{
|
|
"LogLevel": "ERROR",
|
|
"EventSource": "userNotification",
|
|
"CloudWatchLogsConfiguration": {"LogGroupArn": self.LOG_GROUP_ARN},
|
|
}
|
|
],
|
|
)
|
|
|
|
cognito_client.set_log_delivery_configuration(UserPoolId=pool_id, LogConfigurations=[])
|
|
|
|
config = cognito_client.get_log_delivery_configuration(UserPoolId=pool_id)[
|
|
"LogDeliveryConfiguration"
|
|
]
|
|
assert config["LogConfigurations"] == []
|
|
|
|
def test_set_rejects_a_configuration_with_no_destination(self, cognito_client, pool_id):
|
|
"""Every event source in the request must name a destination."""
|
|
with pytest.raises(cognito_client.exceptions.InvalidParameterException):
|
|
cognito_client.set_log_delivery_configuration(
|
|
UserPoolId=pool_id,
|
|
LogConfigurations=[{"LogLevel": "ERROR", "EventSource": "userNotification"}],
|
|
)
|
|
|
|
def test_set_rejects_more_than_two_configurations(self, cognito_client, pool_id):
|
|
"""LogConfigurations is bounded at 2 entries."""
|
|
config = {
|
|
"LogLevel": "ERROR",
|
|
"EventSource": "userNotification",
|
|
"CloudWatchLogsConfiguration": {"LogGroupArn": self.LOG_GROUP_ARN},
|
|
}
|
|
with pytest.raises(cognito_client.exceptions.InvalidParameterException) as excinfo:
|
|
cognito_client.set_log_delivery_configuration(
|
|
UserPoolId=pool_id, LogConfigurations=[config, config, config]
|
|
)
|
|
|
|
assert "Member must have length less than or equal to 2" in str(excinfo.value)
|
|
|
|
def test_set_rejects_a_repeated_event_source(self, cognito_client, pool_id):
|
|
"""An event source may appear at most once across the configurations."""
|
|
config = {
|
|
"LogLevel": "ERROR",
|
|
"EventSource": "userNotification",
|
|
"CloudWatchLogsConfiguration": {"LogGroupArn": self.LOG_GROUP_ARN},
|
|
}
|
|
with pytest.raises(cognito_client.exceptions.InvalidParameterException) as excinfo:
|
|
cognito_client.set_log_delivery_configuration(
|
|
UserPoolId=pool_id, LogConfigurations=[config, config]
|
|
)
|
|
|
|
assert "appear more then once in a request" in str(excinfo.value)
|
|
|
|
def test_a_rejected_request_leaves_the_configuration_alone(self, cognito_client, pool_id):
|
|
"""An oversized request must not be stored."""
|
|
config = {
|
|
"LogLevel": "ERROR",
|
|
"EventSource": "userNotification",
|
|
"CloudWatchLogsConfiguration": {"LogGroupArn": self.LOG_GROUP_ARN},
|
|
}
|
|
with pytest.raises(cognito_client.exceptions.InvalidParameterException):
|
|
cognito_client.set_log_delivery_configuration(
|
|
UserPoolId=pool_id, LogConfigurations=[config, config, config]
|
|
)
|
|
|
|
stored = cognito_client.get_log_delivery_configuration(UserPoolId=pool_id)[
|
|
"LogDeliveryConfiguration"
|
|
]
|
|
assert stored["LogConfigurations"] == []
|
|
|
|
|
|
class TestCognitoDescribeUserPoolStandardAttributes:
|
|
"""DescribeUserPool must return all 20 standard OIDC attributes."""
|
|
|
|
STANDARD_ATTRIBUTES = [
|
|
"sub", "name", "given_name", "family_name", "middle_name", "nickname",
|
|
"preferred_username", "profile", "picture", "website", "email",
|
|
"email_verified", "gender", "birthdate", "zoneinfo", "locale",
|
|
"phone_number", "phone_number_verified", "address", "updated_at",
|
|
]
|
|
|
|
def test_describe_user_pool_returns_all_standard_schema_attributes(self, cognito_client, unique_name):
|
|
response = cognito_client.create_user_pool(PoolName=f"pytest-schema-{unique_name}")
|
|
pool_id = response["UserPool"]["Id"]
|
|
|
|
try:
|
|
described = cognito_client.describe_user_pool(UserPoolId=pool_id)
|
|
schema = described["UserPool"]["SchemaAttributes"]
|
|
names = [a["Name"] for a in schema]
|
|
|
|
assert len(schema) == 20
|
|
for attr in self.STANDARD_ATTRIBUTES:
|
|
assert attr in names, f"Missing standard attribute: {attr}"
|
|
|
|
sub = next(a for a in schema if a["Name"] == "sub")
|
|
assert sub["Required"] is True
|
|
assert sub["Mutable"] is False
|
|
finally:
|
|
cognito_client.delete_user_pool(UserPoolId=pool_id)
|
|
|
|
|
|
class TestCognitoIdentityProvider:
|
|
"""Test Cognito identity provider configuration operations.
|
|
|
|
The response shapes asserted here were measured against the live Cognito API.
|
|
``IdpIdentifiers`` is echoed by CreateIdentityProvider only when the request
|
|
supplied it, while DescribeIdentityProvider always returns it.
|
|
"""
|
|
|
|
OIDC_DETAILS = {
|
|
"client_id": "pytest-client",
|
|
"client_secret": "pytest-secret",
|
|
"attributes_request_method": "GET",
|
|
"oidc_issuer": "https://issuer.example.com",
|
|
"authorize_scopes": "openid",
|
|
}
|
|
|
|
@pytest.fixture
|
|
def pool_id(self, cognito_client, unique_name):
|
|
response = cognito_client.create_user_pool(PoolName=f"pytest-idp-pool-{unique_name}")
|
|
pool_id = response["UserPool"]["Id"]
|
|
yield pool_id
|
|
cognito_client.delete_user_pool(UserPoolId=pool_id)
|
|
|
|
def test_create_defaults_attribute_mapping_and_omits_idp_identifiers(
|
|
self, cognito_client, pool_id
|
|
):
|
|
"""Create defaults AttributeMapping and omits IdpIdentifiers when not supplied."""
|
|
provider = cognito_client.create_identity_provider(
|
|
UserPoolId=pool_id,
|
|
ProviderName="PytestOidc",
|
|
ProviderType="OIDC",
|
|
ProviderDetails=dict(self.OIDC_DETAILS),
|
|
)["IdentityProvider"]
|
|
|
|
assert provider["ProviderType"] == "OIDC"
|
|
assert provider["AttributeMapping"] == {"username": "sub"}
|
|
assert "IdpIdentifiers" not in provider
|
|
|
|
def test_describe_always_returns_idp_identifiers(self, cognito_client, pool_id):
|
|
"""Describe returns IdpIdentifiers even when the stored list is empty."""
|
|
cognito_client.create_identity_provider(
|
|
UserPoolId=pool_id,
|
|
ProviderName="PytestOidc",
|
|
ProviderType="OIDC",
|
|
ProviderDetails=dict(self.OIDC_DETAILS),
|
|
)
|
|
|
|
provider = cognito_client.describe_identity_provider(
|
|
UserPoolId=pool_id, ProviderName="PytestOidc"
|
|
)["IdentityProvider"]
|
|
|
|
assert provider["IdpIdentifiers"] == []
|
|
assert provider["ProviderDetails"]["client_id"] == "pytest-client"
|
|
|
|
def test_update_preserves_members_the_request_omits(self, cognito_client, pool_id):
|
|
"""Omitting AttributeMapping/IdpIdentifiers on update leaves them unchanged."""
|
|
cognito_client.create_identity_provider(
|
|
UserPoolId=pool_id,
|
|
ProviderName="PytestOidc",
|
|
ProviderType="OIDC",
|
|
ProviderDetails=dict(self.OIDC_DETAILS),
|
|
AttributeMapping={"email": "email", "username": "sub"},
|
|
IdpIdentifiers=["pytest-alias"],
|
|
)
|
|
|
|
cognito_client.update_identity_provider(
|
|
UserPoolId=pool_id,
|
|
ProviderName="PytestOidc",
|
|
ProviderDetails=dict(self.OIDC_DETAILS),
|
|
)
|
|
|
|
provider = cognito_client.describe_identity_provider(
|
|
UserPoolId=pool_id, ProviderName="PytestOidc"
|
|
)["IdentityProvider"]
|
|
|
|
assert provider["IdpIdentifiers"] == ["pytest-alias"]
|
|
assert provider["AttributeMapping"] == {"email": "email", "username": "sub"}
|
|
|
|
def test_list_returns_summaries_without_provider_details(self, cognito_client, pool_id):
|
|
"""ListIdentityProviders returns summaries, never provider credentials."""
|
|
cognito_client.create_identity_provider(
|
|
UserPoolId=pool_id,
|
|
ProviderName="PytestOidc",
|
|
ProviderType="OIDC",
|
|
ProviderDetails=dict(self.OIDC_DETAILS),
|
|
)
|
|
|
|
providers = cognito_client.list_identity_providers(UserPoolId=pool_id)["Providers"]
|
|
|
|
assert len(providers) == 1
|
|
assert providers[0]["ProviderName"] == "PytestOidc"
|
|
assert providers[0]["ProviderType"] == "OIDC"
|
|
assert "ProviderDetails" not in providers[0]
|
|
|
|
def test_create_rejects_duplicate_provider_name(self, cognito_client, pool_id):
|
|
"""A second provider with the same name raises DuplicateProviderException."""
|
|
cognito_client.create_identity_provider(
|
|
UserPoolId=pool_id,
|
|
ProviderName="PytestOidc",
|
|
ProviderType="OIDC",
|
|
ProviderDetails=dict(self.OIDC_DETAILS),
|
|
)
|
|
|
|
with pytest.raises(cognito_client.exceptions.DuplicateProviderException):
|
|
cognito_client.create_identity_provider(
|
|
UserPoolId=pool_id,
|
|
ProviderName="PytestOidc",
|
|
ProviderType="OIDC",
|
|
ProviderDetails=dict(self.OIDC_DETAILS),
|
|
)
|
|
|
|
def test_delete_removes_the_provider(self, cognito_client, pool_id):
|
|
"""Delete removes the provider and a second delete raises."""
|
|
cognito_client.create_identity_provider(
|
|
UserPoolId=pool_id,
|
|
ProviderName="PytestOidc",
|
|
ProviderType="OIDC",
|
|
ProviderDetails=dict(self.OIDC_DETAILS),
|
|
)
|
|
|
|
cognito_client.delete_identity_provider(UserPoolId=pool_id, ProviderName="PytestOidc")
|
|
|
|
assert cognito_client.list_identity_providers(UserPoolId=pool_id)["Providers"] == []
|
|
with pytest.raises(cognito_client.exceptions.ResourceNotFoundException):
|
|
cognito_client.delete_identity_provider(
|
|
UserPoolId=pool_id, ProviderName="PytestOidc"
|
|
)
|