|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import * as request from 'supertest'; |
| 3 | +import { INestApplication } from '@nestjs/common'; |
| 4 | +import { AppModule } from '../../app.module'; |
| 5 | +import { WhitebookDto } from './whitebook.dto'; |
| 6 | +import { DataSource } from 'typeorm'; |
| 7 | +import { MemoryStoredFile } from 'nestjs-form-data'; |
| 8 | +import { FileService } from 'src/file/file.service'; |
| 9 | + |
| 10 | +describe('WhitebookController (e2e)', () => { |
| 11 | + let app: INestApplication; |
| 12 | + |
| 13 | + beforeAll(async () => { |
| 14 | + const moduleFixture: TestingModule = await Test.createTestingModule({ |
| 15 | + imports: [AppModule], |
| 16 | + }) |
| 17 | + .overrideProvider(FileService) |
| 18 | + .useValue({ |
| 19 | + uploadFile: jest.fn().mockImplementation(async (key: string) => { |
| 20 | + return `${process.env.S3_CF_DIST_URL}/${key}`; |
| 21 | + }), |
| 22 | + }) |
| 23 | + .compile(); |
| 24 | + |
| 25 | + app = moduleFixture.createNestApplication(); |
| 26 | + await app.init(); |
| 27 | + }); |
| 28 | + |
| 29 | + afterEach(async () => { |
| 30 | + // 각 테스트 끝날 때마다 DB 초기화 |
| 31 | + const dataSource = app.get(DataSource); |
| 32 | + await dataSource.synchronize(true); |
| 33 | + }); |
| 34 | + |
| 35 | + afterAll(async () => { |
| 36 | + await app.close(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('/whitebook [GET] 200', async () => { |
| 40 | + const Dto: WhitebookDto = { |
| 41 | + title: 'New Whitebook', |
| 42 | + content: 'Content of the new whitebook', |
| 43 | + link: 'https://www.example.com', |
| 44 | + show_only_login: false, |
| 45 | + }; |
| 46 | + |
| 47 | + const postResponse = await request(app.getHttpServer()) |
| 48 | + .post('/whitebook') |
| 49 | + .send(Dto); |
| 50 | + |
| 51 | + expect(postResponse.status).toBe(201); |
| 52 | + expect(postResponse.type).toBe('application/json'); |
| 53 | + |
| 54 | + const createdUuid = postResponse.body.uuid; |
| 55 | + const res = await request(app.getHttpServer()).get(`/whitebook`); |
| 56 | + |
| 57 | + expect(res.status).toBe(200); |
| 58 | + expect(res.type).toBe('application/json'); |
| 59 | + expect(Array.isArray(res.body)).toBe(true); |
| 60 | + expect(res.body).toEqual( |
| 61 | + expect.arrayContaining([ |
| 62 | + expect.objectContaining({ |
| 63 | + uuid: createdUuid, |
| 64 | + title: Dto.title, |
| 65 | + content: Dto.content, |
| 66 | + link: Dto.link, |
| 67 | + show_only_login: Dto.show_only_login, |
| 68 | + }), |
| 69 | + ]), |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + it('/whitebook [POST] 201', async () => { |
| 74 | + // PDF 파일 없이 생성 |
| 75 | + const dtoWithoutPdfFile: WhitebookDto = { |
| 76 | + title: 'New Whitebook', |
| 77 | + content: 'Content of the new whitebook', |
| 78 | + link: 'https://www.example.com', |
| 79 | + show_only_login: true, |
| 80 | + }; |
| 81 | + |
| 82 | + const resWihtoutPDF = await request(app.getHttpServer()) |
| 83 | + .post('/whitebook') |
| 84 | + .send(dtoWithoutPdfFile); |
| 85 | + |
| 86 | + expect(resWihtoutPDF.status).toBe(201); |
| 87 | + expect(resWihtoutPDF.type).toBe('application/json'); |
| 88 | + expect(resWihtoutPDF.body).toEqual( |
| 89 | + expect.objectContaining({ |
| 90 | + title: dtoWithoutPdfFile.title, |
| 91 | + content: dtoWithoutPdfFile.content, |
| 92 | + link: dtoWithoutPdfFile.link, |
| 93 | + show_only_login: dtoWithoutPdfFile.show_only_login, |
| 94 | + }), |
| 95 | + ); |
| 96 | + |
| 97 | + // PDF 파일로 생성 |
| 98 | + const dtoWithPdfFile: WhitebookDto = { |
| 99 | + title: 'New Whitebook with PDF', |
| 100 | + content: 'Content of the new whitebook with PDF', |
| 101 | + show_only_login: false, |
| 102 | + }; |
| 103 | + const pdf = new MemoryStoredFile(); |
| 104 | + pdf.originalName = 'test.pdf'; |
| 105 | + pdf.buffer = Buffer.from('test pdf buffer'); |
| 106 | + |
| 107 | + const resWithPdf = await request(app.getHttpServer()) |
| 108 | + .post('/whitebook') |
| 109 | + .attach('pdf_file', pdf.buffer, pdf.originalName) |
| 110 | + .field('title', dtoWithPdfFile.title) |
| 111 | + .field('content', dtoWithPdfFile.content) |
| 112 | + .field('show_only_login', dtoWithPdfFile.show_only_login); |
| 113 | + |
| 114 | + expect(resWithPdf.status).toBe(201); |
| 115 | + expect(resWithPdf.type).toBe('application/json'); |
| 116 | + expect(resWithPdf.body).toEqual( |
| 117 | + expect.objectContaining({ |
| 118 | + title: dtoWithPdfFile.title, |
| 119 | + content: dtoWithPdfFile.content, |
| 120 | + link: expect.stringContaining('whitebook'), |
| 121 | + show_only_login: dtoWithPdfFile.show_only_login, |
| 122 | + }), |
| 123 | + ); |
| 124 | + }); |
| 125 | + |
| 126 | + it('/whitebook/:id [Put] 200', async () => { |
| 127 | + const Dto: WhitebookDto = { |
| 128 | + title: 'New Whitebook', |
| 129 | + content: 'Content of the new whitebook', |
| 130 | + link: 'https://www.example.com', |
| 131 | + show_only_login: false, |
| 132 | + }; |
| 133 | + |
| 134 | + const postResponse = await request(app.getHttpServer()) |
| 135 | + .post('/whitebook') |
| 136 | + .send(Dto); |
| 137 | + |
| 138 | + expect(postResponse.status).toBe(201); |
| 139 | + expect(postResponse.type).toBe('application/json'); |
| 140 | + |
| 141 | + const uuid = postResponse.body.uuid; |
| 142 | + |
| 143 | + // PDF 파일 없이 업데이트 |
| 144 | + const updateDataWithLink = { |
| 145 | + title: 'Updated Whitebook without PDF', |
| 146 | + content: 'Updated content of the whitebook without PDF', |
| 147 | + link: 'https://www.example.com/updated', |
| 148 | + show_only_login: false, |
| 149 | + }; |
| 150 | + |
| 151 | + const putResponse = await request(app.getHttpServer()) |
| 152 | + .put(`/whitebook/${uuid}`) |
| 153 | + .send(updateDataWithLink); |
| 154 | + |
| 155 | + expect(putResponse.status).toBe(200); |
| 156 | + expect(putResponse.type).toBe('application/json'); |
| 157 | + expect(putResponse.body).toEqual( |
| 158 | + expect.objectContaining({ |
| 159 | + affected: 1, |
| 160 | + }), |
| 161 | + ); |
| 162 | + |
| 163 | + const getResponse = await request(app.getHttpServer()).get(`/whitebook`); |
| 164 | + |
| 165 | + expect(getResponse.status).toBe(200); |
| 166 | + expect(getResponse.type).toBe('application/json'); |
| 167 | + expect(getResponse.body).toEqual( |
| 168 | + expect.arrayContaining([ |
| 169 | + expect.objectContaining({ |
| 170 | + title: updateDataWithLink.title, |
| 171 | + content: updateDataWithLink.content, |
| 172 | + link: updateDataWithLink.link, |
| 173 | + show_only_login: updateDataWithLink.show_only_login, |
| 174 | + }), |
| 175 | + ]), |
| 176 | + ); |
| 177 | + |
| 178 | + // PDF 파일로 업데이트 |
| 179 | + const updateDataWithPdf = { |
| 180 | + title: 'Updated Whitebook with PDF', |
| 181 | + content: 'Updated content of the whitebook with PDF', |
| 182 | + show_only_login: false, |
| 183 | + }; |
| 184 | + const pdf = new MemoryStoredFile(); |
| 185 | + pdf.originalName = 'test.pdf'; |
| 186 | + pdf.buffer = Buffer.from('test pdf buffer'); |
| 187 | + |
| 188 | + const putResponseWithPdf = await request(app.getHttpServer()) |
| 189 | + .put(`/whitebook/${uuid}`) |
| 190 | + .attach('pdf_file', pdf.buffer, pdf.originalName) |
| 191 | + .field('title', updateDataWithPdf.title) |
| 192 | + .field('content', updateDataWithPdf.content) |
| 193 | + .field('show_only_login', updateDataWithPdf.show_only_login); |
| 194 | + |
| 195 | + expect(putResponseWithPdf.status).toBe(200); |
| 196 | + expect(putResponseWithPdf.type).toBe('application/json'); |
| 197 | + expect(putResponseWithPdf.body).toEqual( |
| 198 | + expect.objectContaining({ |
| 199 | + affected: 1, |
| 200 | + }), |
| 201 | + ); |
| 202 | + |
| 203 | + const getResponseAfterPdf = await request(app.getHttpServer()).get( |
| 204 | + `/whitebook`, |
| 205 | + ); |
| 206 | + |
| 207 | + expect(getResponseAfterPdf.status).toBe(200); |
| 208 | + expect(getResponseAfterPdf.type).toBe('application/json'); |
| 209 | + expect(getResponseAfterPdf.body).toEqual( |
| 210 | + expect.arrayContaining([ |
| 211 | + expect.objectContaining({ |
| 212 | + title: updateDataWithPdf.title, |
| 213 | + content: updateDataWithPdf.content, |
| 214 | + link: expect.stringContaining('whitebook'), |
| 215 | + }), |
| 216 | + ]), |
| 217 | + ); |
| 218 | + }); |
| 219 | + |
| 220 | + it('/whitebook/:id [DELETE] 200', async () => { |
| 221 | + const Dto: WhitebookDto = { |
| 222 | + title: 'New Whitebook', |
| 223 | + content: 'Content of the new whitebook', |
| 224 | + link: 'https://www.example.com', |
| 225 | + show_only_login: false, |
| 226 | + }; |
| 227 | + |
| 228 | + const postRes = await request(app.getHttpServer()) |
| 229 | + .post('/whitebook') |
| 230 | + .send(Dto); |
| 231 | + |
| 232 | + expect(postRes.status).toBe(201); |
| 233 | + expect(postRes.type).toBe('application/json'); |
| 234 | + |
| 235 | + const uuid = postRes.body.uuid; |
| 236 | + |
| 237 | + const deleteResponse = await request(app.getHttpServer()).delete( |
| 238 | + `/whitebook/${uuid}`, |
| 239 | + ); |
| 240 | + |
| 241 | + expect(deleteResponse.status).toBe(200); |
| 242 | + expect(deleteResponse.type).toBe('application/json'); |
| 243 | + expect(deleteResponse.body).toEqual( |
| 244 | + expect.objectContaining({ |
| 245 | + affected: 1, |
| 246 | + }), |
| 247 | + ); |
| 248 | + |
| 249 | + const getResponse = await request(app.getHttpServer()).get(`/whitebook`); |
| 250 | + |
| 251 | + expect(getResponse.status).toBe(200); |
| 252 | + expect(getResponse.type).toBe('application/json'); |
| 253 | + expect(getResponse.body).not.toEqual( |
| 254 | + expect.arrayContaining([ |
| 255 | + expect.objectContaining({ |
| 256 | + uuid: uuid, |
| 257 | + }), |
| 258 | + ]), |
| 259 | + ); |
| 260 | + }); |
| 261 | +}); |
0 commit comments