@@ -92,12 +92,8 @@ describe('Keto client wrapper E2E', () => {
92
92
controllers : [ ExampleController ] ,
93
93
} ) . compile ( ) ;
94
94
95
- oryPermissionService = module . get < OryPermissionsService > (
96
- OryPermissionsService
97
- ) ;
98
- oryRelationshipsService = module . get < OryRelationshipsService > (
99
- OryRelationshipsService
100
- ) ;
95
+ oryPermissionService = module . get ( OryPermissionsService ) ;
96
+ oryRelationshipsService = module . get ( OryRelationshipsService ) ;
101
97
102
98
app = module . createNestApplication ( ) ;
103
99
await app . init ( ) ;
@@ -107,46 +103,139 @@ describe('Keto client wrapper E2E', () => {
107
103
return app ?. close ( ) ;
108
104
} ) ;
109
105
110
- it ( 'should pass authorization when relation exists in Ory Keto' , async ( ) => {
111
- const object = 'car' ;
112
- const subjectObject = 'Bob' ;
113
- await createOwnerRelation ( object , subjectObject ) ;
106
+ describe ( 'GET /Example/:id' , ( ) => {
107
+ it ( 'should pass authorization when relation exists in Ory Keto' , async ( ) => {
108
+ const object = 'car' ;
109
+ const subjectObject = 'Bob' ;
110
+ await createOwnerRelation ( object , subjectObject ) ;
111
+
112
+ const { body } = await request ( app . getHttpServer ( ) )
113
+ . get ( `/Example/${ object } ` )
114
+ . set ( {
115
+ 'x-current-user-id' : subjectObject ,
116
+ } ) ;
117
+ expect ( body ) . toEqual ( { message : 'OK' } ) ;
118
+ } ) ;
114
119
115
- const { body } = await request ( app . getHttpServer ( ) )
116
- . get ( `/Example/${ object } ` )
117
- . set ( {
118
- 'x-current-user-id' : subjectObject ,
120
+ it ( 'should fail authorization when relation does not exist in Ory Keto' , async ( ) => {
121
+ const object = 'car' ;
122
+ const subjectObject = 'Alice' ;
123
+
124
+ const { body } = await request ( app . getHttpServer ( ) )
125
+ . get ( `/Example/${ object } ` )
126
+ . set ( {
127
+ 'x-current-user-id' : subjectObject ,
128
+ } ) ;
129
+ expect ( body ) . toEqual ( {
130
+ message : 'Forbidden' ,
131
+ statusCode : 403 ,
119
132
} ) ;
120
- expect ( body ) . toEqual ( { message : 'OK' } ) ;
133
+ } ) ;
121
134
} ) ;
122
135
123
- it ( 'should fail authorization when relation does not exist in Ory Keto' , async ( ) => {
124
- const object = 'car' ;
125
- const subjectObject = 'Alice' ;
136
+ describe ( 'GET /Example/complex/:id' , ( ) => {
137
+ it ( 'should pass authorization when relations exist in Ory Keto' , async ( ) => {
138
+ const object = 'tractor' ;
139
+ const subjectObject = 'Bob' ;
140
+ await createOwnerRelation ( object , subjectObject ) ;
141
+ await createAdminRelation ( subjectObject ) ;
142
+ await createPuppetmasterRelation ( object ) ;
143
+
144
+ const { body } = await request ( app . getHttpServer ( ) )
145
+ . get ( `/Example/complex/${ object } ` )
146
+ . set ( {
147
+ 'x-current-user-id' : subjectObject ,
148
+ } ) ;
149
+ expect ( body ) . toEqual ( { message : 'OK' } ) ;
150
+ } ) ;
151
+ } ) ;
126
152
127
- const { body } = await request ( app . getHttpServer ( ) )
128
- . get ( `/Example/${ object } ` )
129
- . set ( {
130
- 'x-current-user-id' : subjectObject ,
153
+ describe ( 'GET /Example/play/:id' , ( ) => {
154
+ it ( 'should fail authorization when relations does not exist in Ory Keto as owner or puppetmaster' , async ( ) => {
155
+ const object = 'truck' ;
156
+ const subjectObject = 'Isabella' ;
157
+
158
+ const { body } = await request ( app . getHttpServer ( ) )
159
+ . get ( `/Example/play/${ object } ` )
160
+ . set ( {
161
+ 'x-current-user-id' : subjectObject ,
162
+ } ) ;
163
+ expect ( body ) . toEqual ( {
164
+ message : 'Forbidden' ,
165
+ statusCode : 403 ,
131
166
} ) ;
132
- expect ( body ) . toEqual ( {
133
- message : 'Forbidden' ,
134
- statusCode : 403 ,
167
+ } ) ;
168
+
169
+ it ( 'should pass authorization when relations exist in Ory Keto as owner' , async ( ) => {
170
+ const object = 'truck' ;
171
+ const subjectObject = 'Honza' ;
172
+ await createOwnerRelation ( object , subjectObject ) ;
173
+
174
+ const { body } = await request ( app . getHttpServer ( ) )
175
+ . get ( `/Example/play/${ object } ` )
176
+ . set ( {
177
+ 'x-current-user-id' : subjectObject ,
178
+ } ) ;
179
+ expect ( body ) . toEqual ( { message : 'OK' } ) ;
180
+ } ) ;
181
+
182
+ it ( 'should pass authorization when relations exist in Ory Keto as puppetmaster' , async ( ) => {
183
+ const object = 'xylophone' ;
184
+ const subjectObject = 'Tomas' ;
185
+ await createAdminRelation ( subjectObject ) ;
186
+ await createPuppetmasterRelation ( object ) ;
187
+
188
+ const { body } = await request ( app . getHttpServer ( ) )
189
+ . get ( `/Example/play/${ object } ` )
190
+ . set ( {
191
+ 'x-current-user-id' : subjectObject ,
192
+ } ) ;
193
+ expect ( body ) . toEqual ( { message : 'OK' } ) ;
135
194
} ) ;
136
195
} ) ;
137
196
138
- it ( 'should pass authorization when relations exist in Ory Keto' , async ( ) => {
139
- const object = 'tractor' ;
140
- const subjectObject = 'Bob' ;
141
- await createOwnerRelation ( object , subjectObject ) ;
142
- await createAdminRelation ( subjectObject ) ;
143
- await createPuppetmasterRelation ( object ) ;
144
-
145
- const { body } = await request ( app . getHttpServer ( ) )
146
- . get ( `/Example/complex/${ object } ` )
147
- . set ( {
148
- 'x-current-user-id' : subjectObject ,
197
+ describe ( 'GET /Example/poly/:id' , ( ) => {
198
+ it . only ( 'should pass authorization when object has NO owner' , async ( ) => {
199
+ const object = 'ice-cream' ;
200
+ const subjectObject = 'Honza' ;
201
+
202
+ const { body } = await request ( app . getHttpServer ( ) )
203
+ . get ( `/Example/poly/${ object } ` )
204
+ . set ( {
205
+ 'x-current-user-id' : subjectObject ,
206
+ } ) ;
207
+ expect ( body ) . toEqual ( { message : 'OK' } ) ;
208
+ } ) ;
209
+
210
+ it ( 'should fail authorization when object has owner' , async ( ) => {
211
+ const object = 'ice-cream' ;
212
+ await createOwnerRelation ( object , 'Jean-Eude' ) ;
213
+
214
+ const { body } = await request ( app . getHttpServer ( ) )
215
+ . get ( `/Example/poly/${ object } ` )
216
+ . set ( {
217
+ 'x-current-user-id' : 'Marek' ,
218
+ } ) ;
219
+ expect ( body ) . toEqual ( {
220
+ message : 'Forbidden' ,
221
+ statusCode : 403 ,
149
222
} ) ;
150
- expect ( body ) . toEqual ( { message : 'OK' } ) ;
223
+ } ) ;
224
+
225
+ it ( 'should pass authorization when user access its own object' , async ( ) => {
226
+ const object = 'ice-cream' ;
227
+ const subjectObject = 'Wojtek' ;
228
+ await createOwnerRelation ( object , subjectObject ) ;
229
+
230
+ const { body } = await request ( app . getHttpServer ( ) )
231
+ . get ( `/Example/poly/${ object } ` )
232
+ . set ( {
233
+ 'x-current-user-id' : subjectObject ,
234
+ } ) ;
235
+ expect ( body ) . toEqual ( {
236
+ message : 'Forbidden' ,
237
+ statusCode : 403 ,
238
+ } ) ;
239
+ } ) ;
151
240
} ) ;
152
241
} ) ;
0 commit comments