Beispiel #1
0
        public async Task TestCheckAndSaveNewRole_NameAlreadyTaken()
        {
            string roleName = "New Role 1 " + DateTime.Now.Ticks;
            var    permRepo = DatabaseFixture.Storage.GetRepository <IRolePermissionRepository>();

            try
            {
                // Arrange
                SaveNewRoleAndGrantsViewModel model = new SaveNewRoleAndGrantsViewModel
                {
                    // Really unique value
                    RoleName   = roleName,
                    Extensions = new System.Collections.Generic.List <string> {
                        "Security"
                    },
                    Permission = Security.Common.Enums.Permission.Write.ToString()
                };

                // Execute
                var result = await CreateRoleAndGrants.CheckAndSaveNewRoleAndGrants(model, DatabaseFixture.RoleManager, DatabaseFixture.Storage);

                // Assert
                Assert.Null(result);

                // Read back and expect to find the Role record for the new role
                var createdRole = await DatabaseFixture.RoleManager.FindByNameAsync(model.RoleName);

                Assert.NotNull(createdRole);

                result = await CreateRoleAndGrants.CheckAndSaveNewRoleAndGrants(model, DatabaseFixture.RoleManager, DatabaseFixture.Storage);

                Assert.NotNull(result);
                Assert.Equal("A role with this name already exists", result);
            }
            finally
            {
                // Cleanup created data
                var createdRole = await DatabaseFixture.RoleManager.FindByNameAsync(roleName);

                Assert.NotNull(createdRole);
                foreach (var rolePermission in permRepo.FilteredByRoleId(createdRole.Id))
                {
                    permRepo.Delete(rolePermission.RoleId, rolePermission.Scope);
                }

                await DatabaseFixture.RoleManager.DeleteAsync(createdRole);
            }
        }
Beispiel #2
0
        public async Task TestCheckAndSaveNewRole_Ok(string extensionNames_)
        {
            string roleName = "New Role 1 " + DateTime.Now.Ticks;
            var    permRepo = DatabaseFixture.Storage.GetRepository <IRolePermissionRepository>();

            try
            {
                // Arrange
                string[] extensions = extensionNames_.Split(',');
                SaveNewRoleAndGrantsViewModel model = new SaveNewRoleAndGrantsViewModel
                {
                    // Really unique value
                    RoleName   = roleName,
                    Extensions = new System.Collections.Generic.List <string> (extensions),
                    Permission = Security.Common.Enums.Permission.Write.ToString()
                };

                // Execute
                var result = await CreateRoleAndGrants.CheckAndSaveNewRoleAndGrants(model, DatabaseFixture.RoleManager, DatabaseFixture.Storage);

                // Assert
                Assert.Null(result);

                // Read back and assert that we have the expected data
                // 1. Expect to find the Role record for the new role
                var createdRole = await DatabaseFixture.RoleManager.FindByNameAsync(model.RoleName);

                Assert.NotNull(createdRole);

                // 2. Expect to have an expected number of records in RolePermission table for the new role
                var rolePermissionRecords = permRepo.FilteredByRoleId(createdRole.Id);
                Assert.Equal(extensions.Count(), rolePermissionRecords.Count());
            }
            finally
            {
                // Cleanup created data
                var createdRole = await DatabaseFixture.RoleManager.FindByNameAsync(roleName);

                Assert.NotNull(createdRole);
                foreach (var rolePermission in permRepo.FilteredByRoleId(createdRole.Id))
                {
                    permRepo.Delete(rolePermission.RoleId, rolePermission.Scope);
                }

                await DatabaseFixture.RoleManager.DeleteAsync(createdRole);
            }
        }
Beispiel #3
0
        public async Task <ObjectResult> SaveNewRoleAndItsPermissions(SaveNewRoleAndGrantsViewModel model_)
        {
            string error = await CreateRoleAndGrants.CheckAndSaveNewRoleAndGrants(model_, _roleManager, Storage);

            return(StatusCode(string.IsNullOrEmpty(error) ? 201 : 400, error));
        }