Ejemplo n.º 1
0
        public async Task ReplaceDocument(bool isValidToken, string documentPrefix, DefaultPartitionKind defaultPartition, string firstName, string lastName, bool result)
        {
            var brokerUrl = IsAzureFunctionLocalEmulator ? _resourceTokenBrokerUrlLocalHost : _resourceTokenBrokerUrl;

            var userContext        = CreateTestUserContext(isValidToken, IsAzureFunctionLocalEmulator);
            var testB2CAuthService = new TestB2CAuthService(userContext);

            await using var cosmosTokenClient = new CosmosTokenClient(testB2CAuthService, brokerUrl);

            var documentId = defaultPartition == DefaultPartitionKind.Shared
                ? documentPrefix
                : $"{documentPrefix}-{userContext.UserIdentifier}";

            var person = new Person {
                FirstName = firstName, LastName = lastName
            };

            try
            {
                await cosmosTokenClient.Replace(documentId, person, defaultPartition);

                Assert.True(result);
                return;
            }
            catch (CosmosClientException ex)
            {
                Assert.True(ex is CosmosClientException);
            }

            Assert.False(result);
        }
Ejemplo n.º 2
0
        public async Task GetList(
            bool isValidToken,
            DefaultPartitionKind defaultPartition,
            int numberOfDocuments)
        {
            var brokerUrl = IsAzureFunctionLocalEmulator ? _resourceTokenBrokerUrlLocalHost : _resourceTokenBrokerUrl;

            var userContext        = CreateTestUserContext(isValidToken, IsAzureFunctionLocalEmulator);
            var testB2CAuthService = new TestB2CAuthService(userContext);

            await using var cosmosTokenClient = new CosmosTokenClient(testB2CAuthService, brokerUrl);

            var documents = await cosmosTokenClient.List <Person>(defaultPartition);

            Assert.True(documents.Count() >= numberOfDocuments);
        }
Ejemplo n.º 3
0
        public async Task ReadDocumentEx(bool isValidToken, string documentPrefix, DefaultPartitionKind defaultPartition, string expectedFirstName, string expectedLastName)
        {
            var brokerUrl = IsAzureFunctionLocalEmulator ? _resourceTokenBrokerUrlLocalHost : _resourceTokenBrokerUrl;

            var userContext        = CreateTestUserContext(isValidToken, IsAzureFunctionLocalEmulator);
            var testB2CAuthService = new TestB2CAuthService(userContext);

            await using var cosmosTokenClient = new CosmosTokenClient(testB2CAuthService, brokerUrl);

            var documentId = defaultPartition == DefaultPartitionKind.Shared
                ? documentPrefix
                : $"{documentPrefix}2-{userContext.UserIdentifier}";

            var document = await cosmosTokenClient.Read <PersonEx>(documentId, defaultPartition);

            Assert.Equal(expectedFirstName, document.FirstName);
            Assert.Equal(expectedLastName, document.LastName);
        }
Ejemplo n.º 4
0
        public async Task TryToReadDocumentThatUserDoesNotHaveAccessTo(bool isValidToken, DefaultPartitionKind defaultPartition, string documentId)
        {
            var brokerUrl = IsAzureFunctionLocalEmulator ? _resourceTokenBrokerUrlLocalHost : _resourceTokenBrokerUrl;

            var userContext        = CreateTestUserContext(isValidToken, IsAzureFunctionLocalEmulator);
            var testB2CAuthService = new TestB2CAuthService(userContext);

            await using var cosmosTokenClient = new CosmosTokenClient(testB2CAuthService, brokerUrl);

            try
            {
                var document = await cosmosTokenClient.Read <Person>(documentId, defaultPartition);
            }
            catch (Exception ex)
            {
                Assert.True(ex is DataException);
            }
        }
 /// <summary>
 ///     <para>
 ///         Create Cosmos document.
 ///     </para>
 /// </summary>
 /// <typeparam name="T">Type of document to create.</typeparam>
 /// <param name="id">Unique id of document.</param>
 /// <param name="item">The instance of document.</param>
 /// <param name="defaultPartition">Partition used (user or shared)</param>
 /// <param name="cancellationToken">Cancellation token</param>
 /// <returns></returns>
 public async Task Create <T>(string id, T item, DefaultPartitionKind defaultPartition, CancellationToken cancellationToken = default) =>
 await ExecuteCosmosCommand(async resourcePermissionResponse =>