Пример #1
0
        public async Task ShouldThrowExceptionOnAddingUserToNonexistantRole()
        {
            DocumentDbUserStore <DocumentDbIdentityUser> userStore = CreateUserStore();
            DocumentDbRoleStore <DocumentDbIdentityRole> roleStore = CreateRoleStore();
            DocumentDbIdentityUser targetUser          = DocumentDbIdentityUserBuilder.Create();
            DocumentDbIdentityRole someNotTargetedRole = DocumentDbIdentityRoleBuilder.Create().WithId().WithNormalizedRoleName();

            // Create a role so there is a differently named role in the store
            await roleStore.CreateAsync(someNotTargetedRole, CancellationToken.None);

            // Add the user to a role name different than the role created before, expecting an exception
            await Assert.ThrowsAsync(typeof(ArgumentException), async() => await userStore.AddToRoleAsync(targetUser, "NotExistantRole", CancellationToken.None));
        }
Пример #2
0
        public async Task ShouldThrowExceptionWhenPassingNotNormalizedNameToAddToRole()
        {
            DocumentDbUserStore <DocumentDbIdentityUser> userStore = CreateUserStore();
            DocumentDbRoleStore <DocumentDbIdentityRole> roleStore = CreateRoleStore();
            DocumentDbIdentityUser targetUser = DocumentDbIdentityUserBuilder.Create();
            DocumentDbIdentityRole targetRole = DocumentDbIdentityRoleBuilder.Create().WithId().WithNormalizedRoleName();

            // Create sample data role
            await roleStore.CreateAsync(targetRole, CancellationToken.None);

            // Add the user to the created role, but pass the not normalized name, expecting an exception
            await Assert.ThrowsAsync(typeof(ArgumentException), async() => await userStore.AddToRoleAsync(targetUser, targetRole.Name, CancellationToken.None));
        }
Пример #3
0
        public async Task ShouldCreateNewRoleInDatabase()
        {
            DocumentDbIdentityRole newRole = DocumentDbIdentityRoleBuilder.Create().WithId();
            DocumentDbRoleStore <DocumentDbIdentityRole> store = CreateRoleStore();

            // Create the new role
            IdentityResult result = store.CreateAsync(newRole, CancellationToken.None).Result;

            // Get it again from the DB to check if it was created correctly
            DocumentDbIdentityRole queriedRole = await store.FindByIdAsync(newRole.Id, CancellationToken.None);

            Assert.True(result.Succeeded);
            Assert.Equal(queriedRole, newRole, new DocumentDbIdentityRoleComparer());
        }
Пример #4
0
        public async Task ShouldAddUserToRole()
        {
            DocumentDbUserStore <DocumentDbIdentityUser> userStore = CreateUserStore();
            DocumentDbRoleStore <DocumentDbIdentityRole> roleStore = CreateRoleStore();
            DocumentDbIdentityUser targetUser = DocumentDbIdentityUserBuilder.Create();
            DocumentDbIdentityRole targetRole = DocumentDbIdentityRoleBuilder.Create("RoleName").WithId().WithNormalizedRoleName();

            // Create sample data role
            await roleStore.CreateAsync(targetRole, CancellationToken.None);

            // Add the created sample data role to the user
            await userStore.AddToRoleAsync(targetUser, targetRole.NormalizedName, CancellationToken.None);

            Assert.Contains(targetUser.Roles, r => r.NormalizedName.Equals(targetRole.NormalizedName));
        }