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 override void Configure( IFunctionsHostBuilder builder) { var services = builder.Services; var entityDataStoreOptions = new EntityDataStoreOptions { ConnectionString = Environment.GetEnvironmentVariable("AzureSqlOptions:ConnectionString") }; services.AddSingleton(entityDataStoreOptions); var azureTokenProviderOptions = new AzureTokenProviderOptions { Authority = Environment.GetEnvironmentVariable("TokenProviderOptions:Authority"), ClientId = Environment.GetEnvironmentVariable("TokenProviderOptions:ClientId"), ClientSecret = Environment.GetEnvironmentVariable("TokenProviderOptions:ClientSecret"), ResourceId = "https://database.windows.net/", TenantId = Environment.GetEnvironmentVariable("TokenProviderOptions:TenantId") }; services.AddSingleton(azureTokenProviderOptions); services.AddSingleton <IAzureTokenProvider, AzureTokenProvider>(); services.AddTransient <IToDoEntityDataStore, ToDoEntityDataStore>(); }
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); }