public async Task GetProjectChildrenAsync_Success()
        {
            // Arrange
            var userId  = 1;
            var session = new Session {
                UserId = userId
            };
            var projectId = 10;
            var children  = new List <Artifact>();
            var mockArtifactRepository = new Mock <IArtifactRepository>();

            mockArtifactRepository.Setup(r => r.GetProjectOrArtifactChildrenAsync(projectId, null, userId)).ReturnsAsync(children);
            var mockArtifactPermissionsRepository = new Mock <IArtifactPermissionsRepository>();
            var mockServiceLogRepository          = new Mock <IServiceLogRepository>();
            var artifactController = new ArtifactController(mockArtifactRepository.Object, mockArtifactPermissionsRepository.Object, _mockSqlPrivilegesRepository.Object, mockServiceLogRepository.Object)
            {
                Request = new HttpRequestMessage()
            };

            artifactController.Request.Properties[ServiceConstants.SessionProperty] = session;

            // Act
            var result = await artifactController.GetProjectChildrenAsync(projectId);

            // Assert
            Assert.AreSame(children, result);
        }
        public async Task GetArtifactNavigationPathAsync_Success()
        {
            // Arrange
            const int userId  = 1;
            var       session = new Session {
                UserId = userId
            };
            const int artifactId             = 10;
            var       navPath                = new List <Artifact>();
            var       mockArtifactRepository = new Mock <IArtifactRepository>();

            mockArtifactRepository.Setup(r => r.GetArtifactNavigationPathAsync(artifactId, userId)).ReturnsAsync(navPath);
            var mockArtifactPermissionsRepository = new Mock <IArtifactPermissionsRepository>();
            var mockServiceLogRepository          = new Mock <IServiceLogRepository>();
            var artifactController = new ArtifactController(mockArtifactRepository.Object, mockArtifactPermissionsRepository.Object, _mockSqlPrivilegesRepository.Object, mockServiceLogRepository.Object)
            {
                Request = new HttpRequestMessage()
            };

            artifactController.Request.Properties[ServiceConstants.SessionProperty] = session;

            // Act
            var result = await artifactController.GetArtifactNavigationPathAsync(artifactId);

            // Assert
            Assert.AreSame(navPath, result);
        }
        public void Initialize()
        {
            session = new Session {
                UserId = userId
            };
            artifactIds = new HashSet <int>()
            {
                1, 2, 3
            };
            processInfo = new List <ProcessInfoDto>()
            {
                new ProcessInfoDto()
                {
                    ItemId      = 1,
                    ProcessType = ProcessType.None
                },
                new ProcessInfoDto()
                {
                    ItemId      = 2,
                    ProcessType = ProcessType.None
                },
                new ProcessInfoDto()
                {
                    ItemId      = 3,
                    ProcessType = ProcessType.None
                }
            };

            filter = StandardArtifactTypes.All;

            mockArtifactRepository            = new Mock <IArtifactRepository>();
            mockServiceLogRepository          = new Mock <IServiceLogRepository>();
            mockArtifactPermissionsRepository = new Mock <IArtifactPermissionsRepository>();
            _mockSqlPrivilegesRepository      = new Mock <IPrivilegesRepository>();
            artifactController = new ArtifactController(mockArtifactRepository.Object,
                                                        mockArtifactPermissionsRepository.Object, _mockSqlPrivilegesRepository.Object, mockServiceLogRepository.Object)
            {
                Request = new HttpRequestMessage()
            };

            artifactController.Request.Properties[ServiceConstants.SessionProperty] = session;
        }
        public async Task GetSubArtifactTreeAsync_HasPermissions_OK()
        {
            // Arrange
            const int userId  = 1;
            var       session = new Session {
                UserId = userId
            };
            const int artifactId            = 2;
            var       artifactIds           = new[] { artifactId };
            var       permissionsDictionary = new Dictionary <int, RolePermissions>();

            permissionsDictionary.Add(artifactId, RolePermissions.Read);
            var subArtifacts = new List <SubArtifact> {
                new SubArtifact {
                    Id = 1
                }
            };
            var mockArtifactRepository = new Mock <IArtifactRepository>();

            mockArtifactRepository.Setup(r => r.GetSubArtifactTreeAsync(artifactId, userId, int.MaxValue, true)).ReturnsAsync(subArtifacts);
            var mockArtifactPermissionsRepository = new Mock <IArtifactPermissionsRepository>();

            mockArtifactPermissionsRepository.Setup(r => r.GetArtifactPermissions(artifactIds, userId, false, int.MaxValue, true, null)).ReturnsAsync(permissionsDictionary);
            var mockServiceLogRepository = new Mock <IServiceLogRepository>();

            var artifactController = new ArtifactController(mockArtifactRepository.Object, mockArtifactPermissionsRepository.Object, _mockSqlPrivilegesRepository.Object, mockServiceLogRepository.Object)
            {
                Request = new HttpRequestMessage()
            };

            artifactController.Request.Properties[ServiceConstants.SessionProperty] = session;

            // Act
            var result = await artifactController.GetSubArtifactTreeAsync(artifactId);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result[0].Id);
        }
        public async Task GetSubArtifactTreeAsync_NoPermissions_ThrowsNoPermissionsException()
        {
            // Arrange
            const int userId  = 1;
            var       session = new Session {
                UserId = userId
            };
            const int artifactId            = 2;
            var       artifactIds           = new[] { artifactId };
            var       permissionsDictionary = new Dictionary <int, RolePermissions>();

            permissionsDictionary.Add(artifactId, RolePermissions.None);
            var mockArtifactRepository            = new Mock <IArtifactRepository>();
            var mockArtifactPermissionsRepository = new Mock <IArtifactPermissionsRepository>();

            mockArtifactPermissionsRepository.Setup(r => r.GetArtifactPermissions(artifactIds, userId, false, int.MaxValue, true, null)).ReturnsAsync(permissionsDictionary);
            var mockServiceLogRepository = new Mock <IServiceLogRepository>();

            var artifactController = new ArtifactController(mockArtifactRepository.Object, mockArtifactPermissionsRepository.Object, _mockSqlPrivilegesRepository.Object, mockServiceLogRepository.Object)
            {
                Request = new HttpRequestMessage()
            };

            artifactController.Request.Properties[ServiceConstants.SessionProperty] = session;

            HttpResponseException result = null;

            // Act
            try {
                await artifactController.GetSubArtifactTreeAsync(artifactId);
            } catch (HttpResponseException e)
            {
                result = e;
            }
            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.Forbidden, result.Response.StatusCode);
        }
        public async Task GetExpandedTreeToArtifactAsync_BadParameter_ThrowsBadRequestException()
        {
            // Arrange
            const int userId  = 1;
            var       session = new Session {
                UserId = userId
            };
            const int projectId                         = 10;
            const int artifactId                        = 0;
            var       mockArtifactRepository            = new Mock <IArtifactRepository>();
            var       mockArtifactPermissionsRepository = new Mock <IArtifactPermissionsRepository>();
            var       mockServiceLogRepository          = new Mock <IServiceLogRepository>();
            var       artifactController                = new ArtifactController(mockArtifactRepository.Object, mockArtifactPermissionsRepository.Object, _mockSqlPrivilegesRepository.Object, mockServiceLogRepository.Object)
            {
                Request = new HttpRequestMessage()
            };

            artifactController.Request.Properties[ServiceConstants.SessionProperty] = session;

            // Act
            await artifactController.GetExpandedTreeToArtifactAsync(projectId, artifactId);

            // Assert
        }