-
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
@httpBearerAuth
integration …
…tests
- Loading branch information
Showing
5 changed files
with
277 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 `@httpBearerAuth` integration tests. |
213 changes: 213 additions & 0 deletions
213
packages/experimental-identity-and-auth/src/integration/httpBearerAuth.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,213 @@ | ||
import { | ||
HttpBearerAuthServiceClient, | ||
OnlyHttpBearerAuthCommand, | ||
OnlyHttpBearerAuthOptionalCommand, | ||
SameAsServiceCommand, | ||
} from "@smithy/identity-and-auth-http-bearer-auth-service"; | ||
import { requireRequestsFrom } from "@smithy/util-test"; | ||
|
||
describe("@httpBearerAuth integration tests", () => { | ||
// Arbitrary mock token | ||
const MOCK_TOKEN = "TOKEN_123"; | ||
|
||
// Arbitrary mock endpoint (`requireRequestsFrom()` intercepts network requests) | ||
const MOCK_ENDPOINT = "https://foo.bar"; | ||
|
||
describe("Operation requires `@httpBearerAuth`", () => { | ||
it("Request is thrown when `token` is not configured", async () => { | ||
const client = new HttpBearerAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
}); | ||
requireRequestsFrom(client).toMatch({}); | ||
await expect(client.send(new OnlyHttpBearerAuthCommand({}))).rejects.toThrow( | ||
"HttpAuthScheme `smithy.api#httpBearerAuth` did not have an IdentityProvider configured." | ||
); | ||
}); | ||
|
||
it("Request is thrown when `token` is configured incorrectly", async () => { | ||
const client = new HttpBearerAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
token: {} as any, | ||
}); | ||
requireRequestsFrom(client).toMatch({}); | ||
await expect(client.send(new OnlyHttpBearerAuthCommand({}))).rejects.toThrow( | ||
"request could not be signed with `token` since the `token` is not defined" | ||
); | ||
}); | ||
|
||
it("Request is thrown given configured `token` identity provider throws", async () => { | ||
const client = new HttpBearerAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
token: async () => { | ||
throw new Error("IdentityProvider throws this error"); | ||
}, | ||
}); | ||
requireRequestsFrom(client).toMatch({}); | ||
await expect(client.send(new OnlyHttpBearerAuthCommand({}))).rejects.toThrow( | ||
"IdentityProvider throws this error" | ||
); | ||
}); | ||
|
||
it("Request is signed given configured `token` identity provider", async () => { | ||
const client = new HttpBearerAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
token: async () => ({ | ||
token: MOCK_TOKEN, | ||
}), | ||
}); | ||
requireRequestsFrom(client).toMatch({ | ||
headers: { | ||
Authorization: `Bearer ${MOCK_TOKEN}`, | ||
}, | ||
}); | ||
await client.send(new OnlyHttpBearerAuthCommand({})); | ||
}); | ||
|
||
it("Request is signed given configured `token` identity", async () => { | ||
const client = new HttpBearerAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
token: { | ||
token: MOCK_TOKEN, | ||
}, | ||
}); | ||
requireRequestsFrom(client).toMatch({ | ||
headers: { | ||
Authorization: `Bearer ${MOCK_TOKEN}`, | ||
}, | ||
}); | ||
await client.send(new OnlyHttpBearerAuthCommand({})); | ||
}); | ||
}); | ||
|
||
describe("Operation has `@httpBearerAuth` and `@optionalAuth`", () => { | ||
it("Request is NOT thrown and NOT signed when `token` is not configured", async () => { | ||
const client = new HttpBearerAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
}); | ||
requireRequestsFrom(client).toMatch({ | ||
headers: { | ||
Authorization: (value) => expect(value).toBeUndefined(), | ||
}, | ||
}); | ||
await client.send(new OnlyHttpBearerAuthOptionalCommand({})); | ||
}); | ||
|
||
it("Request is thrown when `token` is configured incorrectly", async () => { | ||
const client = new HttpBearerAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
token: {} as any, | ||
}); | ||
requireRequestsFrom(client).toMatch({}); | ||
await expect(client.send(new OnlyHttpBearerAuthOptionalCommand({}))).rejects.toThrow( | ||
"request could not be signed with `token` since the `token` is not defined" | ||
); | ||
}); | ||
|
||
it("Request is thrown given configured `token` identity provider throws", async () => { | ||
const client = new HttpBearerAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
token: async () => { | ||
throw new Error("IdentityProvider throws this error"); | ||
}, | ||
}); | ||
requireRequestsFrom(client).toMatch({}); | ||
await expect(client.send(new OnlyHttpBearerAuthOptionalCommand({}))).rejects.toThrow( | ||
"IdentityProvider throws this error" | ||
); | ||
}); | ||
|
||
it("Request is signed given configured `token` identity provider", async () => { | ||
const client = new HttpBearerAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
token: async () => ({ | ||
token: MOCK_TOKEN, | ||
}), | ||
}); | ||
requireRequestsFrom(client).toMatch({ | ||
headers: { | ||
Authorization: `Bearer ${MOCK_TOKEN}`, | ||
}, | ||
}); | ||
await client.send(new OnlyHttpBearerAuthOptionalCommand({})); | ||
}); | ||
|
||
it("Request is signed given configured `token` identity", async () => { | ||
const client = new HttpBearerAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
token: { | ||
token: MOCK_TOKEN, | ||
}, | ||
}); | ||
requireRequestsFrom(client).toMatch({ | ||
headers: { | ||
Authorization: `Bearer ${MOCK_TOKEN}`, | ||
}, | ||
}); | ||
await client.send(new OnlyHttpBearerAuthOptionalCommand({})); | ||
}); | ||
}); | ||
|
||
describe("Service has `@httpBearerAuth`", () => { | ||
it("Request is thrown when `token` is not configured", async () => { | ||
const client = new HttpBearerAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
}); | ||
requireRequestsFrom(client).toMatch({}); | ||
await expect(client.send(new SameAsServiceCommand({}))).rejects.toThrow( | ||
"HttpAuthScheme `smithy.api#httpBearerAuth` did not have an IdentityProvider configured." | ||
); | ||
}); | ||
|
||
it("Request is thrown when `token` is configured incorrectly", async () => { | ||
const client = new HttpBearerAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
token: {} as any, | ||
}); | ||
requireRequestsFrom(client).toMatch({}); | ||
await expect(client.send(new SameAsServiceCommand({}))).rejects.toThrow( | ||
"request could not be signed with `token` since the `token` is not defined" | ||
); | ||
}); | ||
|
||
it("Request is thrown given configured `token` identity provider throws", async () => { | ||
const client = new HttpBearerAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
token: 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 `token` identity provider", async () => { | ||
const client = new HttpBearerAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
token: async () => ({ | ||
token: MOCK_TOKEN, | ||
}), | ||
}); | ||
requireRequestsFrom(client).toMatch({ | ||
headers: { | ||
Authorization: `Bearer ${MOCK_TOKEN}`, | ||
}, | ||
}); | ||
await client.send(new SameAsServiceCommand({})); | ||
}); | ||
|
||
it("Request is signed given configured `token` identity", async () => { | ||
const client = new HttpBearerAuthServiceClient({ | ||
endpoint: MOCK_ENDPOINT, | ||
token: { | ||
token: MOCK_TOKEN, | ||
}, | ||
}); | ||
requireRequestsFrom(client).toMatch({ | ||
headers: { | ||
Authorization: `Bearer ${MOCK_TOKEN}`, | ||
}, | ||
}); | ||
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/httpBearerAuth/HttpBearerAuthService.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.httpBearerAuth | ||
|
||
use common#fakeProtocol | ||
|
||
@fakeProtocol | ||
@httpBearerAuth | ||
service HttpBearerAuthService { | ||
operations: [ | ||
OnlyHttpBearerAuth | ||
OnlyHttpBearerAuthOptional | ||
SameAsService | ||
] | ||
} | ||
|
||
@http(method: "GET", uri: "/OnlyHttpBearerAuth") | ||
@auth([httpBearerAuth]) | ||
operation OnlyHttpBearerAuth {} | ||
|
||
@http(method: "GET", uri: "/OnlyHttpBearerAuthOptional") | ||
@auth([httpBearerAuth]) | ||
@optionalAuth | ||
operation OnlyHttpBearerAuthOptional {} | ||
|
||
@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