我有一个WCF服务,它启用了UserName身份验证和PrincipalPermission Authorization。
如何对该方法进行单元测试。
服务:
[PrincipalPermission(SecurityAction.Demand, Role = "Admin")]
public void UploadEmployees(CustomerRequest request)
{
try
{
ProcessEmployees(request.PacketId, request.Employees);
}
catch (Exception ex)
{
throw new FaultException<CustomerException>(new CustomerException { Status = -1, ErrorMessage = ex.Message });
}
}
My Nunit测试方法:
[Test]
public void CallProcessEmployee_Should_Work()
{
var service=new CustomerService();
var request = new CustomerRequest();
service.UploadEmployees(request);
}
当我尝试执行单元测试时,我得到的错误是:
System.Security.SecurityException : Request for principal permission failed.
如何在启用了身份验证和授权的类上进行单元测试。?
我通过创建一个"setup"方法并将主体设置为当前线程来解决这个问题,如下所示:
[SetUp]
public void SetupUnitTestPrinciple()
{
var identity = new GenericIdentity("Unitest");
System.Threading.Thread.CurrentPrincipal = new CustomPrincipal(identity);
}