Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 48 additions & 48 deletions src/routes/GetUserRoute.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
116 changes: 58 additions & 58 deletions src/routes/GetUserRoute.ts
Original file line number Diff line number Diff line change
@@ -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<IdArgs>;

export default class GetUserRoute extends BaseRoute<UserDocument> {
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<UserDocument> {
// 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<IdArgs>;
export default class GetUserRoute extends BaseRoute<UserDocument> {
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<UserDocument> {
// 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;
}
}