Example #1
0
        public async Task UpdateAsync_should_replace_role()
        {
            var provider = new ServiceCollection()
                           .AddLogging()
                           .AddDbContext <IdentityDbContext <IdentityUser> >(options => options.UseInMemoryDatabase(Guid.NewGuid().ToString()))
                           .AddIdentity <IdentityUser, IdentityRole>()
                           .AddEntityFrameworkStores <IdentityDbContext <IdentityUser> >()
                           .Services.BuildServiceProvider();

            var userManager = provider.GetRequiredService <UserManager <IdentityUser> >();
            var context     = provider.GetRequiredService <IdentityDbContext <IdentityUser> >();
            var sut         = new IdentityUserRoleStore <IdentityUser>(userManager, context, provider.GetRequiredService <ILogger <IdentityUserRoleStore <IdentityUser> > >());

            var id = Guid.NewGuid().ToString();
            await userManager.CreateAsync(new IdentityUser
            {
                Email    = "*****@*****.**",
                Id       = id,
                UserName = id
            }).ConfigureAwait(false);

            var roleManager = provider.GetRequiredService <RoleManager <IdentityRole> >();
            await roleManager.CreateAsync(new IdentityRole
            {
                Id   = id,
                Name = id
            }).ConfigureAwait(false);

            var newId = Guid.NewGuid().ToString();
            await roleManager.CreateAsync(new IdentityRole
            {
                Id   = newId,
                Name = newId
            }).ConfigureAwait(false);

            await sut.CreateAsync(new UserRole
            {
                RoleId = id,
                UserId = id
            } as object).ConfigureAwait(false);


            await sut.UpdateAsync(new UserRole
            {
                Id     = $"{id}@{id}",
                RoleId = newId,
                UserId = id
            } as object).ConfigureAwait(false);

            var removed = await sut.GetAsync($"{id}@{id}", null).ConfigureAwait(false);

            Assert.Null(removed);

            var added = await sut.GetAsync($"{id}@{newId}", null).ConfigureAwait(false);

            Assert.NotNull(added);
        }
        public async Task UdpateAsync_should_update_role()
        {
            using var documentStore = new RavenDbTestDriverWrapper().GetDocumentStore();
            var services = new ServiceCollection()
                           .AddLogging();

            services.AddIdentity <IdentityUser, IdentityRole>()
            .AddRavenDbStores(p => documentStore);

            IServiceProvider provider = services.AddIdentityServer4AdminRavenDbkStores <IdentityUser, IdentityRole>(p => documentStore).BuildServiceProvider();

            using var scope = provider.CreateScope();
            provider        = scope.ServiceProvider;

            var userManager = provider.GetRequiredService <UserManager <IdentityUser> >();
            var user        = new IdentityUser
            {
                Id             = Guid.NewGuid().ToString(),
                Email          = "*****@*****.**",
                EmailConfirmed = true,
                UserName       = Guid.NewGuid().ToString()
            };
            var result = await userManager.CreateAsync(user);

            Assert.True(result.Succeeded);

            var roleManager = provider.GetRequiredService <RoleManager <IdentityRole> >();
            var role        = new IdentityRole
            {
                Id   = Guid.NewGuid().ToString(),
                Name = Guid.NewGuid().ToString()
            };
            var roleResult = await roleManager.CreateAsync(role);

            Assert.True(roleResult.Succeeded);

            result = await userManager.AddToRoleAsync(user, role.Name);

            Assert.True(result.Succeeded);

            var sut = new IdentityUserRoleStore <IdentityUser>(userManager, new ScopedAsynDocumentcSession(documentStore.OpenAsyncSession()), provider.GetRequiredService <ILogger <IdentityUserRoleStore <IdentityUser> > >());
            await sut.UpdateAsync(new Entity.UserRole
            {
                Id     = $"{user.Id}@{role.Id}",
                UserId = user.Id,
                RoleId = role.Id,
            } as object);

            var roles = await userManager.GetRolesAsync(user);

            Assert.Single(roles);
        }