-
Notifications
You must be signed in to change notification settings - Fork 0
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 #175 from mapeveri/feat/174
Add get user endpoint
- Loading branch information
Showing
5 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
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
31 changes: 31 additions & 0 deletions
31
src/languages/app/controllers/v1/user/userGetController.ts
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,31 @@ | ||
import { | ||
ApiBadRequestResponse, | ||
ApiInternalServerErrorResponse, | ||
ApiOkResponse, | ||
ApiTags, | ||
ApiUnauthorizedResponse, | ||
} from '@nestjs/swagger'; | ||
import { Controller, Get, HttpCode, Inject, Param, UseGuards } from '@nestjs/common'; | ||
import { NestJwtAuthGuard } from '@src/shared/guards/nestJwtAuthGuard'; | ||
import { QUERY_BUS, QueryBus } from '@src/shared/domain/bus/queryBus/queryBus'; | ||
import FindUserQuery from '@src/languages/application/user/query/findUserQuery'; | ||
import UserGetResponse from '@src/languages/app/controllers/v1/user/userGetResponse'; | ||
|
||
@ApiTags('Users') | ||
@Controller() | ||
export default class UserGetController { | ||
public constructor(@Inject(QUERY_BUS) private queryBus: QueryBus) {} | ||
|
||
@Get('users/:id') | ||
@HttpCode(200) | ||
@ApiOkResponse({ type: UserGetResponse }) | ||
@ApiBadRequestResponse({ description: 'Bad Request.' }) | ||
@ApiUnauthorizedResponse({ description: 'Unauthorized.' }) | ||
@ApiInternalServerErrorResponse({ description: 'Internal Server Error.' }) | ||
@UseGuards(NestJwtAuthGuard) | ||
async run(@Param('id') id: string): Promise<UserGetResponse> { | ||
const user = await this.queryBus.ask(new FindUserQuery(id)); | ||
|
||
return user.content; | ||
} | ||
} |
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,34 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsArray, IsEmail, IsNotEmpty, IsString } from 'class-validator'; | ||
|
||
export default class UserGetResponse { | ||
@ApiProperty() | ||
@IsNotEmpty() | ||
@IsString() | ||
id: string; | ||
|
||
@ApiProperty() | ||
@IsNotEmpty() | ||
@IsString() | ||
name: string; | ||
|
||
@ApiProperty() | ||
@IsNotEmpty() | ||
@IsString() | ||
provider: string; | ||
|
||
@ApiProperty() | ||
@IsNotEmpty() | ||
@IsEmail() | ||
email: string; | ||
|
||
@ApiProperty() | ||
@IsNotEmpty() | ||
@IsString() | ||
photo: string; | ||
|
||
@ApiProperty({ type: [String] }) | ||
@IsNotEmpty() | ||
@IsArray() | ||
interests: string[]; | ||
} |
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 { beforeAll, describe, beforeEach, afterAll, it } from '@jest/globals'; | ||
import { INestApplication } from '@nestjs/common'; | ||
import request = require('supertest'); | ||
import { MikroORM } from '@mikro-orm/core'; | ||
import { createApplication, truncateTables } from '@test/acceptance/createApplication'; | ||
import { UserPrimitives } from '@src/languages/domain/user/user'; | ||
import { UserMother } from '@test/unit/languages/domain/user/userMother'; | ||
import { UserIdMother } from '@test/unit/languages/domain/user/userIdMother'; | ||
|
||
describe('Get user feature', () => { | ||
let app: INestApplication; | ||
let orm: MikroORM; | ||
|
||
const prepareApp = async () => { | ||
const setup = await createApplication(); | ||
app = setup.app; | ||
orm = setup.orm; | ||
}; | ||
|
||
const closeApp = async () => { | ||
await orm.close(true); | ||
await app.close(); | ||
}; | ||
|
||
beforeAll(async () => { | ||
await prepareApp(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await closeApp(); | ||
}); | ||
|
||
describe('As a user I want to get a user', () => { | ||
let userData: UserPrimitives; | ||
const userId = '4a4df157-8ab8-50af-bb39-88e8ce29eb16'; | ||
|
||
async function startScenario() { | ||
await truncateTables(orm); | ||
|
||
const user = UserMother.random({ id: UserIdMother.random(userId), interests: [], email: '[email protected]' }); | ||
|
||
userData = user.toPrimitives(); | ||
await request(app.getHttpServer()).post('/auth/signup').set('Authorization', 'Bearer mock-token').send({ | ||
name: userData.name, | ||
email: userData.email, | ||
token: '1234', | ||
provider: userData.provider, | ||
photo: userData.photo, | ||
}); | ||
} | ||
|
||
beforeEach(startScenario); | ||
|
||
it('should return an empty object when it does not exists', async () => { | ||
await request(app.getHttpServer()) | ||
.get('/users/a625816c-2117-44a6-a0ba-07fe54cb0ca3') | ||
.set('Authorization', 'Bearer mock-token') | ||
.expect(200) | ||
.expect({}); | ||
}); | ||
|
||
it('should return the user when it exists', async () => { | ||
await request(app.getHttpServer()) | ||
.get(`/users/${userId}`) | ||
.set('Authorization', 'Bearer mock-token') | ||
.expect(200) | ||
.expect(userData); | ||
}); | ||
}); | ||
}); |
37 changes: 37 additions & 0 deletions
37
test/unit/languages/app/controllers/v1/user/userGetController.test.ts
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,37 @@ | ||
import { beforeAll, describe, it, expect, jest } from '@jest/globals'; | ||
import UserGetController from '@src/languages/app/controllers/v1/user/userGetController'; | ||
import { UserMother } from '@test/unit/languages/domain/user/userMother'; | ||
import { QueryBus } from '@src/shared/domain/bus/queryBus/queryBus'; | ||
|
||
describe('Given a UserGetController to handle', () => { | ||
let sut: UserGetController; | ||
let queryBusMock: { ask: jest.Mock }; | ||
|
||
const prepareDependencies = () => { | ||
queryBusMock = { | ||
ask: jest.fn(), | ||
}; | ||
}; | ||
|
||
const initHandler = () => { | ||
sut = new UserGetController(queryBusMock as QueryBus); | ||
|
||
jest.useFakeTimers(); | ||
}; | ||
|
||
beforeAll(() => { | ||
prepareDependencies(); | ||
initHandler(); | ||
}); | ||
|
||
describe('When received a request', () => { | ||
const user = UserMother.random().toPrimitives(); | ||
|
||
it('should return a user', async () => { | ||
queryBusMock.ask.mockReturnValue({ content: user }); | ||
const response = await sut.run(user.id); | ||
|
||
expect(response).toEqual(user); | ||
}); | ||
}); | ||
}); |