forked from 205CDE-1819JANMAY/TEACHING-MATERIALS
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
13 changed files
with
220 additions
and
6 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
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,51 @@ | ||
|
||
'use strict' | ||
|
||
const records = [ | ||
{ | ||
id: 0, | ||
user: 'jdoe', | ||
pass: '$2b$10$vPqO/uGlKchrQCqyBIKdb.8hLEJgaC4aAg4fpre5rausycX1XmkWy' | ||
} | ||
] | ||
|
||
module.exports.open = function() { | ||
return { | ||
all: async sql => { | ||
console.log(`MOCK ${sql}`) | ||
let field = sql.match(/(?<=SELECT\s+).*?(?=\s+FROM)/g)[0] | ||
const condition = sql.match(/\bWHERE\s+(.*)$/g)[0].replace('WHERE ', '').replace(';', '') | ||
const key = condition.split('=')[0].trim() | ||
const val = condition.split('=')[1].replace(/"/g, '').trim() | ||
console.log(`field: "${field}", condition: "${condition}", key: "${key}", val: "${val}"`) | ||
if(field === 'count(id) AS count') field = 'id' | ||
console.log(`field: "${field}"`) | ||
let data = [] | ||
console.log(`${field} : ${key} : ${val}`) | ||
for(const record of records) { | ||
//console.log(record) | ||
console.log(record[field]) | ||
console.log(`"${record[field]}" : "${val}"`) | ||
if(record[field] == val) { | ||
console.log('matching record') | ||
data.push(record) | ||
} | ||
} | ||
console.log(data) | ||
let result = {} | ||
console.log(`field ${field} length: ${field.length}`) | ||
if(field === 'id') { | ||
console.log('need count...') | ||
result.count = data.length | ||
} else { | ||
console.log('need data...') | ||
result[field] = records[0][field] | ||
} | ||
console.log(result) | ||
return result | ||
}, | ||
run: async() => true, // we can just ignore these. | ||
close: async() => true // pretend to close the database. | ||
} | ||
} | ||
|
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,22 @@ | ||
|
||
'use strict' | ||
|
||
const sqlite = require('sqlite-async') | ||
|
||
module.exports.open = function() { | ||
return { | ||
all: async sql => { | ||
console.log(`MOCK ${sql}`) | ||
const db = await sqlite.open(':memory:') | ||
await db.run('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, user TEXT, pass TEXT);') | ||
const user = 'jdoe' | ||
const pass = '$2b$10$vPqO/uGlKchrQCqyBIKdb.8hLEJgaC4aAg4fpre5rausycX1XmkWy' | ||
await db.run(`INSERT INTO URLSearchParams(user, pass) VALUES("${user}", "${pass}");`) | ||
const data = await db.all(sql) | ||
await db.close() | ||
return data | ||
}, | ||
run: async() => true, // we can just ignore these. | ||
close: async() => true // pretend to close the database. | ||
} | ||
} |
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
BIN
+55.6 KB
(100%)
exercises/07_forms_auth/public/avatars/avatar.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,29 @@ | ||
|
||
'use strict' | ||
|
||
const records = [ | ||
{ | ||
user: 'jdoe', | ||
pass: '$2b$10$vPqO/uGlKchrQCqyBIKdb.8hLEJgaC4aAg4fpre5rausycX1XmkWy' | ||
} | ||
] | ||
|
||
//const sql = 'SELECT count(id) AS count FROM users WHERE user="jdoe";' | ||
const sql = 'SELECT user FROM users WHERE user="jdoe"' | ||
const field = sql.match(/(?<=SELECT\s+).*?(?=\s+FROM)/g)[0] | ||
const condition = sql.match(/\bWHERE\s+(.*)$/g)[0].replace('WHERE ', '').replace(';', '') | ||
const key = condition.split('=')[0] | ||
const val = condition.split('=')[1].replace(/"/g, '') | ||
console.log(field) | ||
console.log(key) | ||
console.log(val) | ||
|
||
let data = [] | ||
for(const record of records) if(record[key] == val) data.push(record[key]) | ||
let result = {} | ||
if(field === 'count(id) AS count') { | ||
result.count = data.length | ||
} else { | ||
result[field] = val | ||
} | ||
console.log(result) |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
#!/usr/bin/env bash | ||
|
||
node index.js& | ||
npm run test | ||
node_modules/.bin/jest --detectOpenHandles tests/acceptance/ | ||
kill %1 |
File renamed without changes.
Empty file.
Empty file.
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,62 @@ | ||
|
||
'use strict' | ||
|
||
const accounts = require('../../modules/accounts.js') | ||
//jest.mock('sqlite-async') | ||
|
||
beforeAll( async() => { | ||
console.log('Jest starting!') | ||
// we insert a single user into the database | ||
const sqlite = require('sqlite-async') | ||
const bcrypt = require('bcrypt-promise') | ||
const pass = await bcrypt.hash('goodPassword', 10) | ||
const db = await sqlite.open('./website.db') | ||
await db.run('DROP TABLE IF EXISTS users;') | ||
await db.run('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, user TEXT, pass TEXT);') | ||
console.log(`INSERT INTO users(user, pass) VALUES("jdoe", "${pass}"`) | ||
await db.run(`INSERT INTO users(user, pass) VALUES("jdoe", "${pass}")`) | ||
await db.close() | ||
console.log('database built') | ||
}) | ||
|
||
describe('checkCredentials()', () => { | ||
test('returns true if valid username and password', async done => { | ||
expect.assertions(1) | ||
try { | ||
const result = await accounts.checkCredentials('jdoe', 'goodPassword') | ||
expect(result).toBe(true) | ||
} catch(err) { | ||
console.log(`ERROR: ${err.message}`) | ||
} finally { | ||
done() | ||
} | ||
}) | ||
|
||
test('throws error if invalid username', async done => { | ||
expect.assertions(1) | ||
try { | ||
await accounts.checkCredentials('johndoe', 'goodPassword') | ||
//expect(result).toBe(true) | ||
} catch(err) { | ||
expect(err.message).toBe('invalid username') | ||
} finally { | ||
done() | ||
} | ||
}) | ||
|
||
test('throws error if invalid password', async done => { | ||
expect.assertions(1) | ||
try { | ||
await accounts.checkCredentials('jdoe', 'badPassword') | ||
//expect(result).toBe(true) | ||
} catch(err){ | ||
expect(err.message).toBe('invalid password') | ||
} finally { | ||
done() | ||
} | ||
}) | ||
}) | ||
|
||
describe('addUser()', () => { | ||
// TODO | ||
}) |
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,33 @@ | ||
|
||
'use strict' | ||
|
||
const request = require('supertest') | ||
const status = require('http-status-codes') | ||
|
||
const server = require('../../index.js') | ||
|
||
beforeAll( async() => console.log('Jest starting!')) | ||
|
||
describe('GET /', () => { | ||
// TODO | ||
}) | ||
|
||
describe('GET /register', () => { | ||
// TODO | ||
}) | ||
|
||
describe('POST /register', () => { | ||
// TODO | ||
}) | ||
|
||
describe('GET /login', () => { | ||
// TODO | ||
}) | ||
|
||
describe('POST /login', () => { | ||
// TODO | ||
}) | ||
|
||
describe('GET /logout', () => { | ||
// TODO | ||
}) |