Ejemplo n.º 1
0
        public async Task Pass_thru_when_context_not_in_services()
        {
            var logProvider = new TestLoggerProvider();

            var builder = new WebHostBuilder()
                          .Configure(app =>
            {
                app.UseDatabaseErrorPage();
                app.UseMiddleware <ContextNotRegisteredInServicesMiddleware>();
#pragma warning disable CS0618 // Type or member is obsolete
                app.ApplicationServices.GetService <ILoggerFactory>().AddProvider(logProvider);
#pragma warning restore CS0618 // Type or member is obsolete
            });
            var server = new TestServer(builder);

            var ex = await Assert.ThrowsAsync <SqlException>(async() =>
            {
                try
                {
                    await server.CreateClient().GetAsync("http://localhost/");
                }
                catch (InvalidOperationException exception) when(exception.InnerException != null)
                {
                    throw exception.InnerException;
                }
            });

            Assert.Contains(logProvider.Logger.Messages.ToList(), m =>
                            m.StartsWith(StringsHelpers.GetResourceString("FormatDatabaseErrorPageMiddleware_ContextNotRegistered", typeof(BloggingContext))));
        }
    public async Task Context_not_registered_in_services()
    {
        using var host = new HostBuilder()
                         .ConfigureWebHost(webHostBuilder =>
        {
            webHostBuilder
            .UseTestServer()
            .Configure(app => app.UseMigrationsEndPoint())
            .ConfigureServices(services => services.AddEntityFrameworkSqlite());
        }).Build();

        await host.StartAsync();

        var server = host.GetTestServer();

        var formData = new FormUrlEncodedContent(new List <KeyValuePair <string, string> >
        {
            new KeyValuePair <string, string>("context", typeof(BloggingContext).AssemblyQualifiedName)
        });

        var response = await server.CreateClient().PostAsync("http://localhost" + MigrationsEndPointOptions.DefaultPath, formData);

        var content = await response.Content.ReadAsStringAsync();

        Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
        Assert.StartsWith(StringsHelpers.GetResourceString("FormatMigrationsEndPointMiddleware_ContextNotRegistered", typeof(BloggingContext).AssemblyQualifiedName), content);
        Assert.True(content.Length > 512);
    }
        public async Task Exception_while_applying_migrations()
        {
            using (var database = SqlServerTestStore.CreateScratch())
            {
                var optionsBuilder = new DbContextOptionsBuilder();
                optionsBuilder.UseSqlServer(database.ConnectionString);

                var builder = new WebHostBuilder()
                              .Configure(app => app.UseMigrationsEndPoint())
                              .ConfigureServices(services =>
                {
                    services.AddEntityFramework().AddEntityFrameworkSqlServer();
                    services.AddScoped <BloggingContextWithSnapshotThatThrows>();
                    services.AddSingleton(optionsBuilder.Options);
                });
                var server = new TestServer(builder);

                var formData = new FormUrlEncodedContent(new List <KeyValuePair <string, string> >
                {
                    new KeyValuePair <string, string>("context", typeof(BloggingContextWithSnapshotThatThrows).AssemblyQualifiedName)
                });

                var ex = await Assert.ThrowsAsync <InvalidOperationException>(async() =>
                                                                              await server.CreateClient().PostAsync("http://localhost" + MigrationsEndPointOptions.DefaultPath, formData));

                Assert.StartsWith(StringsHelpers.GetResourceString("FormatMigrationsEndPointMiddleware_Exception", typeof(BloggingContextWithSnapshotThatThrows)), ex.Message);
                Assert.Equal("Welcome to the invalid migration!", ex.InnerException.Message);
            }
        }
        public async Task Pass_thru_when_context_not_in_services()
        {
            using (var database = SqlServerTestStore.CreateScratch())
            {
                var logProvider = new TestLoggerProvider();

                var server = TestServer.Create(app =>
                {
                    app.UseDatabaseErrorPage();
                    app.UseMiddleware <ContextNotRegisteredInServicesMiddleware>();
                    app.ApplicationServices.GetService <ILoggerFactory>().AddProvider(logProvider);
                },
                                               services =>
                {
                    services.AddEntityFramework().AddSqlServer();
                    var optionsBuilder = new DbContextOptionsBuilder();
                    if (!PlatformHelper.IsMono)
                    {
                        optionsBuilder.UseSqlServer(database.ConnectionString);
                    }
                    else
                    {
                        optionsBuilder.UseInMemoryDatabase();
                    }
                    services.AddInstance <DbContextOptions>(optionsBuilder.Options);
                });

                var ex = await Assert.ThrowsAsync <SqlException>(async() =>
                                                                 await server.CreateClient().GetAsync("http://localhost/"));

                Assert.True(logProvider.Logger.Messages.Any(m =>
                                                            m.StartsWith(StringsHelpers.GetResourceString("FormatDatabaseErrorPageMiddleware_ContextNotRegistered", typeof(BloggingContext)))));
            }
        }
    public async Task Invalid_context_type_specified()
    {
        using var host = new HostBuilder()
                         .ConfigureWebHost(webHostBuilder =>
        {
            webHostBuilder
            .UseTestServer()
            .Configure(app =>
            {
                app.UseMigrationsEndPoint();
            });
        }).Build();

        await host.StartAsync();

        var server = host.GetTestServer();

        var typeName = "You won't find this type ;)";
        var formData = new FormUrlEncodedContent(new List <KeyValuePair <string, string> >
        {
            new KeyValuePair <string, string>("context", typeName)
        });

        var response = await server.CreateClient().PostAsync("http://localhost" + MigrationsEndPointOptions.DefaultPath, formData);

        var content = await response.Content.ReadAsStringAsync();

        Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
        Assert.StartsWith(StringsHelpers.GetResourceString("FormatMigrationsEndPointMiddleware_ContextNotRegistered", typeName), content);
        Assert.True(content.Length > 512);
    }
