Unverified Commit 76ee00eb authored by ysaito1001's avatar ysaito1001 Committed by GitHub
Browse files

Add a test helper for creating a dummy Credentials (#2145)

* Add convenience method `Credentials::for_tests`

This commit addresses https://github.com/awslabs/smithy-rs/pull/2122#discussion_r1056499862.

We considered using `#[cfg(test)]` for it but the attribute only works
within the defining crate. To work around it, we have added `#[doc(hidden)]`
to it to prevent the method from appearing in the public doc, though still
accessible in the source code.

* Make AWS runtime crates use test credentials helper

This commit updates the test code in the AWS runtime crates to use
`Credentials::for_tests` wherever applicable. We intentionally did
not replace the following `Credentials::new` occurrences:
* those in public rustdoc
* those that need to configure expiration time

* Make EndpointsCredentialsTest use test credentials helper

This commit replaces `Credentials::new` with `Credentials::for_tests` in
the unit test for EndpointsCredentialsTest.

* Make SDK integration tests use test credentials helper

This commit replaces `Credentials::new` with `Credentials::for_tests` in
the test code in the SDK integration tests wherever applicable.

* Call `Credentials::for_tests` directly in calling `credentials_provider`

* Feature gate `Credentials::for_tests` behind "test-util"

This commit addresses https://github.com/awslabs/smithy-rs/pull/2145#discussion_r1059437196

* Use `Credentials::for_tests` rather than `new`

This commit addresses https://github.com/awslabs/smithy-rs/pull/2145#discussion_r1059437435



Co-authored-by: default avatarYuki Saito <awsaito@amazon.com>
parent 88afcf8c
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -55,6 +55,7 @@ arbitrary = "=1.1.3" # 1.1.4 requires Rust 1.63 to compile
serde = { version = "1", features = ["derive"] }
serde_json = "1"

aws-credential-types = { path = "../../sdk/build/aws-sdk/sdk/aws-credential-types", features = ["test-util"] }
aws-smithy-client = { path = "../../sdk/build/aws-sdk/sdk/aws-smithy-client", features = ["test-util"] }

# used for a usage example
+1 −1
Original line number Diff line number Diff line
@@ -206,7 +206,7 @@ mod test {
        let mut base = HashMap::new();
        base.insert(
            "Environment".into(),
            Arc::new(Credentials::new("key", "secret", None, None, "test")) as _,
            Arc::new(Credentials::for_tests()) as _,
        );
        let provider = NamedProviderFactory::new(base);
        assert!(provider.provider("environment").is_some());
+2 −14
Original line number Diff line number Diff line
@@ -321,13 +321,7 @@ mod test {
            .configure(&provider_conf)
            .region(Region::new("us-east-1"))
            .session_length(Duration::from_secs(1234567))
            .build(SharedCredentialsProvider::new(Credentials::new(
                "base",
                "basesecret",
                Some("token".to_string()),
                None,
                "inner",
            )));
            .build(SharedCredentialsProvider::new(Credentials::for_tests()));
        let _ = provider.provide_credentials().await;
        let req = request.expect_request();
        let str_body = std::str::from_utf8(req.body().bytes().unwrap()).unwrap();
@@ -349,13 +343,7 @@ mod test {
        let provider = AssumeRoleProvider::builder("myrole")
            .configure(&provider_conf)
            .region(Region::new("us-east-1"))
            .build(SharedCredentialsProvider::new(Credentials::new(
                "base",
                "basesecret",
                Some("token".to_string()),
                None,
                "inner",
            )));
            .build(SharedCredentialsProvider::new(Credentials::for_tests()));
        let creds_first = provider
            .provide_credentials()
            .await
+1 −0
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ repository = "https://github.com/awslabs/smithy-rs"

[features]
hardcoded-credentials = []
test-util = []

[dependencies]
aws-smithy-async = { path = "../../../rust-runtime/aws-smithy-async" }
+12 −0
Original line number Diff line number Diff line
@@ -140,6 +140,18 @@ impl Credentials {
        )
    }

    /// Creates a test `Credentials`.
    #[cfg(feature = "test-util")]
    pub fn for_tests() -> Self {
        Self::new(
            "ANOTREAL",
            "notrealrnrELgWzOk3IfjzDKtFBhDby",
            Some("notarealsessiontoken".to_string()),
            None,
            "test",
        )
    }

    /// Returns the access key ID.
    pub fn access_key_id(&self) -> &str {
        &self.0.access_key_id
Loading