Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Commit

Permalink
add integration test
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Fisher <matt.fisher@fermyon.com>
  • Loading branch information
bacongobbler committed Jan 8, 2022
1 parent bf37d18 commit 7ed3b76
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 52 deletions.
2 changes: 2 additions & 0 deletions src/Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,5 @@
}

app.Run();

public partial class Program { }
83 changes: 31 additions & 52 deletions tests/Hippo.FunctionalTests/TestBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using FluentValidation.AspNetCore;
using Hippo.Application;
Expand All @@ -13,6 +14,7 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -24,61 +26,38 @@ namespace Hippo.FunctionalTests;
public class TestBase : IDisposable
{
private static IConfigurationRoot _configuration = null!;
private static IServiceScopeFactory _scopeFactory = null!;
protected static WebApplicationFactory<Program> _factory = null!;
private static Checkpoint _checkpoint = null!;
private static string? _currentUserId;

public TestBase()
{
var builder = new ConfigurationBuilder()
var configBuilder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", true, true)
.AddEnvironmentVariables();
.AddJsonFile("appsettings.json", true, true);

_configuration = builder.Build();
_configuration = configBuilder.Build();

var services = new ServiceCollection();

services.AddSingleton(Mock.Of<IWebHostEnvironment>(w =>
w.EnvironmentName == "Development" &&
w.ApplicationName == "Hippo.Web"));

services.AddLogging();

services.AddApplication();
services.AddInfrastructure(_configuration);

services.AddDatabaseDeveloperPageExceptionFilter();

services.AddSingleton<ICurrentUserService, CurrentUserService>();

services.AddHttpContextAccessor();

services.AddHealthChecks()
.AddDbContextCheck<ApplicationDbContext>();

services.AddControllersWithViews().AddFluentValidation();

services.AddRouting(options => options.LowercaseUrls = true);

services.Configure<ApiBehaviorOptions>(options =>
options.SuppressModelStateInvalidFilter = true);

// Replace service registration for ICurrentUserService
// Remove existing registration
var currentUserServiceDescriptor = services.FirstOrDefault(d =>
d.ServiceType == typeof(ICurrentUserService));

if (currentUserServiceDescriptor != null)
_factory = new WebApplicationFactory<Program>()
.WithWebHostBuilder(builder =>
{
services.Remove(currentUserServiceDescriptor);
}

// Register testing version
services.AddTransient(provider =>
Mock.Of<ICurrentUserService>(s => s.UserId == _currentUserId));

_scopeFactory = services.BuildServiceProvider().GetRequiredService<IServiceScopeFactory>();
builder.ConfigureServices(services =>
{
// Replace service registration for ICurrentUserService
// Remove existing registration
var currentUserServiceDescriptor = services.FirstOrDefault(d =>
d.ServiceType == typeof(ICurrentUserService));

if (currentUserServiceDescriptor != null)
{
services.Remove(currentUserServiceDescriptor);
}

// Register testing version
services.AddTransient(provider =>
Mock.Of<ICurrentUserService>(s => s.UserId == _currentUserId));
});
});

_checkpoint = new Checkpoint
{
Expand All @@ -90,7 +69,7 @@ public TestBase()

private static void EnsureDatabase()
{
using var scope = _scopeFactory.CreateScope();
using var scope = _factory.Services.CreateScope();

var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();

Expand All @@ -102,7 +81,7 @@ private static void EnsureDatabase()

public static async Task<TResponse> SendAsync<TResponse>(IRequest<TResponse> request)
{
using var scope = _scopeFactory.CreateScope();
using var scope = _factory.Services.CreateScope();

var mediator = scope.ServiceProvider.GetRequiredService<ISender>();

Expand All @@ -121,7 +100,7 @@ public static async Task<string> RunAsAdministratorAsync()

public static async Task<string> RunAsUserAsync(string userName, string password, string[] roles)
{
using var scope = _scopeFactory.CreateScope();
using var scope = _factory.Services.CreateScope();

var userManager = scope.ServiceProvider.GetRequiredService<UserManager<Account>>();

Expand Down Expand Up @@ -156,7 +135,7 @@ public static async Task<string> RunAsUserAsync(string userName, string password
public static async Task<TEntity?> FindAsync<TEntity>(params object[] keyValues)
where TEntity : class
{
using var scope = _scopeFactory.CreateScope();
using var scope = _factory.Services.CreateScope();

var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();

Expand All @@ -166,7 +145,7 @@ public static async Task<string> RunAsUserAsync(string userName, string password
public static async Task AddAsync<TEntity>(TEntity entity)
where TEntity : class
{
using var scope = _scopeFactory.CreateScope();
using var scope = _factory.Services.CreateScope();

var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();

Expand All @@ -177,7 +156,7 @@ public static async Task AddAsync<TEntity>(TEntity entity)

public static async Task<int> CountAsync<TEntity>() where TEntity : class
{
using var scope = _scopeFactory.CreateScope();
using var scope = _factory.Services.CreateScope();

var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();

Expand Down
29 changes: 29 additions & 0 deletions tests/Hippo.FunctionalTests/Web/Controllers/AppControllerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Hippo.FunctionalTests;
using Hippo.Web.Controllers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Testing;
using Xunit;

public class AppControllerTests : TestBase
{
[Fact]
public async Task IndexRequiresSignIn()
{
var client = _factory.CreateClient(
new WebApplicationFactoryClientOptions
{
AllowAutoRedirect = false
});

var response = await client.GetAsync("/app");


Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
Assert.StartsWith("http://localhost/Account/Login",
response.Headers.Location?.OriginalString);
}
}

0 comments on commit 7ed3b76

Please sign in to comment.