continuing to lay out testing on back end
This commit is contained in:
@@ -1,12 +1,11 @@
|
|||||||
import { IUser, IUserAuth } from "../schemas";
|
import { IUser, IUserAuth } from "../schemas";
|
||||||
import { User } from "../models/user";
|
import { User } from "../models/user";
|
||||||
import { Collection } from "../models/collection";
|
|
||||||
import createError from "http-errors";
|
import createError from "http-errors";
|
||||||
import bcrypt from "bcrypt";
|
import bcrypt from "bcrypt";
|
||||||
import now from "../util/now";
|
import now from "../util/now";
|
||||||
|
|
||||||
const UserInstance = new User();
|
const UserInstance = new User();
|
||||||
const CollectionInstance = new Collection();
|
|
||||||
export default class AuthService {
|
export default class AuthService {
|
||||||
// methods for local strategies
|
// methods for local strategies
|
||||||
async register(data: IUser) {
|
async register(data: IUser) {
|
||||||
@@ -18,8 +17,6 @@ export default class AuthService {
|
|||||||
data.isadmin = false;
|
data.isadmin = false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let receivedUser: IUser | undefined;
|
|
||||||
|
|
||||||
// not allowed to use email address that already exists
|
// not allowed to use email address that already exists
|
||||||
const user = await UserInstance.getOneByEmail(email);
|
const user = await UserInstance.getOneByEmail(email);
|
||||||
if (user) throw createError('409', 'Email already in use');
|
if (user) throw createError('409', 'Email already in use');
|
||||||
|
|||||||
11
server/jest/helpers/randomEmail.ts
Normal file
11
server/jest/helpers/randomEmail.ts
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
import alphanumeric from "../../util/alphanumeric";
|
||||||
|
|
||||||
|
export default function randomEmail() {
|
||||||
|
let randomName = ''
|
||||||
|
for (let i = 0; i < 20; i++) {
|
||||||
|
const char = Math.floor(Math.random() * alphanumeric.length);
|
||||||
|
randomName += alphanumeric[char];
|
||||||
|
}
|
||||||
|
|
||||||
|
return randomName + '@testemail.com';
|
||||||
|
}
|
||||||
11
server/jest/helpers/randomHandle.ts
Normal file
11
server/jest/helpers/randomHandle.ts
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
import alphanumeric from "../../util/alphanumeric";
|
||||||
|
|
||||||
|
export default function randomHandle() {
|
||||||
|
let randomHandle = '';
|
||||||
|
for (let i = 0; i < 20; i++) {
|
||||||
|
const char = Math.floor(Math.random() * alphanumeric.length);
|
||||||
|
randomHandle += alphanumeric[char];
|
||||||
|
}
|
||||||
|
|
||||||
|
return randomHandle;
|
||||||
|
}
|
||||||
79
server/jest/tests/auth/index.test.ts
Normal file
79
server/jest/tests/auth/index.test.ts
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
import AuthService from "../../../auth";
|
||||||
|
import request from 'supertest';
|
||||||
|
import randomEmail from "../../helpers/randomEmail";
|
||||||
|
import randomHandle from "../../helpers/randomHandle";
|
||||||
|
import { IUser, IUserAuth } from "../../../schemas";
|
||||||
|
|
||||||
|
const server = request.agent('localhost:8080');
|
||||||
|
const myAccount = {
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('class AuthService', () => {
|
||||||
|
let mockUser: IUser;
|
||||||
|
beforeEach(() => {
|
||||||
|
mockUser = {
|
||||||
|
firstname: "mock",
|
||||||
|
lastname: "user",
|
||||||
|
email: randomEmail(),
|
||||||
|
password: 'testpassword',
|
||||||
|
handle: randomHandle(),
|
||||||
|
isadmin: false
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// save user data from successful registration attempt
|
||||||
|
let extantUser: IUser | undefined;
|
||||||
|
|
||||||
|
describe('register', () => {
|
||||||
|
test('register', () => {
|
||||||
|
extantUser = mockUser;
|
||||||
|
|
||||||
|
server
|
||||||
|
.post('/auth/register')
|
||||||
|
.send(mockUser)
|
||||||
|
.set('accept', 'application/json')
|
||||||
|
.end((err, res) => {
|
||||||
|
if (err) throw err;
|
||||||
|
expect(res.status).toEqual(200);
|
||||||
|
expect(res.body.ok).toBe(true);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('login', () => {
|
||||||
|
test('extant user can log in', () => {
|
||||||
|
server.post('/auth/login')
|
||||||
|
.send(extantUser)
|
||||||
|
.end((err, res) => {
|
||||||
|
if (err) throw err;
|
||||||
|
expect(res.status).toEqual(200);
|
||||||
|
expect(res.body.ok).toEqual(true);
|
||||||
|
expect(res.body.user.email).toEqual(extantUser!.email);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('cannot login with incorrect credientials', () => {
|
||||||
|
server.post('/auth/login')
|
||||||
|
.send(mockUser)
|
||||||
|
.end((err, res) => {
|
||||||
|
expect(res.status).toEqual(401);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('cannot login if no matching account exists', () => {
|
||||||
|
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
test('google register', () => {
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
test('google login', () => {
|
||||||
|
|
||||||
|
})
|
||||||
|
*/
|
||||||
|
})
|
||||||
0
server/jest/tests/auth/middlewares.test.ts
Normal file
0
server/jest/tests/auth/middlewares.test.ts
Normal file
@@ -1,4 +1,4 @@
|
|||||||
import now from "../../util/now";
|
import now from "../../../util/now";
|
||||||
|
|
||||||
describe('now utility', () => {
|
describe('now utility', () => {
|
||||||
it('returns a string', () => {
|
it('returns a string', () => {
|
||||||
14
server/jest/tests/util/randomEmail.test.ts
Normal file
14
server/jest/tests/util/randomEmail.test.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import randomEmail from "../../helpers/randomEmail"
|
||||||
|
|
||||||
|
describe('randomEmail', () => {
|
||||||
|
test('returns a string', () => {
|
||||||
|
expect(typeof randomEmail() == 'string').toBeTruthy();
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns unique outputs', () => {
|
||||||
|
const first = randomEmail();
|
||||||
|
const second = randomEmail();
|
||||||
|
|
||||||
|
expect(first).not.toEqual(second);
|
||||||
|
})
|
||||||
|
})
|
||||||
14
server/jest/tests/util/randomHandle.test.ts
Normal file
14
server/jest/tests/util/randomHandle.test.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import randomHandle from "../../helpers/randomHandle"
|
||||||
|
|
||||||
|
describe('randomHandle', () => {
|
||||||
|
test('returns a string', () => {
|
||||||
|
expect(typeof randomHandle() == 'string').toBeTruthy();
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns unique outputs', () => {
|
||||||
|
const first = randomHandle();
|
||||||
|
const second = randomHandle();
|
||||||
|
|
||||||
|
expect(first).not.toEqual(second);
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -10,7 +10,8 @@
|
|||||||
"dev": "rm -rf dist && ./node_modules/.bin/tsc --project ./tsconfig.json --watch & ts-node-dev index.ts",
|
"dev": "rm -rf dist && ./node_modules/.bin/tsc --project ./tsconfig.json --watch & ts-node-dev index.ts",
|
||||||
"prod": "npm run build && node dist/index.js",
|
"prod": "npm run build && node dist/index.js",
|
||||||
"test": "jest --coverage",
|
"test": "jest --coverage",
|
||||||
"test:watch": "jest --watch"
|
"test:watch": "jest --watch",
|
||||||
|
"test:o": "jest -o"
|
||||||
},
|
},
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
|
|||||||
2
server/util/alphanumeric.ts
Normal file
2
server/util/alphanumeric.ts
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
const alphanumeric = 'abcdefghijklmnopqrstuvwxyz1234567890'
|
||||||
|
export default alphanumeric;
|
||||||
Reference in New Issue
Block a user