Ejemplo n.º 6
0
        public async Task Pass_thru_when_exception_in_logic()
        {
            using (var database = SqlTestStore.CreateScratch())
            {
                var logProvider = new TestLoggerProvider();

                using var host = await SetupServer <BloggingContextWithSnapshotThatThrows, ExceptionInLogicMiddleware>(database, logProvider);

                using var server = host.GetTestServer();

                try
                {
                    await server.CreateClient().GetAsync("http://localhost/");
                }
                catch (Exception exception)
                {
                    Assert.True(
                        exception.GetType().Name == "SqliteException" ||
                        exception.InnerException?.GetType().Name == "SqliteException");
                }

                Assert.Contains(logProvider.Logger.Messages.ToList(), m =>
                                m.StartsWith(StringsHelpers.GetResourceString("DatabaseErrorPageMiddleware_Exception")));
            }
        }
        public async Task Error_page_displayed_when_exception_wrapped()
        {
            TestServer          server   = SetupTestServer <BloggingContext, WrappedExceptionMiddleware>();
            HttpResponseMessage response = await server.CreateClient().GetAsync("http://localhost/");

            Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
            var content = await response.Content.ReadAsStringAsync();

            Assert.Contains("I wrapped your exception", content);
            Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_NoDbOrMigrationsTitle", typeof(BloggingContext).Name), content);
        }
        public async Task Error_page_displayed_no_migrations()
        {
            TestServer          server   = SetupTestServer <BloggingContext, NoMigrationsMiddleware>();
            HttpResponseMessage response = await server.CreateClient().GetAsync("http://localhost/");

            Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
            var content = await response.Content.ReadAsStringAsync();

            Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_NoDbOrMigrationsTitle", typeof(BloggingContext).Name), content);
            Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_AddMigrationCommand").Replace(">", "&gt;"), content);
            Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_ApplyMigrationsCommand").Replace(">", "&gt;"), content);
        }
Ejemplo n.º 9
0
        public async Task Pass_thru_when_exception_in_logic()
        {
            var database = await SqlServerTestDatabase.Scratch(createDatabase : false);

            var logProvider = new TestLoggerProvider();

            var server = await SetupTestServer <BloggingContextWithSnapshotThatThrows, ExceptionInLogicMiddleware>(logProvider);

            var ex = await Assert.ThrowsAsync <SqlException>(async() =>
                                                             await server.CreateClient().GetAsync("http://localhost/"));

            Assert.True(logProvider.Logger.Messages.Any(m =>
                                                        m.StartsWith(StringsHelpers.GetResourceString("FormatDatabaseErrorPageMiddleware_Exception"))));
        }
