public async Task CreateRoleAssignment_SuccessfulCreationOfAssignment_ReturnCreatedRoleAssignmentIdResult() { RoleAssignmentDTO roleAssignment = new RoleAssignmentDTO() { GroupId = 1, RoleId = 1 }; int roleAssignmentId = 1; // Arrange _privilegeRepositoryMock .Setup(r => r.GetInstanceAdminPrivilegesAsync(UserId)) .ReturnsAsync(InstanceAdminPrivileges.AccessAllProjectsAdmin); _privilegeRepositoryMock .Setup(r => r.GetProjectAdminPermissionsAsync(UserId, ProjectId)) .ReturnsAsync(ProjectAdminPrivileges.ManageGroupsAndRoles); _instanceRepositoryMock.Setup(repo => repo.CreateRoleAssignmentAsync(ProjectId, roleAssignment)) .ReturnsAsync(roleAssignmentId); // Act var result = await _controller.CreateRoleAssignment(ProjectId, roleAssignment); // Assert Assert.IsNotNull(result); Assert.AreEqual(HttpStatusCode.Created, result.StatusCode); }
public async Task UpdateRoleAssignment_InvalidRoleAssignment_ThrowsBadRequestException() { _roleAssignment = null; // Arrange _privilegeRepositoryMock .Setup(r => r.GetProjectAdminPermissionsAsync(UserId, ProjectId)) .ReturnsAsync(ProjectAdminPrivileges.ManageGroupsAndRoles); // Act var result = await _controller.UpdateRoleAssignment(ProjectId, _roleAssignmentId, _roleAssignment); // Assert }
public async Task <HttpResponseMessage> CreateRoleAssignment(int projectId, [FromBody] RoleAssignmentDTO roleAssignment) { if (roleAssignment == null) { throw new BadRequestException(ErrorMessages.ModelIsEmpty, ErrorCodes.BadRequest); } await _privilegesManager.DemandAny(Session.UserId, projectId, InstanceAdminPrivileges.AccessAllProjectsAdmin, ProjectAdminPrivileges.ManageGroupsAndRoles); RoleAssignmentValidator.ValidateModel(roleAssignment); var createdRoleAssignmentId = await _instanceRepository.CreateRoleAssignmentAsync(projectId, roleAssignment); return(Request.CreateResponse(HttpStatusCode.Created, createdRoleAssignmentId)); }
public async Task <int> CreateRoleAssignmentAsync(int projectId, RoleAssignmentDTO roleAssignment) { if (projectId < 1) { throw new ArgumentOutOfRangeException(nameof(projectId)); } if (roleAssignment == null) { throw new ArgumentOutOfRangeException(nameof(roleAssignment)); } var parameters = new DynamicParameters(); parameters.Add("@ProjectId", projectId); parameters.Add("@GroupId", roleAssignment.GroupId); parameters.Add("@RoleId", roleAssignment.RoleId); parameters.Add("@ErrorCode", dbType: DbType.Int32, direction: ParameterDirection.Output); var result = await _connectionWrapper.ExecuteScalarAsync <int>("CreateProjectRoleAssignment", parameters, commandType : CommandType.StoredProcedure); var errorCode = parameters.Get <int?>("ErrorCode"); if (errorCode.HasValue) { switch (errorCode.Value) { case (int)SqlErrorCodes.ProjectWithCurrentIdNotExist: throw new ResourceNotFoundException(ErrorMessages.ProjectNotExist, ErrorCodes.ResourceNotFound); case (int)SqlErrorCodes.GroupWithCurrentIdNotExist: throw new ResourceNotFoundException(ErrorMessages.GroupIsNotFound, ErrorCodes.ResourceNotFound); case (int)SqlErrorCodes.RolesForProjectNotExist: throw new ResourceNotFoundException(ErrorMessages.RoleIsNotFound, ErrorCodes.ResourceNotFound); case (int)SqlErrorCodes.RoleAssignmentAlreadyExists: throw new ConflictException(ErrorMessages.RoleAssignmentAlreadyExists, ErrorCodes.Conflict); } } return(result); }
public async Task CreateRoleAssignment_RoleAssignmentInvalid_ReturnBadRequestResult() { RoleAssignmentDTO roleAssignment = null; int roleAssignmentId = 1; // Arrange _privilegeRepositoryMock .Setup(r => r.GetInstanceAdminPrivilegesAsync(UserId)) .ReturnsAsync(InstanceAdminPrivileges.AccessAllProjectsAdmin); _privilegeRepositoryMock .Setup(r => r.GetProjectAdminPermissionsAsync(UserId, ProjectId)) .ReturnsAsync(ProjectAdminPrivileges.ManageGroupsAndRoles); _instanceRepositoryMock.Setup(repo => repo.CreateRoleAssignmentAsync(ProjectId, roleAssignment)) .ReturnsAsync(roleAssignmentId); // Act var result = await _controller.CreateRoleAssignment(ProjectId, roleAssignment); }
public static void ValidateModel(RoleAssignmentDTO roleAssignment, OperationMode operationMode = OperationMode.Create, int?roleAssignmentId = null) { if (roleAssignment.RoleId < 1) { throw new BadRequestException(ErrorMessages.RoleNameIsRequiredField, ErrorCodes.BadRequest); } if (roleAssignment.GroupId < 1) { throw new BadRequestException(ErrorMessages.GroupIsRequiredField, ErrorCodes.BadRequest); } if (operationMode == OperationMode.Edit) { if (roleAssignmentId.HasValue && roleAssignmentId < 1) { throw new BadRequestException(ErrorMessages.RoleAssignmentNotFound, ErrorCodes.BadRequest); } } }
public async Task CreateRoleAssignment_NoPermissions_ReturnForbiddenErrorResult() { RoleAssignmentDTO roleAssignment = new RoleAssignmentDTO() { GroupId = 1, RoleId = 1 }; int roleAssignmentId = 1; // Arrange _privilegeRepositoryMock .Setup(r => r.GetInstanceAdminPrivilegesAsync(UserId)) .ReturnsAsync(InstanceAdminPrivileges.ViewProjects); _privilegeRepositoryMock .Setup(r => r.GetProjectAdminPermissionsAsync(UserId, ProjectId)) .ReturnsAsync(ProjectAdminPrivileges.ViewGroupsAndRoles); _instanceRepositoryMock.Setup(repo => repo.CreateRoleAssignmentAsync(ProjectId, roleAssignment)) .ReturnsAsync(roleAssignmentId); // Act var result = await _controller.CreateRoleAssignment(ProjectId, roleAssignment); }
public void Initialize() { _instanceRepositoryMock = new Mock <IInstanceRepository>(); _logRepositoryMock = new Mock <IServiceLogRepository>(); _artifactPermissionsRepositoryMock = new Mock <IArtifactPermissionsRepository>(); _privilegeRepositoryMock = new Mock <IPrivilegesRepository>(); _instanceServiceMock = new Mock <IInstanceService>(); var request = new HttpRequestMessage(); request.Properties[ServiceConstants.SessionProperty] = new Session { UserId = UserId }; _controller = new InstanceController ( _instanceRepositoryMock.Object, _logRepositoryMock.Object, _artifactPermissionsRepositoryMock.Object, _privilegeRepositoryMock.Object, _instanceServiceMock.Object) { Request = request, Configuration = new HttpConfiguration() }; _folder = new FolderDto { Name = "Folder1", ParentFolderId = 2 }; _project = new ProjectDto { Name = "Project1", Description = "Project1Description", ParentFolderId = 1 }; _pagination = new Pagination { Limit = 1, Offset = 0 }; _sorting = new Sorting { Order = SortOrder.Asc, Sort = "Name" }; var projectRolesAssignments = new List <RoleAssignment> { new RoleAssignment { GroupName = "GroupName", Id = 1, RoleName = "RoleName" } }; _rolesAssignmentsQueryResult = new RoleAssignmentQueryResult <RoleAssignment> { Items = projectRolesAssignments, Total = 1, ProjectName = "Project1" }; _instanceRepositoryMock .Setup(repo => repo.GetProjectRoleAssignmentsAsync(It.IsAny <int>(), It.IsAny <TabularData>(), It.IsAny <Func <Sorting, string> >())) .ReturnsAsync(_rolesAssignmentsQueryResult); _privilegeRepositoryMock .Setup(r => r.GetInstanceAdminPrivilegesAsync(UserId)) .ReturnsAsync(InstanceAdminPrivileges.AccessAllProjectsAdmin); _roleAssignment = new RoleAssignmentDTO() { GroupId = 1, RoleId = 1 }; _roleAssignmentId = 1; }