Beispiel #1
0
        public async Task Given_TokenIsInCache_When_GetTokenAsync_Then_ReturnToken()
        {
            // Arrange

            var azureTokenProviderOptions =
                new AzureTokenProviderOptions
            {
                Authority    = _configuration["TokenProviderOptions:Authority"],
                ClientId     = _configuration["TokenProviderOptions:ClientId"],
                ClientSecret = _configuration["TokenProviderOptions:ClientSecret"],
                ResourceId   = "https://database.windows.net/",
                TenantId     = _configuration["TokenProviderOptions:TenantId"]
            };

            var azureTokenProvider =
                new AzureTokenProvider(
                    azureTokenProviderOptions);

            var accessToken =
                await azureTokenProvider.RefreshTokenAsync();

            // Action

            var cachedAccessToken =
                await azureTokenProvider.GetTokenAsync();

            // Assert

            cachedAccessToken.Should().NotBeNull();
            cachedAccessToken.Should().Be(accessToken);
        }
        public void should_get_access_token()
        {
            var azureAdConfigurationOptions = Options.Create(new AzureAdConfiguration {
                TenantId = "teanantid"
            });
            var azureTokenProvider = new AzureTokenProvider(azureAdConfigurationOptions);

            Assert.Throws <AggregateException>(() => azureTokenProvider.GetClientAccessToken("1234", "1234", "1234"));
        }
        public async Task When_AddAsync_ThenToDoAdded()
        {
            // Arrange

            var entityDataStoreOptions =
                new EntityDataStoreOptions
            {
                ConnectionString = _configuration["AzureSqlOptions:ConnectionString"]
            };

            var azureTokenProviderOptions =
                new AzureTokenProviderOptions
            {
                Authority    = _configuration["TokenProviderOptions:Authority"],
                ClientId     = _configuration["TokenProviderOptions:ClientId"],
                ClientSecret = _configuration["TokenProviderOptions:ClientSecret"],
                ResourceId   = "https://database.windows.net/",
                TenantId     = _configuration["TokenProviderOptions:TenantId"]
            };

            var azureTokenProvider =
                new AzureTokenProvider(
                    azureTokenProviderOptions);

            var toDoEntityDataStore =
                new ToDoEntityDataStore(
                    azureTokenProvider,
                    entityDataStoreOptions);

            var toDoEntity =
                _faker.GenerateToDoEntity();

            // Action

            await toDoEntityDataStore.AddAsync(
                toDoEntity);

            // Assert

            var query =
                "SELECT * FROM todos WHERE Id = @Id";

            using var cn  = new SqlConnection(_configuration["AzureSqlOptions:ConnectionString"]);
            using var cmd = new SqlCommand(query, cn);

            cn.AccessToken =
                await azureTokenProvider.GetTokenAsync();

            cn.Open();

            cmd.Parameters.AddWithValue("@Id", toDoEntity.Id);

            var reader = await cmd.ExecuteReaderAsync();

            ToDoEntity toDoEntityFetched = null;

            while (reader.Read())
            {
                toDoEntityFetched = new ToDoEntity
                {
                    Id          = reader.GetGuid(0),
                    Status      = reader.GetString(1),
                    Description = reader.GetString(2),
                    CreatedOn   = reader.GetDateTime(3)
                };
            }

            toDoEntityFetched.Should().NotBeNull();
            toDoEntityFetched.Id.Should().Be(toDoEntity.Id);
            toDoEntityFetched.Status.Should().Be(toDoEntity.Status);
            toDoEntityFetched.Description.Should().Be(toDoEntity.Description);
            toDoEntityFetched.CreatedOn.Should().BeCloseTo(toDoEntity.CreatedOn);
        }