public void Should_Load_Custom_UserService() { // Arrange ApplicationSettings applicationSettings = new ApplicationSettings(); applicationSettings.UserServiceType = "Roadkill.Tests.UserServiceStub"; DependencyManager iocSetup = new DependencyManager(applicationSettings); // Put the UserServiceStub in a new assembly so we can test it's loaded string tempFilename = Path.GetFileName(Path.GetTempFileName()) + ".dll"; string thisAssembly = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Roadkill.Tests.dll"); string pluginSourceDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins", "UserService"); string destPlugin = Path.Combine(pluginSourceDir, tempFilename); if (!Directory.Exists(pluginSourceDir)) { Directory.CreateDirectory(pluginSourceDir); } File.Copy(thisAssembly, destPlugin, true); // Act iocSetup.Configure(); // Assert UserServiceBase userManager = ObjectFactory.GetInstance <UserServiceBase>(); Assert.That(userManager.GetType().FullName, Is.EqualTo("Roadkill.Tests.UserServiceStub")); }
public void TestDatabaseConnection_Should_Allow_Get_And_Return_Json_Result_And_TestResult_With_No_Errors() { // Arrange string sqlCeDbPath = Path.Combine(Settings.LIB_FOLDER, "Empty-databases", "roadkill.sdf"); string sqlCeDbDestPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "testdatabase.sdf"); File.Copy(sqlCeDbPath, sqlCeDbDestPath, true); string connectionString = @"Data Source=|DataDirectory|\testdatabase.sdf"; DependencyManager manager = new DependencyManager(new ApplicationSettings()); manager.Configure(); // Act JsonResult result = _configTesterController.TestDatabaseConnection(connectionString, "SqlServerCE") as JsonResult; // Assert Assert.That(result, Is.Not.Null, "JsonResult"); Assert.That(result.JsonRequestBehavior, Is.EqualTo(JsonRequestBehavior.AllowGet)); TestResult data = result.Data as TestResult; Assert.That(data, Is.Not.Null); Assert.That(data.Success, Is.True, data.ErrorMessage); Assert.That(data.ErrorMessage, Is.Null.Or.Empty); }
protected void Application_Start() { // Get the settings from the web.config ConfigReaderWriter configReader = new FullTrustConfigReaderWriter(""); ApplicationSettings applicationSettings = configReader.GetApplicationSettings(); // Configure StructureMap dependencies DependencyManager iocSetup = new DependencyManager(applicationSettings); iocSetup.Configure(); iocSetup.ConfigureMvc(); // Logging Log.ConfigureLogging(applicationSettings); // Filters GlobalFilters.Filters.Add(new HandleErrorAttribute()); // Areas are used for: // - Site settings (for a cleaner view structure) // - Webapi help. // This should be called before the other routes, for some reason. AreaRegistration.RegisterAllAreas(); // Register routes and JS/CSS bundles Routing.RegisterApi(GlobalConfiguration.Configuration); Routing.Register(RouteTable.Routes); Bundles.Register(); // Custom view engine registration (to add new search paths) ExtendedRazorViewEngine.Register(); Log.Information("Application started"); }
public void SaveSiteSettings_Should_Persist_All_Values() { // Arrange ApplicationSettings appSettings = new ApplicationSettings(); SiteSettings siteSettings = new SiteSettings() { AllowedFileTypes = "jpg, png, gif", AllowUserSignup = true, IsRecaptchaEnabled = true, MarkupType = "markuptype", RecaptchaPrivateKey = "privatekey", RecaptchaPublicKey = "publickey", SiteName = "sitename", SiteUrl = "siteurl", Theme = "theme", }; SettingsViewModel validConfigSettings = new SettingsViewModel() { AllowedFileTypes = "jpg, png, gif", AllowUserSignup = true, IsRecaptchaEnabled = true, MarkupType = "markuptype", RecaptchaPrivateKey = "privatekey", RecaptchaPublicKey = "publickey", SiteName = "sitename", SiteUrl = "siteurl", Theme = "theme", }; RepositoryMock repository = new RepositoryMock(); DependencyManager iocSetup = new DependencyManager(appSettings, repository, new UserContext(null)); // context isn't used iocSetup.Configure(); SettingsService settingsService = new SettingsService(appSettings, repository); // Act settingsService.SaveSiteSettings(validConfigSettings); // Assert SiteSettings actualSettings = settingsService.GetSiteSettings(); Assert.That(actualSettings.AllowedFileTypes.Contains("jpg"), "AllowedFileTypes jpg"); Assert.That(actualSettings.AllowedFileTypes.Contains("gif"), "AllowedFileTypes gif"); Assert.That(actualSettings.AllowedFileTypes.Contains("png"), "AllowedFileTypes png"); Assert.That(actualSettings.AllowUserSignup, Is.True, "AllowUserSignup"); Assert.That(actualSettings.IsRecaptchaEnabled, Is.True, "IsRecaptchaEnabled"); Assert.That(actualSettings.MarkupType, Is.EqualTo("markuptype"), "MarkupType"); Assert.That(actualSettings.RecaptchaPrivateKey, Is.EqualTo("privatekey"), "RecaptchaPrivateKey"); Assert.That(actualSettings.RecaptchaPublicKey, Is.EqualTo("publickey"), "RecaptchaPublicKey"); Assert.That(actualSettings.SiteName, Is.EqualTo("sitename"), "SiteName"); Assert.That(actualSettings.SiteUrl, Is.EqualTo("siteurl"), "SiteUrl"); Assert.That(actualSettings.Theme, Is.EqualTo("theme"), "Theme"); }
public void Should_Register_Controller_Instances() { // Arrange DependencyManager container = new DependencyManager(new ApplicationSettings()); // Act container.Configure(); ObjectFactory.Configure(x => x.For <ConfigReaderWriter>().Use <ConfigReaderWriterStub>()); // this avoids issues with the web.config trying to be loaded IList <Roadkill.Core.Mvc.Controllers.ControllerBase> controllers = ObjectFactory.GetAllInstances <Roadkill.Core.Mvc.Controllers.ControllerBase>(); // Assert Assert.That(controllers.Count, Is.GreaterThanOrEqualTo(9)); }
public void Should_Use_FormsAuthUserService_By_Default() { // Arrange Mock <IRepository> mockRepository = new Mock <IRepository>(); Mock <IUserContext> mockContext = new Mock <IUserContext>(); ApplicationSettings settings = new ApplicationSettings(); // Act DependencyManager iocSetup = new DependencyManager(settings, mockRepository.Object, mockContext.Object); iocSetup.Configure(); // Assert Assert.That(ServiceLocator.GetInstance <UserServiceBase>(), Is.TypeOf(typeof(FormsAuthUserService))); }
public void Custom_Configuration_Repository_And_Context_Types_Should_Be_Registered() { // Arrange ApplicationSettings settings = new ApplicationSettings(); DependencyManager container = new DependencyManager(settings, new RepositoryMock(), new UserContextStub()); // Act container.Configure(); IRepository repository = ObjectFactory.GetInstance <IRepository>(); IUserContext context = ObjectFactory.GetInstance <IUserContext>(); // Assert Assert.That(repository, Is.TypeOf <RepositoryMock>()); Assert.That(context, Is.TypeOf <UserContextStub>()); }
public void Should_Load_Custom_Repository_From_DatabaseType() { // Arrange ApplicationSettings applicationSettings = new ApplicationSettings(); applicationSettings.DataStoreType = DataStoreType.MongoDB; DependencyManager container = new DependencyManager(applicationSettings); // Act container.Configure(); // Assert IRepository respository = ObjectFactory.GetInstance <IRepository>(); Assert.That(respository, Is.TypeOf <MongoDBRepository>()); }
public void RegisterMvcFactoriesAndRouteHandlers_Should_Register_Instances() { // Arrange RouteTable.Routes.Clear(); DependencyManager iocSetup = new DependencyManager(new ApplicationSettings()); // Act iocSetup.Configure(); iocSetup.ConfigureMvc(); // Assert Assert.That(RouteTable.Routes.Count, Is.EqualTo(1)); Assert.That(((Route)RouteTable.Routes[0]).RouteHandler, Is.TypeOf <AttachmentRouteHandler>()); Assert.True(ModelBinders.Binders.ContainsKey(typeof(SettingsViewModel))); Assert.True(ModelBinders.Binders.ContainsKey(typeof(UserViewModel))); }
public void MongoDB_databaseType_Should_Load_Repository() { // Arrange Mock <IRepository> mockRepository = new Mock <IRepository>(); Mock <IUserContext> mockContext = new Mock <IUserContext>(); ApplicationSettings settings = new ApplicationSettings(); settings.DataStoreType = DataStoreType.MongoDB; // Act DependencyManager iocContainer = new DependencyManager(settings, mockRepository.Object, mockContext.Object); iocContainer.Configure(); // Assert Assert.That(ServiceLocator.GetInstance <UserServiceBase>(), Is.TypeOf(typeof(FormsAuthUserService))); }
public void WindowsAuth_Should_Register_ActiveDirectoryUserManager() { // Arrange ApplicationSettings settings = new ApplicationSettings(); settings.UseWindowsAuthentication = true; settings.LdapConnectionString = "LDAP://123.com"; settings.EditorRoleName = "editor;"; settings.AdminRoleName = "admins"; DependencyManager container = new DependencyManager(settings, new RepositoryMock(), new UserContextStub()); // Act container.Configure(); UserServiceBase usermanager = ObjectFactory.GetInstance <UserServiceBase>(); // Assert Assert.That(usermanager, Is.TypeOf <ActiveDirectoryUserService>()); }
public void TestDatabaseConnection_Should_Return_TestResult_With_Errors_For_Invalid_ConnectionString() { // Arrange string connectionString = "invalid connection string"; DependencyManager manager = new DependencyManager(new ApplicationSettings()); manager.Configure(); // Act JsonResult result = _configTesterController.TestDatabaseConnection(connectionString, "SqlServerCE") as JsonResult; // Assert Assert.That(result, Is.Not.Null, "JsonResult"); TestResult data = result.Data as TestResult; Assert.That(data, Is.Not.Null); Assert.That(data.Success, Is.False, data.ErrorMessage); Assert.That(data.ErrorMessage, Is.Not.Null.Or.Empty); }
public void Single_Constructor_Argument_Should_Register_Default_Instances() { // Arrange DependencyManager container = new DependencyManager(new ApplicationSettings()); // Act container.Configure(); ApplicationSettings settings = ObjectFactory.TryGetInstance <ApplicationSettings>(); IRepository repository = ObjectFactory.GetInstance <IRepository>(); IUserContext context = ObjectFactory.GetInstance <IUserContext>(); IPageService pageService = ObjectFactory.GetInstance <IPageService>(); MarkupConverter markupConverter = ObjectFactory.GetInstance <MarkupConverter>(); CustomTokenParser tokenParser = ObjectFactory.GetInstance <CustomTokenParser>(); UserViewModel userModel = ObjectFactory.GetInstance <UserViewModel>(); SettingsViewModel settingsModel = ObjectFactory.GetInstance <SettingsViewModel>(); AttachmentRouteHandler routerHandler = ObjectFactory.GetInstance <AttachmentRouteHandler>(); UserServiceBase userManager = ObjectFactory.GetInstance <UserServiceBase>(); IPluginFactory pluginFactory = ObjectFactory.GetInstance <IPluginFactory>(); IWikiImporter wikiImporter = ObjectFactory.GetInstance <IWikiImporter>(); IAuthorizationProvider authProvider = ObjectFactory.GetInstance <IAuthorizationProvider>(); IActiveDirectoryProvider adProvider = ObjectFactory.GetInstance <IActiveDirectoryProvider>(); // Assert Assert.That(settings, Is.Not.Null); Assert.That(repository, Is.TypeOf <LightSpeedRepository>()); Assert.That(context, Is.TypeOf <UserContext>()); Assert.That(pageService, Is.TypeOf <PageService>()); Assert.That(markupConverter, Is.TypeOf <MarkupConverter>()); Assert.That(tokenParser, Is.TypeOf <CustomTokenParser>()); Assert.That(userModel, Is.TypeOf <UserViewModel>()); Assert.That(settingsModel, Is.TypeOf <SettingsViewModel>()); Assert.That(userManager, Is.TypeOf <FormsAuthUserService>()); Assert.That(pluginFactory, Is.TypeOf <PluginFactory>()); Assert.That(wikiImporter, Is.TypeOf <ScrewTurnImporter>()); Assert.That(authProvider, Is.TypeOf <AuthorizationProvider>()); #if !MONO Assert.That(adProvider, Is.TypeOf <ActiveDirectoryProvider>()); #endif }
public void Should_Load_ActiveDirectory_UserService_When_UseWindowsAuth_Is_True() { // Arrange Mock <IRepository> mockRepository = new Mock <IRepository>(); Mock <IUserContext> mockContext = new Mock <IUserContext>(); ApplicationSettings settings = new ApplicationSettings(); settings.UseWindowsAuthentication = true; settings.LdapConnectionString = "LDAP://dc=roadkill.org"; settings.AdminRoleName = "editors"; settings.EditorRoleName = "editors"; // Act DependencyManager iocSetup = new DependencyManager(settings, mockRepository.Object, mockContext.Object); iocSetup.Configure(); // Assert Assert.That(ServiceLocator.GetInstance <UserServiceBase>(), Is.TypeOf(typeof(ActiveDirectoryUserService))); }
public void Should_Register_Service_Instances_When_Windows_Auth_Enabled() { // Arrange ApplicationSettings settings = new ApplicationSettings(); settings.UseWindowsAuthentication = true; settings.LdapConnectionString = "LDAP://123.com"; settings.EditorRoleName = "editor;"; settings.AdminRoleName = "admins"; DependencyManager container = new DependencyManager(new ApplicationSettings()); // Act container.Configure(); // fake some AD settings for the AD service ObjectFactory.Inject <ApplicationSettings>(settings); IList <ServiceBase> services = ObjectFactory.GetAllInstances <ServiceBase>(); // Assert Assert.That(services.Count, Is.GreaterThanOrEqualTo(7)); }
public static void Configure(IServiceCollection services, string connectionString) { services.AddDbContext <ApplicationDbContext>(options => { options.UseNpgsql(connectionString); }); DependencyManager.Configure(services); var mappingConfig = new MapperConfiguration(mc => { mc.AddProfile(new MappingProfile()); }); IMapper mapper = mappingConfig.CreateMapper(); services.AddSingleton(mapper); services.AddTransient <ICocktailService, CocktailService>(); services.AddTransient <IIngredientService, IngredientService>(); services.AddTransient <IDbSeeder, DbSeeder>(); services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>(); }