Skip to content

Commit

Permalink
Adding Integration test for Auth & API (aws-amplify#77)
Browse files Browse the repository at this point in the history
Cypress integration tests for Auth and API modules
  • Loading branch information
Nidhi Sharma authored Sep 5, 2018
1 parent 1d025b9 commit 005450d
Show file tree
Hide file tree
Showing 14 changed files with 301 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .circleci/com1.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh -xv
cd aws-amplify-cypress-auth
amplify init
amplify add auth
amplify push
echo "executed all Amplify commands"
6 changes: 6 additions & 0 deletions .circleci/com2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh -xv
cd aws-amplify-cypress-api
amplify init
amplify add api
amplify push
echo "executed all Amplify commands"
84 changes: 79 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@ machine:
defaults: &defaults
working_directory: ~/repo
docker:
- image: circleci/node:8.9.1
- image: circleci/node:10.8
parallelism: 8

jobs:
build:
<<: *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:
Expand All @@ -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: |
Expand Down Expand Up @@ -71,6 +142,9 @@ workflows:
- test:
requires:
- build
- integration_test:
requires:
- build
- deploy:
requires:
- build
Expand Down
69 changes: 69 additions & 0 deletions .circleci/enable_api.exp
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions .circleci/enable_auth.exp
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ yarn.lock
package-lock.json
packages/amplify-cli/amplify/.config/
.vscode
packages/graphql*/lib
packages/graphql*/lib
packages/*/node_modules
packages/*/package-lock.json
3 changes: 3 additions & 0 deletions cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"baseUrl": "http://localhost:3000/"
}
5 changes: 5 additions & 0 deletions cypress/fixtures/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Using fixtures to represent data",
"email": "[email protected]",
"body": "Fixtures are a great way to mock data for responses to routes"
}
19 changes: 19 additions & 0 deletions cypress/integration/api_spec.js
Original file line number Diff line number Diff line change
@@ -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')
})
})
15 changes: 15 additions & 0 deletions cypress/integration/auth_spec.js
Original file line number Diff line number Diff line change
@@ -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')
})
})

17 changes: 17 additions & 0 deletions cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -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
}
25 changes: 25 additions & 0 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -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) => { ... })
20 changes: 20 additions & 0 deletions cypress/support/index.js
Original file line number Diff line number Diff line change
@@ -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')
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
"author": "Amazon Web Services",
"license": "Apache-2.0",
"dependencies": {
"lerna": "2.11.0"
"lerna": "^2.11.0"
}
}

0 comments on commit 005450d

Please sign in to comment.