Skip to content

Commit

Permalink
Merge pull request #175 from mapeveri/feat/174
Browse files Browse the repository at this point in the history
Add get user endpoint
  • Loading branch information
mapeveri authored Dec 14, 2024
2 parents 43bd3ee + 425b660 commit 7fb7932
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/languages/_dependencyInjection/controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import DislikeTermPostController from '@src/languages/app/controllers/v1/terms/d
import WordPutController from '@src/languages/app/controllers/v1/terms/words/wordPutController';
import TermGetController from '@src/languages/app/controllers/v1/terms/termGetController';
import SignupPostController from '@src/languages/app/controllers/v1/auth/signupPostController';
import UserGetController from '@src/languages/app/controllers/v1/user/userGetController';

export const controllers = [
LoginPostController,
Expand All @@ -30,6 +31,7 @@ export const controllers = [
CountriesGetController,
CountryGetController,
CountryPostController,
UserGetController,
UserPutController,
SearchTermsSseController,
HealthController,
Expand Down
31 changes: 31 additions & 0 deletions src/languages/app/controllers/v1/user/userGetController.ts
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;
}
}
34 changes: 34 additions & 0 deletions src/languages/app/controllers/v1/user/userGetResponse.ts
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[];
}
70 changes: 70 additions & 0 deletions test/acceptance/languages/auth/getUserAcceptance.test.ts
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);
});
});
});
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);
});
});
});

0 comments on commit 7fb7932

Please sign in to comment.