public void Constructor_CorrectlyInitializesLogSource()
        {
            // Arrange, Act
            var controller = new ConfigController();

            // Assert
            Assert.AreEqual(controller.LogSource, WebApiConfig.LogSourceConfig);
        }
        private static ConfigController CreateController(Dictionary <string, Dictionary <string, string> > globalSettings = null, Dictionary <string, string> appSettings = null, Dictionary <string, bool> features = null)
        {
            var appSettingsRepo = new Mock <IApplicationSettingsRepository>();
            var featuresService = new Mock <IFeaturesService>();
            IHttpClientProvider httpClientProvider;

            if (globalSettings == null)
            {
                httpClientProvider = new TestHttpClientProvider(request => { throw new Exception(); });
            }
            else
            {
                var content = new ObjectContent(globalSettings.GetType(), globalSettings, new JsonMediaTypeFormatter());
                httpClientProvider = new TestHttpClientProvider(request => request.RequestUri.AbsolutePath.EndsWithOrdinal("settings/false") ?
                                                                new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = content
                } : null);
            }

            if (appSettings == null)
            {
                appSettingsRepo
                .Setup(it => it.GetSettingsAsync(true))
                .Throws(new ApplicationException());
            }
            else
            {
                appSettingsRepo
                .Setup(it => it.GetSettingsAsync(true))
                .ReturnsAsync(appSettings.Select(i => new ApplicationSetting {
                    Key = i.Key, Value = i.Value, Restricted = true
                }));
            }

            featuresService
            .Setup(fs => fs.GetFeaturesAsync())
            .ReturnsAsync(features == null
                    ? new Dictionary <string, bool> {
                { "Workflow", true }
            }
                    : features);

            var logMock    = new Mock <IServiceLogRepository>();
            var controller = new ConfigController(appSettingsRepo.Object, null, null, featuresService.Object, httpClientProvider, logMock.Object)
            {
                Request = new HttpRequestMessage()
            };

            controller.Request.Headers.Add("Session-Token", "");
            controller.Request.SetConfiguration(new HttpConfiguration());

            return(controller);
        }
        public async Task GetUserManagementSettings_InstanceAdmin_ReturnsUserManagementSettings()
        {
            // Arrange
            const int userId = 1;
            var       user   = new LoginUser {
                InstanceAdminRoleId = 1
            };
            var settings = new UserManagementSettings
            {
                IsPasswordExpirationEnabled      = false,
                IsFederatedAuthenticationEnabled = true
            };

            var settingsRepositoryMock = new Mock <ISqlSettingsRepository>();

            settingsRepositoryMock
            .Setup(m => m.GetUserManagementSettingsAsync())
            .ReturnsAsync(settings);
            var userRepositoryMock = new Mock <IUserRepository>();

            userRepositoryMock
            .Setup(m => m.GetLoginUserByIdAsync(userId))
            .ReturnsAsync(user);

            var controller = new ConfigController
                             (
                null,
                settingsRepositoryMock.Object,
                userRepositoryMock.Object,
                null,
                null,
                null)
            {
                Request = new HttpRequestMessage()
            };

            controller.Request.Properties[ServiceConstants.SessionProperty] = new Session {
                UserId = userId
            };

            // Act
            var result = await controller.GetUserManagementSettings() as OkNegotiatedContentResult <UserManagementSettings>;

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(result.Content, settings);
        }
        public async Task GetUserManagementSettings_NonInstanceAdmin_ThrowsAuthorizationException()
        {
            // Arrange
            const int userId = 1;
            var       user   = new LoginUser {
                InstanceAdminRoleId = null
            };
            var settings = new UserManagementSettings
            {
                IsPasswordExpirationEnabled      = false,
                IsFederatedAuthenticationEnabled = true
            };

            var settingsRepositoryMock = new Mock <ISqlSettingsRepository>();

            settingsRepositoryMock
            .Setup(m => m.GetUserManagementSettingsAsync())
            .ReturnsAsync(settings);
            var userRepositoryMock = new Mock <IUserRepository>();

            userRepositoryMock
            .Setup(m => m.GetLoginUserByIdAsync(userId))
            .ReturnsAsync(user);
            var controller = new ConfigController
                             (
                null,
                settingsRepositoryMock.Object,
                userRepositoryMock.Object,
                null,
                null,
                null)
            {
                Request = new HttpRequestMessage()
            };

            controller.Request.Properties[ServiceConstants.SessionProperty] = new Session {
                UserId = userId
            };

            // Act
            await controller.GetUserManagementSettings();
        }