Skip to content

Commit 28318b0

Browse files
[Internal] PermissionTests: Adds CosmosPermissionTests Coverage (#3593)
* Ensures that both Direct and Gateway connection modes are tested * Validates that container read works with PermissionMode.Read (test was previously only validating that Delete was blocked - i.e. the negative case).
1 parent 5318646 commit 28318b0

File tree

1 file changed

+80
-20
lines changed

1 file changed

+80
-20
lines changed

Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosPermissionTests.cs

+80-20
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,15 @@ public async Task CRUDTest()
107107
}
108108

109109
[TestMethod]
110-
public async Task ContainerResourcePermissionTest()
110+
[DataRow(ConnectionMode.Gateway)]
111+
[DataRow(ConnectionMode.Direct)]
112+
public async Task ContainerResourcePermissionTest(ConnectionMode mode)
111113
{
114+
CosmosClientOptions cosmosClientOptions = new CosmosClientOptions()
115+
{
116+
ConnectionMode = mode,
117+
};
118+
112119
//create user
113120
string userId = Guid.NewGuid().ToString();
114121
UserResponse userResponse = await this.cosmosDatabase.CreateUserAsync(userId);
@@ -121,7 +128,7 @@ public async Task ContainerResourcePermissionTest()
121128
ContainerResponse containerResponse = await this.cosmosDatabase.CreateContainerAsync(containerId, "/id");
122129
Assert.AreEqual(HttpStatusCode.Created, containerResponse.StatusCode);
123130
Container container = containerResponse.Container;
124-
131+
125132
//create permission
126133
string permissionId = Guid.NewGuid().ToString();
127134
PermissionProperties permissionProperties = new PermissionProperties(permissionId, PermissionMode.Read, container);
@@ -131,9 +138,18 @@ public async Task ContainerResourcePermissionTest()
131138
Assert.AreEqual(permissionId, permission.Id);
132139
Assert.AreEqual(permissionProperties.PermissionMode, permission.PermissionMode);
133140

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))
136142
{
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
137153
try
138154
{
139155
ContainerResponse response = await tokenCosmosClient
@@ -147,14 +163,14 @@ public async Task ContainerResourcePermissionTest()
147163
Assert.AreEqual(HttpStatusCode.Forbidden, ex.StatusCode);
148164
}
149165
}
150-
166+
151167
//update permission to PermissionMode.All
152168
permissionProperties = new PermissionProperties(permissionId, PermissionMode.All, container);
153169
permissionResponse = await user.GetPermission(permissionId).ReplaceAsync(permissionProperties);
154170
permission = permissionResponse.Resource;
155171

