Skip to content

Commit

Permalink
Merge pull request #47 from CS3219-AY2425S1/shishir/setup-delete-api-…
Browse files Browse the repository at this point in the history
…user-service

Setup DELETE API for Hard Deletion of User
  • Loading branch information
shishirbychapur authored Sep 21, 2024
2 parents d3ad0c8 + 8516927 commit a80dc88
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @shishirbychapur @glemenneo @Daviancold @lynnlow175 @yunruu
33 changes: 24 additions & 9 deletions backend/user-service/__tests__/routes/user.routes.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import 'express-async-errors'

import { MongoDBContainer, StartedMongoDBContainer } from '@testcontainers/mongodb'
import { generateKeyPairSync } from 'crypto'
import express, { Express } from 'express'
import 'express-async-errors'
import mongoose from 'mongoose'
import request from 'supertest'

import { Proficiency } from '../../src/types/Proficiency'
import { Role } from '../../src/types/Role'
import config from '../../src/common/config.util'
import logger from '../../src/common/logger.util'
import connectToDatabase from '../../src/common/mongodb.util'
import defaultErrorHandler from '../../src/middlewares/errorHandler.middleware'
import { generateKeyPairSync } from 'crypto'
import logger from '../../src/common/logger.util'
import mongoose from 'mongoose'
import request from 'supertest'
import userRouter from '../../src/routes/user.routes'
import { Proficiency } from '../../src/types/Proficiency'
import { Role } from '../../src/types/Role'

jest.mock('../../src/common/config.util', () => ({
NODE_ENV: 'test',
Expand Down Expand Up @@ -100,7 +102,7 @@ describe('User Routes', () => {
})
})

describe('PUT /users', () => {
describe('PUT /users/:id', () => {
it('should return 200 for successful update', async () => {
const user1 = await request(app).post('/users').send(CREATE_USER_DTO1)
const response = await request(app).put(`/users/${user1.body.id}`).send({
Expand All @@ -121,7 +123,6 @@ describe('User Routes', () => {
it('should return 400 for invalid requests and a list of errors', async () => {
const response = await request(app).put('/users/111').send({})
expect(response.status).toBe(400)
expect(response.body).toHaveLength(1)
})
it('should return 409 for duplicate username', async () => {
const user1 = await request(app).post('/users').send(CREATE_USER_DTO1)
Expand All @@ -133,4 +134,18 @@ describe('User Routes', () => {
expect(response.status).toBe(409)
})
})

describe('DELETE /users/:id', () => {
it('should return 200 for successful deletion', async () => {
const user1 = await request(app).post('/users').send(CREATE_USER_DTO1)
const response = await request(app).delete(`/users/${user1.body.id}`).send()
expect(response.status).toBe(200)
const confirmation = await request(app).delete('/users/${user1.body.id}').send()
expect(confirmation.status).toBe(400)
})
it('should return 400 for requests with invalid ids', async () => {
const response = await request(app).delete('/users/111').send()
expect(response.status).toBe(400)
})
})
})
18 changes: 15 additions & 3 deletions backend/user-service/src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { createUser, findOneUserByEmail, findOneUserByUsername, updateUser } from '../models/user.repository'
import {
createUser,
deleteUser,
findOneUserByEmail,
findOneUserByUsername,
updateUser,
} from '../models/user.repository'

import { ValidationError } from 'class-validator'
import { Response } from 'express'
import { CreateUserDto } from '../types/CreateUserDto'
import { Response } from 'express'
import { TypedRequest } from '../types/TypedRequest'
import { UserDto } from '../types/UserDto'
import { UserProfileDto } from '../types/UserProfileDto'
import { ValidationError } from 'class-validator'
import { hashPassword } from './auth.controller'

export async function handleCreateUser(request: TypedRequest<CreateUserDto>, response: Response): Promise<void> {
Expand Down Expand Up @@ -55,3 +61,9 @@ export async function handleUpdateProfile(request: TypedRequest<UserProfileDto>,
const user = await updateUser(id, createDto)
response.status(200).json(user).send()
}

export async function handleDeleteUser(request: TypedRequest<void>, response: Response): Promise<void> {
const id = request.params.id
await deleteUser(id)
response.status(200).send()
}
3 changes: 2 additions & 1 deletion backend/user-service/src/routes/user.routes.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { handleCreateUser, handleUpdateProfile } from '../controllers/user.controller'
import { handleCreateUser, handleDeleteUser, handleUpdateProfile } from '../controllers/user.controller'

import { Router } from 'express'

const router = Router()

router.post('/', handleCreateUser)
router.put('/:id', handleUpdateProfile)
router.delete('/:id', handleDeleteUser)

export default router

0 comments on commit a80dc88

Please sign in to comment.