diff --git a/clients/client-cognito-identity/.gitignore b/clients/client-cognito-identity/.gitignore index b41c05b597c42..35f221867ed0d 100644 --- a/clients/client-cognito-identity/.gitignore +++ b/clients/client-cognito-identity/.gitignore @@ -11,4 +11,5 @@ package-lock.json *.d.ts *.js +!karma.conf.js *.js.map diff --git a/clients/client-cognito-identity/.npmignore b/clients/client-cognito-identity/.npmignore index b7ff81137c4ad..e40c82df262c4 100644 --- a/clients/client-cognito-identity/.npmignore +++ b/clients/client-cognito-identity/.npmignore @@ -1,4 +1,6 @@ /coverage/ /docs/ +/e2e/ tsconfig.test.json *.tsbuildinfo +jest.config.js diff --git a/clients/client-cognito-identity/e2e/CognitoIdentity.ispec.ts b/clients/client-cognito-identity/e2e/CognitoIdentity.ispec.ts new file mode 100644 index 0000000000000..09d9780bbac0b --- /dev/null +++ b/clients/client-cognito-identity/e2e/CognitoIdentity.ispec.ts @@ -0,0 +1,111 @@ +/// +/** + * 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" + ); + }); +}); diff --git a/clients/client-cognito-identity/karma.conf.js b/clients/client-cognito-identity/karma.conf.js new file mode 100644 index 0000000000000..32475f3900c6f --- /dev/null +++ b/clients/client-cognito-identity/karma.conf.js @@ -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"] + }); +}; diff --git a/clients/client-cognito-identity/package.json b/clients/client-cognito-identity/package.json index 15b534ae08c3f..2b0156708732e 100644 --- a/clients/client-cognito-identity/package.json +++ b/clients/client-cognito-identity/package.json @@ -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" }, @@ -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", diff --git a/clients/client-cognito-identity/tsconfig.json b/clients/client-cognito-identity/tsconfig.json index ca2fadc08187b..3b53843739832 100644 --- a/clients/client-cognito-identity/tsconfig.json +++ b/clients/client-cognito-identity/tsconfig.json @@ -12,7 +12,8 @@ "incremental": true, "resolveJsonModule": true, "declarationDir": "./types", - "outDir": "dist/cjs" + "outDir": "dist/cjs", + "types": ["mocha", "node"] }, "typedocOptions": { "exclude": "**/node_modules/**",