Skip to content

Commit e4f32db

Browse files
authored
Merge pull request #365 from boostcampwm2023/BE-feature/profiles
ํ”„๋กœํ•„ ๋ชจ๋“ˆ ๋ฆฌํŒฉํ† ๋ง
2 parents e325da0 + d25c4c6 commit e4f32db

8 files changed

+31
-98
lines changed

โ€Žnestjs-BE/server/src/profiles/profiles.service.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ describe('ProfilesService', () => {
145145

146146
const res = profilesService.verifyUserProfile(userUuid, profileUuid);
147147

148-
await expect(res).rejects.toThrow(NotFoundException);
148+
await expect(res).rejects.toThrow(ForbiddenException);
149149
});
150150

151151
it('profile user not own', async () => {

โ€Žnestjs-BE/server/src/profiles/profiles.service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class ProfilesService {
7777
profileUuid: string,
7878
): Promise<boolean> {
7979
const profile = await this.findProfileByProfileUuid(profileUuid);
80-
if (!profile) throw new NotFoundException();
80+
if (!profile) throw new ForbiddenException();
8181
if (userUuid !== profile.userUuid) throw new ForbiddenException();
8282
return true;
8383
}

โ€Žnestjs-BE/server/src/spaces/spaces.module.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { SpacesService } from './spaces.service';
33
import { SpacesController } from './spaces.controller';
44
import { UploadModule } from '../upload/upload.module';
55
import { ProfileSpaceModule } from '../profile-space/profile-space.module';
6-
import { UsersModule } from '../users/users.module';
6+
import { ProfilesModule } from '../profiles/profiles.module';
77

88
@Module({
9-
imports: [ProfileSpaceModule, UploadModule, UsersModule],
9+
imports: [ProfileSpaceModule, UploadModule, ProfilesModule],
1010
controllers: [SpacesController],
1111
providers: [SpacesService],
1212
exports: [SpacesService],

โ€Žnestjs-BE/server/src/spaces/spaces.service.spec.ts

+16-16
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ import { CreateSpacePrismaDto } from './dto/create-space.dto';
1212
import { UpdateSpacePrismaDto } from './dto/update-space.dto';
1313
import { PrismaService } from '../prisma/prisma.service';
1414
import { ProfileSpaceService } from '../profile-space/profile-space.service';
15-
import { UsersService } from '../users/users.service';
1615
import { UploadService } from '../upload/upload.service';
16+
import { ProfilesService } from '../profiles/profiles.service';
1717

1818
describe('SpacesService', () => {
1919
let spacesService: SpacesService;
2020
let prisma: PrismaService;
2121
let configService: ConfigService;
2222
let profileSpaceService: ProfileSpaceService;
23-
let usersService: UsersService;
2423
let uploadService: UploadService;
24+
let profilesService: ProfilesService;
2525

2626
beforeEach(async () => {
2727
const module: TestingModule = await Test.createTestingModule({
@@ -45,7 +45,7 @@ describe('SpacesService', () => {
4545
},
4646
},
4747
{
48-
provide: UsersService,
48+
provide: ProfilesService,
4949
useValue: { verifyUserProfile: jest.fn(async () => true) },
5050
},
5151
{
@@ -59,8 +59,8 @@ describe('SpacesService', () => {
5959
prisma = module.get<PrismaService>(PrismaService);
6060
configService = module.get<ConfigService>(ConfigService);
6161
profileSpaceService = module.get<ProfileSpaceService>(ProfileSpaceService);
62-
usersService = module.get<UsersService>(UsersService);
6362
uploadService = module.get<UploadService>(UploadService);
63+
profilesService = module.get<ProfilesService>(ProfilesService);
6464
});
6565

6666
describe('findSpace', () => {
@@ -85,7 +85,7 @@ describe('SpacesService', () => {
8585
});
8686

8787
it('profile user not own', async () => {
88-
(usersService.verifyUserProfile as jest.Mock).mockRejectedValue(
88+
(profilesService.verifyUserProfile as jest.Mock).mockRejectedValue(
8989
new ForbiddenException(),
9090
);
9191

@@ -128,7 +128,7 @@ describe('SpacesService', () => {
128128
});
129129

130130
it('profile not found', async () => {
131-
(usersService.verifyUserProfile as jest.Mock).mockRejectedValue(
131+
(profilesService.verifyUserProfile as jest.Mock).mockRejectedValue(
132132
new NotFoundException(),
133133
);
134134

@@ -185,7 +185,7 @@ describe('SpacesService', () => {
185185
name: 'new space name',
186186
} as CreateSpacePrismaDto;
187187

188-
(usersService.verifyUserProfile as jest.Mock).mockRejectedValue(
188+
(profilesService.verifyUserProfile as jest.Mock).mockRejectedValue(
189189
new NotFoundException(),
190190
);
191191

@@ -206,7 +206,7 @@ describe('SpacesService', () => {
206206
name: 'new space name',
207207
} as CreateSpacePrismaDto;
208208

209-
(usersService.verifyUserProfile as jest.Mock).mockRejectedValue(
209+
(profilesService.verifyUserProfile as jest.Mock).mockRejectedValue(
210210
new ForbiddenException(),
211211
);
212212

@@ -328,7 +328,7 @@ describe('SpacesService', () => {
328328
it('profile user not own', async () => {
329329
const updateSpaceDto = { name: 'new space name' } as UpdateSpacePrismaDto;
330330

331-
(usersService.verifyUserProfile as jest.Mock).mockRejectedValue(
331+
(profilesService.verifyUserProfile as jest.Mock).mockRejectedValue(
332332
new ForbiddenException(),
333333
);
334334

@@ -368,7 +368,7 @@ describe('SpacesService', () => {
368368
it('profile not found', async () => {
369369
const updateSpaceDto = { name: 'new space name' } as UpdateSpacePrismaDto;
370370

371-
(usersService.verifyUserProfile as jest.Mock).mockRejectedValue(
371+
(profilesService.verifyUserProfile as jest.Mock).mockRejectedValue(
372372
new NotFoundException(),
373373
);
374374

@@ -426,7 +426,7 @@ describe('SpacesService', () => {
426426
});
427427

428428
it('profile not found', async () => {
429-
(usersService.verifyUserProfile as jest.Mock).mockRejectedValue(
429+
(profilesService.verifyUserProfile as jest.Mock).mockRejectedValue(
430430
new NotFoundException(),
431431
);
432432

@@ -436,7 +436,7 @@ describe('SpacesService', () => {
436436
});
437437

438438
it('profile user not own', async () => {
439-
(usersService.verifyUserProfile as jest.Mock).mockRejectedValue(
439+
(profilesService.verifyUserProfile as jest.Mock).mockRejectedValue(
440440
new ForbiddenException(),
441441
);
442442

@@ -511,7 +511,7 @@ describe('SpacesService', () => {
511511
});
512512

513513
it('profile not found', async () => {
514-
(usersService.verifyUserProfile as jest.Mock).mockRejectedValue(
514+
(profilesService.verifyUserProfile as jest.Mock).mockRejectedValue(
515515
new NotFoundException(),
516516
);
517517

@@ -521,7 +521,7 @@ describe('SpacesService', () => {
521521
});
522522

523523
it('profile user not own', async () => {
524-
(usersService.verifyUserProfile as jest.Mock).mockRejectedValue(
524+
(profilesService.verifyUserProfile as jest.Mock).mockRejectedValue(
525525
new ForbiddenException(),
526526
);
527527

@@ -550,7 +550,7 @@ describe('SpacesService', () => {
550550
const spaceUuid = 'space uuid';
551551

552552
it('profile not found', async () => {
553-
(usersService.verifyUserProfile as jest.Mock).mockRejectedValue(
553+
(profilesService.verifyUserProfile as jest.Mock).mockRejectedValue(
554554
new NotFoundException(),
555555
);
556556

@@ -564,7 +564,7 @@ describe('SpacesService', () => {
564564
});
565565

566566
it('profile user not own', async () => {
567-
(usersService.verifyUserProfile as jest.Mock).mockRejectedValue(
567+
(profilesService.verifyUserProfile as jest.Mock).mockRejectedValue(
568568
new ForbiddenException(),
569569
);
570570

โ€Žnestjs-BE/server/src/spaces/spaces.service.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ import { UpdateSpacePrismaDto } from './dto/update-space.dto';
1111
import { CreateSpacePrismaDto } from './dto/create-space.dto';
1212
import { PrismaService } from '../prisma/prisma.service';
1313
import { ProfileSpaceService } from '../profile-space/profile-space.service';
14-
import { UsersService } from '../users/users.service';
1514
import { UploadService } from '../upload/upload.service';
15+
import { ProfilesService } from '../profiles/profiles.service';
1616

1717
@Injectable()
1818
export class SpacesService {
1919
constructor(
2020
private readonly prisma: PrismaService,
2121
private readonly configService: ConfigService,
2222
private readonly profileSpaceService: ProfileSpaceService,
23-
private readonly usersService: UsersService,
2423
private readonly uploadService: UploadService,
24+
private readonly profilesService: ProfilesService,
2525
) {}
2626

2727
async findSpaceBySpaceUuid(spaceUuid: string): Promise<Space | null> {
@@ -33,7 +33,7 @@ export class SpacesService {
3333
profileUuid: string,
3434
spaceUuid: string,
3535
): Promise<Space> {
36-
await this.usersService.verifyUserProfile(userUuid, profileUuid);
36+
await this.profilesService.verifyUserProfile(userUuid, profileUuid);
3737
const space = await this.findSpaceBySpaceUuid(spaceUuid);
3838
if (!space) throw new NotFoundException();
3939
const isProfileInSpace = await this.profileSpaceService.isProfileInSpace(
@@ -50,7 +50,7 @@ export class SpacesService {
5050
icon: Express.Multer.File,
5151
createSpaceDto: CreateSpacePrismaDto,
5252
): Promise<Space> {
53-
await this.usersService.verifyUserProfile(userUuid, profileUuid);
53+
await this.profilesService.verifyUserProfile(userUuid, profileUuid);
5454
const iconUrl = icon
5555
? await this.uploadService.uploadFile(icon)
5656
: this.configService.get<string>('APP_ICON_URL');
@@ -72,7 +72,7 @@ export class SpacesService {
7272
icon: Express.Multer.File,
7373
updateSpaceDto: UpdateSpacePrismaDto,
7474
): Promise<Space> {
75-
await this.usersService.verifyUserProfile(userUuid, profileUuid);
75+
await this.profilesService.verifyUserProfile(userUuid, profileUuid);
7676
const isProfileInSpace = await this.profileSpaceService.isProfileInSpace(
7777
profileUuid,
7878
spaceUuid,
@@ -106,7 +106,7 @@ export class SpacesService {
106106
profileUuid: string,
107107
spaceUuid: string,
108108
): Promise<Space> {
109-
await this.usersService.verifyUserProfile(userUuid, profileUuid);
109+
await this.profilesService.verifyUserProfile(userUuid, profileUuid);
110110
try {
111111
await this.profileSpaceService.createProfileSpace(profileUuid, spaceUuid);
112112
} catch (err) {
@@ -131,7 +131,7 @@ export class SpacesService {
131131
profileUuid: string,
132132
spaceUuid: string,
133133
): Promise<void> {
134-
await this.usersService.verifyUserProfile(userUuid, profileUuid);
134+
await this.profilesService.verifyUserProfile(userUuid, profileUuid);
135135
try {
136136
await this.profileSpaceService.deleteProfileSpace(profileUuid, spaceUuid);
137137
} catch (err) {
@@ -157,7 +157,7 @@ export class SpacesService {
157157
profileUuid: string,
158158
spaceUuid: string,
159159
): Promise<Profile[]> {
160-
await this.usersService.verifyUserProfile(userUuid, profileUuid);
160+
await this.profilesService.verifyUserProfile(userUuid, profileUuid);
161161
const isProfileInSpace = await this.profileSpaceService.isProfileInSpace(
162162
profileUuid,
163163
spaceUuid,

โ€Žnestjs-BE/server/src/users/users.module.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ import { Module } from '@nestjs/common';
22
import { UsersService } from './users.service';
33
import { UsersController } from './users.controller';
44
import { PrismaModule } from '../prisma/prisma.module';
5-
import { ProfilesModule } from '../profiles/profiles.module';
65

76
@Module({
8-
imports: [PrismaModule, ProfilesModule],
7+
imports: [PrismaModule],
98
controllers: [UsersController],
109
providers: [UsersService],
1110
exports: [UsersService],

โ€Žnestjs-BE/server/src/users/users.service.spec.ts

-47
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,15 @@ import { UsersService } from './users.service';
33
import { PrismaService } from '../prisma/prisma.service';
44
import { v4 as uuid } from 'uuid';
55
import { KakaoUser, User } from '@prisma/client';
6-
import { ProfilesService } from '../profiles/profiles.service';
7-
import { ForbiddenException, NotFoundException } from '@nestjs/common';
86

97
describe('UsersService', () => {
108
let usersService: UsersService;
11-
let profilesService: ProfilesService;
129
let prisma: PrismaService;
1310

1411
beforeEach(async () => {
1512
const module: TestingModule = await Test.createTestingModule({
1613
providers: [
1714
UsersService,
18-
{
19-
provide: ProfilesService,
20-
useValue: { findProfileByProfileUuid: jest.fn() },
21-
},
2215
{
2316
provide: PrismaService,
2417
useValue: {
@@ -31,7 +24,6 @@ describe('UsersService', () => {
3124
}).compile();
3225

3326
usersService = module.get<UsersService>(UsersService);
34-
profilesService = module.get<ProfilesService>(ProfilesService);
3527
prisma = module.get<PrismaService>(PrismaService);
3628
});
3729

@@ -77,43 +69,4 @@ describe('UsersService', () => {
7769
expect(prisma.user.create).toHaveBeenCalled();
7870
expect(prisma.user.findUnique).not.toHaveBeenCalled();
7971
});
80-
81-
it('verifyUserProfile verified', async () => {
82-
const userMock = { uuid: 'user uuid' };
83-
const profileMock = { uuid: 'profile uuid', userUuid: userMock.uuid };
84-
85-
(profilesService.findProfileByProfileUuid as jest.Mock).mockResolvedValue(
86-
profileMock,
87-
);
88-
89-
const res = usersService.verifyUserProfile(userMock.uuid, profileMock.uuid);
90-
91-
await expect(res).resolves.toBeTruthy();
92-
});
93-
94-
it('verifyUserProfile profile not found', async () => {
95-
const userMock = { uuid: 'user uuid' };
96-
const profileMock = { uuid: 'profile uuid', userUuid: userMock.uuid };
97-
98-
(profilesService.findProfileByProfileUuid as jest.Mock).mockResolvedValue(
99-
null,
100-
);
101-
102-
const res = usersService.verifyUserProfile(userMock.uuid, profileMock.uuid);
103-
104-
await expect(res).rejects.toThrow(NotFoundException);
105-
});
106-
107-
it('verifyUserProfile profile user not own', async () => {
108-
const userMock = { uuid: 'user uuid' };
109-
const profileMock = { uuid: 'profile uuid', userUuid: 'other user uuid' };
110-
111-
(profilesService.findProfileByProfileUuid as jest.Mock).mockResolvedValue(
112-
profileMock,
113-
);
114-
115-
const res = usersService.verifyUserProfile(userMock.uuid, profileMock.uuid);
116-
117-
await expect(res).rejects.toThrow(ForbiddenException);
118-
});
11972
});

โ€Žnestjs-BE/server/src/users/users.service.ts

+2-21
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
import {
2-
ForbiddenException,
3-
Injectable,
4-
NotFoundException,
5-
} from '@nestjs/common';
1+
import { Injectable } from '@nestjs/common';
62
import { PrismaService } from '../prisma/prisma.service';
73
import { CreateUserPrismaDto } from './dto/create-user.dto';
84
import { Space, User } from '@prisma/client';
95
import { v4 as uuid } from 'uuid';
10-
import { ProfilesService } from '../profiles/profiles.service';
116

127
@Injectable()
138
export class UsersService {
14-
constructor(
15-
private readonly prisma: PrismaService,
16-
private readonly profilesService: ProfilesService,
17-
) {}
9+
constructor(private readonly prisma: PrismaService) {}
1810

1911
async getOrCreateUser(data: CreateUserPrismaDto): Promise<User> {
2012
return this.prisma.$transaction(async () => {
@@ -51,15 +43,4 @@ export class UsersService {
5143

5244
return spaces;
5345
}
54-
55-
async verifyUserProfile(
56-
userUuid: string,
57-
profileUuid: string,
58-
): Promise<boolean> {
59-
const profile =
60-
await this.profilesService.findProfileByProfileUuid(profileUuid);
61-
if (!profile) throw new NotFoundException();
62-
if (userUuid !== profile.userUuid) throw new ForbiddenException();
63-
return true;
64-
}
6546
}

0 commit comments

Comments
ย (0)