Skip to content

Commit

Permalink
Merge pull request #136 from boostcampwm-2024/fix/redis-test
Browse files Browse the repository at this point in the history
🐛 fix: redis Service 수정
  • Loading branch information
Jo-Minseok authored Nov 20, 2024
2 parents 835a3eb + 3b2a542 commit 51e0072
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 55 deletions.
2 changes: 1 addition & 1 deletion server/src/admin/admin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class AdminService {

const sessionId = uuid.v4();

await this.redisService.set(
await this.redisService.redisClient.set(
sessionId,
admin.loginId,
`EX`,
Expand Down
2 changes: 1 addition & 1 deletion server/src/common/guard/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class CookieAuthGuard implements CanActivate {
async canActivate(context: ExecutionContext): Promise<boolean> {
const request = context.switchToHttp().getRequest<Request>();
const sid = request.cookies['sessionId'];
const loginId = await this.redisService.get(sid);
const loginId = await this.redisService.redisClient.get(sid);
if (!loginId) {
throw new UnauthorizedException('인증되지 않은 요청입니다.');
}
Expand Down
17 changes: 7 additions & 10 deletions server/src/common/redis/redis.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,15 @@ import Redis_Mock from 'ioredis-mock';
inject: [ConfigService],
useFactory: async (configService: ConfigService) => {
const envType = process.env.NODE_ENV;
let redis: Redis;
if (envType === 'test') {
redis = new Redis_Mock();
} else {
redis = new Redis({
host: configService.get<string>('REDIS_HOST'),
port: configService.get<number>('REDIS_PORT'),
username: configService.get<string>('REDIS_USERNAME'),
password: configService.get<string>('REDIS_PASSWORD'),
});
return new Redis_Mock();
}
return redis;
return new Redis({
host: configService.get<string>('REDIS_HOST'),
port: configService.get<number>('REDIS_PORT'),
username: configService.get<string>('REDIS_USERNAME'),
password: configService.get<string>('REDIS_PASSWORD'),
});
},
},
RedisService,
Expand Down
6 changes: 2 additions & 4 deletions server/src/common/redis/redis.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { Inject, Injectable } from '@nestjs/common';
import Redis from 'ioredis';

@Injectable()
export class RedisService extends Redis {
constructor(@Inject('REDIS_CLIENT') private readonly redisClient: Redis) {
super(redisClient.options);
}
export class RedisService {
constructor(@Inject('REDIS_CLIENT') public readonly redisClient: Redis) {}
}
6 changes: 5 additions & 1 deletion server/src/feed/feed.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ export class FeedService {
}

async getTrendList() {
const trendFeedIdList = await this.redisService.zrange('feed:trend', 0, 3);
const trendFeedIdList = await this.redisService.redisClient.zrange(
'feed:trend',
0,
3,
);
const trendFeeds = await Promise.all(
trendFeedIdList.map(async (feedId) => {
const feed = await this.feedRepository.findTrendFeed(parseInt(feedId));
Expand Down
70 changes: 34 additions & 36 deletions server/test/feed/trend.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppModule } from '../../src/app.module';
import { LoggingInterceptor } from '../../src/common/logger/logger.interceptor';
import { WinstonLoggerService } from '../../src/common/logger/logger.service';
import { InternalExceptionsFilter } from '../../src/common/filters/internal-exceptions.filter';
import { HttpExceptionsFilter } from '../../src/common/filters/http-exception.filter';
Expand All @@ -9,11 +8,12 @@ import { RedisService } from '../../src/common/redis/redis.service';
import { DataSource } from 'typeorm';
import { Feed } from '../../src/feed/feed.entity';
import { Blog } from '../../src/blog/blog.entity';
import { INestApplication } from '@nestjs/common';

describe('Trend API', () => {
let app;
let app: INestApplication;
let moduleFixture: TestingModule;
let redisService;
let redisService: RedisService;
beforeAll(async () => {
moduleFixture = await Test.createTestingModule({
imports: [AppModule],
Expand All @@ -22,7 +22,6 @@ describe('Trend API', () => {
app = moduleFixture.createNestApplication();
const logger = app.get(WinstonLoggerService);
app.setGlobalPrefix('api');
app.useGlobalInterceptors(new LoggingInterceptor(logger));
app.useGlobalFilters(
new InternalExceptionsFilter(logger),
new HttpExceptionsFilter(),
Expand All @@ -33,7 +32,7 @@ describe('Trend API', () => {
const orm = app.get(DataSource);
const feedRepository = orm.getRepository(Feed);
const blogRepository = orm.getRepository(Blog);
await blogRepository.save([
const blogs = await blogRepository.save([
{
id: 1,
name: 'test',
Expand All @@ -59,32 +58,35 @@ describe('Trend API', () => {
await feedRepository.save([
{
id: 1,
createdAt: '2022-09-05T09:00:00.000Z',
title:
'자바스크립트의 구조와 실행 방식 (Ignition, TurboFan, EventLoop)',
viewCount: 0,
path: 'https://asn6878.tistory.com/9',
createdAt: '2022-09-05T09:00:00.000Z',
thumbnail:
'https://img1.daumcdn.net/thumb/R800x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2F2wH52%2FbtsJIskiFgS%2FQlF4XqMVZsM8y51w67dxj1%2Fimg.png',
blog: 1,
blog: blogs[0],
},
{
id: 2,
createdAt: '2024-08-14T14:07:49.000Z',
title:
'[네이버 커넥트재단 부스트캠프 웹・모바일 9기] 날 것 그대로 작성하는 챌린지 수료 후기 - Web',
path: 'https://velog.io/@seok3765/%EB%84%A4%EC%9D%B4%EB%B2%84-%EC%BB%A4%EB%84%A5%ED%8A%B8%EC%9E%AC%EB%8B%A8-%EB%B6%80%EC%8A%A4%ED%8A%B8%EC%BA%A0%ED%94%84-%EC%9B%B9%E3%83%BB%EB%AA%A8%EB%B0%94%EC%9D%BC-9%EA%B8%B0-%EB%82%A0-%EA%B2%83-%EA%B7%B8%EB%8C%80%EB%A1%9C-%EC%9E%91%EC%84%B1%ED%95%98%EB%8A%94-%EC%B1%8C%EB%A6%B0%EC%A7%80-%EC%88%98%EB%A3%8C-%ED%9B%84%EA%B8%B0-Web',
createdAt: '2024-08-14T14:07:49.000Z',
thumbnail:
'https://velog.velcdn.com/images/seok3765/post/2f863481-b594-46f8-9a28-7799afb58aa4/image.jpg',
blog: 2,
viewCount: 0,
blog: blogs[1],
},
{
id: 3,
createdAt: '2022-09-05T09:00:00.000Z',
viewCount: 0,
title: '제목',
path: 'https://asn6878.tistory.com/10',
createdAt: '2022-09-05T09:00:00.000Z',
thumbnail:
'https://img1.daumcdn.net/thumb/R800x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2F2wH52%2FbtsJIskiFgS%2FQlF4XqMVZsM8y51w67dxj1%2Fimg.png',
blog: 3,
blog: blogs[2],
},
{
id: 4,
Expand All @@ -93,7 +95,8 @@ describe('Trend API', () => {
createdAt: '2022-09-05T10:00:00.000Z',
thumbnail:
'https://img1.daumcdn.net/thumb/R800x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2F2wH52%2FbtsJIskiFgS%2FQlF4XqMVZsM8y51w67dxj1%2Fimg.png',
blog: 3,
viewCount: 0,
blog: blogs[2],
},
{
id: 5,
Expand All @@ -102,14 +105,15 @@ describe('Trend API', () => {
createdAt: '2022-09-05T10:00:00.000Z',
thumbnail:
'https://img1.daumcdn.net/thumb/R800x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2F2wH52%2FbtsJIskiFgS%2FQlF4XqMVZsM8y51w67dxj1%2Fimg.png',
blog: 2,
viewCount: 0,
blog: blogs[2],
},
]);
redisService = app.get(RedisService);
});

beforeEach(async () => {
redisService = app.get(RedisService);
await redisService.flushall();
await redisService.redisClient.flushall();
});

afterAll(async () => {
Expand All @@ -129,7 +133,7 @@ describe('Trend API', () => {
});

it('피드 1개만 조회됐을 때', async () => {
await redisService.zadd('feed:trend', 10, '1');
await redisService.redisClient.zadd('feed:trend', 10, '1');
const response = await request(app.getHttpServer()).get(
'/api/feed/trend',
);
Expand All @@ -154,8 +158,8 @@ describe('Trend API', () => {

it('피드 2개만 조회됐을 때', async () => {
await Promise.all([
redisService.zadd('feed:trend', 10, '1'),
redisService.zadd('feed:trend', 20, '2'),
redisService.redisClient.zadd('feed:trend', 10, '1'),
redisService.redisClient.zadd('feed:trend', 20, '2'),
]);
const response = await request(app.getHttpServer()).get(
'/api/feed/trend',
Expand Down Expand Up @@ -191,9 +195,9 @@ describe('Trend API', () => {
});
it('피드 3개만 조회됐을 때', async () => {
await Promise.all([
redisService.zadd('feed:trend', 10, '1'),
redisService.zadd('feed:trend', 20, '2'),
redisService.zadd('feed:trend', 30, '3'),
redisService.redisClient.zadd('feed:trend', 10, '1'),
redisService.redisClient.zadd('feed:trend', 20, '2'),
redisService.redisClient.zadd('feed:trend', 30, '3'),
]);
const response = await request(app.getHttpServer()).get(
'/api/feed/trend',
Expand Down Expand Up @@ -239,10 +243,10 @@ describe('Trend API', () => {
});
it('피드 4개만 조회됐을 때', async () => {
await Promise.all([
redisService.zadd('feed:trend', 10, '1'),
redisService.zadd('feed:trend', 20, '2'),
redisService.zadd('feed:trend', 30, '3'),
redisService.zadd('feed:trend', 40, '4'),
redisService.redisClient.zadd('feed:trend', 10, '1'),
redisService.redisClient.zadd('feed:trend', 20, '2'),
redisService.redisClient.zadd('feed:trend', 30, '3'),
redisService.redisClient.zadd('feed:trend', 40, '4'),
]);
const response = await request(app.getHttpServer()).get(
'/api/feed/trend',
Expand Down Expand Up @@ -298,11 +302,11 @@ describe('Trend API', () => {
});
it('피드 5개 이상 조회됐을 때', async () => {
await Promise.all([
redisService.zadd('feed:trend', 10, '1'),
redisService.zadd('feed:trend', 20, '2'),
redisService.zadd('feed:trend', 30, '3'),
redisService.zadd('feed:trend', 40, '4'),
redisService.zadd('feed:trend', 50, '5'),
redisService.redisClient.zadd('feed:trend', 10, '1'),
redisService.redisClient.zadd('feed:trend', 20, '2'),
redisService.redisClient.zadd('feed:trend', 30, '3'),
redisService.redisClient.zadd('feed:trend', 40, '4'),
redisService.redisClient.zadd('feed:trend', 50, '5'),
]);
const response = await request(app.getHttpServer()).get(
'/api/feed/trend',
Expand Down Expand Up @@ -357,10 +361,4 @@ describe('Trend API', () => {
});
});
});

describe('게시글이 올바르게 존재하지 않을 때', () => {
it('조회 테이블에만 데이터가 있고 RDBMS에 데이터가 없을 때', async () => {
await Promise.all([redisService.zadd('feed:trend', 10, '6')]);
});
});
});
2 changes: 0 additions & 2 deletions server/test/rss/rss.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Rss } from '../../src/rss/rss.entity';
import { Blog } from '../../src/blog/blog.entity';
import { HttpExceptionsFilter } from './../../src/common/filters/http-exception.filter';
import { InternalExceptionsFilter } from '../../src/common/filters/internal-exceptions.filter';
import { LoggingInterceptor } from '../../src/common/logger/logger.interceptor';
import { WinstonLoggerService } from '../../src/common/logger/logger.service';

describe('Rss Register E2E Test : POST /api/rss', () => {
Expand All @@ -25,7 +24,6 @@ describe('Rss Register E2E Test : POST /api/rss', () => {
app = moduleFixture.createNestApplication();
const logger = app.get(WinstonLoggerService);
app.setGlobalPrefix('api');
app.useGlobalInterceptors(new LoggingInterceptor(logger));
app.useGlobalFilters(
new InternalExceptionsFilter(logger),
new HttpExceptionsFilter(),
Expand Down

0 comments on commit 51e0072

Please sign in to comment.