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

发布于 2024-09-05 23:08:06 字数 1690 浏览 1 评论 0原文

我正在对 WCF RIA 服务进行单元测试,这些服务附加了 RequiresRole 或 RequiresAuthentication 属性。我已经能够测试更新、插入和删除方法以确保正确设置属性。这是通过模拟 IServiceProvider、使用该提供程序和正确的 DomainOperationType 创建 DomainServiceContext、添加 IPrincipal 来完成的向服务提供者提供服务,然后使用适当的 ChangeSet 在服务上运行 Submit()。这似乎运作良好。

但是,我无法测试查询调用。这些是通过服务上的 Query() 方法调用的。因此,我正在做与其他人相同的准备工作(创建 IServiceProviderDomainServiceContextIPrincipal),并尝试创建适当的 DomainOperationEntryQueryDescription 传递给 Query()。不幸的是,我还没有在这方面有任何运气。相关代码是:

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);

当在 GetUsers 查询上设置 RequiresAuthentication 时,这应该抛出 UnauthorizedAccessException。但是,无论是否设置了该属性,我都没有得到任何信息。使用在 GetUsers 方法上设置断点的调试器,我可以看到该方法从未被调用。我的猜测是我的 operationName 错误。但我不知道这是否是问题所在,或者如果是,我应该将其更改为什么。

有人对此有任何见解吗?我已经通过 MSDN 进行了搜索,并进行了 Google 搜索,并在这里进行了广泛的搜索。到目前为止我什么都没有。

I'm doing unit testing for our WCF RIA services, which have RequiresRole or RequiresAuthentication attributes attached to them. I've been able to test the Update, Insert, and Delete methods to ensure the attributes are properly set. 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. This seems to work well.

However, I have been unable to test Query calls. These are called via the Query() method on the service. 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(). 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. 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. My guess is I've got the operationName wrong. 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. I've got nothing so far.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

榕城若虚 2024-09-12 23:08:06

我认为单元测试授权时需要做两件事:

首先,检查是否应用了正确的规则。您不必为此执行规则。反思会告诉您是否应用了正确的规则。这是以声明方式应用规则这一事实的副产品。更具体地说,您可以使用反射之外的更高级别的 API - 针对 DomainService 类型的 DomainServiceDescription。

接下来,测试规则是否执行其预期的操作。为此,创建 IPrincipal 的模拟实现和 AuthorizationContext,并调用 AuthorizationAttribute 的 IsAuthorized 方法(其中每个属性对应于您想要进行单元测试的规则)。

希望有帮助。

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.

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).

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文