Skip to content
Permalink
Browse files
first commit
  • Loading branch information
nellurib committed Jul 23, 2020
0 parents commit 462a1d3fc3dafab166db19214052d67a46eb8ef2
Show file tree
Hide file tree
Showing 26 changed files with 11,957 additions and 0 deletions.
@@ -0,0 +1,6 @@
views/register.handlebars
views/error.handlebars

docs/**
node_modules/**
coverage/**
@@ -0,0 +1,77 @@

{
"env": {
"es6": true,
"jasmine": true,
"node": true,
"browser": true,
"jest": true
},
"parserOptions": {
"ecmaVersion": 2018,
"noInlineConfig": true,
"reportUnusedDisableDirectives": true,
"sourceType": "module"
},
"rules": {
"arrow-body-style": "error",
"arrow-spacing": ["warn", {"before": true, "after": true}],
"brace-style": "error",
"camelcase": ["error", {"properties": "never"}],
"complexity": ["error", 5],
"eol-last": "warn",
"eqeqeq": "error",
"func-call-spacing": ["error", "never"],
"global-require": "error",
"handle-callback-err": "warn",
"indent": ["warn", "tab", {"SwitchCase": 1}],
"key-spacing": ["error", {"beforeColon": false, "afterColon": true}],
"linebreak-style": ["warn", "unix"],
"max-depth": ["error", 3],
"max-len": ["warn", { "code": 120, "tabWidth": 4 }],
"max-lines": ["warn", {"max": 150, "skipBlankLines": true, "skipComments": true}],
"max-lines-per-function": ["warn", {"max": 20, "skipBlankLines": true, "skipComments": true}],
"max-nested-callbacks": ["error", 4],
"max-params": ["error", 5],
"max-statements": ["error", 20],
"no-cond-assign": "error",
"no-dupe-args": "error",
"no-dupe-keys": "error",
"no-duplicate-case": "error",
"no-empty": "warn",
"no-empty-function": "error",
"no-multiple-empty-lines": "warn",
"no-extra-parens": "error",
"no-func-assign": "error",
"no-irregular-whitespace": "error",
"no-magic-numbers": ["warn", {"ignore": [-1, 0, 1]}],
"no-multi-spaces": "warn",
"no-multi-str": "off",
"no-unexpected-multiline": "error",
"no-unreachable": "error",
"no-self-assign": "error",
"no-trailing-spaces": "warn",
"no-undef": "error",
"no-unused-vars": "warn",
"no-var": 2,
"prefer-arrow-callback": "warn",
"prefer-const": "error",
"prefer-template": "error",
"quotes": ["warn", "single"],
"semi": ["warn", "never"],
"space-before-blocks": ["error", { "functions": "always", "keywords": "always", "classes": "always" }],
"space-before-function-paren": ["error", "never"],
"strict": ["error", "global"],
"yoda": "error"
},
"overrides": [{
"files": [ "*.test.js", "*.spec.js", "*.steps.js" ],
"rules": {
"global-require": "off",
"max-lines-per-function": "off",
"max-lines": "off",
"max-statements": "off",
"no-magic-numbers": "off"
}
}]
}
@@ -0,0 +1,29 @@
#!/bin/bash

set -e

echo "POST-CHECKOUT"

prevHEAD=$1
newHEAD=$2
checkoutType=$3

[[ $checkoutType == 1 ]] && checkoutType='branch' || checkoutType='file' ;

echo " Checkout type: $checkoutType"
echo " prev HEAD: "`git name-rev --name-only $prevHEAD`
echo " new HEAD: "`git name-rev --name-only $newHEAD`

# this is a file checkout – do nothing
if [ "$3" == "0" ]; then exit; fi

BRANCH_NAME=$(git symbolic-ref --short -q HEAD)
NUM_CHECKOUTS=`git reflog --date=local | grep -o ${BRANCH_NAME} | wc -l`

#if the refs of the previous and new heads are the same
#AND the number of checkouts equals one, a new branch has been created
if [ "$1" == "$2" ] && [ ${NUM_CHECKOUTS} -eq 1 ]; then
echo " new branch '$BRANCH_NAME' created"
fi
echo
coverage/lcov-report/base.css
@@ -0,0 +1,8 @@
coverage/lcov-report/user.js.html
#!/bin/sh

set -e # using the options command to abort script at first error
echo
echo "POST-COMMIT"
# ./node_modules/.bin/markdownlint --ignore node_modules .
echo
@@ -0,0 +1,76 @@
#!/bin/sh

set -e # using the options command to abort script at first error
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
if [ "$branch" = "master" ]; then
read -p " You are about to commit to the master branch, are you sure? [y|n] " -n 1 -r < /dev/tty
echo
if echo $REPLY | grep -E '^[Yy]$' > /dev/null
then
exit 0 # commit will execute
fi
exit 1 # commit will not execute
fi

# is the current branch a direct child of the master branch?
# echo "checking parent branch"
# PARENT=$(git show-branch -a | grep -v `git rev-parse --abbrev-ref HEAD` | grep -v origin | sed 's/.*\[\(.*\)\].*/\1/' | grep -v -e '^$' | grep -v "^----$"
# echo "parent branch is $PARENT")

