简体   繁体   English

如何在ASP.NET MVC应用程序中单元测试“Azure AD和OpenID Connect”代码

[英]How to unit test 'Azure AD and OpenID Connect' code in ASP.NET MVC app

Hi I am using the Azure AD and OpenID Connect to authenticate users to my app. 您好我使用Azure AD和OpenID Connect来验证我的应用程序的用户。 I have used the traditional code as mentioned in azure docs. 我使用了azure docs中提到的传统代码。

Startup.cs Startup.cs

public partial class Startup
{
    /// <summary>
    /// Function to set the authentication configurtion
    /// </summary>
    /// <param name="app">Owin properties</param>
    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app);
    }
}

Startup.Auth.cs Startup.Auth.cs

private void ConfigureAuth(IAppBuilder app) {
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions());

        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions {
                ClientId = "ClientId",
                Authority = "Authority",
                PostLogoutRedirectUri = "PostLogoutRedirectUri"
            });
    }

AccountController.cs AccountController.cs

public class AccountController : Controller
{
    public void SignIn()
    {
        // Send an OpenID Connect sign-in request.
        if (!Request.IsAuthenticated) {
            HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties {RedirectUri = "/"},
                OpenIdConnectAuthenticationDefaults.AuthenticationType);
        }
    }

    /// <summary>
    /// Function to signout the session user from the application
    /// </summary>
    public void SignOut()
    {
        if (Request.Url == null) {
            return;
        }
        string callbackUrl = Url.Action("SignOutCallback", "Account", null, Request.Url.Scheme);

        HttpContext.GetOwinContext().Authentication.SignOut(
            new AuthenticationProperties {RedirectUri = callbackUrl},
            OpenIdConnectAuthenticationDefaults.AuthenticationType,
            CookieAuthenticationDefaults.AuthenticationType);
    }

    /// <summary>
    /// Function to redirect the user to home screen.
    /// </summary>
    /// <returns>Redirects to home screen if authentication still exists</returns>
    public ActionResult SignOutCallback()
    {
        if (Request.IsAuthenticated) {
            return RedirectToAction("Index", "Home");
        }

        return View();
    }
}

Not sure how to start unit testing the above code. 不知道如何开始单元测试上面的代码。 I didn't find much useful resources on this topic, will appreciate any leads. 我没有找到关于这个主题的有用资源,会欣赏任何线索。

@coder89 @ coder89

Unit testing this is in my view not an option. 在我看来,单元测试不是一种选择。 All of the classes which are part of the framework has already been unit tested. 作为框架一部分的所有类都已经过单元测试。 From your question it seems to me what you are looking at is some sort of integration test. 从您的问题来看,在我看来,您正在寻找的是某种集成测试。 As you have the entire pipeline running here you are likely looking at UI testing using something like Selenium to do the trick. 当您在这里运行整个管道时,您可能正在使用像Selenium之类的东西来进行UI测试。 Browserstack is widely used for such kind of testing. Browserstack广泛用于此类测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM