public async Task IsAllowedCached()
        {
            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 tokenSource = new CancellationTokenSource();

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

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

            await auth.IsAllowedAsync(new[] { roleId }, typeof(FirstResource), permission, tokenSource.Token);

            //Assertion
            permissionStore
            .Verify(
                p => p.IsAllowedAsync(new[] { roleId }, convertor.GetResourceUniqueName(typeof(FirstResource)),
                                      convertor.GetPermissionUniqueIdentifier(permission), tokenSource.Token), Times.AtMostOnce);
        }
        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();
        }
        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();
        }
Ejemplo n.º 4
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);
        }