From 85e351281134c0cfa7d292c6e1a158c84c6d7f20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=88=98=ED=98=84?= Date: Tue, 30 Jan 2024 23:47:10 +0900 Subject: [PATCH 1/5] =?UTF-8?q?test:=20images=20=EC=BB=A8=ED=8A=B8?= =?UTF-8?q?=EB=A1=A4=EB=9F=AC=20e2e=20test=20=EC=84=B8=ED=8C=85=20#364?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - supertest 설치 - 테스트코드 작성을 위한 세팅 --- backend/package.json | 2 +- backend/test/images/images.e2e-spec.ts | 24 ++++++++++++++++++++++++ yarn.lock | 14 +++++++------- 3 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 backend/test/images/images.e2e-spec.ts diff --git a/backend/package.json b/backend/package.json index 51611b3..ccd44e5 100644 --- a/backend/package.json +++ b/backend/package.json @@ -65,7 +65,7 @@ "@typescript-eslint/parser": "^6.0.0", "jest": "^29.5.0", "source-map-support": "^0.5.21", - "supertest": "^6.3.3", + "supertest": "^6.3.4", "ts-jest": "^29.1.0", "ts-loader": "^9.4.3", "ts-node": "^10.9.1", diff --git a/backend/test/images/images.e2e-spec.ts b/backend/test/images/images.e2e-spec.ts new file mode 100644 index 0000000..a58ad6c --- /dev/null +++ b/backend/test/images/images.e2e-spec.ts @@ -0,0 +1,24 @@ +import * as request from 'supertest'; +import { INestApplication } from '@nestjs/common'; +import { Test } from '@nestjs/testing'; +import { ImagesService } from 'src/images/images.service'; +import { AppModule } from 'src/app.module'; + +describe('ImagesController (e2e)', () => { + let app: INestApplication; + let imagesService: ImagesService; + + beforeAll(async () => { + const module = await Test.createTestingModule({ + imports: [AppModule], + }).compile(); + + imagesService = module.get(ImagesService); + app = module.createNestApplication(); + await app.init(); + }); + + afterAll(async () => { + await app.close(); + }); +}); diff --git a/yarn.lock b/yarn.lock index 61e7fdf..bb18d6d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8401,7 +8401,7 @@ sucrase@^3.32.0: pirates "^4.0.1" ts-interface-checker "^0.1.9" -superagent@^8.0.5: +superagent@^8.1.2: version "8.1.2" resolved "https://registry.yarnpkg.com/superagent/-/superagent-8.1.2.tgz#03cb7da3ec8b32472c9d20f6c2a57c7f3765f30b" integrity sha512-6WTxW1EB6yCxV5VFOIPQruWGHqc3yI7hEmZK6h+pyk69Lk/Ut7rLUY6W/ONF2MjBuGjvmMiIpsrVJ2vjrHlslA== @@ -8417,13 +8417,13 @@ superagent@^8.0.5: qs "^6.11.0" semver "^7.3.8" -supertest@^6.3.3: - version "6.3.3" - resolved "https://registry.yarnpkg.com/supertest/-/supertest-6.3.3.tgz#42f4da199fee656106fd422c094cf6c9578141db" - integrity sha512-EMCG6G8gDu5qEqRQ3JjjPs6+FYT1a7Hv5ApHvtSghmOFJYtsU5S+pSb6Y2EUeCEY3CmEL3mmQ8YWlPOzQomabA== +supertest@^6.3.4: + version "6.3.4" + resolved "https://registry.yarnpkg.com/supertest/-/supertest-6.3.4.tgz#2145c250570c2ea5d337db3552dbfb78a2286218" + integrity sha512-erY3HFDG0dPnhw4U+udPfrzXa4xhSG+n4rxfRuZWCUvjFWwKl+OxWf/7zk50s84/fAAs7vf5QAb9uRa0cCykxw== dependencies: methods "^1.1.2" - superagent "^8.0.5" + superagent "^8.1.2" supports-color@^5.3.0: version "5.5.0" @@ -9064,7 +9064,7 @@ vite-plugin-pwa@^0.17.4: workbox-window "^7.0.0" vite@^4.5.1: - version "4.5.1" + version "4.5.0" resolved "https://registry.yarnpkg.com/vite/-/vite-4.5.0.tgz#ec406295b4167ac3bc23e26f9c8ff559287cff26" integrity sha512-ulr8rNLA6rkyFAlVWw2q5YJ91v098AFQ2R0PRFwPzREXOUJQPtFUG0t+/ZikhaOCDqFoDhN6/v8Sq0o4araFAw== dependencies: From bb16c714e69b1b570e483c1817754e90cd313504 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=88=98=ED=98=84?= Date: Fri, 2 Feb 2024 13:58:51 +0900 Subject: [PATCH 2/5] =?UTF-8?q?test:=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20?= =?UTF-8?q?=EC=97=85=EB=A1=9C=EB=93=9C=20API=20=EC=A0=95=EC=83=81=20?= =?UTF-8?q?=EB=8F=99=EC=9E=91=20test=20#364?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/test/images/images.e2e-spec.ts | 39 ++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/backend/test/images/images.e2e-spec.ts b/backend/test/images/images.e2e-spec.ts index a58ad6c..93f60d2 100644 --- a/backend/test/images/images.e2e-spec.ts +++ b/backend/test/images/images.e2e-spec.ts @@ -1,23 +1,58 @@ import * as request from 'supertest'; -import { INestApplication } from '@nestjs/common'; +import { ExecutionContext, INestApplication } from '@nestjs/common'; import { Test } from '@nestjs/testing'; import { ImagesService } from 'src/images/images.service'; import { AppModule } from 'src/app.module'; +import { JwtAuthGuard } from 'src/auth/guards/jwtAuth.guard'; +import * as fs from 'fs'; +import { ImagesRepository } from 'src/images/images.repository'; describe('ImagesController (e2e)', () => { let app: INestApplication; let imagesService: ImagesService; + let imagesRepository: ImagesRepository; + const user = { id: 1, nickname: 'testUser' }; beforeAll(async () => { const module = await Test.createTestingModule({ imports: [AppModule], - }).compile(); + }) + .overrideGuard(JwtAuthGuard) + .useValue({ + canActivate: (context: ExecutionContext) => { + const req = context.switchToHttp().getRequest(); + req.user = user; + + return true; + }, + }) + .compile(); imagesService = module.get(ImagesService); + imagesRepository = module.get(ImagesRepository); app = module.createNestApplication(); await app.init(); }); + describe('/images/diaries (POST)', () => { + const url = '/images/diaries'; + + it('이미지 정상 업로드', async () => { + // given + const imageFilePath = './test/testImage.png'; + const uploadedLocation = `https://dandi-object-storage.kr.object.ncloudstorage.com/${user.id}/2024/01/testImage.png`; + imagesRepository.uploadImage = jest.fn(); + (imagesRepository.uploadImage as jest.Mock).mockReturnValue({ Location: uploadedLocation }); + + // when + const response = await request(app.getHttpServer()).post(url).attach('image', imageFilePath); + + // then + expect(response.status).toEqual(201); + expect(response.body.imageURL).toEqual(uploadedLocation); + }); + }); + afterAll(async () => { await app.close(); }); From d4730435222cc543fea006b384783086f5eecac5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=88=98=ED=98=84?= Date: Fri, 2 Feb 2024 16:06:29 +0900 Subject: [PATCH 3/5] =?UTF-8?q?test:=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20?= =?UTF-8?q?=EC=97=85=EB=A1=9C=EB=93=9C=20API=20=EC=98=88=EC=99=B8=20?= =?UTF-8?q?=EC=BC=80=EC=9D=B4=EC=8A=A4=20=EC=9E=91=EC=84=B1=20#364?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - MIME type 맞지 않는 경우 테스트 - 테스트용 txt 파일 생성 --- backend/test/images/images.e2e-spec.ts | 15 +++++++++++---- backend/test/images/testFile.txt | 1 + 2 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 backend/test/images/testFile.txt diff --git a/backend/test/images/images.e2e-spec.ts b/backend/test/images/images.e2e-spec.ts index 93f60d2..4ed80d3 100644 --- a/backend/test/images/images.e2e-spec.ts +++ b/backend/test/images/images.e2e-spec.ts @@ -1,15 +1,12 @@ import * as request from 'supertest'; import { ExecutionContext, INestApplication } from '@nestjs/common'; import { Test } from '@nestjs/testing'; -import { ImagesService } from 'src/images/images.service'; import { AppModule } from 'src/app.module'; import { JwtAuthGuard } from 'src/auth/guards/jwtAuth.guard'; -import * as fs from 'fs'; import { ImagesRepository } from 'src/images/images.repository'; describe('ImagesController (e2e)', () => { let app: INestApplication; - let imagesService: ImagesService; let imagesRepository: ImagesRepository; const user = { id: 1, nickname: 'testUser' }; @@ -28,7 +25,6 @@ describe('ImagesController (e2e)', () => { }) .compile(); - imagesService = module.get(ImagesService); imagesRepository = module.get(ImagesRepository); app = module.createNestApplication(); await app.init(); @@ -51,6 +47,17 @@ describe('ImagesController (e2e)', () => { expect(response.status).toEqual(201); expect(response.body.imageURL).toEqual(uploadedLocation); }); + + it('파일의 MIME type이 맞지 않는 경우 예외 발생', async () => { + // given + const imageFilePath = './test/images/testFile.txt'; + + // when + const response = await request(app.getHttpServer()).post(url).attach('image', imageFilePath); + + // then + expect(response.status).toEqual(400); + }); }); afterAll(async () => { diff --git a/backend/test/images/testFile.txt b/backend/test/images/testFile.txt new file mode 100644 index 0000000..30d74d2 --- /dev/null +++ b/backend/test/images/testFile.txt @@ -0,0 +1 @@ +test \ No newline at end of file From c76b5ce7fb88d8612fe6fad37aeb28bf8f5d53e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=88=98=ED=98=84?= Date: Fri, 2 Feb 2024 17:21:57 +0900 Subject: [PATCH 4/5] =?UTF-8?q?chore:=20=EB=A6=AC=EB=B7=B0=20=EB=B0=98?= =?UTF-8?q?=EC=98=81=20#382?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 코드 정리 - vite 버전 업그레이드 --- backend/test/images/images.e2e-spec.ts | 8 ++++---- frontend/package.json | 1 + yarn.lock | 8 ++++---- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/backend/test/images/images.e2e-spec.ts b/backend/test/images/images.e2e-spec.ts index 4ed80d3..8764341 100644 --- a/backend/test/images/images.e2e-spec.ts +++ b/backend/test/images/images.e2e-spec.ts @@ -30,6 +30,10 @@ describe('ImagesController (e2e)', () => { await app.init(); }); + afterAll(async () => { + await app.close(); + }); + describe('/images/diaries (POST)', () => { const url = '/images/diaries'; @@ -59,8 +63,4 @@ describe('ImagesController (e2e)', () => { expect(response.status).toEqual(400); }); }); - - afterAll(async () => { - await app.close(); - }); }); diff --git a/frontend/package.json b/frontend/package.json index 806d4b0..b28d55c 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -21,6 +21,7 @@ "react-dom": "^18.2.0", "react-router-dom": "^6.18.0", "swiper": "^11.0.5", + "vite": "4.5.2", "zustand": "^4.4.7" }, "devDependencies": { diff --git a/yarn.lock b/yarn.lock index bb18d6d..4b95495 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9063,10 +9063,10 @@ vite-plugin-pwa@^0.17.4: workbox-build "^7.0.0" workbox-window "^7.0.0" -vite@^4.5.1: - version "4.5.0" - resolved "https://registry.yarnpkg.com/vite/-/vite-4.5.0.tgz#ec406295b4167ac3bc23e26f9c8ff559287cff26" - integrity sha512-ulr8rNLA6rkyFAlVWw2q5YJ91v098AFQ2R0PRFwPzREXOUJQPtFUG0t+/ZikhaOCDqFoDhN6/v8Sq0o4araFAw== +vite@4.5.2: + version "4.5.2" + resolved "https://registry.yarnpkg.com/vite/-/vite-4.5.2.tgz#d6ea8610e099851dad8c7371599969e0f8b97e82" + integrity sha512-tBCZBNSBbHQkaGyhGCDUGqeo2ph8Fstyp6FMSvTtsXeZSPpSMGlviAOav2hxVTqFcx8Hj/twtWKsMJXNY0xI8w== dependencies: esbuild "^0.18.10" postcss "^8.4.27" From 5fddbdaac6acd429038dc2622568d9f86b654982 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=88=98=ED=98=84?= Date: Fri, 2 Feb 2024 17:28:30 +0900 Subject: [PATCH 5/5] =?UTF-8?q?chore:=20vite=20devDependencies=EB=A1=9C=20?= =?UTF-8?q?=EC=9E=AC=EC=84=A4=EC=B9=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/frontend/package.json b/frontend/package.json index b28d55c..612ab7f 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -21,7 +21,6 @@ "react-dom": "^18.2.0", "react-router-dom": "^6.18.0", "swiper": "^11.0.5", - "vite": "4.5.2", "zustand": "^4.4.7" }, "devDependencies": { @@ -37,7 +36,7 @@ "postcss": "^8.4.31", "prettier-plugin-tailwindcss": "^0.5.7", "tailwindcss": "^3.3.5", - "vite": "^4.5.1", + "vite": "4.5.2", "vite-plugin-pwa": "^0.17.4" } }