-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from CS3219-AY2425S1/8-be-user-service-impleme…
…nt-post-api-for-user-creation Add POST API for user creation
- Loading branch information
Showing
10 changed files
with
1,002 additions
and
78 deletions.
There are no files selected for viewing
30 changes: 0 additions & 30 deletions
30
backend/user-service/__tests__/models/user.repository.test.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.