From 005450d049ae85be119b77adb003e29f475900cf Mon Sep 17 00:00:00 2001 From: Nidhi Sharma Date: Wed, 5 Sep 2018 16:55:34 -0700 Subject: [PATCH] Adding Integration test for Auth & API (#77) Cypress integration tests for Auth and API modules --- .circleci/com1.sh | 6 +++ .circleci/com2.sh | 6 +++ .circleci/config.yml | 84 ++++++++++++++++++++++++++++++-- .circleci/enable_api.exp | 69 ++++++++++++++++++++++++++ .circleci/enable_auth.exp | 33 +++++++++++++ .gitignore | 4 +- cypress.json | 3 ++ cypress/fixtures/example.json | 5 ++ cypress/integration/api_spec.js | 19 ++++++++ cypress/integration/auth_spec.js | 15 ++++++ cypress/plugins/index.js | 17 +++++++ cypress/support/commands.js | 25 ++++++++++ cypress/support/index.js | 20 ++++++++ package.json | 2 +- 14 files changed, 301 insertions(+), 7 deletions(-) create mode 100644 .circleci/com1.sh create mode 100644 .circleci/com2.sh create mode 100644 .circleci/enable_api.exp create mode 100644 .circleci/enable_auth.exp create mode 100644 cypress.json create mode 100644 cypress/fixtures/example.json create mode 100644 cypress/integration/api_spec.js create mode 100644 cypress/integration/auth_spec.js create mode 100644 cypress/plugins/index.js create mode 100644 cypress/support/commands.js create mode 100644 cypress/support/index.js diff --git a/.circleci/com1.sh b/.circleci/com1.sh new file mode 100644 index 00000000000..5d47da15132 --- /dev/null +++ b/.circleci/com1.sh @@ -0,0 +1,6 @@ +#!/bin/sh -xv +cd aws-amplify-cypress-auth +amplify init +amplify add auth +amplify push +echo "executed all Amplify commands" \ No newline at end of file diff --git a/.circleci/com2.sh b/.circleci/com2.sh new file mode 100644 index 00000000000..fb76cb632ba --- /dev/null +++ b/.circleci/com2.sh @@ -0,0 +1,6 @@ +#!/bin/sh -xv +cd aws-amplify-cypress-api +amplify init +amplify add api +amplify push +echo "executed all Amplify commands" \ No newline at end of file diff --git a/.circleci/config.yml b/.circleci/config.yml index f34b4f735f7..1c34a4b4bfa 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -6,7 +6,7 @@ machine: defaults: &defaults working_directory: ~/repo docker: - - image: circleci/node:8.9.1 + - image: circleci/node:10.8 parallelism: 8 jobs: @@ -14,15 +14,16 @@ jobs: <<: *defaults steps: - checkout + - run: cd packages && rm -rf */package-lock.json && rm -rf */node_modules - run: yarn - run: yarn config set workspaces-experimental true - run: yarn run production-build - save_cache: - key: amplify-npm-deps-{{ .Branch }}-{{ checksum "package-lock.json" }} + key: amplify-cli-npm-deps-{{ .Branch }}-{{ checksum "package-lock.json" }} paths: - ~/.cache - save_cache: - key: amplify-ssh-deps-{{ .Branch }} + key: amplify-cli-ssh-deps-{{ .Branch }} paths: - ~/.ssh - persist_to_workspace: @@ -34,15 +35,85 @@ jobs: - attach_workspace: at: ./ - restore_cache: - key: amplify-npm-deps-{{ .Branch }}-{{ checksum "package-lock.json" }} + key: amplify-cli-npm-deps-{{ .Branch }}-{{ checksum "package-lock.json" }} - run: npm run test + integration_test: + working_directory: ~/repo + docker: + - image: cypress/base:10 + environment: + TERM: dumb + steps: + - attach_workspace: + at: ./ + - restore_cache: + key: amplify-cli-npm-deps-{{ .Branch }}-{{ checksum "package-lock.json" }} + - run: apt-get install -y sudo + - run: sudo apt-get install -y tcl + - run: sudo apt-get install -y expect + - run: sudo apt-get install -y zip + - run: sudo apt-get install -y lsof + - run: sudo npm run setup-dev + - run: amplify + - run: + name: "Clone auth test package" + command: | + git clone $AUTH_CLONE_URL + cd aws-amplify-cypress-auth + yarn + - run: cd .circleci/ && chmod +x com1.sh + - run: expect .circleci/enable_auth.exp + - run: cd aws-amplify-cypress-auth + - run: yarn + - run: cd aws-amplify-cypress-auth/src && cat $(find . -type f -name 'aws-exports*') + - run: + name: "Start Auth test server in background" + command: | + cd aws-amplify-cypress-auth + pwd + yarn start + background: true + - run: cat $(find . -type f -name 'auth_spec*') + - run: + name: "Run cypress tests for auth" + command: | + npm install --save cypress + node_modules/.bin/cypress run --spec $(find . -type f -name 'auth_spec*') + - run: sudo kill -9 $(lsof -t -i:3000) + - run: + name: "Clone API test package" + command: | + git clone $API_CLONE_URL + cd aws-amplify-cypress-api + yarn + - run: cd .circleci/ && chmod +x com2.sh + - run: expect .circleci/enable_api.exp + - run: cd aws-amplify-cypress-api + - run: yarn + - run: cd aws-amplify-cypress-api/src && cat $(find . -type f -name 'aws-exports*') + - run: + name: "Start API test server in background" + command: | + cd aws-amplify-cypress-api + pwd + yarn start + background: true + - run: + name: "Run cypress tests for api" + command: | + npm install --save cypress + node_modules/.bin/cypress run --spec $(find . -type f -name 'api_spec*') + - store_artifacts: + path: cypress/videos + - store_artifacts: + path: cypress/screenshots deploy: <<: *defaults steps: - attach_workspace: at: ./ - restore_cache: - key: amplify-npm-deps-{{ .Branch }}-{{ checksum "package-lock.json" }} + key: amplify-cli-npm-deps-{{ .Branch }}-{{ checksum "package-lock.json" }} - run: name: "Update SSH configs" command: | @@ -71,6 +142,9 @@ workflows: - test: requires: - build + - integration_test: + requires: + - build - deploy: requires: - build diff --git a/.circleci/enable_api.exp b/.circleci/enable_api.exp new file mode 100644 index 00000000000..62a924ea92e --- /dev/null +++ b/.circleci/enable_api.exp @@ -0,0 +1,69 @@ +#!/usr/bin/expect +spawn ./.circleci/com2.sh +expect "Enter a name for the project" +send -- "unauth\r" +expect -exact "Choose your default editor:" +send -- "\r" +expect "Choose the type of app that you're building" +send -- "\r" +expect "Please tell us about your project\rWhat javascript framework are you using" +send -- "\r" +expect -exact "Source Directory Path:" +send -- "\r" +expect -exact "Distribution Directory Path:" +send -- "\r" +expect -exact "Build Command:" +send -- "\r" +expect -exact "Start Command:" +send -- "\r" +expect "Using default provider awscloudformation\nAWS access credentials can not be detected\n.Setup new user" +send -- "n\r" +expect "accessKeyId:" +send -- "$env(ACCESS_KEY_ID)\r" +expect "secretAccessKey:" +send -- "$env(SECRET_ACCESS_KEY)\r" +expect -exact "region:" +send -- "\r" +set timeout 70 +expect "Please select from one of the below mentioned services\r" +send -- "\033\[B\r" +expect "Provide a friendly name for your resource to be used as a label for this category in the project:" +send -- "myAPI\r" +expect "Provide a path (e.g., /items)" +send -- "\r" +expect "Choose a Lambda source" +send -- "\r" +expect "Provide a friendly name for your resource to be used as a label for this category in the project:" +send -- "\r" +expect "Provide the AWS Lambda function name:" +send -- "\r" +expect "Choose the function template that you want to use:" +send -- "\r" +expect "Choose a DynamoDB data source option" +send -- "\033\[B\r" +expect "Please provide a friendly name for your resource that will be used to label this category in the project:" +send -- "\r" +expect "Please provide table name:" +send -- "\r" +expect "What would you like to name this column:" +send -- "itemNo\r" +expect "Please choose the data type:" +send -- "\033\[B\r" +expect "Would you like to add another column?" +send -- "n\r" +expect "Please choose partition key for the table:" +send -- "\r" +expect "Do you want to add a sort key to your table?" +send -- "n\r" +expect "Do you want to add global secondary indexes to your table?" +send -- "n\r" +expect "Do you want to edit the local lambda function now?" +send -- "n\r" +expect "Restrict API access" +send -- "n\r" +expect "Do you want to add another path?" +send -- "N\r" +set timeout 70 +expect "Are you sure you want to continue?\r" +send -- "Y\r" +interact diff --git a/.circleci/enable_auth.exp b/.circleci/enable_auth.exp new file mode 100644 index 00000000000..42f3360459e --- /dev/null +++ b/.circleci/enable_auth.exp @@ -0,0 +1,33 @@ +#!/usr/bin/expect +spawn ./.circleci/com1.sh +expect "Enter a name for the project" +send -- "unauth\r" +expect -exact "Choose your default editor:" +send -- "\r" +expect "Choose the type of app that you're building" +send -- "\r" +expect "Please tell us about your project\rWhat javascript framework are you using" +send -- "\r" +expect -exact "Source Directory Path:" +send -- "\r" +expect -exact "Distribution Directory Path:" +send -- "\r" +expect -exact "Build Command:" +send -- "\r" +expect -exact "Start Command:" +send -- "\r" +expect "Using default provider awscloudformation\nAWS access credentials can not be detected\n.Setup new user" +send -- "n\r" +expect "accessKeyId:" +send -- "$env(ACCESS_KEY_ID)\r" +expect "secretAccessKey:" +send -- "$env(SECRET_ACCESS_KEY)\r" +expect -exact "region:" +send -- "\r" +set timeout 70 +expect "Do you want to use default authentication and security configuration?\r" +send -- "\r" +set timeout 70 +expect "Are you sure you want to continue?\r" +send -- "Y\r" +interact diff --git a/.gitignore b/.gitignore index 6e9e70b22e5..c8ab85f8d95 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,6 @@ yarn.lock package-lock.json packages/amplify-cli/amplify/.config/ .vscode -packages/graphql*/lib \ No newline at end of file +packages/graphql*/lib +packages/*/node_modules +packages/*/package-lock.json \ No newline at end of file diff --git a/cypress.json b/cypress.json new file mode 100644 index 00000000000..4bae1f98466 --- /dev/null +++ b/cypress.json @@ -0,0 +1,3 @@ +{ + "baseUrl": "http://localhost:3000/" +} \ No newline at end of file diff --git a/cypress/fixtures/example.json b/cypress/fixtures/example.json new file mode 100644 index 00000000000..da18d9352a1 --- /dev/null +++ b/cypress/fixtures/example.json @@ -0,0 +1,5 @@ +{ + "name": "Using fixtures to represent data", + "email": "hello@cypress.io", + "body": "Fixtures are a great way to mock data for responses to routes" +} \ No newline at end of file diff --git a/cypress/integration/api_spec.js b/cypress/integration/api_spec.js new file mode 100644 index 00000000000..c67f4b14a36 --- /dev/null +++ b/cypress/integration/api_spec.js @@ -0,0 +1,19 @@ + +describe('API test post and get', function() { + beforeEach(function() { + cy.visit('/') + }) + + it('successfully adds data to dynamodb', function() { + // Check for user not signed up + cy.get('input[name=itemNo]').type('1') + cy.get('.amplify-submit-put-button').contains('Put').click() + cy.get('.amplify-put-result').contains('post call succeed!') + }) + + it('successfully get data from dynamodb', function() { + // Check for user not signed up + cy.get('.amplify-submit-get-button').contains('Get').click() + cy.get('.amplify-get-result').contains('"itemNo":1') + }) +}) \ No newline at end of file diff --git a/cypress/integration/auth_spec.js b/cypress/integration/auth_spec.js new file mode 100644 index 00000000000..4b6bd95cb7f --- /dev/null +++ b/cypress/integration/auth_spec.js @@ -0,0 +1,15 @@ + +describe('withAuthenticator Sign In', function() { + beforeEach(function() { + cy.visit('/') + }) + + it('throws error when user is not signed up', function() { + // Check for user not signed up + cy.get('input[name=username]').type('testuser') + cy.get('input[name=password]').type('testPassword') + cy.get('.amplify-button').contains('Sign In').click() + cy.get('.amplify-error-section').contains('User does not exist') + }) +}) + diff --git a/cypress/plugins/index.js b/cypress/plugins/index.js new file mode 100644 index 00000000000..fd170fba691 --- /dev/null +++ b/cypress/plugins/index.js @@ -0,0 +1,17 @@ +// *********************************************************** +// This example plugins/index.js can be used to load plugins +// +// You can change the location of this file or turn off loading +// the plugins file with the 'pluginsFile' configuration option. +// +// You can read more here: +// https://on.cypress.io/plugins-guide +// *********************************************************** + +// This function is called when a project is opened or re-opened (e.g. due to +// the project's config changing) + +module.exports = (on, config) => { + // `on` is used to hook into various events Cypress emits + // `config` is the resolved Cypress config +} diff --git a/cypress/support/commands.js b/cypress/support/commands.js new file mode 100644 index 00000000000..c1f5a772e2b --- /dev/null +++ b/cypress/support/commands.js @@ -0,0 +1,25 @@ +// *********************************************** +// This example commands.js shows you how to +// create various custom commands and overwrite +// existing commands. +// +// For more comprehensive examples of custom +// commands please read more here: +// https://on.cypress.io/custom-commands +// *********************************************** +// +// +// -- This is a parent command -- +// Cypress.Commands.add("login", (email, password) => { ... }) +// +// +// -- This is a child command -- +// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... }) +// +// +// -- This is a dual command -- +// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... }) +// +// +// -- This is will overwrite an existing command -- +// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... }) diff --git a/cypress/support/index.js b/cypress/support/index.js new file mode 100644 index 00000000000..d68db96df26 --- /dev/null +++ b/cypress/support/index.js @@ -0,0 +1,20 @@ +// *********************************************************** +// This example support/index.js is processed and +// loaded automatically before your test files. +// +// This is a great place to put global configuration and +// behavior that modifies Cypress. +// +// You can change the location of this file or turn off +// automatically serving support files with the +// 'supportFile' configuration option. +// +// You can read more here: +// https://on.cypress.io/configuration +// *********************************************************** + +// Import commands.js using ES2015 syntax: +import './commands' + +// Alternatively you can use CommonJS syntax: +// require('./commands') diff --git a/package.json b/package.json index cc9eaa19ec0..3ae2cadfe3b 100755 --- a/package.json +++ b/package.json @@ -26,6 +26,6 @@ "author": "Amazon Web Services", "license": "Apache-2.0", "dependencies": { - "lerna": "2.11.0" + "lerna": "^2.11.0" } }