Skip to content

Commit

Permalink
test: updateSpace e2e 테스트 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Conut-1 committed Dec 27, 2024
1 parent 341a4f5 commit ae40972
Showing 1 changed file with 125 additions and 114 deletions.
239 changes: 125 additions & 114 deletions nestjs-BE/server/test/spaces.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>('JWT_ACCESS_SECRET'),
{ expiresIn: '5m' },
);
testSpace = await prisma.space.create({
data: {
uuid: uuid(),
name: 'test space',
icon: configService.get<string>('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<string>(
'S3_BUCKET_NAME',
)}\\.s3\\.${configService.get<string>(
'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<string>('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<string>(
'S3_BUCKET_NAME',
)}\\.s3\\.${configService.get<string>(
'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');
Expand All @@ -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',
Expand All @@ -442,95 +441,108 @@ 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<string>('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');
expect(res.body.statusCode).toBe(HttpStatus.BAD_REQUEST);
});
});

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',
statusCode: HttpStatus.UNAUTHORIZED,
});
});

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<string>('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: {
Expand All @@ -540,34 +552,33 @@ describe('SpacesController (e2e)', () => {
nickname: 'test nickname',
},
});
const newToken = sign(
{ sub: newUser.uuid },
configService.get<string>('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 });
});
});

Expand Down

0 comments on commit ae40972

Please sign in to comment.