-
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
Showing
5 changed files
with
279 additions
and
0 deletions.
There are no files selected for viewing
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,5 @@ | ||
--- | ||
"@smithy/experimental-identity-and-auth": patch | ||
--- | ||
|
||
Add `@httpApiKeyAuth` integration tests. |
215 changes: 215 additions & 0 deletions
215
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,215 @@ | ||
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 thrown when `apiKey` is configured incorrectly", async () => { | ||
const client = new HttpApiKeyAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
apiKey: {} as any, | ||
}); | ||
requireRequestsFrom(client).toMatch({}); | ||
await expect(client.send(new OnlyHttpApiKeyAuthCommand({}))).rejects.toThrow( | ||
"request could not be signed with `apiKey` since the `apiKey` is not defined" | ||
); | ||
}); | ||
|
||
it("Request is thrown given configured `apiKey` identity provider throws", async () => { | ||
const client = new HttpApiKeyAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
apiKey: async () => { | ||
throw new Error("IdentityProvider throws this error"); | ||
}, | ||
}); | ||
requireRequestsFrom(client).toMatch({}); | ||
await expect(client.send(new OnlyHttpApiKeyAuthCommand({}))).rejects.toThrow( | ||
"IdentityProvider throws this error" | ||
); | ||
}); | ||
|
||
it("Request is signed given configured `apiKey` identity provider", 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({})); | ||
}); | ||
|
||
it("Request is signed given configured `apiKey` identity", async () => { | ||
const client = new HttpApiKeyAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
apiKey: { | ||
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 thrown when `apiKey` is configured incorrectly", async () => { | ||
const client = new HttpApiKeyAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
apiKey: {} as any, | ||
}); | ||
requireRequestsFrom(client).toMatch({}); | ||
await expect(client.send(new OnlyHttpApiKeyAuthOptionalCommand({}))).rejects.toThrow( | ||
"request could not be signed with `apiKey` since the `apiKey` is not defined" | ||
); | ||
}); | ||
|
||
it("Request is thrown given configured `apiKey` identity provider throws", async () => { | ||
const client = new HttpApiKeyAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
apiKey: async () => { | ||
throw new Error("IdentityProvider throws this error"); | ||
}, | ||
}); | ||
requireRequestsFrom(client).toMatch({}); | ||
await expect(client.send(new OnlyHttpApiKeyAuthOptionalCommand({}))).rejects.toThrow( | ||
"IdentityProvider throws this error" | ||
); | ||
}); | ||
|
||
it("Request is signed given configured `apiKey` identity provider", 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({})); | ||
}); | ||
|
||
it("Request is signed given configured `apiKey` identity", async () => { | ||
const client = new HttpApiKeyAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
apiKey: { | ||
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 thrown when `apiKey` is configured incorrectly", async () => { | ||
const client = new HttpApiKeyAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
apiKey: {} as any, | ||
}); | ||
requireRequestsFrom(client).toMatch({}); | ||
await expect(client.send(new SameAsServiceCommand({}))).rejects.toThrow( | ||
"request could not be signed with `apiKey` since the `apiKey` is not defined" | ||
); | ||
}); | ||
|
||
it("Request is thrown given configured `apiKey` identity provider throws", async () => { | ||
const client = new HttpApiKeyAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
apiKey: async () => { | ||
throw new Error("IdentityProvider throws this error"); | ||
}, | ||
}); | ||
requireRequestsFrom(client).toMatch({}); | ||
await expect(client.send(new SameAsServiceCommand({}))).rejects.toThrow("IdentityProvider throws this error"); | ||
}); | ||
|
||
it("Request is signed given configured `apiKey` identity provider", 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({})); | ||
}); | ||
|
||
it("Request is signed given configured `apiKey` identity", async () => { | ||
const client = new HttpApiKeyAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
apiKey: { | ||
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