diff --git a/Microsoft.Azure.Cosmos/src/Resource/Container/ContainerCore.cs b/Microsoft.Azure.Cosmos/src/Resource/Container/ContainerCore.cs index a657beb685..53c77e949e 100644 --- a/Microsoft.Azure.Cosmos/src/Resource/Container/ContainerCore.cs +++ b/Microsoft.Azure.Cosmos/src/Resource/Container/ContainerCore.cs @@ -307,9 +307,9 @@ public override async Task> GetPartitionKeyRangesAsync( /// A containing the for this container. public override async Task GetCachedContainerPropertiesAsync(CancellationToken cancellationToken = default) { - ClientCollectionCache collectionCache = await this.ClientContext.DocumentClient.GetCollectionCacheAsync(); try { + ClientCollectionCache collectionCache = await this.ClientContext.DocumentClient.GetCollectionCacheAsync(); return await collectionCache.ResolveByNameAsync(HttpConstants.Versions.CurrentVersion, this.LinkUri, cancellationToken); } catch (DocumentClientException ex) diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosPermissionTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosPermissionTests.cs index e8e2dfd842..efd00f6907 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosPermissionTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosPermissionTests.cs @@ -364,12 +364,47 @@ public async Task EnsureUnauthorized_ThrowsCosmosClientException() // Take the key and change some middle character authKey = authKey.Replace("m", "M"); - CosmosClient cosmosClient = new CosmosClient( + using CosmosClient cosmosClient = new CosmosClient( endpoint, authKey); CosmosException exception = await Assert.ThrowsExceptionAsync(() => cosmosClient.GetContainer("test", "test").ReadItemAsync("test", new PartitionKey("test"))); Assert.AreEqual(HttpStatusCode.Unauthorized, exception.StatusCode); } + + [TestMethod] + public async Task EnsureUnauthorized_Writes_ThrowsCosmosClientException() + { + string authKey = ConfigurationManager.AppSettings["MasterKey"]; + string endpoint = ConfigurationManager.AppSettings["GatewayEndpoint"]; + + // Take the key and change some middle character + authKey = authKey.Replace("m", "M"); + + using CosmosClient cosmosClient = new CosmosClient( + endpoint, + authKey); + CosmosException exception = await Assert.ThrowsExceptionAsync(() => cosmosClient.GetContainer("test", "test").CreateItemAsync(new { id = "test" })); + Assert.AreEqual(HttpStatusCode.Unauthorized, exception.StatusCode); + } + + [TestMethod] + public async Task EnsureUnauthorized_Query_ThrowsCosmosClientException() + { + string authKey = ConfigurationManager.AppSettings["MasterKey"]; + string endpoint = ConfigurationManager.AppSettings["GatewayEndpoint"]; + + // Take the key and change some middle character + authKey = authKey.Replace("m", "M"); + + using CosmosClient cosmosClient = new CosmosClient( + endpoint, + authKey); + + using FeedIterator iterator = cosmosClient.GetContainer("test", "test").GetItemQueryIterator("SELECT * FROM c"); + + CosmosException exception = await Assert.ThrowsExceptionAsync(() => iterator.ReadNextAsync()); + Assert.AreEqual(HttpStatusCode.Unauthorized, exception.StatusCode); + } } }