public async Task PartitionedCRUDTest() { string containerName = Guid.NewGuid().ToString(); string partitionKeyPath = "/users"; ContainerResponse containerResponse = await this.cosmosDatabase.CreateContainerAsync(containerName, partitionKeyPath); Assert.AreEqual(HttpStatusCode.Created, containerResponse.StatusCode); Assert.AreEqual(containerName, containerResponse.Resource.Id); Assert.AreEqual(partitionKeyPath, containerResponse.Resource.PartitionKey.Paths.First()); Assert.IsNotNull(containerResponse.Diagnostics); string diagnostics = containerResponse.Diagnostics.ToString(); Assert.IsFalse(string.IsNullOrEmpty(diagnostics)); Assert.IsTrue(diagnostics.Contains("StatusCode")); SelflinkValidator.ValidateContainerSelfLink(containerResponse.Resource.SelfLink); ContainerProperties settings = new ContainerProperties(containerName, partitionKeyPath) { IndexingPolicy = new Cosmos.IndexingPolicy() { IndexingMode = Cosmos.IndexingMode.None, Automatic = false } }; Container container = containerResponse; containerResponse = await container.ReplaceContainerAsync(settings); Assert.AreEqual(HttpStatusCode.OK, containerResponse.StatusCode); Assert.AreEqual(containerName, containerResponse.Resource.Id); Assert.AreEqual(partitionKeyPath, containerResponse.Resource.PartitionKey.Paths.First()); Assert.AreEqual(Cosmos.IndexingMode.None, containerResponse.Resource.IndexingPolicy.IndexingMode); Assert.IsFalse(containerResponse.Resource.IndexingPolicy.Automatic); Assert.IsNotNull(containerResponse.Diagnostics); diagnostics = containerResponse.Diagnostics.ToString(); Assert.IsFalse(string.IsNullOrEmpty(diagnostics)); Assert.IsTrue(diagnostics.Contains("StatusCode")); SelflinkValidator.ValidateContainerSelfLink(containerResponse.Resource.SelfLink); containerResponse = await container.ReadContainerAsync(); Assert.AreEqual(HttpStatusCode.OK, containerResponse.StatusCode); Assert.AreEqual(containerName, containerResponse.Resource.Id); //Assert.AreEqual(Cosmos.PartitionKeyDefinitionVersion.V2, containerResponse.Resource.PartitionKeyDefinitionVersion); Assert.AreEqual(partitionKeyPath, containerResponse.Resource.PartitionKey.Paths.First()); Assert.AreEqual(Cosmos.IndexingMode.None, containerResponse.Resource.IndexingPolicy.IndexingMode); Assert.IsFalse(containerResponse.Resource.IndexingPolicy.Automatic); Assert.IsNotNull(containerResponse.Diagnostics); diagnostics = containerResponse.Diagnostics.ToString(); Assert.IsFalse(string.IsNullOrEmpty(diagnostics)); Assert.IsTrue(diagnostics.Contains("StatusCode")); SelflinkValidator.ValidateContainerSelfLink(containerResponse.Resource.SelfLink); containerResponse = await containerResponse.Container.DeleteContainerAsync(); Assert.AreEqual(HttpStatusCode.NoContent, containerResponse.StatusCode); }
private static void ValidateStoredProcedureSettings(string id, string body, StoredProcedureResponse cosmosResponse) { StoredProcedureProperties settings = cosmosResponse.Resource; Assert.AreEqual(id, settings.Id, "Stored Procedure id do not match"); Assert.AreEqual(body, settings.Body, "Stored Procedure functions do not match"); SelflinkValidator.ValidateSprocSelfLink(cosmosResponse.Resource.SelfLink); }
private static void ValidateTriggerSettings(TriggerProperties triggerSettings, TriggerResponse cosmosResponse) { TriggerProperties settings = cosmosResponse.Resource; Assert.AreEqual(triggerSettings.Body, settings.Body, "Trigger function do not match"); Assert.AreEqual(triggerSettings.Id, settings.Id, "Trigger id do not match"); Assert.IsTrue(cosmosResponse.RequestCharge > 0); Assert.IsNotNull(cosmosResponse.Headers.GetHeaderValue <string>(Documents.HttpConstants.HttpHeaders.MaxResourceQuota)); Assert.IsNotNull(cosmosResponse.Headers.GetHeaderValue <string>(Documents.HttpConstants.HttpHeaders.CurrentResourceQuotaUsage)); SelflinkValidator.ValidateTriggerSelfLink(cosmosResponse.Resource.SelfLink); }
private static void ValidateUserDefinedFunctionSettings(UserDefinedFunctionProperties udfSettings, UserDefinedFunctionResponse cosmosResponse) { UserDefinedFunctionProperties settings = cosmosResponse.Resource; Assert.AreEqual(udfSettings.Body, settings.Body, "User defined function do not match"); Assert.AreEqual(udfSettings.Id, settings.Id, "User defined function id do not match"); Assert.IsTrue(cosmosResponse.RequestCharge > 0); Assert.IsNotNull(cosmosResponse.Headers.GetHeaderValue <string>(Documents.HttpConstants.HttpHeaders.MaxResourceQuota)); Assert.IsNotNull(cosmosResponse.Headers.GetHeaderValue <string>(Documents.HttpConstants.HttpHeaders.CurrentResourceQuotaUsage)); SelflinkValidator.ValidateUdfSelfLink(cosmosResponse.Resource.SelfLink); }
public async Task ReadDatabase() { DatabaseResponse createResponse = await this.CreateDatabaseHelper(); DatabaseResponse readResponse = await createResponse.Database.ReadAsync(cancellationToken : this.cancellationToken); Assert.AreEqual(createResponse.Database.Id, readResponse.Database.Id); Assert.AreEqual(createResponse.Resource.Id, readResponse.Resource.Id); Assert.AreNotEqual(createResponse.ActivityId, readResponse.ActivityId); Assert.IsNotNull(createResponse.Resource.SelfLink); Assert.IsNotNull(readResponse.Resource.SelfLink); Assert.AreEqual(createResponse.Resource.SelfLink, readResponse.Resource.SelfLink); SelflinkValidator.ValidateDbSelfLink(readResponse.Resource.SelfLink); this.ValidateHeaders(readResponse); await createResponse.Database.DeleteAsync(cancellationToken : this.cancellationToken); }
public async Task CRUDTest() { string userId = Guid.NewGuid().ToString(); UserResponse userResponse = await this.cosmosDatabase.CreateUserAsync(userId); Assert.AreEqual(HttpStatusCode.Created, userResponse.StatusCode); Assert.AreEqual(userId, userResponse.Resource.Id); Assert.IsNotNull(userResponse.Resource.ResourceId); SelflinkValidator.ValidateUserSelfLink(userResponse.Resource.SelfLink); string newUserId = Guid.NewGuid().ToString(); userResponse.Resource.Id = newUserId; userResponse = await this.cosmosDatabase.GetUser(userId).ReplaceAsync(userResponse.Resource); Assert.AreEqual(HttpStatusCode.OK, userResponse.StatusCode); Assert.AreEqual(newUserId, userResponse.Resource.Id); SelflinkValidator.ValidateUserSelfLink(userResponse.Resource.SelfLink); userResponse = await this.cosmosDatabase.GetUser(userResponse.Resource.Id).ReadAsync(); Assert.AreEqual(HttpStatusCode.OK, userResponse.StatusCode); Assert.AreEqual(newUserId, userResponse.Resource.Id); SelflinkValidator.ValidateUserSelfLink(userResponse.Resource.SelfLink); userResponse = await this.cosmosDatabase.GetUser(newUserId).DeleteAsync(); Assert.AreEqual(HttpStatusCode.NoContent, userResponse.StatusCode); userId = Guid.NewGuid().ToString(); userResponse = await this.cosmosDatabase.UpsertUserAsync(userId); Assert.AreEqual(HttpStatusCode.Created, userResponse.StatusCode); Assert.AreEqual(userId, userResponse.Resource.Id); Assert.IsNotNull(userResponse.Resource.ResourceId); SelflinkValidator.ValidateUserSelfLink(userResponse.Resource.SelfLink); newUserId = Guid.NewGuid().ToString(); userResponse.Resource.Id = newUserId; userResponse = await this.cosmosDatabase.UpsertUserAsync(userResponse.Resource.Id); Assert.AreEqual(newUserId, userResponse.Resource.Id); SelflinkValidator.ValidateUserSelfLink(userResponse.Resource.SelfLink); }
public async Task ReadReplaceThroughputResponseTests() { int toStreamCount = 0; int fromStreamCount = 0; CosmosSerializerHelper mockJsonSerializer = new CosmosSerializerHelper( null, (x) => fromStreamCount++, (x) => toStreamCount++); //Create a new cosmos client with the mocked cosmos json serializer CosmosClient client = TestCommon.CreateCosmosClient( (cosmosClientBuilder) => cosmosClientBuilder.WithCustomSerializer(mockJsonSerializer)); int databaseThroughput = 10000; Cosmos.Database databaseNoThroughput = await client.CreateDatabaseAsync(Guid.NewGuid().ToString(), throughput : null); Cosmos.Database databaseWithThroughput = await client.CreateDatabaseAsync(Guid.NewGuid().ToString(), databaseThroughput, null); string containerId = Guid.NewGuid().ToString(); string partitionPath = "/users"; Container containerNoThroughput = await databaseWithThroughput.CreateContainerAsync(containerId, partitionPath, throughput : null); try { await containerNoThroughput.ReadThroughputAsync(new RequestOptions()); Assert.Fail("Should through not found exception as throughput is not configured"); } catch (CosmosException exception) { Assert.AreEqual(HttpStatusCode.NotFound, exception.StatusCode); } try { await containerNoThroughput.ReplaceThroughputAsync(2000, new RequestOptions()); Assert.Fail("Should through not found exception as throughput is not configured"); } catch (CosmosException exception) { Assert.AreEqual(HttpStatusCode.NotFound, exception.StatusCode); } int containerThroughput = 1000; Container container = await databaseNoThroughput.CreateContainerAsync(Guid.NewGuid().ToString(), "/id", throughput : containerThroughput); int?containerResponseThroughput = await container.ReadThroughputAsync(); Assert.AreEqual(containerThroughput, containerResponseThroughput); ThroughputResponse containerThroughputResponse = await container.ReadThroughputAsync(new RequestOptions()); Assert.IsNotNull(containerThroughputResponse); Assert.IsNotNull(containerThroughputResponse.Resource); Assert.IsNotNull(containerThroughputResponse.MinThroughput); Assert.IsNotNull(containerThroughputResponse.Resource.Throughput); Assert.AreEqual(containerThroughput, containerThroughputResponse.Resource.Throughput.Value); SelflinkValidator.ValidateTroughputSelfLink(containerThroughputResponse.Resource.SelfLink); containerThroughput += 500; containerThroughputResponse = await container.ReplaceThroughputAsync(containerThroughput, new RequestOptions()); Assert.IsNotNull(containerThroughputResponse); Assert.IsNotNull(containerThroughputResponse.Resource); Assert.IsNotNull(containerThroughputResponse.Resource.Throughput); Assert.AreEqual(containerThroughput, containerThroughputResponse.Resource.Throughput.Value); SelflinkValidator.ValidateTroughputSelfLink(containerThroughputResponse.Resource.SelfLink); Assert.AreEqual(0, toStreamCount, "Custom serializer to stream should not be used for offer operations"); Assert.AreEqual(0, fromStreamCount, "Custom serializer from stream should not be used for offer operations"); await databaseNoThroughput.DeleteAsync(); }
public async Task CRUDTest() { string containerId = Guid.NewGuid().ToString(); ContainerResponse containerResponse = await this.cosmosDatabase.CreateContainerAsync(containerId, "/id"); Assert.AreEqual(HttpStatusCode.Created, containerResponse.StatusCode); string userId = Guid.NewGuid().ToString(); UserResponse userResponse = await this.cosmosDatabase.CreateUserAsync(userId); User user = userResponse.User; Assert.AreEqual(HttpStatusCode.Created, userResponse.StatusCode); Assert.AreEqual(userId, user.Id); string permissionId = Guid.NewGuid().ToString(); PermissionProperties permissionProperties = new PermissionProperties(permissionId, PermissionMode.Read, containerResponse.Container); PermissionResponse permissionResponse = await user.CreatePermissionAsync(permissionProperties); Assert.AreEqual(HttpStatusCode.Created, userResponse.StatusCode); Assert.AreEqual(permissionId, permissionResponse.Resource.Id); Assert.AreEqual(permissionProperties.PermissionMode, permissionResponse.Resource.PermissionMode); Assert.IsNotNull(permissionResponse.Resource.Token); SelflinkValidator.ValidatePermissionSelfLink(permissionResponse.Resource.SelfLink); PermissionProperties newPermissionProperties = new PermissionProperties(permissionId, PermissionMode.All, containerResponse.Container); permissionResponse = await user.GetPermission(permissionId).ReplaceAsync(newPermissionProperties); //Backend returns Created instead of OK Assert.AreEqual(HttpStatusCode.Created, userResponse.StatusCode); Assert.AreEqual(permissionId, permissionResponse.Resource.Id); Assert.AreEqual(newPermissionProperties.PermissionMode, permissionResponse.Resource.PermissionMode); SelflinkValidator.ValidatePermissionSelfLink(permissionResponse.Resource.SelfLink); permissionResponse = await user.GetPermission(permissionId).ReadAsync(); Assert.AreEqual(HttpStatusCode.OK, permissionResponse.StatusCode); Assert.AreEqual(permissionId, permissionResponse.Resource.Id); SelflinkValidator.ValidatePermissionSelfLink(permissionResponse.Resource.SelfLink); permissionResponse = await user.GetPermission(permissionId).DeleteAsync(); Assert.AreEqual(HttpStatusCode.NoContent, permissionResponse.StatusCode); try { permissionResponse = await user.GetPermission(permissionId).ReadAsync(); Assert.Fail(); } catch (CosmosException ex) { Assert.AreEqual(HttpStatusCode.NotFound, ex.StatusCode); } permissionId = Guid.NewGuid().ToString(); permissionProperties = new PermissionProperties(permissionId, PermissionMode.Read, containerResponse.Container); permissionResponse = await user.CreatePermissionAsync(permissionProperties); Assert.AreEqual(HttpStatusCode.Created, userResponse.StatusCode); Assert.AreEqual(permissionId, permissionResponse.Resource.Id); Assert.AreEqual(permissionProperties.PermissionMode, permissionResponse.Resource.PermissionMode); Assert.IsNotNull(permissionResponse.Resource.Token); newPermissionProperties = new PermissionProperties(permissionId, PermissionMode.All, containerResponse.Container); permissionResponse = await user.UpsertPermissionAsync(newPermissionProperties); Assert.AreEqual(HttpStatusCode.OK, permissionResponse.StatusCode); Assert.AreEqual(permissionId, permissionResponse.Resource.Id); Assert.AreEqual(newPermissionProperties.PermissionMode, permissionResponse.Resource.PermissionMode); }
public async Task ValidateAzureKeyCredentialDirectModeUpdateAsync() { string authKey = ConfigurationManager.AppSettings["MasterKey"]; string endpoint = ConfigurationManager.AppSettings["GatewayEndpoint"]; AzureKeyCredential masterKeyCredential = new AzureKeyCredential(authKey); using (CosmosClient client = new CosmosClient( endpoint, masterKeyCredential)) { string databaseName = Guid.NewGuid().ToString(); try { Cosmos.Database database = client.GetDatabase(databaseName); ResponseMessage responseMessage = await database.ReadStreamAsync(); Assert.AreEqual(HttpStatusCode.NotFound, responseMessage.StatusCode); { // Random key: Next set of actions are expected to fail => 401 (UnAuthorized) masterKeyCredential.Update(Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()))); responseMessage = await database.ReadStreamAsync(); Assert.AreEqual(HttpStatusCode.Unauthorized, responseMessage.StatusCode); string diagnostics = responseMessage.Diagnostics.ToString(); Assert.IsTrue(diagnostics.Contains("AuthProvider LifeSpan InSec"), diagnostics.ToString()); } { // Resetting back to master key => 404 (NotFound) masterKeyCredential.Update(authKey); responseMessage = await database.ReadStreamAsync(); Assert.AreEqual(HttpStatusCode.NotFound, responseMessage.StatusCode); } // Test with resource token interchageability masterKeyCredential.Update(authKey); database = await client.CreateDatabaseAsync(databaseName); string containerId = Guid.NewGuid().ToString(); ContainerResponse containerResponse = await database.CreateContainerAsync(containerId, "/id"); Assert.AreEqual(HttpStatusCode.Created, containerResponse.StatusCode); { // Resource token with ALL permissoin's string userId = Guid.NewGuid().ToString(); UserResponse userResponse = await database.CreateUserAsync(userId); Cosmos.User user = userResponse.User; Assert.AreEqual(HttpStatusCode.Created, userResponse.StatusCode); Assert.AreEqual(userId, user.Id); string permissionId = Guid.NewGuid().ToString(); PermissionProperties permissionProperties = new PermissionProperties(permissionId, Cosmos.PermissionMode.All, client.GetContainer(databaseName, containerId)); PermissionResponse permissionResponse = await database.GetUser(userId).CreatePermissionAsync(permissionProperties); Assert.AreEqual(HttpStatusCode.Created, permissionResponse.StatusCode); Assert.AreEqual(permissionId, permissionResponse.Resource.Id); Assert.AreEqual(Cosmos.PermissionMode.All, permissionResponse.Resource.PermissionMode); Assert.IsNotNull(permissionResponse.Resource.Token); SelflinkValidator.ValidatePermissionSelfLink(permissionResponse.Resource.SelfLink); // Valdiate ALL on contianer masterKeyCredential.Update(permissionResponse.Resource.Token); ToDoActivity item = ToDoActivity.CreateRandomToDoActivity(); Cosmos.Container container = client.GetContainer(databaseName, containerId); responseMessage = await container.ReadContainerStreamAsync(); Assert.AreEqual(HttpStatusCode.OK, responseMessage.StatusCode); responseMessage = await container.CreateItemStreamAsync(TestCommon.SerializerCore.ToStream(item), new Cosmos.PartitionKey(item.id)); Assert.AreEqual(HttpStatusCode.Created, responseMessage.StatusCode); // Read Only resorce token } // Reset to master key for new permission creation masterKeyCredential.Update(authKey); { // Resource token with Read-ONLY permissoin's string userId = Guid.NewGuid().ToString(); UserResponse userResponse = await database.CreateUserAsync(userId); Cosmos.User user = userResponse.User; Assert.AreEqual(HttpStatusCode.Created, userResponse.StatusCode); Assert.AreEqual(userId, user.Id); string permissionId = Guid.NewGuid().ToString(); PermissionProperties permissionProperties = new PermissionProperties(permissionId, Cosmos.PermissionMode.Read, client.GetContainer(databaseName, containerId)); PermissionResponse permissionResponse = await database.GetUser(userId).CreatePermissionAsync(permissionProperties); //Backend returns Created instead of OK Assert.AreEqual(HttpStatusCode.Created, permissionResponse.StatusCode); Assert.AreEqual(permissionId, permissionResponse.Resource.Id); Assert.AreEqual(Cosmos.PermissionMode.Read, permissionResponse.Resource.PermissionMode); // Valdiate read on contianer masterKeyCredential.Update(permissionResponse.Resource.Token); ToDoActivity item = ToDoActivity.CreateRandomToDoActivity(); Cosmos.Container container = client.GetContainer(databaseName, containerId); responseMessage = await container.ReadContainerStreamAsync(); Assert.AreEqual(HttpStatusCode.OK, responseMessage.StatusCode); responseMessage = await container.CreateItemStreamAsync(TestCommon.SerializerCore.ToStream(item), new Cosmos.PartitionKey(item.id)); Assert.AreEqual(HttpStatusCode.Forbidden, responseMessage.StatusCode); // Read Only resorce token } { // Reset to master key for new permission creation masterKeyCredential.Update(Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()))); ToDoActivity item = ToDoActivity.CreateRandomToDoActivity(); Cosmos.Container container = client.GetContainer(databaseName, containerId); responseMessage = await container.CreateItemStreamAsync(TestCommon.SerializerCore.ToStream(item), new Cosmos.PartitionKey(item.id)); Assert.AreEqual(HttpStatusCode.Unauthorized, responseMessage.StatusCode); // Read Only resorce token string diagnostics = responseMessage.Diagnostics.ToString(); Assert.IsTrue(diagnostics.Contains("AuthProvider LifeSpan InSec"), diagnostics.ToString()); } } finally { // Reset to master key for clean-up masterKeyCredential.Update(authKey); await TestCommon.DeleteDatabaseAsync(client, client.GetDatabase(databaseName)); } } }