@@ -107,8 +107,15 @@ public async Task CRUDTest()
107
107
}
108
108
109
109
[ TestMethod ]
110
- public async Task ContainerResourcePermissionTest ( )
110
+ [ DataRow ( ConnectionMode . Gateway ) ]
111
+ [ DataRow ( ConnectionMode . Direct ) ]
112
+ public async Task ContainerResourcePermissionTest ( ConnectionMode mode )
111
113
{
114
+ CosmosClientOptions cosmosClientOptions = new CosmosClientOptions ( )
115
+ {
116
+ ConnectionMode = mode ,
117
+ } ;
118
+
112
119
//create user
113
120
string userId = Guid . NewGuid ( ) . ToString ( ) ;
114
121
UserResponse userResponse = await this . cosmosDatabase . CreateUserAsync ( userId ) ;
@@ -121,7 +128,7 @@ public async Task ContainerResourcePermissionTest()
121
128
ContainerResponse containerResponse = await this . cosmosDatabase . CreateContainerAsync ( containerId , "/id" ) ;
122
129
Assert . AreEqual ( HttpStatusCode . Created , containerResponse . StatusCode ) ;
123
130
Container container = containerResponse . Container ;
124
-
131
+
125
132
//create permission
126
133
string permissionId = Guid . NewGuid ( ) . ToString ( ) ;
127
134
PermissionProperties permissionProperties = new PermissionProperties ( permissionId , PermissionMode . Read , container ) ;
@@ -131,9 +138,18 @@ public async Task ContainerResourcePermissionTest()
131
138
Assert . AreEqual ( permissionId , permission . Id ) ;
132
139
Assert . AreEqual ( permissionProperties . PermissionMode , permission . PermissionMode ) ;
133
140
134
- //delete resource with PermissionMode.Read
135
- using ( CosmosClient tokenCosmosClient = TestCommon . CreateCosmosClient ( clientOptions : null , resourceToken : permission . Token ) )
141
+ using ( CosmosClient tokenCosmosClient = TestCommon . CreateCosmosClient ( cosmosClientOptions , resourceToken : permission . Token ) )
136
142
{
143
+ Container readContainerRef = tokenCosmosClient . GetContainer ( this . cosmosDatabase . Id , containerId ) ;
144
+
145
+ //read resource with PermissionMode.Read
146
+ using FeedIterator < dynamic > feedIterator = readContainerRef . GetItemQueryIterator < dynamic > ( "SELECT * FROM c" ) ;
147
+ while ( feedIterator . HasMoreResults )
148
+ {
149
+ _ = await feedIterator . ReadNextAsync ( ) ;
150
+ }
151
+
152
+ //delete resource with PermissionMode.Read
137
153
try
138
154
{
139
155
ContainerResponse response = await tokenCosmosClient
@@ -147,14 +163,14 @@ public async Task ContainerResourcePermissionTest()
147
163
Assert . AreEqual ( HttpStatusCode . Forbidden , ex . StatusCode ) ;
148
164
}
149
165
}
150
-
166
+
151
167
//update permission to PermissionMode.All
152
168
permissionProperties = new PermissionProperties ( permissionId , PermissionMode . All , container ) ;
153
169
permissionResponse = await user . GetPermission ( permissionId ) . ReplaceAsync ( permissionProperties ) ;
154
170
permission = permissionResponse . Resource ;
155
171
156
172
//delete resource with PermissionMode.All
157
- using ( CosmosClient tokenCosmosClient = TestCommon . CreateCosmosClient ( clientOptions : null , resourceToken : permission . Token ) )
173
+ using ( CosmosClient tokenCosmosClient = TestCommon . CreateCosmosClient ( cosmosClientOptions , resourceToken : permission . Token ) )
158
174
{
159
175
ContainerResponse response = await tokenCosmosClient
160
176
. GetDatabase ( this . cosmosDatabase . Id )
@@ -284,8 +300,15 @@ await container.CreateItemAsync<ToDoActivity>(
284
300
}
285
301
286
302
[ TestMethod ]
287
- public async Task ItemResourcePermissionTest ( )
303
+ [ DataRow ( ConnectionMode . Gateway ) ]
304
+ [ DataRow ( ConnectionMode . Direct ) ]
305
+ public async Task ItemResourcePermissionTest ( ConnectionMode connectionMode )
288
306
{
307
+ CosmosClientOptions cosmosClientOptions = new CosmosClientOptions ( )
308
+ {
309
+ ConnectionMode = connectionMode
310
+ } ;
311
+
289
312
//create user
290
313
string userId = Guid . NewGuid ( ) . ToString ( ) ;
291
314
UserResponse userResponse = await this . cosmosDatabase . CreateUserAsync ( userId ) ;
@@ -313,13 +336,15 @@ public async Task ItemResourcePermissionTest()
313
336
Assert . AreEqual ( permissionId , permission . Id ) ;
314
337
Assert . AreEqual ( permissionProperties . PermissionMode , permission . PermissionMode ) ;
315
338
316
- //delete resource with PermissionMode.Read
317
- using ( CosmosClient tokenCosmosClient = TestCommon . CreateCosmosClient ( clientOptions : null , resourceToken : permission . Token ) )
339
+ using ( CosmosClient tokenCosmosClient = TestCommon . CreateCosmosClient ( clientOptions : cosmosClientOptions , resourceToken : permission . Token ) )
318
340
{
319
341
Container tokenContainer = tokenCosmosClient . GetContainer ( this . cosmosDatabase . Id , containerId ) ;
342
+
343
+ //read resource with PermissionMode.Read
320
344
ItemResponse < dynamic > readPermissionItem = await tokenContainer . ReadItemAsync < dynamic > ( itemId , partitionKey ) ;
321
345
Assert . AreEqual ( itemId , readPermissionItem . Resource . id . ToString ( ) ) ;
322
346
347
+ //delete resource with PermissionMode.Read
323
348
try
324
349
{
325
350
ItemResponse < dynamic > response = await tokenContainer . DeleteItemAsync < dynamic > (
@@ -340,7 +365,7 @@ public async Task ItemResourcePermissionTest()
340
365
permission = permissionResponse . Resource ;
341
366
342
367
//delete resource with PermissionMode.All
343
- using ( CosmosClient tokenCosmosClient = TestCommon . CreateCosmosClient ( clientOptions : null , resourceToken : permission . Token ) )
368
+ using ( CosmosClient tokenCosmosClient = TestCommon . CreateCosmosClient ( clientOptions : cosmosClientOptions , resourceToken : permission . Token ) )
344
369
{
345
370
using ( FeedIterator < dynamic > feed = tokenCosmosClient
346
371
. GetDatabase ( this . cosmosDatabase . Id )
@@ -357,8 +382,15 @@ public async Task ItemResourcePermissionTest()
357
382
}
358
383
359
384
[ TestMethod ]
360
- public async Task EnsureUnauthorized_ThrowsCosmosClientException ( )
385
+ [ DataRow ( ConnectionMode . Gateway ) ]
386
+ [ DataRow ( ConnectionMode . Direct ) ]
387
+ public async Task EnsureUnauthorized_ThrowsCosmosClientException ( ConnectionMode connectionMode )
361
388
{
389
+ CosmosClientOptions cosmosClientOptions = new CosmosClientOptions ( )
390
+ {
391
+ ConnectionMode = connectionMode
392
+ } ;
393
+
362
394
string authKey = ConfigurationManager . AppSettings [ "MasterKey" ] ;
363
395
string endpoint = ConfigurationManager . AppSettings [ "GatewayEndpoint" ] ;
364
396
@@ -367,55 +399,83 @@ public async Task EnsureUnauthorized_ThrowsCosmosClientException()
367
399
368
400
using CosmosClient cosmosClient = new CosmosClient (
369
401
endpoint ,
370
- authKey ) ;
402
+ authKey ,
403
+ cosmosClientOptions ) ;
371
404
372
405
CosmosException exception = await Assert . ThrowsExceptionAsync < CosmosException > ( ( ) => cosmosClient . GetContainer ( "test" , "test" ) . ReadItemAsync < dynamic > ( "test" , new PartitionKey ( "test" ) ) ) ;
373
406
Assert . AreEqual ( HttpStatusCode . Unauthorized , exception . StatusCode ) ;
374
407
}
375
408
376
409
[ TestMethod ]
377
- public async Task EnsureUnauthorized_ThrowsCosmosClientException_ReadAccountAsync ( )
410
+ [ DataRow ( ConnectionMode . Gateway ) ]
411
+ [ DataRow ( ConnectionMode . Direct ) ]
412
+ public async Task EnsureUnauthorized_ThrowsCosmosClientException_ReadAccountAsync ( ConnectionMode connectionMode )
378
413
{
414
+ CosmosClientOptions cosmosClientOptions = new CosmosClientOptions ( )
415
+ {
416
+ ConnectionMode = connectionMode
417
+ } ;
418
+
379
419
string authKey = ConfigurationManager . AppSettings [ "MasterKey" ] ;
380
420
string endpoint = ConfigurationManager . AppSettings [ "GatewayEndpoint" ] ;
381
421
382
422
// Take the key and change some middle character
383
423
authKey = authKey . Replace ( "m" , "M" ) ;
384
- CosmosClient cosmosClient = new CosmosClient ( endpoint , authKey ) ;
424
+ using CosmosClient cosmosClient = new CosmosClient (
425
+ endpoint ,
426
+ authKey ,
427
+ cosmosClientOptions ) ;
385
428
386
429
CosmosException exception1 = await Assert . ThrowsExceptionAsync < CosmosException > ( ( ) => cosmosClient . ReadAccountAsync ( ) ) ;
387
430
Assert . AreEqual ( HttpStatusCode . Unauthorized , exception1 . StatusCode ) ;
388
431
389
432
}
390
433
391
434
[ TestMethod ]
392
- public async Task EnsureUnauthorized_Writes_ThrowsCosmosClientException ( )
435
+ [ DataRow ( ConnectionMode . Gateway ) ]
436
+ [ DataRow ( ConnectionMode . Direct ) ]
437
+ public async Task EnsureUnauthorized_Writes_ThrowsCosmosClientException ( ConnectionMode connectionMode )
393
438
{
439
+ CosmosClientOptions cosmosClientOptions = new CosmosClientOptions ( )
440
+ {
441
+ ConnectionMode = connectionMode
442
+ } ;
443
+
394
444
string authKey = ConfigurationManager . AppSettings [ "MasterKey" ] ;
395
445
string endpoint = ConfigurationManager . AppSettings [ "GatewayEndpoint" ] ;
396
-
446
+
397
447
// Take the key and change some middle character
398
448
authKey = authKey . Replace ( "m" , "M" ) ;
399
449
400
450
using CosmosClient cosmosClient = new CosmosClient (
401
451
endpoint ,
402
- authKey ) ;
452
+ authKey ,
453
+ cosmosClientOptions ) ;
454
+
403
455
CosmosException exception = await Assert . ThrowsExceptionAsync < CosmosException > ( ( ) => cosmosClient . GetContainer ( "test" , "test" ) . CreateItemAsync < dynamic > ( new { id = "test" } ) ) ;
404
456
Assert . AreEqual ( HttpStatusCode . Unauthorized , exception . StatusCode ) ;
405
457
}
406
458
407
459
[ TestMethod ]
408
- public async Task EnsureUnauthorized_Query_ThrowsCosmosClientException ( )
460
+ [ DataRow ( ConnectionMode . Gateway ) ]
461
+ [ DataRow ( ConnectionMode . Direct ) ]
462
+ public async Task EnsureUnauthorized_Query_ThrowsCosmosClientException ( ConnectionMode connectionMode )
409
463
{
464
+ CosmosClientOptions cosmosClientOptions = new CosmosClientOptions ( )
465
+ {
466
+ ConnectionMode = connectionMode
467
+ } ;
468
+
410
469
string authKey = ConfigurationManager . AppSettings [ "MasterKey" ] ;
411
470
string endpoint = ConfigurationManager . AppSettings [ "GatewayEndpoint" ] ;
412
-
471
+
413
472
// Take the key and change some middle character
414
473
authKey = authKey . Replace ( "m" , "M" ) ;
415
474
416
475
using CosmosClient cosmosClient = new CosmosClient (
417
476
endpoint ,
418
- authKey ) ;
477
+ authKey ,
478
+ cosmosClientOptions ) ;
419
479
420
480
using FeedIterator < dynamic > iterator = cosmosClient . GetContainer ( "test" , "test" ) . GetItemQueryIterator < dynamic > ( "SELECT * FROM c" ) ;
421
481
0 commit comments