6

मैं ईएफ 7 का उपयोग कर अपने एएसपी.नेट 5 एमवीसी 6 एपीआई के लिए एकीकरण परीक्षण करने की कोशिश कर रहा हूं। मैं डिफ़ॉल्ट परियोजना का उपयोग कर रहा हूं जो पहले से लागू पहचान के साथ आता है।एकीकरण परीक्षण एएसपी.नेट 5 पहचान

यहाँ कार्रवाई im मेरी नियंत्रक में परीक्षण करने के लिए कोशिश कर रहा है मैं एक inmemory डेटाबेस बनाने और फिर उस के खिलाफ एकीकरण परीक्षण करने

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

अपने परीक्षण परियोजना में (उपयोगकर्ता द्वारा प्रवेश के लिए सभी बच्चों को हो जाता है)

यहाँ बेस मैं एकीकरण के लिए उपयोग

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

परीक्षण और यहां TestStartup (वह जगह है जहाँ मैं विधि है कि SQLServer कहते हैं ओवरराइड है

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

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

} 

एक है कि inmemory परीक्षण डेटाबेस कहते हैं) और कार्रवाई के लिए परीक्षण के साथ

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

किसी को भी मुझे कैसे संभावित CurrentPrincipal या प्राप्त करने के लिए किसी अन्य तरीके से स्थापित करने के लिए पर सही दिशा में बात कर सकते हैं मेरे एकीकरण परीक्षण काम कर रहे हैं?

+0

किया आप अपनी समस्या का समाधान करते हैं? –

उत्तर

0

सवाल यह है कि आप अपने परीक्षण में प्रमाणीकरण कैसे कर रहे हैं? अपनी परियोजना के प्रारंभ में आप की तरह नीचे

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

और फिर अपने परीक्षण परियोजना में एक स्टार्टअप वर्ग व्युत्पन्न एक आभासी समारोह जोड़ सकते हैं और कुछ भी नहीं करना है या आप नीचे के रूप में एक मध्यम-बर्तन का उपयोग कर सकते दावे को जोड़ने के लिए प्रमाणीकरण विधि को भी पार कर

TestStartUp

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

मध्य बर्तन वर्ग

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") 
     }; 
    } 
} 
संबंधित मुद्दे