156172
//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))
158174
{
159175
ContainerResponse response = await tokenCosmosClient
160176
.GetDatabase(this.cosmosDatabase.Id)
@@ -284,8 +300,15 @@ await container.CreateItemAsync<ToDoActivity>(
284300
}
285301

286302
[TestMethod]
287-
public async Task ItemResourcePermissionTest()
303+
[DataRow(ConnectionMode.Gateway)]
304+
[DataRow(ConnectionMode.Direct)]
305+
public async Task ItemResourcePermissionTest(ConnectionMode connectionMode)
288306
{
307+
CosmosClientOptions cosmosClientOptions = new CosmosClientOptions()
308+
{
309+
ConnectionMode = connectionMode
310+
};
311+
289312
//create user
290313
string userId = Guid.NewGuid().ToString();
291314
UserResponse userResponse = await this.cosmosDatabase.CreateUserAsync(userId);
@@ -313,13 +336,15 @@ public async Task ItemResourcePermissionTest()
313336
Assert.AreEqual(permissionId, permission.Id);
314337
Assert.AreEqual(permissionProperties.PermissionMode, permission.PermissionMode);
315338

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))
318340
{
319341
Container tokenContainer = tokenCosmosClient.GetContainer(this.cosmosDatabase.Id, containerId);
342+
343+
//read resource with PermissionMode.Read
320344
ItemResponse<dynamic> readPermissionItem = await tokenContainer.ReadItemAsync<dynamic>(itemId, partitionKey);
321345
Assert.AreEqual(itemId, readPermissionItem.Resource.id.ToString());
322346

347+
//delete resource with PermissionMode.Read
323348
try
324349
{
325350
ItemResponse<dynamic> response = await tokenContainer.DeleteItemAsync<dynamic>(
@@ -340,7 +365,7 @@ public async Task ItemResourcePermissionTest()
340365
permission = permissionResponse.Resource;
341366

342367
//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))
344369
{
345370
using (FeedIterator<dynamic> feed = tokenCosmosClient
346371
.GetDatabase(this.cosmosDatabase.Id)
@@ -357,8 +382,15 @@ public async Task ItemResourcePermissionTest()
357382
}
358383

359384
[TestMethod]
360-
public async Task EnsureUnauthorized_ThrowsCosmosClientException()
385+
[DataRow(ConnectionMode.Gateway)]
386+
[DataRow(ConnectionMode.Direct)]
387+
public async Task EnsureUnauthorized_ThrowsCosmosClientException(ConnectionMode connectionMode)
361388
{
389+
CosmosClientOptions cosmosClientOptions = new CosmosClientOptions()
390+
{
391+
ConnectionMode = connectionMode
392+
};
393+
362394
string authKey = ConfigurationManager.AppSettings["MasterKey"];
363395
string endpoint = ConfigurationManager.AppSettings["GatewayEndpoint"];
364396

@@ -367,55 +399,83 @@ public async Task EnsureUnauthorized_ThrowsCosmosClientException()
367399

368400
using CosmosClient cosmosClient = new CosmosClient(
369401
endpoint,
370-
authKey);
402+
authKey,
403+
cosmosClientOptions);
371404

372405
CosmosException exception = await Assert.ThrowsExceptionAsync<CosmosException>(() => cosmosClient.GetContainer("test", "test").ReadItemAsync<dynamic>("test", new PartitionKey("test")));
373406
Assert.AreEqual(HttpStatusCode.Unauthorized, exception.StatusCode);
374407
}
375408

376409
[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)
378413
{
414+
CosmosClientOptions cosmosClientOptions = new CosmosClientOptions()
415+
{
416+
ConnectionMode = connectionMode
417+
};
418+
379419
string authKey = ConfigurationManager.AppSettings["MasterKey"];
380420
string endpoint = ConfigurationManager.AppSettings["GatewayEndpoint"];
381421

382422
// Take the key and change some middle character
383423
authKey = authKey.Replace("m", "M");
384-
CosmosClient cosmosClient = new CosmosClient(endpoint, authKey);
424+
using CosmosClient cosmosClient = new CosmosClient(
425+
endpoint,
426+
authKey,
427+
cosmosClientOptions);
385428

386429
CosmosException exception1 = await Assert.ThrowsExceptionAsync<CosmosException>(() => cosmosClient.ReadAccountAsync());
387430
Assert.AreEqual(HttpStatusCode.Unauthorized, exception1.StatusCode);
388431

389432
}
390433

391434
[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)
393438
{
439+
CosmosClientOptions cosmosClientOptions = new CosmosClientOptions()
440+
{
441+
ConnectionMode = connectionMode
442+
};
443+
394444
string authKey = ConfigurationManager.AppSettings["MasterKey"];
395445
string endpoint = ConfigurationManager.AppSettings["GatewayEndpoint"];
396-
446+
397447
// Take the key and change some middle character
398448
authKey = authKey.Replace("m", "M");
399449

400450
using CosmosClient cosmosClient = new CosmosClient(
401451
endpoint,
402-
authKey);
452+
authKey,
453+
cosmosClientOptions);
454+
403455
CosmosException exception = await Assert.ThrowsExceptionAsync<CosmosException>(() => cosmosClient.GetContainer("test", "test").CreateItemAsync<dynamic>(new { id = "test" }));
404456
Assert.AreEqual(HttpStatusCode.Unauthorized, exception.StatusCode);
405457
}
406458

407459
[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)
409463
{
464+
CosmosClientOptions cosmosClientOptions = new CosmosClientOptions()
465+
{
466+
ConnectionMode = connectionMode
467+
};
468+
410469
string authKey = ConfigurationManager.AppSettings["MasterKey"];
411470
string endpoint = ConfigurationManager.AppSettings["GatewayEndpoint"];
412-
471+
413472
// Take the key and change some middle character
414473
authKey = authKey.Replace("m", "M");
415474

416475
using CosmosClient cosmosClient = new CosmosClient(
417476
endpoint,
418-
authKey);
477+
authKey,
478+
cosmosClientOptions);
419479

420480
using FeedIterator<dynamic> iterator = cosmosClient.GetContainer("test", "test").GetItemQueryIterator<dynamic>("SELECT * FROM c");
421481

0 commit comments

Comments
 (0)