且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

单元测试,如何设置和Thread.CurrentPrincipal中IsAuthenticated

更新时间:2023-12-03 18:42:10

我会换用Thread.CurrentPrincipal中一类叫然后提取接口。这种方法可以让我通过我的虚拟实现,作为一个依赖



另一种方法是准备这样的静态类:

 公共静态类ApplicationPrincipal 
{
私有静态Func键<&的IPrincipal GT; _current =()=> Thread.CurrentPrincipal中;


公共静态的IPrincipal当前
{
{返回_current(); }
}

公共静态无效SwitchCurrentPrincipal(Func键<&的IPrincipal GT;主)
{
_current =本金;
}

}



然后,你必须使用ApplicationPrincipal。当前在你的仓库,而不是直接调用。在测试中,你将能够通过调用ApplicationPrincipal.SwitchCurrentPrincipal切换默认的逻辑(()=> myPrincipal)。



编辑:



我会改变:

 公共BOOL IsUserAuthenticated 
{
{返回((UserPrincipal)校长).Identity.IsAuthenticated; }
}

使用:

 公共BOOL IsUserAuthenticated 
{
{返回ApplicationPrincipal.Current.Identity.IsAuthenticated; }
}



然后在安排的测试,我会改变默认的IPrincipal与部分:

  VAR principalMock =新UserPrincipal(isAuthenticated:真); 

ApplicationPrincipal.SwitchCurrentPrincipal(()=> principalMock);



我假设你能以某种方式重载IsAuthenticated的UserPrincipal对象的值。


I am trying to perform a unit test of an n-tier application, service layer, repository layer and web api controllers. My repositories are checking the Thread.CurrentPrincipal object to get the current user. This is where I have a problem, when I create the implementation class that inherits from IPrincipal it does allow me to set the IsUserAuthenticated. Is there a way to simulate this for my unit tests. I would like to set the Thread.CurrentPrincipal to my implementation object. How would I simulate a user in this way?

In my RepositoryBase code, I call the following to determine if user is authenticated:

public bool IsUserAuthenticated
{
   get { return ((UserPrincipal)Principal).Identity.IsAuthenticated; }
}

The actual test looks something like:
Contract.Requires<UserAccessException>(IsUserAuthenticated, "Illegal Access.");




I am pulling IsUserAuthenticated from the UserPrincipal below:

namespace HeyLetsTrain.Toolkit.Helper.Authentication
{
public class UserPrincipal : IPrincipal
{
    IIdentity _identity;
    string[] _role;
    string _emailAddress;


    public UserPrincipal(string name, string[] role)
    {
        if (role != null) 
        {
            _role = new string[role.Length];
            _role.CopyTo(role, 0);
            Array.Sort(_role);
        }
        _identity = new GenericIdentity(name);
    }

    public IIdentity Identity
    {
        get { return _identity; }
    }

    public string EmailAddress
    {
        get { return _emailAddress; }
        set { this._emailAddress = value; }
    }

    public bool IsInRole(string role)
    {
        return Array.BinarySearch(_role, role) >= 0 ? true : false;
    }

    public bool IsInAllRoles( params string [] roles )
    {
      foreach (string searchrole in roles )
      {
        if (Array.BinarySearch(_role, searchrole) < 0 )
          return false;
      }
      return true;
    }

    public bool IsInAnyRoles( params string [] roles )
    {
      foreach (string searchrole in roles )
      {
        if (Array.BinarySearch(_role, searchrole ) > 0 )
          return true;
      }
      return false;
    }
}
}

I would wrap Thread.CurrentPrincipal call with a class and then extract the interface. This approach would allow me to pass my dummy implementation as a dependency.

Another approach is to prepare static class like this:

public static class ApplicationPrincipal
{
  private static Func<IPrincipal> _current = () => Thread.CurrentPrincipal;


  public static IPrincipal Current
  {
      get { return _current(); }
  }

  public static void SwitchCurrentPrincipal(Func<IPrincipal> principal)
  {
      _current = principal;
  }

}

Then you have to use ApplicationPrincipal.Current in your repositories instead of direct call. In your tests you will be able to switch default logic by calling ApplicationPrincipal.SwitchCurrentPrincipal(() => myPrincipal).

Edit:

I would change:

public bool IsUserAuthenticated
{
   get { return ((UserPrincipal)Principal).Identity.IsAuthenticated; }
}

With:

public bool IsUserAuthenticated
{
   get { return ApplicationPrincipal.Current.Identity.IsAuthenticated; }
}

And then in arrange section of the test I would change default IPrincipal with:

var principalMock = new UserPrincipal(isAuthenticated: true);

ApplicationPrincipal.SwitchCurrentPrincipal(() => principalMock);

I assume you can somehow overload the value of IsAuthenticated in UserPrincipal object.