Skip to content

test(client-cognito-identity): add cross platform integ tests #1309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clients/client-cognito-identity/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ package-lock.json

*.d.ts
*.js
!karma.conf.js
*.js.map
2 changes: 2 additions & 0 deletions clients/client-cognito-identity/.npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/coverage/
/docs/
/e2e/
tsconfig.test.json
*.tsbuildinfo
jest.config.js
111 changes: 111 additions & 0 deletions clients/client-cognito-identity/e2e/CognitoIdentity.ispec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/// <reference types="mocha" />
/**
* This is the integration test that make sure the client can make request cross-platform-ly
* in NodeJS, Chromium and Firefox. This test is written in mocha.
*/
import { expect } from "chai";
import { CognitoIdentity } from "../index";
import { Credentials } from "@aws-sdk/types";
import { IAM } from "@aws-sdk/client-iam";
// There will be default values of defaultRegion, credentials, and isBrowser variable in browser tests.
// Define the values for Node.js tests
const region: string | undefined =
(globalThis as any).defaultRegion || undefined;
const credentials: Credentials | undefined =
(globalThis as any).credentials || undefined;

describe("@aws-sdk/client-cognito-identity", function () {
this.timeout(50000);
// Required in run
const unAuthClient = new CognitoIdentity({
region
});
const authClient = new CognitoIdentity({
credentials,
region
});
const iam = new IAM({ credentials, region });
const identityPoolName = `aws_sdk_unit_test_${Date.now()}`;
const identityPoolRoleName = `${identityPoolName}-role`;
let identityPoolRole: string;
let identityPoolId: string;

before(async () => {
// Create an identity pool
const createIdentityPoolResult = await authClient.createIdentityPool({
IdentityPoolName: identityPoolName,
AllowUnauthenticatedIdentities: true
});
expect(createIdentityPoolResult.$metadata.httpStatusCode).to.equal(200);
expect(typeof createIdentityPoolResult.IdentityPoolId).to.equal("string");
identityPoolId = createIdentityPoolResult.IdentityPoolId as string;

// Create a role to be attached to identity pool
const { Role } = await iam.createRole({
RoleName: identityPoolRoleName,
AssumeRolePolicyDocument: `{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "cognito-identity.amazonaws.com"
},
"Action": "sts:AssumeRoleWithWebIdentity"
}
]
}`
});
identityPoolRole = Role?.Arn as string;

// wait for role creating
// TODO: add RoleExists waiter
await new Promise(resolve => setTimeout(resolve, 5000));

// Set role to identity pool
await authClient.setIdentityPoolRoles({
IdentityPoolId: identityPoolId,
Roles: {
unauthenticated: identityPoolRole!
}
});

// wait for role propagating
// TODO: add waiters
await new Promise(resolve => setTimeout(resolve, 10000));
});

after(async () => {
// Delete the identity pool
if (identityPoolId) {
await authClient.deleteIdentityPool({
IdentityPoolId: identityPoolId
});
}
// Delete identity pool role
if (identityPoolRole) {
await iam.deleteRole({ RoleName: identityPoolRoleName });
}
});

it("should successfully fetch Id and get credentials", async () => {
// Test getId()
const getIdResult = await unAuthClient.getId({
IdentityPoolId: identityPoolId
});
expect(getIdResult.$metadata.httpStatusCode).to.equal(200);
expect(typeof getIdResult.IdentityId).to.equal("string");

// Test getCredentialsForIdentity() with Id from above
const getCredentialsResult = await unAuthClient.getCredentialsForIdentity({
IdentityId: getIdResult.IdentityId
});
expect(getCredentialsResult.$metadata.httpStatusCode).to.equal(200);
expect(typeof getCredentialsResult.Credentials?.AccessKeyId).to.equal(
"string"
);
expect(typeof getCredentialsResult.Credentials?.SecretKey).to.equal(
"string"
);
});
});
64 changes: 64 additions & 0 deletions clients/client-cognito-identity/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module.exports = function (config) {
config.set({
basePath: "",
frameworks: ["mocha", "chai"],
files: ["e2e/**/*.ispec.ts"],
preprocessors: {
"e2e/**/*.ispec.ts": ["webpack", "sourcemap", "credentials"]
},
webpackMiddleware: {
stats: "minimal"
},
webpack: {
resolve: {
extensions: [".ts", ".js"]
},
mode: "development",
module: {
rules: [
{
test: /\.tsx?$/,
use: [
{
loader: "ts-loader",
options: {
configFile: "tsconfig.json",
compilerOptions: {
rootDir: "./"
}
}
}
],
exclude: /node_modules/
}
]
},
devtool: "inline-source-map"
},
plugins: [
"@aws-sdk/karma-credential-loader",
"karma-chrome-launcher",
"karma-firefox-launcher",
"karma-mocha",
"karma-chai",
"karma-webpack",
"karma-coverage",
"karma-sourcemap-loader"
],
reporters: ["progress"],
port: 9876,
colors: true,
logLevel: config.LOG_WARN,
autoWatch: false,
browsers: ["ChromeHeadless", "FirefoxHeadless"],
customLaunchers: {
FirefoxHeadless: {
base: "Firefox",
flags: ["-headless"]
}
},
singleRun: true,
concurrency: Infinity,
exclude: ["**/*.d.ts", "*.spec.ts"]
});
};
8 changes: 6 additions & 2 deletions clients/client-cognito-identity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"remove-documentation": "rimraf ./docs",
"remove-js": "rimraf *.js && rimraf ./commands/*.js && rimraf ./models/*.js && rimraf ./protocols/*.js",
"remove-maps": "rimraf *.js.map && rimraf ./commands/*.js.map && rimraf ./models/*.js.map && rimraf ./protocols/*.js.map",
"test": "exit 0",
"test:unit": "mocha **/cjs/**/*.spec.js",
"test:e2e": "mocha **/cjs/**/*.ispec.js && karma start karma.conf.js",
"test": "yarn test:unit",
"build:es": "tsc -p tsconfig.es.json",
"build": "yarn pretest && yarn build:es"
},
Expand Down Expand Up @@ -60,8 +62,10 @@
"tslib": "^1.8.0"
},
"devDependencies": {
"@types/chai": "^4.2.11",
"@types/mocha": "^7.0.2",
"@types/node": "^12.7.5",
"jest": "^25.1.0",
"@aws-sdk/client-iam": "1.0.0-gamma.2",
"rimraf": "^3.0.0",
"tslib": "^1.8.0",
"typedoc": "^0.15.0",
Expand Down
3 changes: 2 additions & 1 deletion clients/client-cognito-identity/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"incremental": true,
"resolveJsonModule": true,
"declarationDir": "./types",
"outDir": "dist/cjs"
"outDir": "dist/cjs",
"types": ["mocha", "node"]
},
"typedocOptions": {
"exclude": "**/node_modules/**",
Expand Down