diff --git a/nestjs-BE/server/src/profiles/profiles.service.spec.ts b/nestjs-BE/server/src/profiles/profiles.service.spec.ts index d8fd603b..938bee15 100644 --- a/nestjs-BE/server/src/profiles/profiles.service.spec.ts +++ b/nestjs-BE/server/src/profiles/profiles.service.spec.ts @@ -145,7 +145,7 @@ describe('ProfilesService', () => { const res = profilesService.verifyUserProfile(userUuid, profileUuid); - await expect(res).rejects.toThrow(NotFoundException); + await expect(res).rejects.toThrow(ForbiddenException); }); it('profile user not own', async () => { diff --git a/nestjs-BE/server/src/profiles/profiles.service.ts b/nestjs-BE/server/src/profiles/profiles.service.ts index 5149e838..42c57130 100644 --- a/nestjs-BE/server/src/profiles/profiles.service.ts +++ b/nestjs-BE/server/src/profiles/profiles.service.ts @@ -77,7 +77,7 @@ export class ProfilesService { profileUuid: string, ): Promise { const profile = await this.findProfileByProfileUuid(profileUuid); - if (!profile) throw new NotFoundException(); + if (!profile) throw new ForbiddenException(); if (userUuid !== profile.userUuid) throw new ForbiddenException(); return true; } diff --git a/nestjs-BE/server/src/spaces/spaces.module.ts b/nestjs-BE/server/src/spaces/spaces.module.ts index 8c00d20f..3bc7d956 100644 --- a/nestjs-BE/server/src/spaces/spaces.module.ts +++ b/nestjs-BE/server/src/spaces/spaces.module.ts @@ -3,10 +3,10 @@ import { SpacesService } from './spaces.service'; import { SpacesController } from './spaces.controller'; import { UploadModule } from '../upload/upload.module'; import { ProfileSpaceModule } from '../profile-space/profile-space.module'; -import { UsersModule } from '../users/users.module'; +import { ProfilesModule } from '../profiles/profiles.module'; @Module({ - imports: [ProfileSpaceModule, UploadModule, UsersModule], + imports: [ProfileSpaceModule, UploadModule, ProfilesModule], controllers: [SpacesController], providers: [SpacesService], exports: [SpacesService], diff --git a/nestjs-BE/server/src/spaces/spaces.service.spec.ts b/nestjs-BE/server/src/spaces/spaces.service.spec.ts index 68d6bbcd..40251892 100644 --- a/nestjs-BE/server/src/spaces/spaces.service.spec.ts +++ b/nestjs-BE/server/src/spaces/spaces.service.spec.ts @@ -12,16 +12,16 @@ import { CreateSpacePrismaDto } from './dto/create-space.dto'; import { UpdateSpacePrismaDto } from './dto/update-space.dto'; import { PrismaService } from '../prisma/prisma.service'; import { ProfileSpaceService } from '../profile-space/profile-space.service'; -import { UsersService } from '../users/users.service'; import { UploadService } from '../upload/upload.service'; +import { ProfilesService } from '../profiles/profiles.service'; describe('SpacesService', () => { let spacesService: SpacesService; let prisma: PrismaService; let configService: ConfigService; let profileSpaceService: ProfileSpaceService; - let usersService: UsersService; let uploadService: UploadService; + let profilesService: ProfilesService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ @@ -45,7 +45,7 @@ describe('SpacesService', () => { }, }, { - provide: UsersService, + provide: ProfilesService, useValue: { verifyUserProfile: jest.fn(async () => true) }, }, { @@ -59,8 +59,8 @@ describe('SpacesService', () => { prisma = module.get(PrismaService); configService = module.get(ConfigService); profileSpaceService = module.get(ProfileSpaceService); - usersService = module.get(UsersService); uploadService = module.get(UploadService); + profilesService = module.get(ProfilesService); }); describe('findSpace', () => { @@ -85,7 +85,7 @@ describe('SpacesService', () => { }); it('profile user not own', async () => { - (usersService.verifyUserProfile as jest.Mock).mockRejectedValue( + (profilesService.verifyUserProfile as jest.Mock).mockRejectedValue( new ForbiddenException(), ); @@ -128,7 +128,7 @@ describe('SpacesService', () => { }); it('profile not found', async () => { - (usersService.verifyUserProfile as jest.Mock).mockRejectedValue( + (profilesService.verifyUserProfile as jest.Mock).mockRejectedValue( new NotFoundException(), ); @@ -185,7 +185,7 @@ describe('SpacesService', () => { name: 'new space name', } as CreateSpacePrismaDto; - (usersService.verifyUserProfile as jest.Mock).mockRejectedValue( + (profilesService.verifyUserProfile as jest.Mock).mockRejectedValue( new NotFoundException(), ); @@ -206,7 +206,7 @@ describe('SpacesService', () => { name: 'new space name', } as CreateSpacePrismaDto; - (usersService.verifyUserProfile as jest.Mock).mockRejectedValue( + (profilesService.verifyUserProfile as jest.Mock).mockRejectedValue( new ForbiddenException(), ); @@ -328,7 +328,7 @@ describe('SpacesService', () => { it('profile user not own', async () => { const updateSpaceDto = { name: 'new space name' } as UpdateSpacePrismaDto; - (usersService.verifyUserProfile as jest.Mock).mockRejectedValue( + (profilesService.verifyUserProfile as jest.Mock).mockRejectedValue( new ForbiddenException(), ); @@ -368,7 +368,7 @@ describe('SpacesService', () => { it('profile not found', async () => { const updateSpaceDto = { name: 'new space name' } as UpdateSpacePrismaDto; - (usersService.verifyUserProfile as jest.Mock).mockRejectedValue( + (profilesService.verifyUserProfile as jest.Mock).mockRejectedValue( new NotFoundException(), ); @@ -426,7 +426,7 @@ describe('SpacesService', () => { }); it('profile not found', async () => { - (usersService.verifyUserProfile as jest.Mock).mockRejectedValue( + (profilesService.verifyUserProfile as jest.Mock).mockRejectedValue( new NotFoundException(), ); @@ -436,7 +436,7 @@ describe('SpacesService', () => { }); it('profile user not own', async () => { - (usersService.verifyUserProfile as jest.Mock).mockRejectedValue( + (profilesService.verifyUserProfile as jest.Mock).mockRejectedValue( new ForbiddenException(), ); @@ -511,7 +511,7 @@ describe('SpacesService', () => { }); it('profile not found', async () => { - (usersService.verifyUserProfile as jest.Mock).mockRejectedValue( + (profilesService.verifyUserProfile as jest.Mock).mockRejectedValue( new NotFoundException(), ); @@ -521,7 +521,7 @@ describe('SpacesService', () => { }); it('profile user not own', async () => { - (usersService.verifyUserProfile as jest.Mock).mockRejectedValue( + (profilesService.verifyUserProfile as jest.Mock).mockRejectedValue( new ForbiddenException(), ); @@ -550,7 +550,7 @@ describe('SpacesService', () => { const spaceUuid = 'space uuid'; it('profile not found', async () => { - (usersService.verifyUserProfile as jest.Mock).mockRejectedValue( + (profilesService.verifyUserProfile as jest.Mock).mockRejectedValue( new NotFoundException(), ); @@ -564,7 +564,7 @@ describe('SpacesService', () => { }); it('profile user not own', async () => { - (usersService.verifyUserProfile as jest.Mock).mockRejectedValue( + (profilesService.verifyUserProfile as jest.Mock).mockRejectedValue( new ForbiddenException(), ); diff --git a/nestjs-BE/server/src/spaces/spaces.service.ts b/nestjs-BE/server/src/spaces/spaces.service.ts index 0c60b81b..5f48798a 100644 --- a/nestjs-BE/server/src/spaces/spaces.service.ts +++ b/nestjs-BE/server/src/spaces/spaces.service.ts @@ -11,8 +11,8 @@ import { UpdateSpacePrismaDto } from './dto/update-space.dto'; import { CreateSpacePrismaDto } from './dto/create-space.dto'; import { PrismaService } from '../prisma/prisma.service'; import { ProfileSpaceService } from '../profile-space/profile-space.service'; -import { UsersService } from '../users/users.service'; import { UploadService } from '../upload/upload.service'; +import { ProfilesService } from '../profiles/profiles.service'; @Injectable() export class SpacesService { @@ -20,8 +20,8 @@ export class SpacesService { private readonly prisma: PrismaService, private readonly configService: ConfigService, private readonly profileSpaceService: ProfileSpaceService, - private readonly usersService: UsersService, private readonly uploadService: UploadService, + private readonly profilesService: ProfilesService, ) {} async findSpaceBySpaceUuid(spaceUuid: string): Promise { @@ -33,7 +33,7 @@ export class SpacesService { profileUuid: string, spaceUuid: string, ): Promise { - await this.usersService.verifyUserProfile(userUuid, profileUuid); + await this.profilesService.verifyUserProfile(userUuid, profileUuid); const space = await this.findSpaceBySpaceUuid(spaceUuid); if (!space) throw new NotFoundException(); const isProfileInSpace = await this.profileSpaceService.isProfileInSpace( @@ -50,7 +50,7 @@ export class SpacesService { icon: Express.Multer.File, createSpaceDto: CreateSpacePrismaDto, ): Promise { - await this.usersService.verifyUserProfile(userUuid, profileUuid); + await this.profilesService.verifyUserProfile(userUuid, profileUuid); const iconUrl = icon ? await this.uploadService.uploadFile(icon) : this.configService.get('APP_ICON_URL'); @@ -72,7 +72,7 @@ export class SpacesService { icon: Express.Multer.File, updateSpaceDto: UpdateSpacePrismaDto, ): Promise { - await this.usersService.verifyUserProfile(userUuid, profileUuid); + await this.profilesService.verifyUserProfile(userUuid, profileUuid); const isProfileInSpace = await this.profileSpaceService.isProfileInSpace( profileUuid, spaceUuid, @@ -106,7 +106,7 @@ export class SpacesService { profileUuid: string, spaceUuid: string, ): Promise { - await this.usersService.verifyUserProfile(userUuid, profileUuid); + await this.profilesService.verifyUserProfile(userUuid, profileUuid); try { await this.profileSpaceService.createProfileSpace(profileUuid, spaceUuid); } catch (err) { @@ -131,7 +131,7 @@ export class SpacesService { profileUuid: string, spaceUuid: string, ): Promise { - await this.usersService.verifyUserProfile(userUuid, profileUuid); + await this.profilesService.verifyUserProfile(userUuid, profileUuid); try { await this.profileSpaceService.deleteProfileSpace(profileUuid, spaceUuid); } catch (err) { @@ -157,7 +157,7 @@ export class SpacesService { profileUuid: string, spaceUuid: string, ): Promise { - await this.usersService.verifyUserProfile(userUuid, profileUuid); + await this.profilesService.verifyUserProfile(userUuid, profileUuid); const isProfileInSpace = await this.profileSpaceService.isProfileInSpace( profileUuid, spaceUuid, diff --git a/nestjs-BE/server/src/users/users.module.ts b/nestjs-BE/server/src/users/users.module.ts index 6fd5b2ad..acfee4ee 100644 --- a/nestjs-BE/server/src/users/users.module.ts +++ b/nestjs-BE/server/src/users/users.module.ts @@ -2,10 +2,9 @@ import { Module } from '@nestjs/common'; import { UsersService } from './users.service'; import { UsersController } from './users.controller'; import { PrismaModule } from '../prisma/prisma.module'; -import { ProfilesModule } from '../profiles/profiles.module'; @Module({ - imports: [PrismaModule, ProfilesModule], + imports: [PrismaModule], controllers: [UsersController], providers: [UsersService], exports: [UsersService], diff --git a/nestjs-BE/server/src/users/users.service.spec.ts b/nestjs-BE/server/src/users/users.service.spec.ts index c98fbd32..81f58428 100644 --- a/nestjs-BE/server/src/users/users.service.spec.ts +++ b/nestjs-BE/server/src/users/users.service.spec.ts @@ -3,22 +3,15 @@ import { UsersService } from './users.service'; import { PrismaService } from '../prisma/prisma.service'; import { v4 as uuid } from 'uuid'; import { KakaoUser, User } from '@prisma/client'; -import { ProfilesService } from '../profiles/profiles.service'; -import { ForbiddenException, NotFoundException } from '@nestjs/common'; describe('UsersService', () => { let usersService: UsersService; - let profilesService: ProfilesService; let prisma: PrismaService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [ UsersService, - { - provide: ProfilesService, - useValue: { findProfileByProfileUuid: jest.fn() }, - }, { provide: PrismaService, useValue: { @@ -31,7 +24,6 @@ describe('UsersService', () => { }).compile(); usersService = module.get(UsersService); - profilesService = module.get(ProfilesService); prisma = module.get(PrismaService); }); @@ -77,43 +69,4 @@ describe('UsersService', () => { expect(prisma.user.create).toHaveBeenCalled(); expect(prisma.user.findUnique).not.toHaveBeenCalled(); }); - - it('verifyUserProfile verified', async () => { - const userMock = { uuid: 'user uuid' }; - const profileMock = { uuid: 'profile uuid', userUuid: userMock.uuid }; - - (profilesService.findProfileByProfileUuid as jest.Mock).mockResolvedValue( - profileMock, - ); - - const res = usersService.verifyUserProfile(userMock.uuid, profileMock.uuid); - - await expect(res).resolves.toBeTruthy(); - }); - - it('verifyUserProfile profile not found', async () => { - const userMock = { uuid: 'user uuid' }; - const profileMock = { uuid: 'profile uuid', userUuid: userMock.uuid }; - - (profilesService.findProfileByProfileUuid as jest.Mock).mockResolvedValue( - null, - ); - - const res = usersService.verifyUserProfile(userMock.uuid, profileMock.uuid); - - await expect(res).rejects.toThrow(NotFoundException); - }); - - it('verifyUserProfile profile user not own', async () => { - const userMock = { uuid: 'user uuid' }; - const profileMock = { uuid: 'profile uuid', userUuid: 'other user uuid' }; - - (profilesService.findProfileByProfileUuid as jest.Mock).mockResolvedValue( - profileMock, - ); - - const res = usersService.verifyUserProfile(userMock.uuid, profileMock.uuid); - - await expect(res).rejects.toThrow(ForbiddenException); - }); }); diff --git a/nestjs-BE/server/src/users/users.service.ts b/nestjs-BE/server/src/users/users.service.ts index 133c2e9d..b1687ad6 100644 --- a/nestjs-BE/server/src/users/users.service.ts +++ b/nestjs-BE/server/src/users/users.service.ts @@ -1,20 +1,12 @@ -import { - ForbiddenException, - Injectable, - NotFoundException, -} from '@nestjs/common'; +import { Injectable } from '@nestjs/common'; import { PrismaService } from '../prisma/prisma.service'; import { CreateUserPrismaDto } from './dto/create-user.dto'; import { Space, User } from '@prisma/client'; import { v4 as uuid } from 'uuid'; -import { ProfilesService } from '../profiles/profiles.service'; @Injectable() export class UsersService { - constructor( - private readonly prisma: PrismaService, - private readonly profilesService: ProfilesService, - ) {} + constructor(private readonly prisma: PrismaService) {} async getOrCreateUser(data: CreateUserPrismaDto): Promise { return this.prisma.$transaction(async () => { @@ -51,15 +43,4 @@ export class UsersService { return spaces; } - - async verifyUserProfile( - userUuid: string, - profileUuid: string, - ): Promise { - const profile = - await this.profilesService.findProfileByProfileUuid(profileUuid); - if (!profile) throw new NotFoundException(); - if (userUuid !== profile.userUuid) throw new ForbiddenException(); - return true; - } }