From 54ee50eceaa0b6fc5ba4982369be082acce3c881 Mon Sep 17 00:00:00 2001 From: Dusk Date: Sun, 17 Oct 2021 16:28:27 -0400 Subject: [PATCH] ticket 11 completed, all test passed --- src/routes/GetUserRoute.test.ts | 96 +++++++++++++------------- src/routes/GetUserRoute.ts | 116 ++++++++++++++++---------------- 2 files changed, 106 insertions(+), 106 deletions(-) diff --git a/src/routes/GetUserRoute.test.ts b/src/routes/GetUserRoute.test.ts index 87a10d2..02b9b7f 100644 --- a/src/routes/GetUserRoute.test.ts +++ b/src/routes/GetUserRoute.test.ts @@ -1,48 +1,48 @@ -import { TEST_AUTH_COOKIE } from '../../jest.setup'; -import User, { UserDocument } from '../models/User'; -import TestUtils from '../utils/TestUtils'; - -/** - * TODO: (11.05) - * - Remove the ".skip" from the following function. - * - Go to your terminal and run the following command: - * npm run test GetUser - * - Delete this comment. - */ -describe.skip('GET /users/:id', () => { - let user: UserDocument = null; - - beforeAll(async () => { - user = await User.create({ phoneNumber: '9095250112' }); - }); - - test('If the user is not authenticated, should return a 401.', async () => { - await TestUtils.agent.get(`/users/${user._id}`).expect(401); - }); - - test('If the given user ID is not valid, should return a 400.', async () => { - await TestUtils.agent - .get(`/users/${TestUtils.INVALID_ID}`) - .set('Cookie', TEST_AUTH_COOKIE) - .expect(400); - }); - - test('If there is no user found with the given ID, should return a 404.', async () => { - await TestUtils.agent - .get(`/users/${TestUtils.NOT_FOUND_ID}`) - .set('Cookie', TEST_AUTH_COOKIE) - .expect(404); - }); - - test('If there IS a user found with the given ID, should return a 200.', async () => { - await TestUtils.agent - .get(`/users/${user._id}`) - .set('Cookie', TEST_AUTH_COOKIE) - .expect(200) - .then(({ body }) => { - expect(body).not.toBeNull(); - expect(body._id.toString()).toBe(user._id.toString()); - expect(body.phoneNumber).toBe(user.phoneNumber); - }); - }); -}); +import { TEST_AUTH_COOKIE } from '../../jest.setup'; +import User, { UserDocument } from '../models/User'; +import TestUtils from '../utils/TestUtils'; + +/** + * TODO: (11.05) + * - Remove the ".skip" from the following function. + * - Go to your terminal and run the following command: + * npm run test GetUser + * - Delete this comment. + */ +describe('GET /users/:id', () => { + let user: UserDocument = null; + + beforeAll(async () => { + user = await User.create({ phoneNumber: '9095250112' }); + }); + + test('If the user is not authenticated, should return a 401.', async () => { + await TestUtils.agent.get(`/users/${user._id}`).expect(401); + }); + + test('If the given user ID is not valid, should return a 400.', async () => { + await TestUtils.agent + .get(`/users/${TestUtils.INVALID_ID}`) + .set('Cookie', TEST_AUTH_COOKIE) + .expect(400); + }); + + test('If there is no user found with the given ID, should return a 404.', async () => { + await TestUtils.agent + .get(`/users/${TestUtils.NOT_FOUND_ID}`) + .set('Cookie', TEST_AUTH_COOKIE) + .expect(404); + }); + + test('If there IS a user found with the given ID, should return a 200.', async () => { + await TestUtils.agent + .get(`/users/${user._id}`) + .set('Cookie', TEST_AUTH_COOKIE) + .expect(200) + .then(({ body }) => { + expect(body).not.toBeNull(); + expect(body._id.toString()).toBe(user._id.toString()); + expect(body.phoneNumber).toBe(user.phoneNumber); + }); + }); +}); diff --git a/src/routes/GetUserRoute.ts b/src/routes/GetUserRoute.ts index aab6135..3722cd5 100644 --- a/src/routes/GetUserRoute.ts +++ b/src/routes/GetUserRoute.ts @@ -1,58 +1,58 @@ -import { param } from 'express-validator'; - -import User, { UserDocument } from '../models/User'; -import { ApplicationRequest } from '../utils/ApplicationRequest'; -import BaseRoute from '../utils/BaseRoute'; -import { RouteMethod } from '../utils/constants'; -import MiddlewareUtils from '../utils/MiddlewareUtils'; -import { IdArgs } from '../utils/types'; - -type GetUserRequest = ApplicationRequest; - -export default class GetUserRoute extends BaseRoute { - constructor() { - super({ - /** - * TODO: (11.01) - * - Should the user be authenticated to hit this route? - * - Replace null with the correct route type from the RouteMethod enum - * in the constants.ts file. - * - Fill in the path string with the appropriate path to this endpoint. - * - Delete this comment. - */ - authenticated: false, - method: null, - path: '/' - }); - } - - /** - * Validate the following inputs: - * - params.id - */ - middleware() { - // TODO: (11.02) Just read this and try your best to understand what's - // going on here. - return [ - param('id') - .custom(MiddlewareUtils.isMongoId) - .custom((id: string) => MiddlewareUtils.isFound(User, { _id: id })) - .withMessage((id: string) => ({ - message: `Could not find user with the ID: ${id}.`, - statusCode: 404 - })) - ]; - } - - /** - * Returns the user with the given ID. - */ - async content(req: GetUserRequest): Promise { - // TODO: (11.03) Get the id of the user from the request parameters. - - // TODO: (11.04) Fetch the user associated with the ID. - - // TODO: (11.04) Return the user! - return null; - } -} +import { param } from 'express-validator'; + +import User, { UserDocument } from '../models/User'; +import { ApplicationRequest } from '../utils/ApplicationRequest'; +import BaseRoute from '../utils/BaseRoute'; +import { RouteMethod } from '../utils/constants'; +import MiddlewareUtils from '../utils/MiddlewareUtils'; +import { IdArgs } from '../utils/types'; + +type GetUserRequest = ApplicationRequest; + +export default class GetUserRoute extends BaseRoute { + constructor() { + super({ + /** + * TODO: (11.01) + * - Should the user be authenticated to hit this route? + * - Replace null with the correct route type from the RouteMethod enum + * in the constants.ts file. + * - Fill in the path string with the appropriate path to this endpoint. + * - Delete this comment. + */ + // authenticated: true, + method: RouteMethod.GET, + path: '/users/:id' + }); + } + + /** + * Validate the following inputs: + * - params.id + */ + middleware() { + // TODO: (11.02) Just read this and try your best to understand what's + // going on here. + return [ + param('id') + .custom(MiddlewareUtils.isMongoId) // checking id is mongo id + .custom((id: string) => MiddlewareUtils.isFound(User, { _id: id })) // checking id is in database + .withMessage((id: string) => ({ + message: `Could not find user with the ID: ${id}.`, // error message and status code if id fails validations + statusCode: 404 + })) + ]; + } + + /** + * Returns the user with the given ID. + */ + async content(req: GetUserRequest): Promise { + // TODO: (11.03) Get the id of the user from the request parameters. + const { id: userID } = req.params; + // TODO: (11.04) Fetch the user associated with the ID. + const thisUser: UserDocument = await User.findById(userID); + // TODO: (11.04) Return the user! + return thisUser; + } +}