Skip to content

Commit

Permalink
feat(experimentalIdentityAndAuth): add @httpBearerAuth integration …
Browse files Browse the repository at this point in the history
…tests
  • Loading branch information
Steven Yuan authored and syall committed Oct 11, 2023
1 parent 56bdadd commit 49f75b4
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/eleven-lamps-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smithy/experimental-identity-and-auth": patch
---

Add `@httpBearerAuth` integration tests.
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({}));
});
});
});
9 changes: 9 additions & 0 deletions scripts/build-generated-test-packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ const httpApiKeyAuthClientDir = path.join(
"typescript-codegen"
);

// TODO(experimentalIdentityAndAuth): add `@httpBearerAuth` client for integration tests
const httpBearerAuthClientDir = path.join(
codegenTestDir,
"identity-and-auth-http-bearer-auth",
"typescript-codegen"
);

const nodeModulesDir = path.join(root, "node_modules");

const buildAndCopyToNodeModules = async (packageName, codegenDir, nodeModulesDir) => {
Expand Down Expand Up @@ -70,6 +77,8 @@ const buildAndCopyToNodeModules = async (packageName, codegenDir, nodeModulesDir
await buildAndCopyToNodeModules("@smithy/weather-experimental-identity-and-auth", weatherExperimentalIdentityAndAuthClientDir, nodeModulesDir);
// TODO(experimentalIdentityAndAuth): add `@httpApiKeyAuth` client for integration tests
await buildAndCopyToNodeModules("@smithy/identity-and-auth-http-api-key-auth-service", httpApiKeyAuthClientDir, nodeModulesDir);
// TODO(experimentalIdentityAndAuth): add `@httpBearerAuth` client for integration tests
await buildAndCopyToNodeModules("@smithy/identity-and-auth-http-bearer-auth-service", httpBearerAuthClientDir, nodeModulesDir);
} catch (e) {
console.log(e);
process.exit(1);
Expand Down
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 {}
23 changes: 23 additions & 0 deletions smithy-typescript-codegen-test/smithy-build.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,29 @@
"experimentalIdentityAndAuth": true
}
}
},
"identity-and-auth-http-bearer-auth": {
"transforms": [
{
"name": "includeServices",
"args": {
"services": ["identity.auth.httpBearerAuth#HttpBearerAuthService"]
}
}
],
"plugins": {
"typescript-codegen": {
"service": "identity.auth.httpBearerAuth#HttpBearerAuthService",
"targetNamespace": "HttpBearerAuthService",
"package": "@smithy/identity-and-auth-http-bearer-auth-service",
"packageVersion": "0.0.1",
"packageJson": {
"license": "Apache-2.0",
"private": true
},
"experimentalIdentityAndAuth": true
}
}
}
},
"plugins": {
Expand Down

0 comments on commit 49f75b4

Please sign in to comment.