Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

프로필 모듈 리팩토링 #365

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nestjs-BE/server/src/profiles/profiles.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
2 changes: 1 addition & 1 deletion nestjs-BE/server/src/profiles/profiles.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class ProfilesService {
profileUuid: string,
): Promise<boolean> {
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;
}
Expand Down
4 changes: 2 additions & 2 deletions nestjs-BE/server/src/spaces/spaces.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
32 changes: 16 additions & 16 deletions nestjs-BE/server/src/spaces/spaces.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -45,7 +45,7 @@ describe('SpacesService', () => {
},
},
{
provide: UsersService,
provide: ProfilesService,
useValue: { verifyUserProfile: jest.fn(async () => true) },
},
{
Expand All @@ -59,8 +59,8 @@ describe('SpacesService', () => {
prisma = module.get<PrismaService>(PrismaService);
configService = module.get<ConfigService>(ConfigService);
profileSpaceService = module.get<ProfileSpaceService>(ProfileSpaceService);
usersService = module.get<UsersService>(UsersService);
uploadService = module.get<UploadService>(UploadService);
profilesService = module.get<ProfilesService>(ProfilesService);
});

describe('findSpace', () => {
Expand All @@ -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(),
);

Expand Down Expand Up @@ -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(),
);

Expand Down Expand Up @@ -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(),
);

Expand All @@ -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(),
);

Expand Down Expand Up @@ -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(),
);

Expand Down Expand Up @@ -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(),
);

Expand Down Expand Up @@ -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(),
);

Expand All @@ -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(),
);

Expand Down Expand Up @@ -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(),
);

Expand All @@ -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(),
);

Expand Down Expand Up @@ -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(),
);

Expand All @@ -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(),
);

Expand Down
16 changes: 8 additions & 8 deletions nestjs-BE/server/src/spaces/spaces.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ 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 {
constructor(
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<Space | null> {
Expand All @@ -33,7 +33,7 @@ export class SpacesService {
profileUuid: string,
spaceUuid: string,
): Promise<Space> {
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(
Expand All @@ -50,7 +50,7 @@ export class SpacesService {
icon: Express.Multer.File,
createSpaceDto: CreateSpacePrismaDto,
): Promise<Space> {
await this.usersService.verifyUserProfile(userUuid, profileUuid);
await this.profilesService.verifyUserProfile(userUuid, profileUuid);
const iconUrl = icon
? await this.uploadService.uploadFile(icon)
: this.configService.get<string>('APP_ICON_URL');
Expand All @@ -72,7 +72,7 @@ export class SpacesService {
icon: Express.Multer.File,
updateSpaceDto: UpdateSpacePrismaDto,
): Promise<Space> {
await this.usersService.verifyUserProfile(userUuid, profileUuid);
await this.profilesService.verifyUserProfile(userUuid, profileUuid);
const isProfileInSpace = await this.profileSpaceService.isProfileInSpace(
profileUuid,
spaceUuid,
Expand Down Expand Up @@ -106,7 +106,7 @@ export class SpacesService {
profileUuid: string,
spaceUuid: string,
): Promise<Space> {
await this.usersService.verifyUserProfile(userUuid, profileUuid);
await this.profilesService.verifyUserProfile(userUuid, profileUuid);
try {
await this.profileSpaceService.createProfileSpace(profileUuid, spaceUuid);
} catch (err) {
Expand All @@ -131,7 +131,7 @@ export class SpacesService {
profileUuid: string,
spaceUuid: string,
): Promise<void> {
await this.usersService.verifyUserProfile(userUuid, profileUuid);
await this.profilesService.verifyUserProfile(userUuid, profileUuid);
try {
await this.profileSpaceService.deleteProfileSpace(profileUuid, spaceUuid);
} catch (err) {
Expand All @@ -157,7 +157,7 @@ export class SpacesService {
profileUuid: string,
spaceUuid: string,
): Promise<Profile[]> {
await this.usersService.verifyUserProfile(userUuid, profileUuid);
await this.profilesService.verifyUserProfile(userUuid, profileUuid);
const isProfileInSpace = await this.profileSpaceService.isProfileInSpace(
profileUuid,
spaceUuid,
Expand Down
3 changes: 1 addition & 2 deletions nestjs-BE/server/src/users/users.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
47 changes: 0 additions & 47 deletions nestjs-BE/server/src/users/users.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -31,7 +24,6 @@ describe('UsersService', () => {
}).compile();

usersService = module.get<UsersService>(UsersService);
profilesService = module.get<ProfilesService>(ProfilesService);
prisma = module.get<PrismaService>(PrismaService);
});

Expand Down Expand Up @@ -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);
});
});
23 changes: 2 additions & 21 deletions nestjs-BE/server/src/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -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<User> {
return this.prisma.$transaction(async () => {
Expand Down Expand Up @@ -51,15 +43,4 @@ export class UsersService {

return spaces;
}

async verifyUserProfile(
userUuid: string,
profileUuid: string,
): Promise<boolean> {
const profile =
await this.profilesService.findProfileByProfileUuid(profileUuid);
if (!profile) throw new NotFoundException();
if (userUuid !== profile.userUuid) throw new ForbiddenException();
return true;
}
}