# 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..."
@@ -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
@@ -0,0 +1,31 @@

.DS_Store
node_modules/
coverage/
screenshots/*
docs/

data/
coverage/
sessions/
screenshots/
__image_snapshots_/
__diff_output__/
trace/
.node-persist/

*.db
*snap.png
*diff.png
*trace.json
*.0x

config.json
#Should probably be re-added
#Or at least provide documentation on how to populate

*.pptx
*.mp4

*.codio
*.c9
@@ -0,0 +1,31 @@

const Koa = require('koa')
const session = require('koa-session')
const staticDir = require('koa-static')
const views = require('koa-views')

const apiRouter = require('./routes/routes')

const app = new Koa()
app.keys = ['darkSecret']

const defaultPort = 8080
const port = process.env.PORT || defaultPort

app.use(staticDir('public'))
app.use(session(app))
app.use(views(`${__dirname}/views`, { extension: 'handlebars' }, {map: { handlebars: 'handlebars' }}))

app.use( async(ctx, next) => {
console.log(`${ctx.method} ${ctx.path}`)
ctx.hbs = {
authorised: ctx.session.authorised,
host: `https://${ctx.host}`
}
for(const key in ctx.query) ctx.hbs[key] = ctx.query[key]
await next()
})

app.use(apiRouter.routes(), apiRouter.allowedMethods())

module.exports = app.listen(port, async() => console.log(`listening on port ${port}`))
@@ -0,0 +1,22 @@

{
"tags": {
"allowUnknownTags": true,
"dictionaries": ["jsdoc","closure"]
},
"source": {
"include": [ "." ],
"exclude": [ "node_modules" ],
"includePattern": ".+\\.js(doc|x)?$",
"excludePattern": "(^|\\/|\\\\)_"
},
"plugins": ["jsdoc-route-plugin"],
"templates": {
"cleverLinks": false,
"monospaceLinks": false
},
"opts": {
"destination": "docs/jsdoc",
"recurse": true
}
}
@@ -0,0 +1,62 @@

const bcrypt = require('bcrypt-promise')
const sqlite = require('sqlite-async')

const saltRounds = 10

module.exports = class User {

constructor(dbName = ':memory:') {
return (async() => {
this.db = await sqlite.open(dbName)
// we need this table to store the user accounts
const sql = 'CREATE TABLE IF NOT EXISTS users\
(id INTEGER PRIMARY KEY AUTOINCREMENT, user TEXT, pass TEXT, email TEXT);'
await this.db.run(sql)
return this
})()
}

/**
* registers a new user
* @param {String} user the chosen username
* @param {String} pass the chosen password
* @returns {Boolean} returns true if the new user has been added
*/
async register(user, pass, email) {
Array.from(arguments).forEach( val => {
if(val.length === 0) throw new Error('missing field')
})
let sql = `SELECT COUNT(id) as records FROM users WHERE user="${user}";`
const data = await this.db.get(sql)
if(data.records !== 0) throw new Error(`username "${user}" already in use`)
sql = `SELECT COUNT(id) as records FROM users WHERE email="${email}";`
const emails = await this.db.get(sql)
if(emails.records !== 0) throw new Error(`email address "${email}" is already in use`)
pass = await bcrypt.hash(pass, saltRounds)
sql = `INSERT INTO users(user, pass, email) VALUES("${user}", "${pass}", "${email}")`
await this.db.run(sql)
return true
}

/**
* checks to see if a set of login credentials are valid
* @param {String} username the username to check
* @param {String} password the password to check
* @returns {Boolean} returns true if credentials are valid
*/
async login(username, password) {
let sql = `SELECT count(id) AS count FROM users WHERE user="${username}";`
const records = await this.db.get(sql)
if(!records.count) throw new Error(`username "${username}" not found`)
sql = `SELECT pass FROM users WHERE user = "${username}";`
const record = await this.db.get(sql)
const valid = await bcrypt.compare(password, record.pass)
if(valid === false) throw new Error(`invalid password for account "${username}"`)
return true
}

async tearDown() {
await this.db.close()
}
}

0 comments on commit 462a1d3

Please sign in to comment.