6

Sto cercando di ottenere il test di integrazione funzionante per il mio ASP.NET 5 MVC6 Api utilizzando EF7. Sto usando il progetto predefinito che viene fornito con Identity già implementato.test di integrazione ASP.NET 5 Identity

Ecco l'im azione cercando di testare a mio controller (ottiene tutti i bambini per l'utente collegato)

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

Nel mio progetto di test creo una base di dati INT e poi fare il test di integrazione nei confronti di tale

qui è la base che uso per l'integrazione test

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

Ed ecco la TestStartup (dove ho l'override del metodo che aggiunge SQLServer con uno che aggiunge il database di test INT)

public class TestStartup : Startup 
{ 
    public TestStartup(IHostingEnvironment env) : base(env) 
    { 
    } 

    public override void AddSqlServer(IServiceCollection services) 
    { 
     services.AddEntityFramework() 
      .AddInMemoryDatabase() 
      .AddDbContext<ApplicationDbContext>(options => { 
       options.UseInMemoryDatabase(); 
      }); 
    } 

} 

e il test per l'azione

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 
    } 
} 

nessuno mi può punto nella giusta direzione su come impostare potenzialmente la CurrentPrincipal o qualche altro modo per ottenere i miei test di integrazione funzionano?

+0

Forse hai risolto il tuo problema? –

risposta

0

La domanda è come si sta autenticando nel test? Sul vostro avvio del progetto è possibile aggiungere una funzione virtuale come di seguito

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 
    }); 
} 

E poi derivata una classe di avvio del progetto di test e l'override del metodo di autenticazione di non fare nulla o per aggiungere pretesa è possibile utilizzare un middleware, come di seguito

TestStartUp

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

classe ware Medio

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") 
     }; 
    } 
} 
Problemi correlati