Ejemplo n.º 10
0
        public async Task Error_page_displayed_pending_migrations()
        {
            TestServer          server   = SetupTestServer <BloggingContextWithMigrations, PendingMigrationsMiddleware>();
            HttpResponseMessage response = await server.CreateClient().GetAsync("http://localhost/");

            Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);

            var content = await response.Content.ReadAsStringAsync();

            Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_PendingMigrationsTitle", typeof(BloggingContextWithMigrations).Name), content);
            Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_ApplyMigrationsCommandPMC").Replace(">", "&gt;"), content);
            Assert.Contains("<li>111111111111111_MigrationOne</li>", content);
            Assert.Contains("<li>222222222222222_MigrationTwo</li>", content);

            Assert.DoesNotContain(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_AddMigrationCommandPMC").Replace(">", "&gt;"), content);
        }
Ejemplo n.º 11
0
        public async Task Context_type_not_specified()
        {
            var server = TestServer.Create(app =>
            {
                app.UseMigrationsEndPoint();
            });

            var formData = new FormUrlEncodedContent(new List <KeyValuePair <string, string> >());

            var response = await server.CreateClient().PostAsync("http://localhost" + MigrationsEndPointOptions.DefaultPath, formData);

            var content = await response.Content.ReadAsStringAsync();

            Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
            Assert.StartsWith(StringsHelpers.GetResourceString("FormatMigrationsEndPointMiddleware_NoContextType"), content);
            Assert.True(content.Length > 512);
        }
Ejemplo n.º 12
0
        public async Task Error_page_displayed_pending_model_changes()
        {
            using (var database = SqlServerTestStore.CreateScratch())
            {
                TestServer          server   = SetupTestServer <BloggingContextWithPendingModelChanges, PendingModelChangesMiddleware>(database);
                HttpResponseMessage response = await server.CreateClient().GetAsync("http://localhost/");

                Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);

                var content = await response.Content.ReadAsStringAsync();

                Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_PendingChangesTitle", typeof(BloggingContextWithPendingModelChanges).Name), content);
                Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_AddMigrationCommandCLI").Replace(">", "&gt;"), content);
                Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_AddMigrationCommandPMC").Replace(">", "&gt;"), content);
                Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_ApplyMigrationsCommandCLI").Replace(">", "&gt;"), content);
                Assert.Contains(StringsHelpers.GetResourceString("FormatDatabaseErrorPage_ApplyMigrationsCommandPMC").Replace(">", "&gt;"), content);
            }
        }
Ejemplo n.º 13
0
        public async Task Error_page_displayed_when_exception_wrapped()
        {
            using (var database = SqlTestStore.CreateScratch())
            {
                using var host = await SetupServer <BloggingContext, WrappedExceptionMiddleware>(database);

                using var server = host.GetTestServer();
                HttpResponseMessage response = await server.CreateClient().GetAsync("http://localhost/");

                Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
                var content = await response.Content.ReadAsStringAsync();

                Assert.Contains("I wrapped your exception", content);
                Assert.Contains(StringsHelpers.GetResourceString("DatabaseErrorPage_NoDbOrMigrationsTitle"), content);
                Assert.Contains(StringsHelpers.GetResourceString("DatabaseErrorPage_NoDbOrMigrationsInfo"), content);
                Assert.Contains(typeof(BloggingContext).Name, content);
            }
        }
