Skip to content

Commit

Permalink
api integration
Browse files Browse the repository at this point in the history
  • Loading branch information
eldpswp99 committed Nov 9, 2023
1 parent 57a8db6 commit 19f7ec2
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
94 changes: 94 additions & 0 deletions backend/src/test/user/getUserInfo.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { NestExpressApplication } from '@nestjs/platform-express';
import { AppModule } from '../../app.module';
import { Test } from '@nestjs/testing';
import { DataSource } from 'typeorm';
import { appSetting } from '../../main';
import * as supertest from 'supertest';
import { UserEntity } from '../../user/models/user.entity';
import { UserFixture } from '../fixture/user.fixture';
import { HttpStatus } from '@nestjs/common';
import { FollowEntity } from '../../user/models/follow.entity';
import { validateDtoKeys } from '../utils';

describe('getUserMe test', () => {
let testServer: NestExpressApplication;
let dataSource: DataSource;
let user: UserEntity;
let accessToken: string;
let anotherUser: UserEntity;

beforeAll(async () => {
const module = await Test.createTestingModule({
imports: [AppModule],
}).compile();

testServer = module.createNestApplication<NestExpressApplication>();
dataSource = testServer.get(DataSource);
await dataSource.synchronize(true);
appSetting(testServer);

await testServer.init();
});

beforeEach(async () => {
await dataSource.synchronize(true);

user = await UserFixture.create({
name: '황승준',
username: 'hello',
password: 'world',
});

anotherUser = await UserFixture.create({
name: 'hi',
username: 'helloasdfasdfasdf',
password: 'world',
});

const { body } = await supertest(testServer.getHttpServer())
.post('/auth/login')
.send({
username: 'hello',
password: 'world',
})
.expect(HttpStatus.CREATED);

accessToken = body.accessToken;
});

it('unauthorized', async () => {
await supertest(testServer.getHttpServer())
.get('/user/me')
.expect(HttpStatus.UNAUTHORIZED);
});

it('OK', async () => {
await supertest(testServer.getHttpServer())
.get(`/user/${anotherUser.id}`)
.set('Authorization', `Bearer ${accessToken}`)
.expect(HttpStatus.OK);
});

it('DTO test', async () => {
await FollowEntity.create({
user: anotherUser,
follower: user,
}).save();

const { body } = await supertest(testServer.getHttpServer())
.get(`/user/${anotherUser.id}`)
.set('Authorization', `Bearer ${accessToken}`)
.expect(HttpStatus.OK);

validateDtoKeys(body, [
'id',
'name',
'username',
'followerCount',
'followingCount',
]);

expect(body.followingCount).toBe(1);
expect(body.followerCount).toBe(0);
});
});
2 changes: 2 additions & 0 deletions backend/src/user/out-dtos/userInfo.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { UserEntity } from '../models/user.entity';

export class UserInfoDto {
private id: number;
private profileImage: string;
private name: string;
private username: string;
private followerCount: number;
Expand All @@ -13,6 +14,7 @@ export class UserInfoDto {
followingCount: number,
) {
this.id = id;
this.profileImage = `${process.env.CDN_URL!}profile${id % 10}.jpg`;
this.name = name;
this.username = username;
this.followerCount = followerCount;
Expand Down
18 changes: 18 additions & 0 deletions backend/src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,24 @@ export class UserController {
return new UserInfoDto(user, followerCount, followingCount);
}

@UseGuards(JwtAccessGuard)
@Get('/:userId')
async getUserInfo(
@Req() { user }: UserRequest,
@Param('userId') userId: number,
) {
const findUser = await this.userRepository.findOneBy({
id: userId,
});

if (!findUser) throw new NotFoundException('존재하지 않는 유저입니다.');

const followerCount = await findUser.getFollowerCount();
const followingCount = await findUser.getFollowingCount();

return new UserInfoDto(findUser, followerCount, followingCount);
}

@UseGuards(JwtAccessGuard)
@Get('/search/:name')
async search(@Param('name') name: string) {
Expand Down

0 comments on commit 19f7ec2

Please sign in to comment.