ASP.NET 5 Identity Integration Testing

I am trying to get integration testing working for my ASP.NET 5 MVC6 Api using EF7. I am using the default project that comes with Identity already installed.

Here is the action I am trying to check in my controller (gets all the children for the logged in user)

[Authorize] [HttpGet("api/children")] public JsonResult GetAllChildren() { var children = _repository.GetAllChildren(User.GetUserId()); var childrenViewModel = Mapper.Map<List<ChildViewModel>>(children); return Json(childrenViewModel); } 

In my test project, I create an inmemory database and then run integration tests with this

Here is the base I use for integration tests

 public class IntegrationTestBase { public TestServer TestServer; public IntegrationTestBase() { TestServer = new TestServer(TestServer.CreateBuilder().UseStartup<TestStartup>()); } } 

And here is TestStartup (where I redefine the method that SQLServer adds with the one that adds the test inmemory database)

 public class TestStartup : Startup { public TestStartup(IHostingEnvironment env) : base(env) { } public override void AddSqlServer(IServiceCollection services) { services.AddEntityFramework() .AddInMemoryDatabase() .AddDbContext<ApplicationDbContext>(options => { options.UseInMemoryDatabase(); }); } } 

And a test for action

 public class ChildTests : IntegrationTestBase { [Fact] public async Task GetAllChildren_Test() { //TODO Set Current Principal?? var result = await TestServer.CreateClient().GetAsync("/api/children"); result.IsSuccessStatusCode.Should().BeTrue(); var body = await result.Content.ReadAsStringAsync(); body.Should().NotBeNull(); //TODO more asserts } } 

Can someone point me in the right direction how to potentially set CurrentPrincipal or some other way to make my integration tests work?

+6
source share
1 answer

The question is, how do you authenticate in your test? When starting your project, you can add a virtual function as shown below

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { // ... UseAuthentication(app); // ... app.UseMvcWithDefaultRoute(); } protected virtual void UseAuthentication(IApplicationBuilder app) { app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationScheme = "Cookies", AutomaticAuthenticate = true, AutomaticChallenge = true }); } 

And then bring the launch class to your test project and override the authentication method so that you don’t do anything or add an application, you can use an intermediate product as shown below

Teststartup

 internal TestStartUp : Startup { protected override void UseAuthentication(IApplicationBuilder app) { app.UseMiddleware<TestAuthMiddlewareToByPass>(); } } 

Middle class cookware

 public class TestAuthMiddlewareToByPass { public const string TestingCookieAuthentication = "TestCookieAuthentication"; private readonly RequestDelegate _next; public TestAuthMiddlewareToByPass(RequestDelegate next) { _next = next; } public async Task Invoke(HttpContext context) { // fake user ClaimsIdentity claimsIdentity = new ClaimsIdentity(Claims(), TestingCookieAuthentication); ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(claimsIdentity); context.User = claimsPrincipal; await _next(context); } protected virtual List<Claim> Claims() { return new List<Claim> { new Claim(ClaimTypes.Name, "admin"), new Claim(ClaimTypes.Role, "admin") }; } } 
0
source

All Articles