Example #1
0
        public async Task RemoveFromAdminsShouldRemoveRoleFromUser()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;

            AutoMapperConfig.RegisterMappings(Assembly.Load("CookingBook.Web.ViewModels"));
            var dbContext = new ApplicationDbContext(options);
            var userRepo  = new EfDeletableEntityRepository <ApplicationUser>(dbContext);
            await userRepo.AddAsync(new ApplicationUser
            {
                Name               = "Trayan Keranov",
                Id                 = "trk",
                UserName           = "******",
                NormalizedUserName = "******",
                Email              = "*****@*****.**",
                NormalizedEmail    = "*****@*****.**",
                PasswordHash       = "AQAAAAEAACcQAAAAEKDTOW0hiJThqFCz2cTS+wMBN2HthJkHT1jCsqVhgYwc0XikiVo0ESJYcqs8yrZkgg==",
                SecurityStamp      = "5ZMAFTFQEDOZPXC573KIOV5B56KVMHKS",
                ConcurrencyStamp   = "5ed4afbd-318e-456b-8d1b-19a36f2d82f1",
                CreatedOn          = DateTime.Parse("1993-03-21 08:10:00.2228617"),
                EmailConfirmed     = true,
                Roles              = new List <IdentityUserRole <string> >(),
                LockoutEnabled     = true,
                LockoutEnd         = DateTimeOffset.MaxValue,
            });

            await userRepo.SaveChangesAsync();

            var rolesRepo = new EfDeletableEntityRepository <ApplicationRole>(dbContext);
            await rolesRepo.AddAsync(new ApplicationRole { Name = GlobalConstants.AdministratorRoleName, Id = "admin", CreatedOn = DateTime.MinValue, ConcurrencyStamp = "asdfghjkl", NormalizedName = "ADMIN" });

            await rolesRepo.SaveChangesAsync();

            var service = new UsersService(userRepo, rolesRepo);

            await service.AddToAdmins("trk");

            Assert.Equal(1, dbContext.Users.Find("trk").Roles.Count);

            await service.RemoveFromAdmins("trk");

            Assert.Equal(0, dbContext.Users.Find("trk").Roles.Count);
        }