-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(experimentalIdentityAndAuth): add
@httpApiKeyAuth
integration …
…tests
- Loading branch information
Steven Yuan
committed
Oct 8, 2023
1 parent
5fb6d4c
commit 804ab92
Showing
4 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
packages/experimental-identity-and-auth/src/integration/httpApiKeyAuth.integ.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { | ||
HttpApiKeyAuthServiceClient, | ||
OnlyHttpApiKeyAuthCommand, | ||
OnlyHttpApiKeyAuthOptionalCommand, | ||
SameAsServiceCommand, | ||
} from "@smithy/identity-and-auth-http-api-key-auth-service"; | ||
import { requireRequestsFrom } from "@smithy/util-test"; | ||
|
||
describe("@httpApiKeyAuth integration tests", () => { | ||
// TODO(experimentalIdentityAndAuth): should match `HttpApiKeyAuthService` `@httpApiKeyAuth` trait | ||
const MOCK_API_KEY_NAME = "Authorization"; | ||
const MOCK_API_KEY_SCHEME = "ApiKey"; | ||
const MOCK_API_KEY = "APIKEY_123"; | ||
|
||
// Arbitrary mock endpoint (`requireRequestsFrom()` intercepts network requests) | ||
const MOCK_ENDPOINT = "https://foo.bar"; | ||
|
||
describe("Operation requires `@httpApiKeyAuth`", () => { | ||
it("Request is thrown when `apiKey` is not configured", async () => { | ||
const client = new HttpApiKeyAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
}); | ||
requireRequestsFrom(client).toMatch({}); | ||
await expect(client.send(new OnlyHttpApiKeyAuthCommand({}))).rejects.toThrow( | ||
"HttpAuthScheme `smithy.api#httpApiKeyAuth` did not have an IdentityProvider configured." | ||
); | ||
}); | ||
|
||
it("Request is signed given configured `apiKey`", async () => { | ||
const client = new HttpApiKeyAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
apiKey: async () => ({ | ||
apiKey: MOCK_API_KEY, | ||
}), | ||
}); | ||
requireRequestsFrom(client).toMatch({ | ||
headers: { | ||
[MOCK_API_KEY_NAME]: `${MOCK_API_KEY_SCHEME} ${MOCK_API_KEY}`, | ||
}, | ||
}); | ||
await client.send(new OnlyHttpApiKeyAuthCommand({})); | ||
}); | ||
}); | ||
|
||
describe("Operation has `@httpApiKeyAuth` and `@optionalAuth`", () => { | ||
it("Request is NOT thrown and NOT signed when `apiKey` is not configured", async () => { | ||
const client = new HttpApiKeyAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
}); | ||
requireRequestsFrom(client).toMatch({ | ||
headers: { | ||
[MOCK_API_KEY_NAME]: (value) => expect(value).toBeUndefined(), | ||
}, | ||
}); | ||
await client.send(new OnlyHttpApiKeyAuthOptionalCommand({})); | ||
}); | ||
|
||
it("Request is signed given configured `apiKey`", async () => { | ||
const client = new HttpApiKeyAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
apiKey: async () => ({ | ||
apiKey: MOCK_API_KEY, | ||
}), | ||
}); | ||
requireRequestsFrom(client).toMatch({ | ||
headers: { | ||
[MOCK_API_KEY_NAME]: `${MOCK_API_KEY_SCHEME} ${MOCK_API_KEY}`, | ||
}, | ||
}); | ||
await client.send(new OnlyHttpApiKeyAuthOptionalCommand({})); | ||
}); | ||
}); | ||
|
||
describe("Service has `@httpApiKeyAuth`", () => { | ||
it("Request is thrown when `apiKey` is not configured", async () => { | ||
const client = new HttpApiKeyAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
}); | ||
requireRequestsFrom(client).toMatch({}); | ||
await expect(client.send(new SameAsServiceCommand({}))).rejects.toThrow( | ||
"HttpAuthScheme `smithy.api#httpApiKeyAuth` did not have an IdentityProvider configured." | ||
); | ||
}); | ||
|
||
it("Request is signed given configured `apiKey`", async () => { | ||
const client = new HttpApiKeyAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
apiKey: async () => ({ | ||
apiKey: MOCK_API_KEY, | ||
}), | ||
}); | ||
requireRequestsFrom(client).toMatch({ | ||
headers: { | ||
[MOCK_API_KEY_NAME]: `${MOCK_API_KEY_SCHEME} ${MOCK_API_KEY}`, | ||
}, | ||
}); | ||
await client.send(new SameAsServiceCommand({})); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...pescript-codegen-test/model/identity-and-auth/httpApiKeyAuth/HttpApiKeyAuthService.smithy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
$version: "2.0" | ||
|
||
namespace identity.auth.httpApiKeyAuth | ||
|
||
use common#fakeProtocol | ||
|
||
@fakeProtocol | ||
@httpApiKeyAuth(scheme: "ApiKey", name: "Authorization", in: "header") | ||
service HttpApiKeyAuthService { | ||
operations: [ | ||
OnlyHttpApiKeyAuth | ||
OnlyHttpApiKeyAuthOptional | ||
SameAsService | ||
] | ||
} | ||
|
||
@http(method: "GET", uri: "/OnlyHttpApiKeyAuth") | ||
@auth([httpApiKeyAuth]) | ||
operation OnlyHttpApiKeyAuth {} | ||
|
||
@http(method: "GET", uri: "/OnlyHttpApiKeyAuthOptional") | ||
@auth([httpApiKeyAuth]) | ||
@optionalAuth | ||
operation OnlyHttpApiKeyAuthOptional {} | ||
|
||
@http(method: "GET", uri: "/SameAsService") | ||
operation SameAsService {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters