【问题标题】:How to unit test code that uses OWIN Cookie Authenthication如何对使用 OWIN Cookie 身份验证的代码进行单元测试
【发布时间】:2014-10-15 13:28:26
【问题描述】:

我了解到 OWIN 有这个很棒的 Microsoft.Owin.Testing 库,可以让您在内存中测试您的 Web 应用程序。但是,我的站点需要在访问资源之前进行身份验证,这使得编写测试代码很复杂。

在使用 Microsoft.Owin.Testing 时,有没有一种方便的方式来“模拟”身份验证?

我希望我的单元测试不需要遇到进程外 STS,并且我不希望编写以编程方式登录内存中 STS 的代码(例如 Thinktecture.IdentityServer.v3) .

我想出的最简单的解决方案是禁用单元测试的身份验证代码,我不喜欢。

我正在使用带有 Cookie 身份验证的 OpenID Connect。这是一个包含的示例。需要为实际服务器填写 OpenId Connect 的配置字符串。

[Test]
public async void AccessAuthenthicatedResourceTest()
{
    const string ClientId = "";
    const string RedirectUri = "";
    const string Authority = "";

    TestServer server = TestServer.Create(
        app =>
            {
                //Configure Open ID Connect With Cookie Authenthication
                app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
                app.UseCookieAuthentication(new CookieAuthenticationOptions());
                app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
                    {
                    ClientId = ClientId,
                    RedirectUri = RedirectUri,
                    Authority = Authority
                    });

                // Requires Authentication
                app.Use(
                    async ( context, next ) =>
                        {
                            var user = context.Authentication.User;
                            if ( user == null
                                 || user.Identity == null
                                 || !user.Identity.IsAuthenticated )
                            {
                                context.Authentication.Challenge();
                                return;
                            }

                            await next();
                        } );

                app.Run( async context => await context.Response.WriteAsync( "My Message" ) );
            } );


    //Do or Bypass authenthication

    HttpResponseMessage message = await server.CreateRequest( "/" ).GetAsync();

    Assert.AreEqual("My Message", await message.Content.ReadAsStringAsync());
}

【问题讨论】:

    标签: c# .net unit-testing owin katana


    【解决方案1】:

    我认为模拟是在你的控制器中测试一部分代码。 您可以使用 mock 为用户注入虚假数据。您必须为用户提供者创建一个接口。

     public interface IUserProvider
        {
            string GetUserId();
            string GetUserName();
        }
    

    并将其注入您的基类:

     protected BaseController(IUnitOfWork data, IUserProvider userProvider)
            {
                this.data = data;
                this.userProvider = userProvider;
            }
    

    之后你可以像这样模拟 IUserProvider :

     var userMockReposioty = new Mock<IRepository<ApplicationUser>>();
                var userMockUserProvider = new Mock<IUserProvider>();
                userMockUserProvider.Setup(x => x.GetUserName())
                    .Returns("FakeUserName");
    
                userMockUserProvider.Setup(x => x.GetUserId())
                  .Returns("c52b2a96-8258-4cb0-b844-a6e443acb04b");
    
     mockUnitOfWork.Setup(x => x.Users).Returns(userMockReposioty.Object);
    

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2017-05-06
      • 2023-04-05
      • 2015-09-23
      • 2013-10-23
      • 1970-01-01
      • 1970-01-01
      • 2016-04-03
      • 2014-09-10
      • 2017-02-13
      相关资源
      最近更新 更多