Ejemplo n.º 14
0
        public async Task Error_page_displayed_no_migrations()
        {
            using (var database = SqlTestStore.CreateScratch())
            {
                using var host = await SetupServer <BloggingContext, NoMigrationsMiddleware>(database);

                using var server = host.GetTestServer();
                HttpResponseMessage response = await server.CreateClient().GetAsync("http://localhost/");

                Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
                var content = await response.Content.ReadAsStringAsync();

                Assert.Contains(StringsHelpers.GetResourceString("DatabaseErrorPage_NoDbOrMigrationsTitle"), content);
                Assert.Contains(StringsHelpers.GetResourceString("DatabaseErrorPage_NoDbOrMigrationsInfo"), content);
                Assert.Contains(typeof(BloggingContext).Name, content);
                Assert.Contains(StringsHelpers.GetResourceString("DatabaseErrorPage_AddMigrationCommandPMC").Replace(">", "&gt;"), content);
                Assert.Contains(StringsHelpers.GetResourceString("DatabaseErrorPage_ApplyMigrationsCommandPMC").Replace(">", "&gt;"), content);
            }
        }
Ejemplo n.º 15
0
        public async Task Pass_thru_when_context_not_in_services()
        {
            var logProvider = new TestLoggerProvider();

            using var host = new HostBuilder()
                             .ConfigureWebHost(webHostBuilder =>
            {
                webHostBuilder
                .UseTestServer()
                .Configure(app =>
                {
#pragma warning disable CS0618 // Type or member is obsolete
                    app.UseDatabaseErrorPage();
#pragma warning restore CS0618 // Type or member is obsolete
                    app.UseMiddleware <ContextNotRegisteredInServicesMiddleware>();
#pragma warning disable CS0618 // Type or member is obsolete
                    app.ApplicationServices.GetService <ILoggerFactory>().AddProvider(logProvider);
#pragma warning restore CS0618 // Type or member is obsolete
                });
            }).Build();

            await host.StartAsync();


            try
            {
                using var server = host.GetTestServer();
                await server.CreateClient().GetAsync("http://localhost/");
            }
            catch (Exception exception)
            {
                Assert.True(
                    exception.GetType().Name == "SqliteException" ||
                    exception.InnerException?.GetType().Name == "SqliteException");
            }

            Assert.Contains(logProvider.Logger.Messages.ToList(), m =>
                            m.StartsWith(StringsHelpers.GetResourceString("FormatDatabaseErrorPageMiddleware_ContextNotRegistered", typeof(BloggingContext)), StringComparison.Ordinal));
        }
Ejemplo n.º 16
0
        public async Task Pass_thru_when_exception_in_logic()
        {
            using (var database = SqlServerTestStore.CreateScratch())
            {
                var logProvider = new TestLoggerProvider();

                var server = SetupTestServer <BloggingContextWithSnapshotThatThrows, ExceptionInLogicMiddleware>(database, logProvider);

                var ex = await Assert.ThrowsAsync <SqlException>(async() =>
                {
                    try
                    {
                        await server.CreateClient().GetAsync("http://localhost/");
                    }
                    catch (InvalidOperationException exception) when(exception.InnerException != null)
                    {
                        throw exception.InnerException;
                    }
                });

                Assert.Contains(logProvider.Logger.Messages.ToList(), m =>
                                m.StartsWith(StringsHelpers.GetResourceString("FormatDatabaseErrorPageMiddleware_Exception")));
            }
        }
Ejemplo n.º 17
0
 public static void NotDisplaysApplyMigrations(Type contextType, string content)
 {
     Assert.DoesNotContain(StringsHelpers.GetResourceString("DatabaseErrorPage_PendingMigrationsTitle"), content);
     Assert.DoesNotContain(StringsHelpers.GetResourceString("DatabaseErrorPage_PendingMigrationsInfo"), content);
 }
Ejemplo n.º 18
0
 public static void NotDisplaysScaffoldNextMigraion(Type contextType, string content)
 {
     Assert.DoesNotContain(StringsHelpers.GetResourceString("DatabaseErrorPage_PendingChangesTitle"), content);
     Assert.DoesNotContain(StringsHelpers.GetResourceString("DatabaseErrorPage_PendingChangesInfo"), content);
 }
Ejemplo n.º 19
0
 public static void DisplaysScaffoldFirstMigration(Type contextType, string content)
 {
     Assert.Contains(StringsHelpers.GetResourceString("DatabaseErrorPage_NoDbOrMigrationsTitle"), content);
     Assert.Contains(StringsHelpers.GetResourceString("DatabaseErrorPage_NoDbOrMigrationsInfo"), content);
 }