예제 #1
0
        public void UserService_EditRolesAsync_Remove_RemovesRoles()
        {
            // Arrange
            var userIds = new string[] { "dummyUserId1", "dummyUserId2" };
            var dbRoles = new string[] { "dummyRole1", "dummyRole2" };

            var dbEntry1 = new DBUser
            {
                Id = "dummyUserId1",
                UserName = "******",
                Email = "Email1",
                LDAPAuthenticated_bl = false
            };
            var dbEntry2 = new DBUser
            {
                Id = "dummyUserId2",
                UserName = "******",
                Email = "Email2",
                LDAPAuthenticated_bl = true
            };
            var dbEntries = (new List<DBUser> { dbEntry1, dbEntry2 }).AsQueryable();

            var mockDbSet = new Mock<DbSet<DBUser>>();
            mockDbSet.As<IDbAsyncEnumerable<DBUser>>().Setup(m => m.GetAsyncEnumerator())
                .Returns(new MockDbAsyncEnumerator<DBUser>(dbEntries.GetEnumerator()));
            mockDbSet.As<IQueryable<DBUser>>().Setup(m => m.Provider)
                .Returns(new MockDbAsyncQueryProvider<PersonLogEntry>(dbEntries.Provider));
            mockDbSet.As<IQueryable<DBUser>>().Setup(m => m.Expression).Returns(dbEntries.Expression);
            mockDbSet.As<IQueryable<DBUser>>().Setup(m => m.ElementType).Returns(dbEntries.ElementType);
            mockDbSet.As<IQueryable<DBUser>>().Setup(m => m.GetEnumerator()).Returns(dbEntries.GetEnumerator());
            mockDbSet.Setup(x => x.Include(It.IsAny<string>())).Returns(mockDbSet.Object);

            var mockRolesList = new Mock<IList<string>>();
            mockRolesList.Setup(x => x.Contains(dbRoles[0])).Returns(true);
            mockRolesList.Setup(x => x.Contains(dbRoles[1])).Returns(true);
            var mockAppUserManager = new Mock<IAppUserManager>();
            mockAppUserManager.Setup(x => x.FindByIdAsync(It.IsAny<string>())).Returns(Task.FromResult(new DBUser { }));
            mockAppUserManager.Setup(x => x.GetRolesAsync(It.IsAny<string>())).Returns(Task.FromResult(mockRolesList.Object));
            mockAppUserManager.Setup(x => x.AddToRoleAsync(It.IsAny<string>(), It.IsAny<string>())).Returns(Task.FromResult(IdentityResult.Success));
            mockAppUserManager.Setup(x => x.RemoveFromRoleAsync(It.IsAny<string>(), It.IsAny<string>())).Returns(Task.FromResult(IdentityResult.Success));
            mockAppUserManager.Setup(x => x.Users).Returns(mockDbSet.Object);
            
            var mockDbContextScopeFac = new Mock<IDbContextScopeFactory>();

            var dbUserService = new DBUserService(mockDbContextScopeFac.Object, mockAppUserManager.Object, true);

            //Act   
            dbUserService.AddRemoveRolesAsync(userIds, dbRoles, false).Wait();

            //Assert
            mockAppUserManager.Verify(x => x.AddToRoleAsync(It.IsAny<string>(), It.IsAny<string>()), Times.Never);
            mockAppUserManager.Verify(x => x.RemoveFromRoleAsync(It.IsAny<string>(), It.IsAny<string>()), Times.Exactly(4));
        }
예제 #2
0
        public void UserService_EditRolesAsync_ReturnErrorIfUserNotFound()
        {
            // Arrange
            var userIds = new string[] { "dummyUserId1", "dummyUserId2", "dummyUserId3" };
            var dbRoles = new string[] { "dummyRole1", "dummyRole2" };

            var dbEntry1 = new DBUser
            {
                Id = "dummyUserId1",
                UserName = "******",
                Email = "Email1",
                LDAPAuthenticated_bl = false
            };
            var dbEntry2 = new DBUser
            {
                Id = "dummyUserId2",
                UserName = "******",
                Email = "Email2",
                LDAPAuthenticated_bl = true
            };
            var dbEntries = (new List<DBUser> { dbEntry1, dbEntry2 }).AsQueryable();

            var mockDbSet = new Mock<DbSet<DBUser>>();
            mockDbSet.As<IDbAsyncEnumerable<DBUser>>().Setup(m => m.GetAsyncEnumerator())
                .Returns(new MockDbAsyncEnumerator<DBUser>(dbEntries.GetEnumerator()));
            mockDbSet.As<IQueryable<DBUser>>().Setup(m => m.Provider)
                .Returns(new MockDbAsyncQueryProvider<PersonLogEntry>(dbEntries.Provider));
            mockDbSet.As<IQueryable<DBUser>>().Setup(m => m.Expression).Returns(dbEntries.Expression);
            mockDbSet.As<IQueryable<DBUser>>().Setup(m => m.ElementType).Returns(dbEntries.ElementType);
            mockDbSet.As<IQueryable<DBUser>>().Setup(m => m.GetEnumerator()).Returns(dbEntries.GetEnumerator());
            mockDbSet.Setup(x => x.Include(It.IsAny<string>())).Returns(mockDbSet.Object);

            var mockAppUserManager = new Mock<IAppUserManager>();
            mockAppUserManager.Setup(x => x.Users).Returns(mockDbSet.Object);

            var mockDbContextScopeFac = new Mock<IDbContextScopeFactory>();

            var dbUserService = new DBUserService(mockDbContextScopeFac.Object, mockAppUserManager.Object, true);

            //Act   
            dbUserService.AddRemoveRolesAsync(userIds, dbRoles, true).Wait();

            //Assert
        }