@@ -6,6 +6,9 @@ import { CreateWorkspaceDto } from './dtos/createWorkspace.dto';
66import { WorkspaceResponseMessage } from './workspace.controller' ;
77import { NotWorkspaceOwnerException } from '../exception/workspace-auth.exception' ;
88import { UserWorkspaceDto } from './dtos/userWorkspace.dto' ;
9+ import { TokenService } from '../auth/token/token.service' ;
10+ import { WorkspaceNotFoundException } from '../exception/workspace.exception' ;
11+ import { ForbiddenAccessException } from '../exception/access.exception' ;
912
1013describe ( 'WorkspaceController' , ( ) => {
1114 let controller : WorkspaceController ;
@@ -21,8 +24,15 @@ describe('WorkspaceController', () => {
2124 createWorkspace : jest . fn ( ) ,
2225 deleteWorkspace : jest . fn ( ) ,
2326 getUserWorkspaces : jest . fn ( ) ,
27+ generateInviteUrl : jest . fn ( ) ,
28+ processInviteUrl : jest . fn ( ) ,
29+ checkAccess : jest . fn ( ) ,
2430 } ,
2531 } ,
32+ {
33+ provide : TokenService ,
34+ useValue : { } ,
35+ } ,
2636 ] ,
2737 } )
2838 . overrideGuard ( JwtAuthGuard )
@@ -116,14 +126,126 @@ describe('WorkspaceController', () => {
116126 } ,
117127 ] as UserWorkspaceDto [ ] ;
118128
129+ const expectedResult = {
130+ message : WorkspaceResponseMessage . WORKSPACES_RETURNED ,
131+ workspaces : mockWorkspaces ,
132+ } ;
133+
119134 jest
120135 . spyOn ( service , 'getUserWorkspaces' )
121136 . mockResolvedValue ( mockWorkspaces ) ;
122137
123138 const result = await controller . getUserWorkspaces ( req ) ;
124139
125140 expect ( service . getUserWorkspaces ) . toHaveBeenCalledWith ( req . user . sub ) ;
126- expect ( result ) . toEqual ( mockWorkspaces ) ;
141+ expect ( result ) . toEqual ( expectedResult ) ;
142+ } ) ;
143+ } ) ;
144+
145+ it ( '컨트롤러가 정상적으로 인스턴스화된다.' , ( ) => {
146+ expect ( controller ) . toBeDefined ( ) ;
147+ } ) ;
148+
149+ describe ( 'generateInviteLink' , ( ) => {
150+ it ( '초대 링크를 생성하고 반환한다.' , async ( ) => {
151+ const req = { user : { sub : 1 } } ;
152+ const workspaceId = 'workspace-snowflake-id' ;
153+ const mockInviteUrl =
154+ 'https://example.com/api/workspace/join?token=abc123' ;
155+
156+ jest . spyOn ( service , 'generateInviteUrl' ) . mockResolvedValue ( mockInviteUrl ) ;
157+
158+ const result = await controller . generateInviteLink ( req , workspaceId ) ;
159+
160+ expect ( service . generateInviteUrl ) . toHaveBeenCalledWith (
161+ req . user . sub ,
162+ workspaceId ,
163+ ) ;
164+ expect ( result ) . toEqual ( {
165+ message : WorkspaceResponseMessage . WORKSPACE_INVITED ,
166+ inviteUrl : mockInviteUrl ,
167+ } ) ;
168+ } ) ;
169+ } ) ;
170+
171+ describe ( 'joinWorkspace' , ( ) => {
172+ it ( '초대 토큰을 처리하고 성공 메시지를 반환한다.' , async ( ) => {
173+ const req = { user : { sub : 1 } } ;
174+ const token = 'valid-token' ;
175+
176+ jest . spyOn ( service , 'processInviteUrl' ) . mockResolvedValue ( ) ;
177+
178+ const result = await controller . joinWorkspace ( req , token ) ;
179+
180+ expect ( service . processInviteUrl ) . toHaveBeenCalledWith (
181+ req . user . sub ,
182+ token ,
183+ ) ;
184+ expect ( result ) . toEqual ( {
185+ message : WorkspaceResponseMessage . WORKSPACE_INVITED ,
186+ } ) ;
187+ } ) ;
188+ } ) ;
189+
190+ describe ( 'checkWorkspaceAccess' , ( ) => {
191+ it ( '워크스페이스에 접근 가능한 경우 메시지를 반환한다.' , async ( ) => {
192+ const workspaceId = 'workspace-snowflake-id' ;
193+ const userId = 'user-snowflake-id' ;
194+
195+ jest . spyOn ( service , 'checkAccess' ) . mockResolvedValue ( undefined ) ;
196+
197+ const result = await controller . checkWorkspaceAccess ( workspaceId , userId ) ;
198+
199+ expect ( service . checkAccess ) . toHaveBeenCalledWith ( userId , workspaceId ) ;
200+ expect ( result ) . toEqual ( {
201+ message : WorkspaceResponseMessage . WORKSPACE_ACCESS_CHECKED ,
202+ } ) ;
203+ } ) ;
204+
205+ it ( '로그인하지 않은 사용자의 경우 null로 처리하고 접근 가능한 경우 메시지를 반환한다.' , async ( ) => {
206+ const workspaceId = 'workspace-snowflake-id' ;
207+ const userId = 'null' ; // 로그인되지 않은 상태를 나타냄
208+
209+ jest . spyOn ( service , 'checkAccess' ) . mockResolvedValue ( undefined ) ;
210+
211+ const result = await controller . checkWorkspaceAccess ( workspaceId , userId ) ;
212+
213+ expect ( service . checkAccess ) . toHaveBeenCalledWith ( null , workspaceId ) ;
214+ expect ( result ) . toEqual ( {
215+ message : WorkspaceResponseMessage . WORKSPACE_ACCESS_CHECKED ,
216+ } ) ;
217+ } ) ;
218+
219+ it ( '권한이 없는 경우 ForbiddenAccessException을 던진다.' , async ( ) => {
220+ const workspaceId = 'workspace-snowflake-id' ;
221+ const userId = 'user-snowflake-id' ;
222+
223+ // 권한 없음
224+ jest
225+ . spyOn ( service , 'checkAccess' )
226+ . mockRejectedValue ( new ForbiddenAccessException ( ) ) ;
227+
228+ await expect (
229+ controller . checkWorkspaceAccess ( workspaceId , userId ) ,
230+ ) . rejects . toThrow ( ForbiddenAccessException ) ;
231+
232+ expect ( service . checkAccess ) . toHaveBeenCalledWith ( userId , workspaceId ) ;
233+ } ) ;
234+
235+ it ( '워크스페이스가 존재하지 않는 경우 WorkspaceNotFoundException을 던진다.' , async ( ) => {
236+ const workspaceId = 'invalid-snowflake-id' ;
237+ const userId = 'user-snowflake-id' ;
238+
239+ // 워크스페이스 없음
240+ jest
241+ . spyOn ( service , 'checkAccess' )
242+ . mockRejectedValue ( new WorkspaceNotFoundException ( ) ) ;
243+
244+ await expect (
245+ controller . checkWorkspaceAccess ( workspaceId , userId ) ,
246+ ) . rejects . toThrow ( WorkspaceNotFoundException ) ;
247+
248+ expect ( service . checkAccess ) . toHaveBeenCalledWith ( userId , workspaceId ) ;
127249 } ) ;
128250 } ) ;
129251} ) ;
0 commit comments