public FakeContactsService()
        {
            UserFaker  userFaker  = new UserFaker();
            GroupFaker groupFaker = new GroupFaker();

            contacts = userFaker.Generate(50)
                       .OfType <Contact>()
                       .Concat(groupFaker.Generate(10))
                       .ToList();
        }
Example #2
0
        public static IServiceCollection RegisterUserServicesData(this IServiceCollection services)
        {
            services.AddDbContext <UserContext>(opt => opt.UseLazyLoadingProxies().UseSqlite("Data Source=./UserDatabase.db"))
            .AddIdentity <UserDB, IdentityRole>()
            .AddEntityFrameworkStores <UserContext>()
            .AddDefaultTokenProviders();
            var o = new DbContextOptionsBuilder <UserContext>();

            o.UseLazyLoadingProxies().UseSqlite("Data Source=./UserDatabase.db");

            using (var context = new UserContext(o.Options))
            {
                context.Database.EnsureCreated();
                if (!context.Users.Any())
                {
                    context.Users.AddRange(UserFaker.Generate());
                    context.SaveChanges();
                }
            }
            services.AddScoped <IUnitOfWork, UnitOfWork>();
            return(services);
        }
    /// <inheritdoc />
    public UserToRegister GetExamples()
    {
        User user = _faker.Generate();

        return(_mapper.Map <UserToRegister>(user));
    }
Example #4
0
        static async Task <IList <User> > SeedUsers(DataContext context, UserManager <User> userManager, string defaultPassword, IList <City> cities, IConfiguration configuration, IHostEnvironment environment, ILogger logger)
        {
            UserFaker usersFaker = new UserFaker(cities)
            {
                // random specified genders cannot exceed 100 because the max random number for user image is 99
                MaxSpecifiedGender = 100
            };
            IList <User> users = usersFaker.Generate(USERS_COUNT);
            IDictionary <Genders, IDictionary <string, string> > images = await DownloadUserImages(users, configuration, environment, logger);

            User admin = users[0];

            admin.UserName             = "******";
            admin.Email                = "*****@*****.**";
            admin.EmailConfirmed       = true;
            admin.PhoneNumberConfirmed = true;
            admin.LockoutEnabled       = false;
            logger?.LogInformation("Adding users data.");

            int usersAdded = 0;

            for (int i = 0; i < users.Count; i++)
            {
                User user     = users[i];
                User userInDb = await userManager.FindByNameAsync(user.UserName);

                if (userInDb != null)
                {
                    users[i] = userInDb;
                    continue;
                }

                bool userIsAdmin = user.UserName.IsSame("administrator");
                user.Created    = DateTime.UtcNow;
                user.Modified   = DateTime.UtcNow;
                user.LastActive = DateTime.UtcNow;
                logger?.LogInformation($"Adding '{user.UserName}' user data.");
                IdentityResult result = await userManager.CreateAsync(user, defaultPassword);

                if (!result.Succeeded)
                {
                    if (userIsAdmin)
                    {
                        throw new DataException(result.Errors.Aggregate(new StringBuilder($"Could not add admin user '{user.UserName}'."),
                                                                        (builder, error) => builder.AppendWithLine(error.Description),
                                                                        builder => builder.ToString()));
                    }

                    continue;
                }

                logger?.LogInformation($"Added '{user.UserName}' user data.");
                usersAdded++;
                result = userIsAdmin
                                                        ? await userManager.AddToRolesAsync(user, new[]
                {
                    Role.Administrators,
                    Role.Members
                })
                                                        : await userManager.AddToRoleAsync(user, Role.Members);

                logger?.LogInformation($"Adding '{user.UserName}' to roles.");

                if (!result.Succeeded)
                {
                    throw new DataException(result.Errors.Aggregate(new StringBuilder($"Could not add user '{user.UserName}' to role."),
                                                                    (builder, error) => builder.AppendWithLine(error.Description),
                                                                    builder => builder.ToString()));
                }

                logger?.LogInformation($"Added '{user.UserName}' to roles.");
                if (images == null ||
                    user.Gender == Genders.Unspecified ||
                    !images.TryGetValue(user.Gender, out IDictionary <string, string> files) ||
                    !files.TryGetValue(user.Id, out string photoUrl))
                {
                    continue;
                }

                Photo photo = new Photo
                {
                    UserId    = user.Id,
                    Url       = photoUrl,
                    IsDefault = true,
                    DateAdded = user.Created
                };
                await context.Photos.AddAsync(photo);

                user.Photos.Add(photo);
                logger?.LogInformation($"Assigned '{user.UserName}' a default image '{photo.Url}'.");
            }

            await context.SaveChangesAsync();

            logger?.LogInformation($"Added {usersAdded} users.");
            return(users);
        }
        public FakeUsersService(UserFaker userFaker, int count = 10)
        {
            this.userFaker = userFaker;

            this.entities = userFaker.Generate(count);
        }