public void grant() { DependencyContext.Using<IPermissionService, IRequestContext>((c, service, requestContext) => { var stack = new Stack<HashSet<string>>(); var hashSet = new HashSet<string>(); hashSet.Add("test"); stack.Push(hashSet); var requestItemsNotEmpty = new Dictionary<string, Stack<HashSet<string>>> { {"GrantingStack", stack} }; A.CallTo(() => c.Resolver.Resolve<IRequestContext>()).Returns(requestContext); var tgPermissionService = new TransientGrantingPermissionService(service); Assert.Throws<ArgumentNullException>(() => tgPermissionService.Grant(null)); Assert.Throws<ArgumentNullException>(() => tgPermissionService.Grant(new string[] { })); A.CallTo(() => requestContext.Items).Returns(null); Assert.False(tgPermissionService.HasPermission("Grant")); tgPermissionService.Grant("Grant"); Assert.True(tgPermissionService.HasPermission("Grant")); A.CallTo(() => requestContext.Items).Returns(requestItemsNotEmpty); tgPermissionService.Grant("Grant2"); Assert.True(tgPermissionService.HasPermission("Grant2")); tgPermissionService.GrantAll(); tgPermissionService.Grant("Grant3"); Assert.True(tgPermissionService.HasPermission("Grant3")); }); }
public void HasPermissionTest() { DependencyContext.Using<IPermissionService, IRequestContext>((c, service, requestContext) => { var stack = new Stack<HashSet<string>>(); var hashSet = new HashSet<string>(); hashSet.Add("test"); stack.Push(hashSet); var requestItemsNotEmpty = new Dictionary<string, Stack<HashSet<string>>> { {"GrantingStack", stack} }; var requestItemsEmpty = new Dictionary<string, Stack<HashSet<string>>> { {"GrantingStack", new Stack<HashSet<string>>()} }; A.CallTo(() => c.Resolver.Resolve<IRequestContext>()).Returns(requestContext); var tgPermissionService = new TransientGrantingPermissionService(service); A.CallTo(() => requestContext.Items).Returns(null); A.CallTo(() => service.HasPermission(A<string>.Ignored)).Returns(false); Assert.False(tgPermissionService.HasPermission("test")); //fake service return false granting stack is null A.CallTo(() => requestContext.Items).Returns(requestItemsEmpty); Assert.False(tgPermissionService.HasPermission("test")); //fake service return false granting stack count less than 1 A.CallTo(() => service.HasPermission(A<string>.Ignored)).Returns(true); Assert.True(tgPermissionService.HasPermission("test")); //fake service return true granting stack count less than 1 A.CallTo(() => requestContext.Items).Returns(requestItemsNotEmpty); Assert.True(tgPermissionService.HasPermission("test")); //granting stack is not null and count much than 0 service doesnt work A.CallTo(() => requestContext.Items).Returns(null); A.CallTo(() => service.HasPermission(A<string>.Ignored)).Returns(false); Assert.False(tgPermissionService.HasPermission("test")); //fake service return false ensuring about service state A.CallTo(() => requestContext.Items).Returns(requestItemsNotEmpty); tgPermissionService.GrantAll(); Assert.True(tgPermissionService.HasPermission("Admin")); //grant all tgPermissionService.UndoGrant(); Assert.False(tgPermissionService.HasPermission("Admin")); //dont grant tgPermissionService.UndoGrant(); Assert.Throws<InvalidOperationException>(() => tgPermissionService.UndoGrant()); //from granting stack doesnt empty A.CallTo(() => requestContext.Items).Returns(requestItemsEmpty); Assert.Throws<InvalidOperationException>(() => tgPermissionService.UndoGrant()); //from granting stack empty Assert.Throws<ArgumentNullException>(() => tgPermissionService.Grant(null)); Assert.Throws<ArgumentNullException>(() => tgPermissionService.Grant(new string[] { })); }); }