Esempio n. 1
0
        public async Task ValidateThrowsWithNull()
        {
            // Setup
            var validator = new RoleValidator<TestRole>();
            var manager = MockHelpers.TestRoleManager<TestRole>();

            // Act
            // Assert
            await Assert.ThrowsAsync<ArgumentNullException>("manager", async () => await validator.ValidateAsync(null, null));
            await Assert.ThrowsAsync<ArgumentNullException>("role", async () => await validator.ValidateAsync(manager, null));
        }
Esempio n. 2
0
    public async Task ValidateThrowsWithNull()
    {
        // Setup
        var validator = new RoleValidator <PocoRole>();
        var manager   = MockHelpers.TestRoleManager <PocoRole>();

        // Act
        // Assert
        await Assert.ThrowsAsync <ArgumentNullException>("manager", async() => await validator.ValidateAsync(null, null));

        await Assert.ThrowsAsync <ArgumentNullException>("role", async() => await validator.ValidateAsync(manager, null));
    }
        /// <summary>
        /// Update an existing role.
        /// </summary>
        /// <param name="role">The role.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentException">Roles cannot be assigned a new hostId.</exception>
        /// <exception cref="System.ArgumentException">Roles cannot be assigned a new hostId.</exception>
        public override async Task <IdentityResult> UpdateAsync(TRole role)
        {
            //x Helpers.ThrowIfNull(role != null, "role");

            ThrowIfDisposed();

            var existing = await FindByIdAsync(role.Id);

            if (existing != null && !role.HostId.Equals(existing.HostId))
            {
                throw new ArgumentException("Roles cannot be assigned a new hostId.");
            }

            if (role.IsGlobal && !role.HostId.Equals(this.SystemHostId))
            {
                throw new ArgumentException("Global roles must belong to system host.");
            }

            var result = await RoleValidator.ValidateAsync(role).WithCurrentCulture();

            if (!result.Succeeded)
            {
                return(result);
            }

            await HyperRoleStore.UpdateAsync(role).WithCurrentCulture();

            return(IdentityResult.Success);
        }
        /// <summary>
        /// Creates a role for the host specified in <c>role.HostId</c> or the current host if unspecified.
        /// </summary>
        /// <param name="role">The role.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentException">Global roles must belong to system host.</exception>
        public override async Task <IdentityResult> CreateAsync(TRole role)
        {
            //x Helpers.ThrowIfNull(role != null, "role");

            ThrowIfDisposed();

            if (role.HostId.Equals(default(TKey)))
            {
                role.HostId = this.CurrentHostId;
            }

            if (role.IsGlobal && !role.HostId.Equals(this.SystemHostId))
            {
                throw new ArgumentException("Global roles must belong to system host.");
            }

            var result = await RoleValidator.ValidateAsync(role).WithCurrentCulture();

            if (!result.Succeeded)
            {
                return(result);
            }

            await HyperRoleStore.CreateAsync(role).WithCurrentCulture();

            return(IdentityResult.Success);
        }
Esempio n. 5
0
        public async Task ValidateFailsWithTooShortRoleName(string input)
        {
            // Setup
            var validator = new RoleValidator<TestRole>();
            var manager = MockHelpers.TestRoleManager<TestRole>();
            var user = new TestRole {Name = input};

            // Act
            var result = await validator.ValidateAsync(manager, user);

            // Assert
            IdentityResultAssert.IsFailure(result, IdentityErrorDescriber.Default.InvalidRoleName(input));
        }
Esempio n. 6
0
        public async Task ValidateFailsWithTooShortRoleName(string input)
        {
            // Setup
            var validator = new RoleValidator<TestRole>();
            var manager = new RoleManager<TestRole>(new NoopRoleStore(), validator);
            var user = new TestRole {Name = input};

            // Act
            var result = await validator.ValidateAsync(manager, user);

            // Assert
            IdentityResultAssert.IsFailure(result, "Name cannot be null or empty.");
        }
Esempio n. 7
0
        public async Task ValidateFailsWithTooShortRoleName(string input)
        {
            // Setup
            var validator = new RoleValidator <PocoRole>();
            var manager   = MockHelpers.TestRoleManager <PocoRole>();
            var user      = new PocoRole {
                Name = input
            };

            // Act
            var result = await validator.ValidateAsync(manager, user);

            // Assert
            IdentityResultAssert.IsFailure(result, new IdentityErrorDescriber().InvalidRoleName(input));
        }
Esempio n. 8
0
        public async Task ValidateFailsWithTooShortRoleName(string input)
        {
            // Setup
            var validator = new RoleValidator <TestRole>();
            var manager   = new RoleManager <TestRole>(new NoopRoleStore(), null);
            var user      = new TestRole {
                Name = input
            };

            // Act
            var result = await validator.ValidateAsync(manager, user);

            // Assert
            IdentityResultAssert.IsFailure(result, "Name cannot be null or empty.");
        }
Esempio n. 9
0
        public async Task ValidateFailsWithTooShortRoleName(string input)
        {
            // Setup
            var validator = new RoleValidator <TestRole>();
            var manager   = new RoleManager <TestRole>(new NoopRoleStore());
            var user      = new TestRole {
                Name = input
            };

            // Act
            var result = await validator.ValidateAsync(manager, user);

            // Assert
            IdentityResultAssert.IsFailure(result, IdentityErrorDescriber.Default.InvalidRoleName(input));
        }