/// <summary>
        /// Load Profanity data
        /// </summary>
        public void LoadInitialData_Profanity()
        {
            // Uncomment this to actually run.
            var t = 5; if (t > 1)
            {
                return;
            }

            string pathToSuggestionFile = Directory.GetCurrentDirectory();

            pathToSuggestionFile = pathToSuggestionFile + "\\..\\..\\..\\..\\Data\\InitialData\\ProphanityData\\en.txt";

            var context = new BestDataContext();

            if (context.BadWords.Any())
            {
                return;
            }

            var endOfFile = false;

            using (var sr = new StreamReader(new FileStream(pathToSuggestionFile, FileMode.Open)))
            {
                while (!endOfFile)
                {
                    for (var i = 0; i < 1000; i++)
                    {
                        var line = sr.ReadLine();
                        if (line == null)
                        {
                            endOfFile = true;
                            break;
                        }
                        else
                        {
                            context.BadWords.Add(new BadWord()
                            {
                                Phrase = line
                            });
                        }
                    }
                    context.SaveChanges();
                }
            }
        }
Ejemplo n.º 2
0
        public async Task AddTwentyUsers()
        {
            // Uncomment this to actually run.
            var t = 5; if (t > 1)
            {
                return;
            }

            // Result of some operations
            IdentityResult identityResult = null;

            // Create data context
            var dataContext = new BestDataContext();

            // Create role manager
            var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(dataContext), null, null, null, null, null);

            // Create password hasher
            PasswordHasher <ApplicationUser> passwordHasher = new PasswordHasher <ApplicationUser>();
            // Create userManager
            var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(dataContext), null, passwordHasher, null, null, null, null, null, null);

            for (int i = 0; i < 20; i++)
            {
                string userName     = "******" + i;
                string userEmail    = userName + "@apinioner.com";
                string userPassword = "******";
                // Search for user
                var user = await userManager.FindByEmailAsync(userEmail);

                // Create user if does not exist.
                if (user == null)
                {
                    user           = new ApplicationUser();
                    user.Email     = userEmail;
                    user.UserName  = userName;
                    identityResult = await userManager.CreateAsync(user, userPassword);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// This test will add admin roles and user if they are missing in the database.
        /// </summary>
        /// <returns></returns>
        public async Task AddAdminUserTests_AddAdminUser_AddsAdminUser()
        {
            // Uncomment this to actually run.
            var t = 5; if (t > 1)
            {
                return;
            }
            // Result of some operations
            IdentityResult identityResult = null;

            // Create data context
            var dataContext = new BestDataContext();

            // Create role manager
            var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(dataContext), null, null, null, null, null);
            // Search for admin role.
            var adminRole = await roleManager.FindByNameAsync(Roles.Admin.ToString());

            // Create role if does not exist.
            if (adminRole == null)
            {
                adminRole      = new IdentityRole(Roles.Admin.ToString());
                identityResult = await roleManager.CreateAsync(adminRole);

                if (!identityResult.Succeeded)
                {
                    throw new Exception("Could not create role");
                }
            }

            // Create password hasher
            PasswordHasher <ApplicationUser> passwordHasher = new PasswordHasher <ApplicationUser>();
            // Create userManager
            var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(dataContext), null, passwordHasher, null, null, null, null, null, null);
            // Search for admin user
            var adminUser = await userManager.FindByEmailAsync("*****@*****.**");

            // Create user if does not exist.
            if (adminUser == null)
            {
                adminUser       = new ApplicationUser();
                adminUser.Email = "*****@*****.**";
                // adminUser.NormalizedEmail = adminUser.Email.ToUpper();
                adminUser.UserName = "******";
                // adminUser.NormalizedUserName = adminUser.Email.ToUpper();
                identityResult = await userManager.CreateAsync(adminUser, "1zzzelllo_Boom");

                if (!identityResult.Succeeded)
                {
                    throw new Exception("Could add admin user");
                }
            }

            // Add user to role or role to user. Variable is for debugging.
            var isInRole = await userManager.IsInRoleAsync(adminUser, Roles.Admin.ToString());

            // Add if not there
            if (!isInRole)
            {
                identityResult = await userManager.AddToRoleAsync(adminUser, Roles.Admin.ToString());

                if (!identityResult.Succeeded)
                {
                    throw new Exception("Could not add person to role");
                }
            }
        }