From ae40972d53b7aa21f1b8983fb43a79e584d81d83 Mon Sep 17 00:00:00 2001 From: Conut-1 <1mim1@naver.com> Date: Fri, 27 Dec 2024 16:19:13 +0900 Subject: [PATCH] =?UTF-8?q?test:=20updateSpace=20e2e=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nestjs-BE/server/test/spaces.e2e-spec.ts | 239 ++++++++++++----------- 1 file changed, 125 insertions(+), 114 deletions(-) diff --git a/nestjs-BE/server/test/spaces.e2e-spec.ts b/nestjs-BE/server/test/spaces.e2e-spec.ts index 1fce5433..61b14956 100644 --- a/nestjs-BE/server/test/spaces.e2e-spec.ts +++ b/nestjs-BE/server/test/spaces.e2e-spec.ts @@ -352,64 +352,66 @@ describe('SpacesController (e2e)', () => { }); }); - describe('/spaces/:space_uuid?profile_uuid={profile_uuid} (PATCH)', () => { - it('update success', async () => { - const newSpace = { - name: 'new test space', - icon: testImagePath, - iconContentType: 'image/png', - }; + describe('/spaces/:space_uuid (PATCH)', () => { + let testUser: User; + let testProfile: Profile; + let testSpace: Space; + let testToken: string; + + beforeEach(async () => { + testUser = await prisma.user.create({ data: { uuid: uuid() } }); + testProfile = await prisma.profile.create({ + data: { + uuid: uuid(), + userUuid: testUser.uuid, + image: 'test image', + nickname: 'test nickname', + }, + }); + testToken = sign( + { sub: testUser.uuid }, + configService.get('JWT_ACCESS_SECRET'), + { expiresIn: '5m' }, + ); + testSpace = await prisma.space.create({ + data: { + uuid: uuid(), + name: 'test space', + icon: configService.get('APP_ICON_URL'), + }, + }); await prisma.profileSpace.create({ - data: { spaceUuid: testSpace.uuid, profileUuid: testProfile.uuid }, + data: { profileUuid: testProfile.uuid, spaceUuid: testSpace.uuid }, }); - const imageUrlPattern = `^https\\:\\/\\/${configService.get( - 'S3_BUCKET_NAME', - )}\\.s3\\.${configService.get( - 'AWS_REGION', - )}\\.amazonaws\\.com\\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}-`; - const imageRegExp = new RegExp(imageUrlPattern); + }); + + it('respond ok when space update success', async () => { + const newSpace = { name: 'new test space', icon: testImage }; return request(app.getHttpServer()) - .patch(`/spaces/${testSpace.uuid}?profile_uuid=${testProfile.uuid}`) + .patch(`/spaces/${testSpace.uuid}`) .auth(testToken, { type: 'bearer' }) .field('name', newSpace.name) - .attach('icon', newSpace.icon, { - contentType: newSpace.iconContentType, - }) + .field('profile_uuid', testProfile.uuid) + .attach('icon', newSpace.icon, 'base_image.png') .expect(HttpStatus.OK) .expect((res) => { expect(res.body.message).toBe('OK'); expect(res.body.statusCode).toBe(HttpStatus.OK); expect(res.body.data.uuid).toBe(testSpace.uuid); expect(res.body.data.name).toBe(newSpace.name); - expect(res.body.data.icon).not.toBe( - configService.get('APP_ICON_URL'), - ); expect(res.body.data.icon).toMatch(imageRegExp); }); }); - it('request without name', async () => { - const newSpace = { - icon: testImagePath, - iconContentType: 'image/png', - }; - const imageUrlPattern = `^https\\:\\/\\/${configService.get( - 'S3_BUCKET_NAME', - )}\\.s3\\.${configService.get( - 'AWS_REGION', - )}\\.amazonaws\\.com\\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}-`; - const imageRegExp = new RegExp(imageUrlPattern); - await prisma.profileSpace.create({ - data: { spaceUuid: testSpace.uuid, profileUuid: testProfile.uuid }, - }); + it('respond ok when request without name', async () => { + const newSpace = { icon: testImage }; return request(app.getHttpServer()) - .patch(`/spaces/${testSpace.uuid}?profile_uuid=${testProfile.uuid}`) + .patch(`/spaces/${testSpace.uuid}`) .auth(testToken, { type: 'bearer' }) - .attach('icon', newSpace.icon, { - contentType: newSpace.iconContentType, - }) + .field('profile_uuid', testProfile.uuid) + .attach('icon', newSpace.icon, 'base_image.png') .expect(HttpStatus.OK) .expect((res) => { expect(res.body.message).toBe('OK'); @@ -420,16 +422,13 @@ describe('SpacesController (e2e)', () => { }); }); - it('request without icon', async () => { + it('respond ok when request without icon', async () => { const newSpace = { name: 'new test space' }; - await prisma.profileSpace.create({ - data: { spaceUuid: testSpace.uuid, profileUuid: testProfile.uuid }, - }); return request(app.getHttpServer()) - .patch(`/spaces/${testSpace.uuid}?profile_uuid=${testProfile.uuid}`) + .patch(`/spaces/${testSpace.uuid}`) .auth(testToken, { type: 'bearer' }) - .send({ name: newSpace.name }) + .send({ name: newSpace.name, profile_uuid: testProfile.uuid }) .expect(HttpStatus.OK) .expect({ message: 'OK', @@ -442,37 +441,49 @@ describe('SpacesController (e2e)', () => { }); }); - it('profile uuid needed', async () => { - const newSpace = { - name: 'new test space', - icon: testImagePath, - iconContentType: 'image/png', - }; + it('respond ok when request without icon (Content-Type multipart/form-data)', async () => { + const newSpace = { name: 'new test space' }; return request(app.getHttpServer()) .patch(`/spaces/${testSpace.uuid}`) .auth(testToken, { type: 'bearer' }) .field('name', newSpace.name) - .attach('icon', newSpace.icon, { - contentType: newSpace.iconContentType, - }) + .field('profile_uuid', testProfile.uuid) + .expect(HttpStatus.OK) + .expect({ + message: 'OK', + statusCode: HttpStatus.OK, + data: { + uuid: testSpace.uuid, + name: newSpace.name, + icon: configService.get('APP_ICON_URL'), + }, + }); + }); + + it('respond bad request when profile uuid needed', async () => { + const newSpace = { name: 'new test space', icon: testImage }; + + return request(app.getHttpServer()) + .patch(`/spaces/${testSpace.uuid}`) + .auth(testToken, { type: 'bearer' }) + .field('name', newSpace.name) + .attach('icon', newSpace.icon, 'base_image.png') .expect(HttpStatus.BAD_REQUEST) .expect({ message: 'Bad Request', statusCode: HttpStatus.BAD_REQUEST }); }); - it('icon is string', async () => { - const newSpace = { - name: 'new test space', - icon: 'string value', - }; - await prisma.profileSpace.create({ - data: { spaceUuid: testSpace.uuid, profileUuid: testProfile.uuid }, - }); + it('respond bad request when icon is string', async () => { + const newSpace = { name: 'new test space', icon: 'string value' }; return request(app.getHttpServer()) - .patch(`/spaces/${testSpace.uuid}?profile_uuid=${testProfile.uuid}`) + .patch(`/spaces/${testSpace.uuid}`) .auth(testToken, { type: 'bearer' }) - .send({ name: newSpace.name, icon: newSpace.icon }) + .send({ + name: newSpace.name, + icon: newSpace.icon, + profile_uuid: testProfile.uuid, + }) .expect(HttpStatus.BAD_REQUEST) .expect((res) => { expect(res.body.message).toBe('icon is string'); @@ -480,14 +491,30 @@ describe('SpacesController (e2e)', () => { }); }); - it('unauthorized', async () => { - const icon = await readFile(resolve(__dirname, './base_image.png')); - const newSpace = { name: 'new test space', icon }; + it('respond bad request when icon is string (Content-Type: multipart/form-data)', async () => { + const newSpace = { name: 'new test space', icon: 'string value' }; return request(app.getHttpServer()) - .patch(`/spaces/${testSpace.uuid}?profile_uuid=${testProfile.uuid}`) + .patch(`/spaces/${testSpace.uuid}`) + .auth(testToken, { type: 'bearer' }) + .field('name', newSpace.name) + .field('icon', newSpace.icon) + .field('profile_uuid', testProfile.uuid) + .expect(HttpStatus.BAD_REQUEST) + .expect((res) => { + expect(res.body.message).toBe('icon is string'); + expect(res.body.statusCode).toBe(HttpStatus.BAD_REQUEST); + }); + }); + + it('respond unauthorized when access token not include', async () => { + const newSpace = { name: 'new test space', icon: testImage }; + + return request(app.getHttpServer()) + .patch(`/spaces/${testSpace.uuid}`) .field('name', newSpace.name) - .attach('icon', newSpace.icon) + .field('profile_uuid', testProfile.uuid) + .attach('icon', newSpace.icon, 'base_image.png') .expect(HttpStatus.UNAUTHORIZED) .expect({ message: 'Unauthorized', @@ -495,42 +522,27 @@ describe('SpacesController (e2e)', () => { }); }); - it("profile user doesn't have", async () => { - const newSpace = { - name: 'new test space', - icon: testImagePath, - iconContentType: 'image/png', - }; + it('respond forbidden when user does not own profile', async () => { + const newSpace = { name: 'new test space', icon: testImage }; const newUser = await prisma.user.create({ data: { uuid: uuid() } }); - const newProfile = await prisma.profile.create({ - data: { - uuid: uuid(), - userUuid: newUser.uuid, - image: 'test image', - nickname: 'test nickname', - }, - }); - await prisma.profileSpace.create({ - data: { spaceUuid: testSpace.uuid, profileUuid: newProfile.uuid }, - }); + const newToken = sign( + { sub: newUser.uuid }, + configService.get('JWT_ACCESS_SECRET'), + { expiresIn: '5m' }, + ); return request(app.getHttpServer()) - .patch(`/spaces/${testSpace.uuid}?profile_uuid=${newProfile.uuid}`) - .auth(testToken, { type: 'bearer' }) + .patch(`/spaces/${testSpace.uuid}`) + .auth(newToken, { type: 'bearer' }) .field('name', newSpace.name) - .attach('icon', newSpace.icon, { - contentType: newSpace.iconContentType, - }) + .field('profile_uuid', testProfile.uuid) + .attach('icon', newSpace.icon, 'base_image.png') .expect(HttpStatus.FORBIDDEN) .expect({ message: 'Forbidden', statusCode: HttpStatus.FORBIDDEN }); }); - it('profile not joined space', async () => { - const newSpace = { - name: 'new test space', - icon: testImagePath, - iconContentType: 'image/png', - }; + it('respond forbidden when profile not joined space', async () => { + const newSpace = { name: 'new test space', icon: testImage }; const newUser = await prisma.user.create({ data: { uuid: uuid() } }); const newProfile = await prisma.profile.create({ data: { @@ -540,34 +552,33 @@ describe('SpacesController (e2e)', () => { nickname: 'test nickname', }, }); + const newToken = sign( + { sub: newUser.uuid }, + configService.get('JWT_ACCESS_SECRET'), + { expiresIn: '5m' }, + ); return request(app.getHttpServer()) - .patch(`/spaces/${testSpace.uuid}?profile_uuid=${newProfile.uuid}`) - .auth(testToken, { type: 'bearer' }) + .patch(`/spaces/${testSpace.uuid}`) + .auth(newToken, { type: 'bearer' }) .field('name', newSpace.name) - .attach('icon', newSpace.icon, { - contentType: newSpace.iconContentType, - }) + .field('profile_uuid', newProfile.uuid) + .attach('icon', newSpace.icon, 'base_image.png') .expect(HttpStatus.FORBIDDEN) .expect({ message: 'Forbidden', statusCode: HttpStatus.FORBIDDEN }); }); - it('profile not found', () => { - const newSpace = { - name: 'new test space', - icon: testImagePath, - iconContentType: 'image/png', - }; + it('respond forbidden when profile not found', () => { + const newSpace = { name: 'new test space', icon: testImage }; return request(app.getHttpServer()) - .patch(`/spaces/${testSpace.uuid}?profile_uuid=${uuid()}`) + .patch(`/spaces/${testSpace.uuid}`) .auth(testToken, { type: 'bearer' }) .field('name', newSpace.name) - .attach('icon', newSpace.icon, { - contentType: newSpace.iconContentType, - }) - .expect(HttpStatus.NOT_FOUND) - .expect({ message: 'Not Found', statusCode: HttpStatus.NOT_FOUND }); + .field('profile_uuid', uuid()) + .attach('icon', newSpace.icon, 'base_image.png') + .expect(HttpStatus.FORBIDDEN) + .expect({ message: 'Forbidden', statusCode: HttpStatus.FORBIDDEN }); }); });