public async Task ensure_has_claim()
        {
            var claim = new Claim(SomeRandom.String(), SomeRandom.String());
            var user  = DbContext.Users.First();

            var result = await Target.EnsureHasClaimAsync(user.Id, claim.Type, claim.Value);

            result.Succeeded.Should().BeTrue();
            await Target.EnsureHasClaimAsync(user.Id, claim.Type, claim.Value);

            var claims = await Target.GetClaimsAsync(user.Id);

            claims.Where(c => c.Type == claim.Type).Should().HaveCount(1);
        }
예제 #2
0
        public static void SeedContext(ApplicationDbContext context)
        {
            var userManager = new ApplicationUserManager(new UserStore <ApplicationUser>(context));
            var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));

            roleManager.Create(new IdentityRole()
            {
                Name = Constants.Roles.Administrator
            });
            var adminEmail = "*****@*****.**";
            var user       = userManager.FindByName(adminEmail);

            if (user == null)
            {
                user = new ApplicationUser()
                {
                    UserName = "******",
                    Email    = "*****@*****.**",
                    Person   = new Person()
                    {
                        FirstName = "Admin",
                        LastName  = "Istrator",
                    }
                };
                var result = userManager.Create(user, Guid.NewGuid().ToString() + SomeRandom.String(10).ToUpper() + "!");
                if (!result.Succeeded)
                {
                    throw new InvalidOperationException(String.Join(";", result.Errors));
                }
                userManager.AddClaim(user.Id, new Claim(Constants.Claims.Staff, bool.TrueString));
            }
            try
            {
                if (!userManager.IsInRole(user.Id, Constants.Roles.Administrator))
                {
                    userManager.AddToRole(user.Id, Constants.Roles.Administrator);
                }
            }
            catch
            {
                // we'll need to run again.
            }
            var userShortage = 200 - context.Users.Count();

            for (int i = 0; i < userShortage; i++)
            {
                var randomUser = new ApplicationUser()
                {
                    UserName = $"{SomeRandom.String()}@example.com",
                    Email    = $"{SomeRandom.String()}@example.com",
                    Person   = new Person()
                    {
                        FirstName    = SomeRandom.Name(),
                        LastName     = SomeRandom.Name(),
                        EmailAddress = SomeRandom.EmailAddress(),
                        PhoneNumber  = SomeRandom.PhoneNumber()
                    }
                };
                userManager.Create(randomUser, SomeRandom.Name(20) + ".1");
            }

            var tb = new TagBuilder(context);

            if (!context.Tags.Any())
            {
                tb.CreateContextTag("Business", "Business", "A loss in a business context.")
                .WithLosses("Employee", "Employee family member")
                .WithRelationships("HR", "Employee", "Employee family member", "Owner", "Other");
                tb.CreateContextTag("Personal", "Personal", "A loss in a family context.")
                .WithLosses("Child", "Parent", "Sibling", "Friend", "Other")
                .WithRelationships("Parent", "Child", "Sibling", "Spouse", "Extended family", "Pastor", "Other");
                tb.CreateContextTag("Educational", "Educational", "A loss at a school or university.")
                .WithLosses("Student", "Faculty", "Staff", "Family of student")
                .WithRelationships("Administrator", "Faculty", "Student", "Family", "Other");
                tb.CreateContextTag("Organizational", "Church, sports team, or other organization", "A loss at a church or social organization.")
                .WithLosses("Member", "Affiliate")
                .WithRelationships("Member", "Leader");
            }

            for (int i = 1; i < 7; i++)
            {
                context.Items.AddOrUpdate(new Article()
                {
                    Id         = i,
                    CreatedBy  = user.UserName,
                    CreatedUtc = DateTimeOffset.UtcNow,
                    Content    =
                        "Lorem ipsum dolor sit amet, sapien etiam, nunc amet dolor ac odio mauris justo. Luctus arcu, urna praesent at id quisque ac. Arcu es massa vestibulum malesuada, integer vivamus elit eu mauris eus, cum eros quis aliquam wisi. Nulla wisi laoreet suspendisse integer vivamus elit eu mauris hendrerit facilisi, mi mattis pariatur aliquam pharetra eget.",
                    Title = "Article " + i,
                    Order = i,
                });
            }

            for (int i = 1; i < 5; i++)
            {
                context.Items.AddOrUpdate(new ToDo()
                {
                    Id         = i + 7,
                    CreatedBy  = user.UserName,
                    CreatedUtc = DateTimeOffset.UtcNow,
                    Content    =
                        "Luctus arcu, urna praesent at id quisque ac. Arcu es massa vestibulum malesuada, integer vivamus elit eu mauris eus.",
                    Title = "ToDo " + i,
                    Order = i,
                });
            }
        }