Sco*_*den 5 authentication testing asp.net-mvc
我已经为我的域对象编写了一段时间的测试,但是我仍然不太确定如何在我的web项目中测试安全性.某些环境中的某些用户可以访问我的模型的某些属性等,但您将如何进行测试呢?现在,我基于当前经过身份验证的用户,但我将如何注入假身份验证提供程序?
这可能是一个愚蠢的问题,但如果有人能帮我摆脱测试的黑暗时代,那将非常感激.
这个链接是单向的,但使用模拟更好:
Mock<ControllerContext> MockContext(string userName)
{
var mockContext = new Mock<ControllerContext>();
// mock an authenticated user
mockContext.SetupGet(p => p.HttpContext.User.Identity.Name).Returns(userName);
mockContext.SetupGet(p => p.HttpContext.User.Identity.IsAuthenticated).Returns(true);
return mockContext;
}
[TestMethod]
public void DinnersController_Delete_Should_Fail_With_InvalidOwner_Given_Wrong_User()
{
//set by default
var mockContext = MockContext("scottha");
// mock an authenticated user
_dinnerController.ControllerContext = mockContext.Object;
ViewResult result = _dinnerController.Delete(1, "") as ViewResult;
Assert.AreEqual("InvalidOwner", result.ViewName);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
808 次 |
最近记录: |