简体   繁体   English

如何编写单元测试以验证WCF-RIA服务DomainService查询方法需要身份验证?

[英]How should a unit test be written to verify a WCF-RIA Services DomainService query method requires authentication?

I'm doing unit testing for our WCF RIA services, which have RequiresRole or RequiresAuthentication attributes attached to them. 我正在为WCF RIA服务进行单元测试,这些服务具有RequiresRoleRequiresAuthentication属性。 I've been able to test the Update, Insert, and Delete methods to ensure the attributes are properly set. 我已经能够测试Update,Insert和Delete方法,以确保正确设置属性。 This is done by mocking a IServiceProvider , creating a DomainServiceContext with that provider and the correct DomainOperationType , adding an IPrincipal service to the service provider and then running Submit() on the service with an appropriate ChangeSet . 这是通过DomainServiceContext IServiceProvider ,使用该提供程序和正确的DomainOperationType创建DomainOperationType ,将IPrincipal服务添加到服务提供程序,然后使用适当的ChangeSet在该服务上运行Submit()来完成的。 This seems to work well. 这似乎运作良好。

However, I have been unable to test Query calls. 但是,我一直无法测试查询调用。 These are called via the Query() method on the service. 这些是通过服务上的Query()方法调用的。 So I am doing the same prep work as with the others (Creating the IServiceProvider , DomainServiceContext and IPrincipal ) and trying to create an appropriate DomainOperationEntry and QueryDescription to pass to Query() . 因此,我正在做与其他人相同的准备工作(创建IServiceProviderDomainServiceContextIPrincipal ),并尝试创建适当的DomainOperationEntryQueryDescription传递给Query() Unfortunately, I've not had any luck with this yet. 不幸的是,我还没有运气。 The relevant code is: 相关代码为:

string operationName = "GetUsers";
DomainServiceContext domainServiceContext = GetDomainServiceContext(
    authenticate: false,
    operationType: DomainOperationType.Query);
DomainOperationQuery operationQuery = mocks.DynamicMock<DomainOperationEntry>(
    typeof(UserService), operationName, DomainOperation.Query,
    typeof(IQueryable<User>), new List<DomainOperationParameter>(),
    new AttributeCollection());
mocks.ReplayAll();

service.Initialize(domainServiceContext);

int totalCount;
IEnumerable<ValidationResult> validationErrors;
QueryDescription = new QueryDescription(operationEntry);

service.Query(queryDescription, out ValidatoinErrors, out TotalCount);

This should throw an UnauthorizedAccessException , when RequiresAuthentication is set on the GetUsers query. GetUsers查询上设置RequiresAuthentication时,这应该引发UnauthorizedAccessException However, I don't get anything, regardless of whether the attribute is set. 但是,无论是否设置了属性,我什么都不会得到。 Using the debugger with a breakpoint set on the GetUsers method I can see that method is never called. 将调试器与在GetUsers方法上设置的断点一起使用,我可以看到该方法从未被调用过。 My guess is I've got the operationName wrong. 我的猜测是我的operationName错误。 But I don't know whether that's the problem, or, if it is, what I should change it to. 但是我不知道这是问题所在,还是我应该将其更改为什么。

Does anyone have any insight on this? 有人对此有见识吗? I've searched all through MSDN and done Google searches and searched here extensively. 我已经通过MSDN进行了全部搜索,并完成了Google搜索,并在这里进行了广泛的搜索。 I've got nothing so far. 到目前为止我什么都没有。

I think there are two things to do when unit testing authorization: 我认为进行单元测试授权时需要做两件事:

First, check that the right rules have been applied. 首先,检查是否已应用正确的规则。 You don't have to execute the rule for this. 您不必为此执行规则。 Reflection tells you if the right rule has been applied. 反思会告诉您是否已应用正确的规则。 That is a by-product of the fact that rules are declaratively applied. 这是规则以声明方式应用的事实的副产品。 More specifically, you'd use a higher level API above and beyond reflection - DomainServiceDescription against a DomainService type. 更具体地说,您将使用比反射更高级的API-针对DomainService类型的DomainServiceDescription。

Next, test the rule does what it is supposed to do. 接下来,测试规则是否应执行的操作。 For this create a mock implementation of IPrincipal, and an AuthorizationContext, and call the IsAuthorized method of the AuthorizationAttribute (where each attribute corresponds to a rule that you want to unit test). 为此,创建一个IPrincipal和AuthorizationContext的模拟实现,并调用AuthorizationAttribute的IsAuthorized方法(其中每个属性对应于您要进行单元测试的规则)。

Hope that helps. 希望能有所帮助。

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

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