in progress: loginUser util for testing
This commit is contained in:
@@ -5,16 +5,18 @@ import { StatusCode } from '../util/types';
|
|||||||
const UserInstance = new User();
|
const UserInstance = new User();
|
||||||
|
|
||||||
export default class UserCtl {
|
export default class UserCtl {
|
||||||
ok?: boolean
|
|
||||||
code?: number
|
|
||||||
|
|
||||||
async getAll() {
|
async getAll() {
|
||||||
try {
|
try {
|
||||||
const users = await UserInstance.getAllUsers();
|
// attempt to get users from database
|
||||||
|
const users: IUser[] | null = await UserInstance.getAllUsers();
|
||||||
|
|
||||||
|
// construct controller response
|
||||||
const ok: boolean = users !== null;
|
const ok: boolean = users !== null;
|
||||||
const code: StatusCode = ok ? StatusCode.OK : StatusCode.NotFound;
|
const code: StatusCode = ok ? StatusCode.OK : StatusCode.NotFound;
|
||||||
const data = ok ? users : "No users found.";
|
const data: IUser[] | string = ok ? users! : "No users found.";
|
||||||
return new ControllerResponse(ok, code, data)
|
|
||||||
|
// send formatted response with either data or informative error message
|
||||||
|
return new ControllerResponse<IUser[] | string>(ok, code, data)
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
throw new Error(error);
|
throw new Error(error);
|
||||||
}
|
}
|
||||||
@@ -23,7 +25,7 @@ export default class UserCtl {
|
|||||||
async post(body: IUser) {
|
async post(body: IUser) {
|
||||||
try {
|
try {
|
||||||
const response = await UserInstance.post(body);
|
const response = await UserInstance.post(body);
|
||||||
const ok: boolean = response !== null;
|
const ok = response !== null;
|
||||||
const code = ok ? StatusCode.NewContent : StatusCode.BadRequest
|
const code = ok ? StatusCode.NewContent : StatusCode.BadRequest
|
||||||
const data = ok ? response : "Bad request"
|
const data = ok ? response : "Bad request"
|
||||||
return new ControllerResponse(ok, code, data);
|
return new ControllerResponse(ok, code, data);
|
||||||
@@ -35,7 +37,7 @@ export default class UserCtl {
|
|||||||
async getOne(id: number | string) {
|
async getOne(id: number | string) {
|
||||||
try {
|
try {
|
||||||
const user = await UserInstance.getOneByID(id);
|
const user = await UserInstance.getOneByID(id);
|
||||||
const ok: boolean = user !== null;
|
const ok = user !== null;
|
||||||
const code = ok ? StatusCode.OK : StatusCode.NotFound;
|
const code = ok ? StatusCode.OK : StatusCode.NotFound;
|
||||||
const data = ok ? user : "User by this ID not found";
|
const data = ok ? user : "User by this ID not found";
|
||||||
return new ControllerResponse(ok, code, data);
|
return new ControllerResponse(ok, code, data);
|
||||||
|
|||||||
19
server/jest/helpers/loginUser.ts
Normal file
19
server/jest/helpers/loginUser.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import request from 'supertest';
|
||||||
|
|
||||||
|
const agent = request('localhost:8080');
|
||||||
|
|
||||||
|
export default async function loginUser(auth: { token: any }) {
|
||||||
|
const onResponse = (err: any, res: any) => {
|
||||||
|
if (err) throw err;
|
||||||
|
auth.token = res.body.token;
|
||||||
|
}
|
||||||
|
|
||||||
|
agent.post('/auth/login')
|
||||||
|
.send({
|
||||||
|
email: "verifieduser@test.com",
|
||||||
|
password: "verifieduser"
|
||||||
|
})
|
||||||
|
.end(onResponse);
|
||||||
|
|
||||||
|
return auth;
|
||||||
|
}
|
||||||
13
server/jest/tests/util/loginUser.test.ts
Normal file
13
server/jest/tests/util/loginUser.test.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import loginUser from "../../helpers/loginUser"
|
||||||
|
|
||||||
|
describe('login user', () => {
|
||||||
|
let auth = { token: undefined }
|
||||||
|
beforeAll(async () => {
|
||||||
|
auth = await loginUser(auth);
|
||||||
|
})
|
||||||
|
|
||||||
|
it('authenticates a hard-coded user', () => {
|
||||||
|
console.log(auth);
|
||||||
|
expect(auth.token).toBeDefined();
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -26,7 +26,7 @@ export const recipeRoute = (app: Express) => {
|
|||||||
const { filterby } = req.query;
|
const { filterby } = req.query;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let result: CtlResponse<IRecipe | string>;
|
let result: CtlResponse<IRecipe[] | string>;
|
||||||
switch (filterby) {
|
switch (filterby) {
|
||||||
case "allaccessible":
|
case "allaccessible":
|
||||||
// result = await recipectl.getAllAccessible(user.id);
|
// result = await recipectl.getAllAccessible(user.id);
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ export const userRoute = (app: Express) => {
|
|||||||
|
|
||||||
// get all users
|
// get all users
|
||||||
router.get('/', async (req, res) => {
|
router.get('/', async (req, res) => {
|
||||||
const result: CtlResponse<IUser | string> = await userCtl.getAll();
|
const result: CtlResponse<IUser[] | string> = await userCtl.getAll();
|
||||||
res.status(result.code).send(result.data);
|
res.status(result.code).send(result.data);
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user