コード例 #1
0
ファイル: CreateRoleTest.cs プロジェクト: next-pixel/Base
        public async Task CheckAndSaveNewRole_Error_NameAlreadyTakenAsync()
        {
            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"
                    },
                    PermissionValue = Permission.Write.ToString()
                };

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

                // 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.CheckAndSaveNewRoleAndGrantsAsync(DatabaseFixture.Storage, DatabaseFixture.RoleManager, model);

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

                if (createdRole != null)
                {
                    foreach (var rolePermission in permRepo.FilteredByRoleId(createdRole.Id))
                    {
                        permRepo.Delete(rolePermission.RoleId, rolePermission.Extension);
                    }

                    await DatabaseFixture.RoleManager.DeleteAsync(createdRole);
                }
            }
        }
コード例 #2
0
ファイル: CreateRoleTest.cs プロジェクト: next-pixel/Base
        public async Task CheckAndSaveNewRole_OkAsync(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),
                    PermissionValue = Permission.Write.ToString()
                };

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

                // 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);

                if (createdRole != null)
                {
                    foreach (var rolePermission in permRepo.FilteredByRoleId(createdRole.Id))
                    {
                        permRepo.Delete(rolePermission.RoleId, rolePermission.Extension);
                    }

                    await DatabaseFixture.RoleManager.DeleteAsync(createdRole);
                }
            }
        }
コード例 #3
0
ファイル: CreateRoleTest.cs プロジェクト: next-pixel/Base
        public async Task CheckAndSaveNewRole_Error_NoExtensionSelectedAsync()
        {
            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      = null,
                    PermissionValue = Permission.Write.ToString()
                };

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

                // Assert
                Assert.NotNull(result);
                Assert.Equal("At least one extension must be selected", result);
            }
            finally
            {
                // Cleanup created data
                var createdRole = await DatabaseFixture.RoleManager.FindByNameAsync(roleName);

                if (createdRole != null)
                {
                    foreach (var rolePermission in permRepo.FilteredByRoleId(createdRole.Id))
                    {
                        permRepo.Delete(rolePermission.RoleId, rolePermission.Extension);
                    }

                    await DatabaseFixture.RoleManager.DeleteAsync(createdRole);
                }
            }
        }
コード例 #4
0
        public async Task <IActionResult> SaveNewRoleAndItsPermissionsAsync([FromBody] SaveNewRoleAndGrantsViewModel model_)
        {
            string error = await CreateRoleAndGrants.CheckAndSaveNewRoleAndGrantsAsync(Storage, _roleManager, model_);

            return(StatusCode(string.IsNullOrEmpty(error) ? (int)HttpStatusCode.Created : (int)HttpStatusCode.BadRequest, error));
        }