public static async Task <IDisposableEntity <ApplicationUser> > CreateUserAsync(
            this TestServiceScope scope,
            string name     = TestingConstants.Placeholder,
            string email    = TestingConstants.Placeholder,
            string password = TestingConstants.Placeholder,
            string phone    = TestingConstants.Placeholder,
            string[] roles  = null,
            string role     = null)
        {
            var userManager = scope.GetService <UserManager <ApplicationUser> >();

            if (email == TestingConstants.Placeholder)
            {
                email = $"{Guid.NewGuid()}@email.com";
            }

            if (name == TestingConstants.Placeholder)
            {
                name = email;
            }

            if (password == TestingConstants.Placeholder)
            {
                password = TestingConstants.DefaultPassword;
            }

            if (phone == TestingConstants.Placeholder)
            {
                phone = "+11111111111";
            }

            if (roles == null && !string.IsNullOrEmpty(role))
            {
                roles = new[] { role };
            }

            var user = new ApplicationUser
            {
                Name                 = name,
                UserName             = email,
                Email                = email,
                EmailConfirmed       = true,
                PhoneNumber          = phone,
                PhoneNumberConfirmed = !string.IsNullOrEmpty(phone)
            };

            await userManager.CreateAsync(user, password);

            if (roles?.Length > 0)
            {
                await userManager.AddToRolesAsync(user, roles);
            }

            return(new DisposableUser(user, userManager));
        }
Ejemplo n.º 2
0
        public static async Task <IDisposableEntity <ApplicationUser> > CreateUserAsync(
            this TestServiceScope scope,
            string name               = TestingConstants.Placeholder,
            string email              = TestingConstants.Placeholder,
            string password           = TestingConstants.Placeholder,
            string phone              = TestingConstants.Placeholder,
            string[] roles            = null,
            string role               = null,
            Organization organization = null,
            bool archived             = false)
        {
            var userManager = scope.GetService <UserManager <ApplicationUser> >();

            if (email == TestingConstants.Placeholder)
            {
                email = $"{Guid.NewGuid()}@email.com";
            }

            if (name == TestingConstants.Placeholder)
            {
                name = email;
            }

            if (password == TestingConstants.Placeholder)
            {
                password = TestingConstants.DefaultPassword;
            }

            if (phone == TestingConstants.Placeholder)
            {
                phone = $"+{DateTimeOffset.Now.ToUnixTimeMilliseconds():####}";
            }

            if (roles == null && !string.IsNullOrEmpty(role))
            {
                roles = new[] { role };
            }

            var user = new ApplicationUser
            {
                Name                 = name,
                UserName             = email,
                Email                = email,
                EmailConfirmed       = true,
                PhoneNumber          = phone,
                PhoneNumberConfirmed = !string.IsNullOrEmpty(phone),
                Archived             = archived
            };

            await userManager.CreateAsync(user, password);

            if (roles?.Length > 0)
            {
                await userManager.AddToRolesAsync(user, roles);
            }

            if (organization != null)
            {
                await scope.CreateOrganizationMemberAsync(user, organization, role : role, roles : roles);
            }

            return(new DisposableUser(user, userManager));
        }