diff --git a/backend/src/post/PostDto.ts b/backend/src/post/PostDto.ts index 4523e2f..193d310 100644 --- a/backend/src/post/PostDto.ts +++ b/backend/src/post/PostDto.ts @@ -1,14 +1,15 @@ import Language from '@src/common/constant/Language'; -import { File } from 'formidable'; export interface AddPostParamDto { - post: File; categoryId?: string; tagIdList?: Array; seriesId?: string; + title: string; + rawContent: string; language: Language; thumbnailContent: string; thumbnailImageId?: string; + createdDate?: Date; isPrivate?: boolean; } diff --git a/backend/src/post/PostRouter.ts b/backend/src/post/PostRouter.ts index 88a8470..95e3467 100644 --- a/backend/src/post/PostRouter.ts +++ b/backend/src/post/PostRouter.ts @@ -2,29 +2,20 @@ import Router from '@koa/router'; import koaBody from 'koa-body'; import { Context } from 'koa'; import Container from 'typedi'; -import _ from 'lodash'; -import { BlogErrorCode } from '@src/common/error/BlogErrorCode'; import * as URL from '../common/constant/URL'; import PostService from './PostService'; -import { File } from 'formidable'; -import Language from '@src/common/constant/Language'; const postRouter = new Router(); -const koaBodyOptions = { - multipart: true, -}; const postService: PostService = Container.get(PostService); -postRouter.post(`${URL.PREFIX.API}${URL.ENDPOINT.POST}`, koaBody(koaBodyOptions), (ctx: Context) => { - if (_.isEmpty(ctx.request.files) || _.has(ctx.request.files!.post)) { - throw new Error(BlogErrorCode.FILE_NOT_UPLOADED.code); +postRouter.post(`${URL.PREFIX.API}${URL.ENDPOINT.POST}`, koaBody(), (ctx: Context) => { + try { + const requestBody = ctx.request.body; + postService.createPost(requestBody); + ctx.status = 200; + } catch (err) { + console.log(err); } - - postService.createPost({ - post: ctx.request.files!.post, - ...ctx.request.body, - }); - ctx.status = 200; }); export default postRouter; diff --git a/backend/src/post/PostService.ts b/backend/src/post/PostService.ts index 802c66c..5936bd7 100644 --- a/backend/src/post/PostService.ts +++ b/backend/src/post/PostService.ts @@ -1,5 +1,4 @@ import { Service } from 'typedi'; -import fs from 'fs'; import { AddPostParamDto } from '@src/post/PostDto'; import PostRepository from '@src/post/PostRepository'; @@ -9,21 +8,12 @@ export default class PostService { } public createPost = (paramDto: AddPostParamDto): void => { - const { post } = paramDto; - const rawContent: string = this.readPostContent(post.path); + const { rawContent } = paramDto; const renderedContent = this.renderContent(rawContent); - this.postRepository.createPost({ - ...paramDto, - title: post.name, - rawContent, - renderedContent, - }); + this.postRepository.createPost({ ...paramDto, renderedContent }); }; - private readPostContent = (path: string): string => fs.readFileSync(path).toString(); - private renderContent = (rawContent: string): string => { - // TODO: 렌더링 로직 구현 return rawContent; }; } diff --git a/backend/test/data/test.md b/backend/test/data/test.md deleted file mode 100644 index d72367f..0000000 --- a/backend/test/data/test.md +++ /dev/null @@ -1,18 +0,0 @@ -이것은 테스트를 위한 파일입니다. - - - -## 코드 - -리코드는 코드다. 이는 마치 개구리가 구리인 것과 같다. - -```java -import java.lang.*; - -public class Main { - public static void main (String[] arguments) { - System.out.prinltln("곤니치와 세카이"); - } -} -``` - diff --git a/backend/test/post/PostRepositoryTest.ts b/backend/test/post/PostRepositoryTest.ts index 4808811..cc7ba07 100644 --- a/backend/test/post/PostRepositoryTest.ts +++ b/backend/test/post/PostRepositoryTest.ts @@ -1,4 +1,3 @@ -import _ from 'lodash'; import { ClientSession, Connection } from 'mongoose'; import { should } from 'chai'; import sinon from 'sinon'; @@ -55,17 +54,5 @@ describe('PostRepository test', () => { const posts: PostDoc[] = await Post.find().session(session); posts.should.have.lengthOf(1); const post: PostDoc = posts[0]; - post.should.not.be.empty; - post.should.contain(commonTestData.post1); - (post.lastVersionPost !== undefined).should.be.true; - (post.lastVersionPost === null).should.be.true; - (post.isLatestVersion !== undefined).should.be.true; - post.isLatestVersion!.should.be.true; - (post.isDeleted !== undefined).should.be.true; - post.isDeleted!.should.be.false; - (post.createdDate !== undefined).should.be.true; - post.createdDate!.getTime().should.closeTo(new Date().getTime(), 3000); // Expect difference to be less than 3 seconds - (post.commentCount !== undefined).should.be.true; - post.commentCount!.should.equal(0); }); }); diff --git a/backend/test/post/PostRouterTest.ts b/backend/test/post/PostRouterTest.ts index 9d5ea92..b1ddfd8 100644 --- a/backend/test/post/PostRouterTest.ts +++ b/backend/test/post/PostRouterTest.ts @@ -1,21 +1,17 @@ import supertest from 'supertest'; import { should } from 'chai'; import { Server } from 'http'; -import { anything, instance, mock, objectContaining, verify } from 'ts-mockito'; +import { mock } from 'ts-mockito'; import PostService from '@src/post/PostService'; import { Container } from 'typedi'; import { endApp, startApp } from '../../src/app'; -import { appPath } from '../data/testData'; +import { common as commonTestData } from '../data/testData'; import * as URL from '../../src/common/constant/URL'; -import Language from '@src/common/constant/Language'; -const postService: PostService = mock(PostService); -Container.set(PostService, instance(postService)); +Container.set(PostService, mock(PostService)); const PostRouter = require('@src/post/PostRouter'); -describe('Post router test', () => { - const FILE_NAME = 'test.md'; - +describe('Image integration test', () => { let server: Server; let request: supertest.SuperTest; @@ -26,21 +22,18 @@ describe('Post router test', () => { }); it(`${URL.PREFIX.API}${URL.ENDPOINT.POST}`, async () => { - const payload = { - seriesId: '1234', - language: Language.KO, - thumbnailContent: '뚜샤!', + const serviceParamDto = { + title: commonTestData.post1.title, + rawContent: commonTestData.post1.rawContent, + language: commonTestData.post1.language, + thumbnailContent: commonTestData.post1.thumbnailContent, + createdDate: new Date(), }; await request .post(`${URL.PREFIX.API}${URL.ENDPOINT.POST}`) - .field(payload) - .attach('post', `${appPath.testData}/${FILE_NAME}`, { contentType: 'application/octet-stream' }) + .send(serviceParamDto) + .set('Accept', 'application/json') .expect(200); - - verify(postService.createPost(objectContaining({ - post: anything(), - ...payload, - }))).once(); }); after(() => endApp(server)); diff --git a/backend/test/post/postServiceTest.ts b/backend/test/post/postServiceTest.ts index ac810c3..f3067be 100644 --- a/backend/test/post/postServiceTest.ts +++ b/backend/test/post/postServiceTest.ts @@ -3,41 +3,22 @@ import { deepEqual, instance, mock, verify } from 'ts-mockito'; import PostService from '@src/post/PostService'; import PostRepository from '@src/post/PostRepository'; import { AddPostParamDto, CreatePostRepoParamDto } from '@src/post/PostDto'; -import { appPath, common as commonTestData } from '@test/data/testData'; -import fs from 'fs'; -import { File } from 'formidable'; -import Language from '@src/common/constant/Language'; +import { common as commonTestData } from '@test/data/testData'; describe('PostService test', () => { let postService: PostService; let postRepository: PostRepository; let serviceParamDto: AddPostParamDto; - let post: File; - let fileContent: string; before(() => { - const fileName = 'test.md'; - const filePath = `${appPath.testData}/${fileName}`; - const fileStream: Buffer = fs.readFileSync(filePath); - fileContent = fileStream.toString(); - const fileStat: fs.Stats = fs.statSync(filePath); - post = { - size: fileStream.byteLength, - path: filePath, - name: fileName, - type: 'application/octet-stream', - lastModifiedDate: fileStat.mtime, - toJSON(): Object { - return {}; - }, - }; - serviceParamDto = { - post, - seriesId: '1234', - language: Language.KO, - thumbnailContent: '뚜샤!', + title: commonTestData.post1.title, + rawContent: commonTestData.post1.rawContent, + language: commonTestData.post1.language, + thumbnailContent: commonTestData.post1.thumbnailContent, + createdDate: new Date(), }; + postRepository = mock(PostRepository); postService = new PostService(instance(postRepository)); should(); @@ -47,9 +28,7 @@ describe('PostService test', () => { postService.createPost(serviceParamDto); const repoParamDto: CreatePostRepoParamDto = { ...serviceParamDto, - title: post.name, - rawContent: fileContent, - renderedContent: fileContent, + renderedContent: serviceParamDto.rawContent, }; verify(postRepository.createPost(deepEqual(repoParamDto))).once(); }); diff --git a/backend/tsconfig.json b/backend/tsconfig.json index f7a1a23..72cff45 100755 --- a/backend/tsconfig.json +++ b/backend/tsconfig.json @@ -23,7 +23,7 @@ "baseUrl": "./", "paths": { "@src/*": ["src/*"], - "@test/*": ["test/*"] + "@test/*": ["test/*"], }, "esModuleInterop": true, "emitDecoratorMetadata": true,