public void GetAllowedKeys_ShouldPass()
        {
            const int roleId     = 1;
            var       permission = FirstPermissions.Edit;

            //Init
            var permissionStore = new Mock <IPermissionStore <int> >();
            var registry        = GetRegistry();
            var convertor       = new DefaultNamingConvertor(registry);
            var auth            = CreateManager(registry, convertor, permissionStore);
            var allowed         = new List <int> {
                1, 3, 5
            };
            var tokenSource = new CancellationTokenSource();

            //Pre-Condition
            permissionStore
            .Setup(p => p.GetAllowedResourceIdsAsync(new[] { roleId }, convertor.GetResourceUniqueName(typeof(FirstResource)),
                                                     convertor.GetPermissionUniqueIdentifier(permission), tokenSource.Token))
            .Returns(Task.FromResult(allowed.AsEnumerable()));

            //Act
            var allowedResult = auth.GetAllowedKeysAsync(new[] { roleId }, typeof(FirstResource), permission, tokenSource.Token).Result;

            Assert.AreEqual(allowed, allowedResult);

            //Assertion
            permissionStore.VerifyAll();
        }
        public void ResourceTypeAndIdPermission_ShouldPass()
        {
            const int roleId     = 1;
            const int resourceId = 1;
            var       permission = FirstPermissions.Edit;

            //Init
            var registry        = GetRegistry();
            var convertor       = new DefaultNamingConvertor(registry);
            var permissionStore = new Mock <IPermissionStore <int> >();
            var auth            = CreateManager(registry, convertor, permissionStore);

            var tokenSource = new CancellationTokenSource();

            //Pre-Condition
            permissionStore
            .Setup(p => p.IsAllowedAsync(new[] { roleId }, convertor.GetResourceUniqueName(typeof(FirstResource)), resourceId,
                                         convertor.GetPermissionUniqueIdentifier(permission), tokenSource.Token))
            .Returns(Task.FromResult((bool?)true));

            //Act
            Assert.IsTrue(auth.IsAllowedAsync(new[] { roleId }, typeof(FirstResource), resourceId, permission, tokenSource.Token).Result);

            //Assertion
            permissionStore.VerifyAll();
        }
        private void GetAllPermissionsForInstanceResource_LoadAllPermission(bool isAllowed)
        {
            var roleId       = 5;
            var resourceType = typeof(FirstResource);
            var resourceName = typeof(FirstResource).FullName;
            var resourceId   = 1;

            var resourceRegistry = new ResourceRegistry();

            resourceRegistry.Register(resourceType.Assembly);
            var tokenSource = new CancellationTokenSource();

            var namingConvertor = new DefaultNamingConvertor(resourceRegistry);

            var permissionStoreMock = new Mock <IPermissionStore <int> >();

            permissionStoreMock
            .Setup(p => p.IsAllowedAsync(roleId, resourceName, resourceId, namingConvertor.GetPermissionUniqueIdentifier(FirstPermissions.Edit), tokenSource.Token))
            .Returns(Task.FromResult((bool?)isAllowed));

            //Act
            var manager     = CreateManager(resourceRegistry, namingConvertor, permissionStoreMock);
            var permissions = manager.GetAllPermissionsAsync(roleId, resourceName, resourceId, tokenSource.Token).Result.ToList();

            Assert.AreEqual(2, permissions.Count());

            var edit = permissions.First();

            Assert.AreEqual(namingConvertor.GetPermissionUniqueIdentifier(FirstPermissions.Edit), edit.UniqueIdentifier);
            Assert.IsFalse(string.IsNullOrWhiteSpace(edit.Name));
            Assert.IsFalse(string.IsNullOrWhiteSpace(edit.Description));
            Assert.AreEqual(isAllowed, edit.IsAllowed);
        }
예제 #4
0
        public void GetResourceTypeByUniqueName_ThrowExceptinIfTypeCanNotBeFound()
        {
            var resourceRegistry = new ResourceRegistry();
            var convertor        = new DefaultNamingConvertor(resourceRegistry);

            convertor.GetResourceTypeByUniqueName(typeof(FirstResource).FullName);
        }
        public void Allow_PermissionIsNotAllowedForResource_ShouldFail()
        {
            //Init
            var resourceRegistry = GetRegistry();
            var namigConvertor   = new DefaultNamingConvertor(resourceRegistry);
            var manager          = CreateManager(resourceRegistry);

            //Act
            manager.SetPermission(1, typeof(FirstResource).FullName, default(int), namigConvertor.GetPermissionUniqueIdentifier(SecondPermissions.Edit), true);
        }
        private PermissionManager <int> CreateManager(
            ResourceRegistry resourceRegistry            = null,
            DefaultNamingConvertor namigConvertor        = null,
            Mock <IPermissionStore <int> > permStore     = null,
            Mock <IResourceInstanceProvider> resProvider = null)
        {
            var registry        = resourceRegistry ?? GetRegistry();
            var permissionStore = permStore ?? new Mock <IPermissionStore <int> >();
            var resourceInstanceQueryFactory = resProvider ?? new Mock <IResourceInstanceProvider>();
            var convertor = namigConvertor ?? new DefaultNamingConvertor(registry);

            return(new PermissionManager <int>(permissionStore.Object, registry, resourceInstanceQueryFactory.Object, convertor));
        }
예제 #7
0
        public void ResourceConversion()
        {
            var resourceType = typeof(FirstResource);

            var resourceRegistry = new ResourceRegistry();

            resourceRegistry.Register(resourceType.Assembly);

            var convertor = new DefaultNamingConvertor(resourceRegistry);

            var resourceName = convertor.GetResourceUniqueName(resourceType);

            Assert.IsFalse(string.IsNullOrWhiteSpace(resourceName));

            var resourceType2 = convertor.GetResourceTypeByUniqueName(resourceName);

            Assert.AreEqual(resourceType, resourceType2);
        }