private ExicoShopifyDbContext _InitDbContext() { var options = new DbContextOptionsBuilder <ExicoShopifyDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; ExicoShopifyDbContext testContext = new ExicoShopifyDbContext(options); //emptying testContext.Database.EnsureDeleted(); ////recreating testContext.Database.EnsureCreated(); //seeding var s = new SystemSetting() { DefaultValue = "APP_DASHBOARD_CONTOLLER", Description = "APP_DASHBOARD_CONTOLLER", DisplayName = "APP_DASHBOARD_CONTOLLER", GroupName = DbSettingsReaderExtensions.CORE_SETTINGS_GROUP_NAME, SettingName = CORE_SYSTEM_SETTING_NAMES.DASHBOARD_CONTOLLER.ToString(), Value = "DashBoard" }; testContext.SystemSettings.Add(s); testContext.SaveChanges(); return(testContext); }
private ExicoShopifyDbContext SetUpDbContext() { var options = new DbContextOptionsBuilder <ExicoShopifyDbContext>() .UseInMemoryDatabase("ip_address_helper") .Options; ExicoShopifyDbContext testContext = new ExicoShopifyDbContext(options); //emptying testContext.Database.EnsureDeleted(); ////recreating testContext.Database.EnsureCreated(); //seeding var s = new SystemSetting() { DefaultValue = "127.0.0.1", Description = "Admin IP addresses", DisplayName = "Admin Ips", GroupName = DbSettingsReaderExtensions.CORE_SETTINGS_GROUP_NAME, SettingName = CORE_SYSTEM_SETTING_NAMES.PRIVILEGED_IPS.ToString(), Value = ADMIN_IPS }; testContext.SystemSettings.Add(s); testContext.SaveChanges(); return(testContext); }
private ExicoShopifyDbContext SetUpDbContext() { var options = new DbContextOptionsBuilder <ExicoShopifyDbContext>() .UseInMemoryDatabase("password_generator_db") .Options; ExicoShopifyDbContext testContext = new ExicoShopifyDbContext(options); //emptying testContext.Database.EnsureDeleted(); ////recreating testContext.Database.EnsureCreated(); //seeding var s = new SystemSetting() { DefaultValue = "", Description = "Password salt", DisplayName = "Password salt", GroupName = DbSettingsReaderExtensions.CORE_SETTINGS_GROUP_NAME, SettingName = CORE_SYSTEM_SETTING_NAMES.PASSWORD_SALT.ToString(), Value = "1234567" }; testContext.SystemSettings.Add(s); testContext.SaveChanges(); return(testContext); }
private ExicoShopifyDbContext InitDbContext() { var options = new DbContextOptionsBuilder <ExicoShopifyDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; ExicoShopifyDbContext testContext = new ExicoShopifyDbContext(options); //emptying testContext.Database.EnsureDeleted(); ////recreating testContext.Database.EnsureCreated(); //seeding //list of allowed IPS List <IPAddress> ips = new List <IPAddress>(); ips.Add(new IPAddress(IP_10_0_0_1)); ips.Add(new IPAddress(IP_10_0_0_2)); ips.Add(new IPAddress(IP_10_0_0_3)); var s = new SystemSetting() { DefaultValue = "127.0.0.1", Description = "Allowed Ips", DisplayName = "Allowed Ips", GroupName = DbSettingsReaderExtensions.CORE_SETTINGS_GROUP_NAME, SettingName = CORE_SYSTEM_SETTING_NAMES.PRIVILEGED_IPS.ToString(), Value = string.Join(',', ips.Select(x => x.ToString()).ToList().ToArray()) }; testContext.SystemSettings.Add(s); testContext.SaveChanges(); return(testContext); }
public Test_Settings_Reader() { var serviceProvider = new ServiceCollection().AddLogging().BuildServiceProvider(); var factory = serviceProvider.GetService <ILoggerFactory>(); logger = factory.CreateLogger <IDbSettingsReader>(); context = InitDbContext(); }
private static void SeedData(ExicoShopifyDbContext exicoDbContext, ExicoIdentityDbContext identityContext, ILogger logger, IConfiguration config, IServiceScope scope) { SeedSettingsData(exicoDbContext, config, logger); SeedPlanData(exicoDbContext, logger, config); SeedAdminRoleAndUser(identityContext, exicoDbContext, logger, config, scope); }
protected IDbService <Plan> GetServiceOnly() { var options = new DbContextOptionsBuilder <ExicoShopifyDbContext>() .UseInMemoryDatabase("fixed_name") .Options; ExicoShopifyDbContext testContext = new ExicoShopifyDbContext(options); return(new TestService <Plan>(new TestRepository <Plan>(new TestUnitOfWork(testContext)))); }
protected static IDbRepository <Plan> GetInMemoryRepositoryShared() { var options = new DbContextOptionsBuilder <ExicoShopifyDbContext>() .UseInMemoryDatabase("fixed_name") .Options; ExicoShopifyDbContext testContext = new ExicoShopifyDbContext(options); return(new TestRepository <Plan>(new TestUnitOfWork(testContext))); }
//Adds 16 CORE settings plus 2 cutoms app setting = 18 in total private ExicoShopifyDbContext InitDbContext() { var options = new DbContextOptionsBuilder <ExicoShopifyDbContext>() .UseInMemoryDatabase("settings_db") .Options; ExicoShopifyDbContext testContext = new ExicoShopifyDbContext(options); //emptying testContext.Database.EnsureDeleted(); ////recreating testContext.Database.EnsureCreated(); //seeding for (int i = 0; i < TOTAL_CORE_SETTINGS; i++) { var settingName = Enum.GetNames(typeof(CORE_SYSTEM_SETTING_NAMES)).ToList()[i]; var s = new SystemSetting() { DefaultValue = $"{settingName}-DefaultValue", Description = $"{settingName}-Description", DisplayName = $"{settingName}-DisplayName", GroupName = DbSettingsReaderExtensions.CORE_SETTINGS_GROUP_NAME, SettingName = settingName, Value = $"{settingName}-Value" }; testContext.SystemSettings.Add(s); } //add two custom settings which only have default value for (int i = 1; i <= TOTAL_APP_SETTINGS; i++) { var settingName = $"AppSetting{i}"; var s = new SystemSetting() { DefaultValue = $"{settingName}-DefaultValue", Description = $"{settingName}-Description", DisplayName = $"{settingName}-DisplayName", GroupName = APP_SETTING_GROUP_NAME, SettingName = settingName, Value = "" }; testContext.SystemSettings.Add(s); } testContext.SaveChanges(); return(testContext); }
private ExicoShopifyDbContext InitDbContext() { var options = new DbContextOptionsBuilder <ExicoShopifyDbContext>() .UseInMemoryDatabase("settings_db") .Options; ExicoShopifyDbContext testContext = new ExicoShopifyDbContext(options); //emptying testContext.Database.EnsureDeleted(); ////recreating testContext.Database.EnsureCreated(); //seeding testContext.SystemSettings.Add(new SystemSetting() { Value = APP_BASE_URL, GroupName = "CORE", SettingName = CORE_SYSTEM_SETTING_NAMES.APP_BASE_URL.ToString() }); testContext.SystemSettings.Add(new SystemSetting() { Value = "shopify", GroupName = "CORE", SettingName = CORE_SYSTEM_SETTING_NAMES.SHOPIFY_CONTROLLER.ToString() }); testContext.SystemSettings.Add(new SystemSetting() { Value = "appuninstaller", GroupName = "CORE", SettingName = CORE_SYSTEM_SETTING_NAMES.UNINSTALL_CONTROLLER.ToString() }); //testContext.SystemSettings.Add(new SystemSetting() //{ // Value = "uninstall", // GroupName = "CORE", // SettingName = CORE_SYSTEM_SETTING_NAMES.UNINSTALL_WEBHOOK_ACTION_NAME.ToString() //}); testContext.SystemSettings.Add(new SystemSetting() { Value = "https://apps.shopify.com", GroupName = "CORE", SettingName = CORE_SYSTEM_SETTING_NAMES.SHOPIFY_APP_STOER_URL.ToString() }); testContext.SaveChanges(); return(testContext); }
public IDbService <Plan> GetDbService() { var options = new DbContextOptionsBuilder <ExicoShopifyDbContext>().UseInMemoryDatabase("plan_helper").Options; ExicoShopifyDbContext testContext = new ExicoShopifyDbContext(options); //emptying testContext.Database.EnsureDeleted(); ////recreating testContext.Database.EnsureCreated(); //seeding for (int i = 1; i <= TOTAL_PLAN; i++) { var plan = new Plan() { Active = true, Description = $"Test Description {i}", Footer = $"Test Footer {i}", IsDev = (i == 1), IsTest = false, Name = $"Test Plan {i}", Price = i, DisplayOrder = i, TrialDays = (short)(5 * i), Id = i }; for (int j = 1; j <= 5; j++) { plan.PlanDefinitions.Add(new PlanDefinition() { Id = (5 * (i - 1)) + j, Description = $"Description {j}", OptionName = $"Option {j}", OptionValue = $"Value {j}", PlanId = i }); } testContext.Add(plan); } testContext.SaveChanges(); return(new TestService <Plan>(new TestRepository <Plan>(new TestUnitOfWork(testContext)))); }
protected IDbService <Plan> GetServiceWithData(int?total_plans = null, int?total_plan_definitions = null) { var options = new DbContextOptionsBuilder <ExicoShopifyDbContext>() .UseInMemoryDatabase("fixed_name") .Options; ExicoShopifyDbContext testContext = new ExicoShopifyDbContext(options); //emptying testContext.Database.EnsureDeleted(); ////recreating testContext.Database.EnsureCreated(); //seeding for (int i = 1; i <= (total_plans ?? TOTAL_PLAN_RECORDS); i++) { var plan = new Plan() { Active = true, Description = $"Test Description {i}", Footer = $"Test Footer {i}", IsDev = true, IsTest = false, Name = $"Test Plan {i}", Price = i, DisplayOrder = i, TrialDays = (short)(5 * i), Id = i }; for (int j = 1; j <= (total_plan_definitions ?? TOTAL_PLAN_DEFINITION_RECORDS_PER_PLAN); j++) { plan.PlanDefinitions.Add(new PlanDefinition() { Id = (5 * (i - 1)) + j, Description = $"Description {j}", OptionName = $"Option {j}", OptionValue = $"Value {j}", PlanId = i }); } testContext.Plans.Add(plan); } testContext.SaveChanges(); return(new TestService <Plan>(new TestRepository <Plan>(new TestUnitOfWork(testContext)))); }
protected IDbService <AspNetUser> InitService() { var options = new DbContextOptionsBuilder <ExicoShopifyDbContext>() .UseInMemoryDatabase("Test_User_Db_Service_Helper_DB") .Options; ExicoShopifyDbContext testContext = new ExicoShopifyDbContext(options); //emptying testContext.Database.EnsureDeleted(); ////recreating testContext.Database.EnsureCreated(); //seeding testContext.AspNetUsers.Add(UserOne); testContext.AspNetUsers.Add(UserTwo); testContext.SaveChanges(); return(new TestService <AspNetUser>(new TestRepository <AspNetUser>(new TestUnitOfWork(testContext)))); }
private IDbSettingsReader GetSettings() { var options = new DbContextOptionsBuilder <ExicoShopifyDbContext>() .UseInMemoryDatabase("settings_db_my_profile") .Options; ExicoShopifyDbContext testContext = new ExicoShopifyDbContext(options); //emptying testContext.Database.EnsureDeleted(); ////recreating testContext.Database.EnsureCreated(); //seeding testContext.SystemSettings.Add(new SystemSetting() { Value = "shopify", GroupName = "CORE", SettingName = CORE_SYSTEM_SETTING_NAMES.SHOPIFY_CONTROLLER.ToString() }); testContext.SystemSettings.Add(new SystemSetting() { Value = "myprofile", GroupName = "CORE", SettingName = CORE_SYSTEM_SETTING_NAMES.MY_PROFILE_CONTOLLER.ToString() }); testContext.SaveChanges(); var serviceProvider = new ServiceCollection().AddLogging().BuildServiceProvider(); var factory = serviceProvider.GetService <Microsoft.Extensions.Logging.ILoggerFactory>(); ILogger <IDbSettingsReader> logger = factory.CreateLogger <IDbSettingsReader>(); var m = new MemoryCache(new MemoryCacheOptions() { }); var service = new TestService <SystemSetting>(new TestRepository <SystemSetting>(new TestUnitOfWork(testContext))); DbSettingsReader r = new DbSettingsReader(service, m, logger); return(r); }
private static void SeedPlanData(ExicoShopifyDbContext exicoDbContext, ILogger logger, IConfiguration config) { if (!exicoDbContext.Plans.Any()) { List <Plan> plans = new List <Plan>(); config.Bind("PlansSeed", plans); logger.LogInformation("Seeding plan data {@plans}.", plans); if (plans.Count() > 0) { var totalPlanAdded = 0; foreach (var p in plans) { exicoDbContext.Plans.Add(p); int i = exicoDbContext.SaveChanges(); if (i == 0) { logger.LogError($"Could not seed Plan data for {p.Name}."); } else { totalPlanAdded++; logger.LogInformation($"Successfully added plan {p.Name}."); } } logger.LogInformation($"Finished seeding plan data. Total '{totalPlanAdded}' record(s)."); } else { logger.LogInformation("Plans seed data contains no plan data."); } } else { logger.LogInformation("Skipping plans data seeding as database alraedy has some."); } }
public TestUnitOfWork(ExicoShopifyDbContext context) : base(context) { }
private static void SeedAdminRoleAndUser(ExicoIdentityDbContext identityContext, ExicoShopifyDbContext exicoDbContext, ILogger logger, IConfiguration config, IServiceScope scope) { //TODO check and make it a little bit robus var roleStore = new RoleStore <IdentityRole>(identityContext); IdentityResult result = null; logger.LogInformation("creating admin role."); if (!identityContext.Roles.Any(r => r.Name == UserInContextHelper.ADMIN_ROLE)) { result = roleStore.CreateAsync(new IdentityRole() { Name = UserInContextHelper.ADMIN_ROLE, NormalizedName = UserInContextHelper.ADMIN_ROLE }).Result; if (result.Succeeded == false) { logger.LogError($"Could not create admin role.{string.Join('.', result.Errors)}"); } else { logger.LogInformation("Successfully created admin role"); } } else { logger.LogInformation("Admin role already exists.So skipping role creation."); result = IdentityResult.Success; } if (result.Succeeded) { UserManager <AspNetUser> _userManager = scope.ServiceProvider.GetService <UserManager <AspNetUser> >(); //read config values for admin user var user = new AspNetUser(); config.Bind("AdminSeed", user); if (!identityContext.Users.Any(u => u.UserName == user.UserName)) { logger.LogInformation("Creating user."); //prepare user user.PlanId = user.PlanId ?? exicoDbContext.Plans.First().Id; user.BillingOn = user.BillingOn ?? DateTime.Today; user.ShopifyAccessToken = user.ShopifyAccessToken ?? "an-invalid-token"; user.ShopifyChargeId = user.ShopifyChargeId ?? 123456; var passGenrerator = scope.ServiceProvider.GetService <IGenerateUserPassword>(); var pass = passGenrerator.GetPassword(new Data.Domain.AppModels.PasswordGeneratorInfo(user)); result = _userManager.CreateAsync(user, pass).Result; if (result.Succeeded) { logger.LogInformation($"Finished creating user '{user.UserName}'"); } else { logger.LogError($"Error creating user.{string.Join('.', result.Errors)}"); } } else { result = IdentityResult.Success; logger.LogInformation($"Skipping user creation. User '{user.UserName}' already exists."); } if (result.Succeeded) { logger.LogInformation($"Assigning admin role to user '{user.UserName}'"); user = _userManager.FindByEmailAsync(user.Email).Result; var userIsInRole = _userManager.IsInRoleAsync(user, UserInContextHelper.ADMIN_ROLE).Result; if (!userIsInRole) { result = _userManager.AddToRolesAsync(user, new string[] { UserInContextHelper.ADMIN_ROLE }).Result; if (result.Succeeded) { logger.LogInformation($"Finished assigning admin role to user '{user.UserName}'"); } else { logger.LogError($"Error assigning user to admin role.{string.Join('.', result.Errors)}"); } } else { logger.LogWarning($"Skipping assigning user to admin role because user '{user.UserName}' already is an admin."); } } else { logger.LogWarning($"Skipping assigning user to admin role because user reation failed."); } } logger.LogInformation("Finished seeding admin role and user"); }
private static void SeedSettingsData(ExicoShopifyDbContext exicoDbContext, IConfiguration config, ILogger logger) { if (!exicoDbContext.SystemSettings.Any()) { SettingsSeederAppModel settSeed = new SettingsSeederAppModel(); config.Bind("SettingsSeed", settSeed); logger.LogInformation("Seeding settings data {@data}.", settSeed); exicoDbContext.SystemSettings.Add(new SystemSetting() { DefaultValue = "Account", Description = "Default account controller name without the controller part.", DisplayName = "Account controller", GroupName = "CORE", SettingName = CORE_SYSTEM_SETTING_NAMES.ACCOUNT_CONTOLLER.ToString(), Value = settSeed.ACCOUNT_CONTOLLER ?? "Account" }); exicoDbContext.SystemSettings.Add(new SystemSetting() { DefaultValue = "Dashboard", Description = "Default dashboard controller name without the controller part.", DisplayName = "Dashboard controller", GroupName = "CORE", SettingName = CORE_SYSTEM_SETTING_NAMES.DASHBOARD_CONTOLLER.ToString(), Value = settSeed.DASHBOARD_CONTOLLER ?? "Dashboard" }); exicoDbContext.SystemSettings.Add(new SystemSetting() { DefaultValue = "MyProfile", Description = "Default my profile controller name without the controller part.", DisplayName = "My profile controller", GroupName = "CORE", SettingName = CORE_SYSTEM_SETTING_NAMES.MY_PROFILE_CONTOLLER.ToString(), Value = settSeed.MY_PROFILE_CONTOLLER ?? "MyProfile", }); exicoDbContext.SystemSettings.Add(new SystemSetting() { DefaultValue = "Shopify", Description = "Default my Shopify controller name without the controller part.", DisplayName = "Shopify controller", GroupName = "CORE", SettingName = CORE_SYSTEM_SETTING_NAMES.SHOPIFY_CONTROLLER.ToString(), Value = settSeed.SHOPIFY_CONTROLLER ?? "Shopify", }); exicoDbContext.SystemSettings.Add(new SystemSetting() { DefaultValue = "AppUninstall", Description = "Default my app uninstall controller name without the controller part.", DisplayName = "App uninstall controller", GroupName = "CORE", SettingName = CORE_SYSTEM_SETTING_NAMES.UNINSTALL_CONTROLLER.ToString(), Value = settSeed.UNINSTALL_CONTROLLER ?? "Uninstall", }); exicoDbContext.SystemSettings.Add(new SystemSetting() { DefaultValue = "127.0.0.1", Description = "List of privileged ip addresses.Use comma for multiples.", DisplayName = "Privileged IPs", GroupName = "CORE", SettingName = CORE_SYSTEM_SETTING_NAMES.PRIVILEGED_IPS.ToString(), Value = settSeed.PRIVILEGED_IPS ?? "127.0.0.1", }); exicoDbContext.SystemSettings.Add(new SystemSetting() { DefaultValue = "Welcome to the app!", Description = "A welcome message template for new users.It can contain HTML.", DisplayName = "Welcome Message", GroupName = "CORE", SettingName = CORE_SYSTEM_SETTING_NAMES.WELCOME_EMAIL_TEMPLATE.ToString(), Value = settSeed.WELCOME_EMAIL_TEMPLATE ?? "Welcome to the app!", }); exicoDbContext.SystemSettings.Add(new SystemSetting() { DefaultValue = "*****@*****.**", Description = "App support email address.", DisplayName = "Support Email", GroupName = "CORE", SettingName = CORE_SYSTEM_SETTING_NAMES.APP_SUPPORT_EMAIL_ADDRESS.ToString(), Value = settSeed.APP_SUPPORT_EMAIL_ADDRESS ?? "" }); exicoDbContext.SystemSettings.Add(new SystemSetting() { DefaultValue = "shopiyfy ap key", Description = "API key of your shopify app.", DisplayName = "API Key", GroupName = "CORE", SettingName = CORE_SYSTEM_SETTING_NAMES.API_KEY.ToString(), Value = settSeed.API_KEY ?? "" }); exicoDbContext.SystemSettings.Add(new SystemSetting() { DefaultValue = "exicoShopifyFramework", Description = "App name same as shopify app store.", DisplayName = "App Name", GroupName = "CORE", SettingName = CORE_SYSTEM_SETTING_NAMES.APP_NAME.ToString(), Value = settSeed.APP_NAME ?? "" }); exicoDbContext.SystemSettings.Add(new SystemSetting() { DefaultValue = "https://*****:*****@myapp.com", Description = "Email address that will be used as from address.", DisplayName = "From Email", GroupName = "CORE", SettingName = CORE_SYSTEM_SETTING_NAMES.SHOPIFY_EMAILS_FROM_ADDRESS.ToString(), Value = settSeed.SHOPIFY_EMAILS_FROM_ADDRESS ?? "" }); exicoDbContext.SystemSettings.Add(new SystemSetting() { DefaultValue = "*****@*****.**", Description = "Emaile address that will receive any emails generated by the app/framework.Use comma for multiples.", DisplayName = "Subscribers", GroupName = "CORE", SettingName = CORE_SYSTEM_SETTING_NAMES.SHOPIFY_EVENT_EMAIL_SUBSCRIBERS.ToString(), Value = settSeed.SHOPIFY_EVENT_EMAIL_SUBSCRIBERS ?? "" }); exicoDbContext.SystemSettings.Add(new SystemSetting() { DefaultValue = "1.0.0", Description = "Version of your app", DisplayName = "Version", GroupName = "CORE", SettingName = CORE_SYSTEM_SETTING_NAMES.APP_VERSION.ToString(), Value = settSeed.APP_VERSION ?? "1.0.0" }); exicoDbContext.SystemSettings.Add(new SystemSetting() { DefaultValue = "0.0.0", Description = "Version of the Exico Shopify Framework that created this database", DisplayName = "Seeder Framework Version", GroupName = "CORE", SettingName = CORE_SYSTEM_SETTING_NAMES.SEEDER_FRAMEWORK_VERSION.ToString(), Value = AppSettingsAccessor.GetFrameWorkBuildNumber(true) }); exicoDbContext.SystemSettings.Add(new SystemSetting() { DefaultValue = "", Description = "Default auser password salt", DisplayName = "User Password Salt", GroupName = "CORE", SettingName = CORE_SYSTEM_SETTING_NAMES.PASSWORD_SALT.ToString(), Value = Guid.NewGuid().ToString() }); var total = exicoDbContext.SaveChanges(); logger.LogInformation($"Finished seeding settings data. Total {total} records."); } else { logger.LogInformation("Settings data alraedy exists.Skipping seeding for settings data."); } }