Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
217 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "Jest", | ||
"program": "${workspaceFolder}/node_modules/.bin/jest", | ||
"args": ["--runInBand"], | ||
"console": "integratedTerminal", | ||
"internalConsoleOptions": "neverOpen", | ||
"disableOptimisticBPs": true, | ||
"windows": { | ||
"program": "${workspaceFolder}/node_modules/jest/bin/jest", | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict' | ||
|
||
module.exports = { | ||
displayName: 'test', | ||
verbose: true, | ||
collectCoverage: true, | ||
coverageThreshold: { | ||
global: { | ||
branches: 0, | ||
functions: 0, | ||
lines: 0, | ||
statements: 0 | ||
} | ||
}, | ||
testPathIgnorePatterns: [ | ||
'/node_modules/', | ||
'/__tests__/fixtures/', | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* Accounts module | ||
* @module modules/accounts | ||
*/ | ||
|
||
'use strict' | ||
|
||
var sqlite = require('sqlite-async'); | ||
let bcrypt = require('bcrypt-promise'); | ||
|
||
/** | ||
* This is a generic function that opens the database, executes a query, | ||
* closes the database connection and returns the data. | ||
* @param {String} query - The SQL statement to execute. | ||
* @returns {Object} - the date returned by the query. | ||
*/ | ||
async function runSQL(query) { | ||
try { | ||
console.log(query) | ||
let DBName = "./website.db"; | ||
const db = await sqlite.open(DBName); | ||
const data = await db.all(query); | ||
await db.close(); | ||
if(data.length === 1) return data[0] | ||
return data; | ||
} catch(err) { | ||
throw err | ||
} | ||
} | ||
|
||
module.exports.checkCredentials = async(username, password)=> { | ||
try { | ||
var records = await runSQL(`SELECT count(id) AS count FROM users WHERE user="${username}";`); | ||
if(!records.count) throw new Error("invalid username") | ||
const record = await runSQL(`SELECT pass FROM users WHERE user = "${username}";`) | ||
const valid = await bcrypt.compare(password, record.pass) | ||
if(valid == false) throw new Error(`invalid password`) | ||
return true | ||
} catch(err) { | ||
throw err | ||
} | ||
} | ||
|
||
|
||
|
||
/* ----------------------------- STUB FUNCTIONS ----------------------------- */ | ||
|
||
/** | ||
* This function checks the database to see if a username already exists in | ||
* the database. If it detects a duplicate it throws an exception. | ||
* @param {String} username - The username to check. | ||
* @returns {boolean} - returns true if the username does not exist. | ||
* @throws {Error} - throws an error if the username already exists. | ||
*/ | ||
async function checkNoDuplicateUsername (username) { | ||
return true | ||
} | ||
|
||
/** | ||
* This function takes data from an uploaded image and saves it to the `avatars` directory. The file name will be the username. | ||
* @param {String} path - the location of the uploaded image | ||
* @param {String} mimeType - the mime type of the uploaded file. | ||
* @returns {boolean} - returns true if the image is valid and is saved. | ||
* @throws {TypeError} - throws an error if the file is not a png or jpg image. | ||
*/ | ||
async function saveImage(path, mimetype) { | ||
return true | ||
} | ||
|
||
/** | ||
* This function adds new users to the database. | ||
* @param {String} username - The username to to add. | ||
* @param {String} password - The password to add. | ||
* @returns {boolean} - returns true if the username does not exist. | ||
* @throws {Error} - throws an error if the new user account has been created. | ||
*/ | ||
module.exports.addUser = async(username, password) => { | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
'use strict' | ||
|
||
const Accounts = require('../modules/accounts.js') | ||
|
||
describe('register()', () => { | ||
test('register a valid account', async done => { | ||
expect.assertions(1) | ||
try { | ||
const account = await new Accounts() | ||
// test goes here | ||
} catch(err) { | ||
|
||
} finally { | ||
|
||
} | ||
}) | ||
}) | ||
|
||
describe('login()', () => { | ||
test('log in with valid credentials', done => { | ||
expect.assertions(1) | ||
try { | ||
const account = await new Accounts() | ||
// test goes here | ||
} catch(err) { | ||
|
||
} finally { | ||
|
||
} | ||
}) | ||
}) | ||
|
||
describe('checkCredentials()', () => { | ||
test('check valid credentials', done => { | ||
expect.assertions(1) | ||
try { | ||
const account = await new Accounts() | ||
// test goes here | ||
} catch(err) { | ||
|
||
} finally { | ||
|
||
} | ||
}) | ||
}) |