Skip to content

Commit

Permalink
Merge pull request #26 from CS3219-AY2425S1/8-be-user-service-impleme…
Browse files Browse the repository at this point in the history
…nt-post-api-for-user-creation

Add POST API for user creation
  • Loading branch information
Daviancold authored Sep 19, 2024
2 parents 05f2e08 + 418c2f0 commit 7f74a37
Show file tree
Hide file tree
Showing 10 changed files with 1,002 additions and 78 deletions.
30 changes: 0 additions & 30 deletions backend/user-service/__tests__/models/user.repository.test.ts

This file was deleted.

70 changes: 70 additions & 0 deletions backend/user-service/__tests__/routes/user.routes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { MongoDBContainer, StartedMongoDBContainer } from '@testcontainers/mongodb'
import express, { Express } from 'express'
import mongoose from 'mongoose'
import request from 'supertest'
import logger from '../../src/common/logger.util'
import connectToDatabase from '../../src/common/mongodb.util'
import userRouter from '../../src/routes/user.routes'
import { Proficiency } from '../../src/types/Proficiency'
import { Role } from '../../src/types/Role'

describe('User Routes', () => {
let app: Express
let startedContainer: StartedMongoDBContainer

beforeAll(async () => {
const container: MongoDBContainer = new MongoDBContainer().withExposedPorts(27017)
startedContainer = await container.start()

const connectionString = `${startedContainer.getConnectionString()}?directConnection=true`
logger.info(`[User Routes Test] MongoDB container started on ${connectionString}`)

app = express()
app.use(express.json())
app.use('/users', userRouter)

await connectToDatabase(connectionString)
}, 60000)

afterAll(async () => {
await startedContainer.stop()
logger.info(`[User Routes Test] MongoDB container stopped`)
})

afterEach(async () => {
await mongoose.connection.db!.dropDatabase()
})

describe('POST /users', () => {
const CREATE_USER_DTO = {
username: 'test',
password: 'Test1234!',
email: '[email protected]',
role: Role.ADMIN,
proficiency: Proficiency.INTERMEDIATE,
}
it('should return 201 and return the new user', async () => {
const response = await request(app).post('/users').send(CREATE_USER_DTO)
expect(response.status).toBe(201)
expect(response.body).toEqual(
expect.objectContaining({
id: expect.any(String),
username: 'test',
email: '[email protected]',
role: Role.ADMIN,
proficiency: Proficiency.INTERMEDIATE,
})
)
})
it('should return 400 for invalid requests and a list of errors', async () => {
const response = await request(app).post('/users').send({})
expect(response.status).toBe(400)
expect(response.body).toHaveLength(4)
})
it('should return 409 for duplicate username or email', async () => {
await request(app).post('/users').send(CREATE_USER_DTO)
const response = await request(app).post('/users').send(CREATE_USER_DTO)
expect(response.status).toBe(409)
})
})
})
Loading

0 comments on commit 7f74a37

Please sign in to comment.