From 28ed331cd45d2978fe6cbd90f3b59b9def067f67 Mon Sep 17 00:00:00 2001 From: Vicente Pastor Pastor Date: Tue, 26 Nov 2019 14:05:44 +0000 Subject: [PATCH] Downloaded latest changes from template --- .githooks/post-commit | 6 ++- .githooks/pre-commit | 57 ++++++++++++++++++++- .githooks/pre-merge-commit | 4 ++ .githooks/pre-push | 18 +++++++ .githooks/prepare-commit-msg | 37 ++++++++++++++ .gitlab-ci.yml | 42 ++++++++++++++++ acceptanceTests/login.test.js | 70 ++++++++++++++++++++++++++ acceptanceTests/scripts/afterAll.sh | 17 +++++++ acceptanceTests/scripts/beforeAll.sh | 12 +++++ acceptanceTests/scripts/beforeEach.sh | 9 ++++ acceptanceTests/scripts/test.sh | 11 ++++ jest-test.config.js | 3 +- jest.puppeteer.config.js | 14 ++++++ package.json | 34 +++++++++---- {unit tests => unitTests}/user.spec.js | 2 +- 15 files changed, 321 insertions(+), 15 deletions(-) create mode 100644 .githooks/pre-merge-commit create mode 100644 .githooks/pre-push create mode 100644 .githooks/prepare-commit-msg create mode 100644 .gitlab-ci.yml create mode 100644 acceptanceTests/login.test.js create mode 100644 acceptanceTests/scripts/afterAll.sh create mode 100644 acceptanceTests/scripts/beforeAll.sh create mode 100644 acceptanceTests/scripts/beforeEach.sh create mode 100644 acceptanceTests/scripts/test.sh create mode 100644 jest.puppeteer.config.js rename {unit tests => unitTests}/user.spec.js (97%) diff --git a/.githooks/post-commit b/.githooks/post-commit index 1dd5e6e..a71bdcb 100644 --- a/.githooks/post-commit +++ b/.githooks/post-commit @@ -1,5 +1,7 @@ #!/bin/sh set -e # using the options command to abort script at first error -echo "running the 'post-commit' script" -./node_modules/.bin/markdownlint --ignore node_modules . +echo +echo "POST-COMMIT" +# ./node_modules/.bin/markdownlint --ignore node_modules . +echo diff --git a/.githooks/pre-commit b/.githooks/pre-commit index eb642eb..f7fbefd 100644 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -1,5 +1,60 @@ #!/bin/sh set -e # using the options command to abort script at first error -echo "running the 'pre-commit' script" +echo +echo "PRE-COMMIT" + +EMAIL=$(git config user.email) + +# make sure the user has registered a valid university email address +if [[ $EMAIL != *"@coventry.ac.uk" ]]; then + echo " invalid config settings" + echo " Your registered email is currently '$EMAIL'" + echo " please run the following git commands:" + echo " $ git config user.email xxx@coventry.ac.uk" + echo " $ git config user.name 'zzz'" + echo " where 'xxx' is your university username" + echo " and 'zzz' is your name as it appears on your university ID badge" + echo + exit 1 +fi + +# see if the user is trying to merge a branch into master +branch="$(git rev-parse --abbrev-ref HEAD)" +if [[ $2 == 'merge' ]]; then + echo "merging branch" + if [[ "$branch" == "master" ]]; then + echo " trying to merge into the 'master' branch" + echo " you should push the local branch to GitHub" + echo " and merge to master using a pull request" + echo + exit 1 + fi +fi + +# see if the user is trying to commit to the master branch +echo " you are trying to commit to the '$branch' branch" +if [ "$branch" = "master" ]; then + echo " you can't commit directly to the master branch" + echo " create a local feature branch first" + echo + exit 1 +fi + +# check for valid branch name: + +# valid_branch_regex="^iss\d{3}\/[a-z\-]+$" + +# if [[ ! $local_branch =~ $valid_branch_regex ]] +# then +# echo "invalid branch name" +# echo " format is: 'iss000/issue-name'" +# echo " replacing '000' with the issue number and 'issue-name' with the issue name" +# echo " only lower-case letters and replace spaces in the issue name with dashes" +# echo " rename your branch and try again" +# exit 1 +# fi + ./node_modules/.bin/eslint . + +echo " commit successful..." diff --git a/.githooks/pre-merge-commit b/.githooks/pre-merge-commit new file mode 100644 index 0000000..128c360 --- /dev/null +++ b/.githooks/pre-merge-commit @@ -0,0 +1,4 @@ +#!/bin/sh + +set -e # using the options command to abort script at first error +echo "running the 'pre-merge-commit' script" diff --git a/.githooks/pre-push b/.githooks/pre-push new file mode 100644 index 0000000..b99229a --- /dev/null +++ b/.githooks/pre-push @@ -0,0 +1,18 @@ +#!/bin/sh + +echo +echo "PRE-PUSH" + +protected_branch='master' +current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,') + +if [ $protected_branch = $current_branch ] +then + read -p " You're about to push master, is that what you intended? [y|n] " -n 1 -r < /dev/tty + echo + if echo $REPLY | grep -E '^[Yy]$' > /dev/null + then + exit 0 # push will execute + fi + exit 1 # push will not execute +fi diff --git a/.githooks/prepare-commit-msg b/.githooks/prepare-commit-msg new file mode 100644 index 0000000..60c7dec --- /dev/null +++ b/.githooks/prepare-commit-msg @@ -0,0 +1,37 @@ +#!/bin/sh + +# With thanks to Sergio Vaccaro + +set -e # using the options command to abort script at first error +echo +echo "PREPARE-COMMIT-MSG" + +# Branch to protect +PROTECTED_BRANCH="master" + +# Remote +REMOTE="" + +# Check for merges +if [[ $2 != 'merge' ]]; then + # Not a merge + ECHO " not a merge" + exit 0 +fi + +# Current branch +CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) + +# Check if in PROTECTED_BRANCH +if [[ "$CURRENT_BRANCH" != "$PROTECTED_BRANCH" ]]; then + # Not in PROTECTED_BRANCH: can proceed + ECHO " not in the ${PROTECTED_BRANCH} branch" + exit 0 +fi + +echo " you are trying to merge into the ${PROTECTED_BRANCH} branch" +echo " merging branches to master must be done by creating a pull request" +echo " this merge has been cancelled however you will need to" +echo " reset the operation before continuing by running git reset --merge" +echo +exit 1 diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..3f4455b --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,42 @@ +image: node:latest + +stages: + - code-testing + - staging-server + - acceptance-testing + +linting: + stage: code-testing + script: + - npm install + - npm run linter + +dependency-checks: + stage: code-testing + script: + - npm install + - npm run dependency + +unit-testing: + stage: code-testing + script: + - npm install + - npm test + +code-coverage: + stage: code-testing + script: + - npm install + - npm run coverage + +coverage-report: + stage: staging-server + script: + - npm install + - npm run coverage + artifacts: + paths: + - docs + expire_in: 30 days + only: + - master diff --git a/acceptanceTests/login.test.js b/acceptanceTests/login.test.js new file mode 100644 index 0000000..a4c41f5 --- /dev/null +++ b/acceptanceTests/login.test.js @@ -0,0 +1,70 @@ +'use strict' + +const puppeteer = require('puppeteer') +const { configureToMatchImageSnapshot } = require('jest-image-snapshot') +const PuppeteerHar = require('puppeteer-har') +const shell = require('shelljs') + +const width = 800 +const height = 600 +const delayMS = 5 + +let browser +let page +let har + +// threshold is the difference in pixels before the snapshots dont match +const toMatchImageSnapshot = configureToMatchImageSnapshot({ + customDiffConfig: { threshold: 2 }, + noColors: true, +}) +expect.extend({ toMatchImageSnapshot }) + +beforeAll( async() => { + browser = await puppeteer.launch({ headless: true, slowMo: delayMS, args: [`--window-size=${width},${height}`] }) + page = await browser.newPage() + har = new PuppeteerHar(page) + await page.setViewport({ width, height }) + await shell.exec('acceptanceTests/scripts/beforeAll.sh') +}) + +afterAll( async() => { + browser.close() + await shell.exec('acceptanceTests/scripts/afterAll.sh') +}) + +beforeEach(async() => { + await shell.exec('acceptanceTests/scripts/beforeEach.sh') +}) + +describe('Registering', () => { + test('Register a user', async done => { + //start generating a trace file. + await page.tracing.start({path: 'trace/registering_user_har.json',screenshots: true}) + await har.start({path: 'trace/registering_user_trace.har'}) + //ARRANGE + await page.goto('http://localhost:8080/register', { timeout: 30000, waitUntil: 'load' }) + //ACT + await page.type('input[name=user]', 'NewUser') + await page.type('input[name=pass]', 'password') + await page.click('input[type=submit]') + await page.goto('http://localhost:8080/login', { timeout: 30000, waitUntil: 'load' }) + await page.type('input[name=user]', 'NewUser') + await page.type('input[name=pass]', 'password') + await page.click('input[type=submit]') + //ASSERT + //check that the user is taken to the homepage after attempting to login as the new user: + await page.waitForSelector('h1') + expect( await page.evaluate( () => document.querySelector('h1').innerText ) ) + .toBe('Home') + + // grab a screenshot + const image = await page.screenshot() + // compare to the screenshot from the previous test run + expect(image).toMatchImageSnapshot() + // stop logging to the trace files + await page.tracing.stop() + await har.stop() + done() + }, 16000) +}) diff --git a/acceptanceTests/scripts/afterAll.sh b/acceptanceTests/scripts/afterAll.sh new file mode 100644 index 0000000..bbdbd8d --- /dev/null +++ b/acceptanceTests/scripts/afterAll.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +set -e +echo afterAll +#Delete the databases that were used for the acceptance testing. +FILE=website.db +if test -f "$FILE"; then + rm -rf website.db +fi +#Restore the databases from before the acceptance tests were run, and delete the backups. +FILE=websiteBackup.db +if test -f "$FILE"; then + cp websiteBackup.db website.db + rm -rf websiteBackup.db +fi + + diff --git a/acceptanceTests/scripts/beforeAll.sh b/acceptanceTests/scripts/beforeAll.sh new file mode 100644 index 0000000..85f6d0e --- /dev/null +++ b/acceptanceTests/scripts/beforeAll.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +set -e +echo beforeAll + +#Make backups of the databases. +FILE=website.db +if test -f "$FILE"; then + cp website.db websiteBackup.db + rm -rf website.db +fi + diff --git a/acceptanceTests/scripts/beforeEach.sh b/acceptanceTests/scripts/beforeEach.sh new file mode 100644 index 0000000..03decd0 --- /dev/null +++ b/acceptanceTests/scripts/beforeEach.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +set -e +echo beforeEach +#Delete the database files. +FILE=website.db +if test -f "$FILE"; then + rm -rf website.db +fi \ No newline at end of file diff --git a/acceptanceTests/scripts/test.sh b/acceptanceTests/scripts/test.sh new file mode 100644 index 0000000..42e63c6 --- /dev/null +++ b/acceptanceTests/scripts/test.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +set -e +echo hello +mkdir -p screenshots +mkdir -p trace +# [ ! -d "node_modules" ] && echo "INSTALLING MODULES" && npm install +node index.js& +node_modules/.bin/jest --runInBand --detectOpenHandles acceptanceTests/* +read -p "Press enter to continue" +kill %1 diff --git a/jest-test.config.js b/jest-test.config.js index e905299..85c8e34 100644 --- a/jest-test.config.js +++ b/jest-test.config.js @@ -1,9 +1,10 @@ - +'use strict' module.exports = { displayName: 'test', verbose: true, collectCoverage: true, + 'preset': 'jest-puppeteer', coverageThreshold: { global: { branches: 0, diff --git a/jest.puppeteer.config.js b/jest.puppeteer.config.js new file mode 100644 index 0000000..9af998a --- /dev/null +++ b/jest.puppeteer.config.js @@ -0,0 +1,14 @@ + +'use strict' + +module.exports = { + server: { + command: 'node index.js', + port: 8080, + }, + launch: { + headless: false, + devtools: true, + timeout: 30000 + } +} diff --git a/package.json b/package.json index 70ce70d..0d329c3 100644 --- a/package.json +++ b/package.json @@ -1,24 +1,22 @@ { "name": "my_music", "version": "1.1.3", + "author": "Vicente Pastor Pastor, Charles Neal, Luke Charles, Mateusz Watras, Gwyn Robins, Ricards Goldbergs", + "license": "ISC", "description": "My Music Project", "main": "index.js", "engines": { "node": "12.x" }, "scripts": { + "coverage": "node_modules/.bin/jest --coverage 'unit tests/' && ./node_modules/.bin/istanbul check-coverage --statement 100 --branch 100 --function 100 --line 100", + "dependency": "node_modules/.bin/dependency-check -i bcrypt --unused --no-dev . && node_modules/.bin/dependency-check -i modules --missing .", "start": "node index.js", "jsdoc": "node_modules/.bin/jsdoc -c jsdoc.conf", "linter": "node_modules/.bin/eslint .", - "unit": "node_modules/.bin/jest --coverage 'unit tests/'" - }, - "jest": { - "projects": [ - "/jest-test.config.js" - ] + "test": "node_modules/.bin/jest --coverage", + "acceptance": "acceptanceTests/scripts/test.sh" }, - "author": "", - "license": "ISC", "dependencies": { "bcryptjs": "^2.4.3", "fs-extra": "^7.0.1", @@ -34,17 +32,33 @@ "markdown": "^0.5.0", "markdownlint-cli": "^0.18.0", "mime-types": "^2.1.25", - "sqlite": "^3.0.3", "sqlite-async": "^1.0.12" }, "devDependencies": { "babel-eslint": "^10.0.3", + "coverage": "^0.4.1", + "dependency": "0.0.1", + "dependency-check": "^4.1.0", "eslint": "^5.15.2", "handlebars-validate": "^0.1.2", "http-status-codes": "^1.3.2", + "istanbul": "^0.4.5", "jest": "^24.9.0", + "jest-image-snapshot": "^2.11.0", + "jest-puppeteer": "^4.3.0", + "jscpd": "^2.0.16", "jsdoc": "^3.6.3", "jsdoc-route-plugin": "^0.1.0", - "markdownlint": "^0.17.0" + "markdownlint": "^0.17.0", + "puppeteer": "^1.20.0", + "puppeteer-har": "^1.1.1", + "start-server-and-test": "^1.10.6", + "supertest": "^4.0.2" + }, + "jest": { + "projects": [ + "/jest-test.config.js" + ], + "preset": "jest-puppeteer" } } diff --git a/unit tests/user.spec.js b/unitTests/user.spec.js similarity index 97% rename from unit tests/user.spec.js rename to unitTests/user.spec.js index fd6c64a..080cd2a 100644 --- a/unit tests/user.spec.js +++ b/unitTests/user.spec.js @@ -1,6 +1,6 @@ -const Accounts = require('../modules/user.js') +const Accounts = require('../modules/accounts.js') describe('register()', () => {