コード例 #1
0
        public async Task GetSurveyAsync_Returns_Survey_Contributors()
        {
            // All contexts that share the same service provider will share the same InMemory database
            var options = CreateNewContextOptions();

            // Run the test against one instance of the context
            using (var context = new ApplicationDbContext(options))
            {
                var survey = new Survey
                {
                    Id = 1,
                    Contributors = new List<SurveyContributor>
                    {
                        new SurveyContributor { SurveyId = 1, UserId = 2 }
                    }
                };
                context.Add(survey);
                context.SaveChanges();
            }

            // Use a separate instance of the context to verify correct data was saved to database
            using (var context = new ApplicationDbContext(options))
            {
                var store = new SqlServerSurveyStore(context);
                var result = await store.GetSurveyAsync(1);

                Assert.NotNull(result.Contributors);
                Assert.NotEmpty(result.Contributors);
            }
        }
コード例 #2
0
        public UserManager(ApplicationDbContext dbContext, IHttpContextAccessor contextAccessor)
        {
            Guard.ArgumentNotNull(dbContext, nameof(dbContext));

            _dbContext = dbContext;
            _cancellationToken = contextAccessor?.HttpContext?.RequestAborted ?? CancellationToken.None;
        }
コード例 #3
0
        public async Task GetSurveyAsync_Returns_CorrectSurvey()
        {
            // All contexts that share the same service provider will share the same InMemory database
            var options = CreateNewContextOptions();

            using (var context = new ApplicationDbContext(options))
            {
                context.Add(new Survey { Id = 1 });
                context.SaveChanges();
            }

            using (var context = new ApplicationDbContext(options))
            {
                var store = new SqlServerSurveyStore(context);
                var result = await store.GetSurveyAsync(1);
                Assert.Equal(1, result.Id);
            }
        }
 public SqlServerContributorRequestStore(ApplicationDbContext dbContext)
 {
     _dbContext = dbContext;
 }
コード例 #5
0
 public SqlServerQuestionStore(ApplicationDbContext dbContext)
 {
     _dbContext = dbContext;
 }
コード例 #6
0
        public async Task GetPublishedSurveysAsync_Returns_CorrectSurveys()
        {
            // All contexts that share the same service provider will share the same InMemory database
            var options = CreateNewContextOptions();

            // Run the test against one instance of the context
            using (var context = new ApplicationDbContext(options))
            {
                context.AddRange(
                    new Survey { Id = 1, TenantId = 1 },
                    new Survey { Id = 2, TenantId = 1, Published = true },
                    new Survey { Id = 3, TenantId = 2 },
                    new Survey { Id = 4, TenantId = 2, Published = true }
                    );
                context.SaveChanges();
            }

            // Use a separate instance of the context to verify correct data was saved to database
            using (var context = new ApplicationDbContext(options))
            {
                var store = new SqlServerSurveyStore(context);
                var result = await store.GetPublishedSurveysAsync();

                Assert.Equal(2, result.Count);
                Assert.True(result.All(x => x.Published));
            }
        }
コード例 #7
0
        public async Task GetSurveysByContributorAsync_Returns_CorrectSurveys()
        {
            // All contexts that share the same service provider will share the same InMemory database
            var options = CreateNewContextOptions();

            // Run the test against one instance of the context
            using (var context = new ApplicationDbContext(options))
            {
                context.AddRange(
                    new SurveyContributor { SurveyId = 1, UserId = 10 },
                    new SurveyContributor { SurveyId = 2, UserId = 10 },
                    new SurveyContributor { SurveyId = 3, UserId = 20 }
                    );
                context.AddRange(
                    new Survey { Id = 1, OwnerId = 1 },
                    new Survey { Id = 2, OwnerId = 2 },
                    new Survey { Id = 3, OwnerId = 3 },
                    new Survey { Id = 4, OwnerId = 4 }
                    );

                context.SaveChanges();
            }

            // Use a separate instance of the context to verify correct data was saved to database
            using (var context = new ApplicationDbContext(options))
            {
                var store = new SqlServerSurveyStore(context);
                var result = await store.GetSurveysByContributorAsync(10);

                Assert.Equal(2, result.Count);
                Assert.Contains(result, s => s.Id == 1);
                Assert.Contains(result, s => s.Id == 2);
            }
        }
コード例 #8
0
        public async Task GetSurveysByOwnerAsync_Returns_CorrectSurveys()
        {
            // All contexts that share the same service provider will share the same InMemory database
            var options = CreateNewContextOptions();

            // Run the test against one instance of the context
            using (var context = new ApplicationDbContext(options))
            {
                context.AddRange(
                    new Survey { Id = 1, OwnerId = 1 },
                    new Survey { Id = 2, OwnerId = 1 },
                    new Survey { Id = 3, OwnerId = 2 }
                    );
                context.SaveChanges();
            }

            // Use a separate instance of the context to verify correct data was saved to database
            using (var context = new ApplicationDbContext(options))
            {
                var store = new SqlServerSurveyStore(context);
                var result = await store.GetSurveysByOwnerAsync(1);

                Assert.NotEmpty(result);
                // Returned collection should only contain surveys with the matching owner ID.
                Assert.True(result.All(s => s.OwnerId == 1));
            }
        }
 public SqlServerSurveyStore(ApplicationDbContext dbContext)
 {
     _dbContext = dbContext;
 }
コード例 #10
0
        // Configure is called after ConfigureServices is called.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ApplicationDbContext dbContext, ILoggerFactory loggerFactory)
        {
            var configOptions = new AppConfiguration.ConfigurationOptions();
            Configuration.Bind(configOptions);

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();

                app.UseDatabaseErrorPage();
            }

            app.UseJwtBearerAuthentication(new JwtBearerOptions {
                Audience = configOptions.AzureAd.WebApiResourceId,
                Authority = Constants.AuthEndpointPrefix,
                TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters {
                    ValidateIssuer = false
                },
                Events= new SurveysJwtBearerEvents(loggerFactory.CreateLogger<SurveysJwtBearerEvents>())
            });
            
            // Add MVC to the request pipeline.
            app.UseMvc();
        }
        // Configure is called after ConfigureServices is called.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ApplicationDbContext dbContext, ILoggerFactory loggerFactory)
        {
            if (env.IsDevelopment())
            {
                //app.UseBrowserLink();
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage(options =>
                {
                    options.ShowExceptionDetails = true;
                });
            }

            app.UseIISPlatformHandler();

            app.UseJwtBearerAuthentication(options =>
            {
                options.Audience = _configOptions.AzureAd.WebApiResourceId;
                options.Authority = Constants.AuthEndpointPrefix + "common/";
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    //Instead of validating against a fixed set of known issuers, we perform custom multi-tenant validation logic
                    ValidateIssuer = false,
                };
                options.Events = new SurveysJwtBearerEvents(loggerFactory.CreateLogger<SurveysJwtBearerEvents>());
            });
            // Add MVC to the request pipeline.
            app.UseMvc();
        }