Example #1
0
        public void UserService_EditAsync_CreatesUserIfNotInDbWithPassword()
        {
            // Arrange
            var dbUser = new DBUser { Id = "dummyId", UserName = "******", Email = "Email2", LDAPAuthenticated_bl = false, Password = "******" };
            
            var dbUsers = new DBUser[] { dbUser };

            var mockAppUserManager = new Mock<IAppUserManager>();
            mockAppUserManager.Setup(x => x.FindByIdAsync(dbUser.Id)).Returns(Task.FromResult<DBUser>(null));
            mockAppUserManager.Setup(x => x.CreateAsync(dbUser, It.IsIn(new[] { dbUser.Password }))).Returns(Task.FromResult(IdentityResult.Success));
            mockAppUserManager.Setup(x => x.UpdateAsync(It.IsAny<DBUser>())).Verifiable();

            var mockDbContextScopeFac = new Mock<IDbContextScopeFactory>();

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

            //Act   
            dbUserService.EditAsync(dbUsers).Wait();

            //Assert
            mockAppUserManager.Verify(x => x.CreateAsync(It.IsAny<DBUser>(), It.IsAny<string>()), Times.Once);
            mockAppUserManager.Verify(x => x.UpdateAsync(It.IsAny<DBUser>()), Times.Never);
            Assert.IsTrue(dbUser.Password == "NewPassword");
            
        }
Example #2
0
        public void UserService_EditAsync_UpdateSingleUserwithGUID()
        {
            // Arrange
            var dbEntry = new DBUser { Id = "dummyId1", UserName = "******", Email = "Email", LDAPAuthenticated_bl = false };
            var dbUser = new DBUser { Id = "dummyId2", UserName = "******", Email = "Email2", LDAPAuthenticated_bl = true, 
                Password = "******", ModifiedProperties = new[] {"Password"} };
            
            var dbUsers = new DBUser[] { dbUser };

            var mockAppUserManager = new Mock<IAppUserManager>();
            mockAppUserManager.Setup(x => x.FindByIdAsync(dbUser.Id)).Returns(Task.FromResult(dbEntry));
            mockAppUserManager.Setup(x => x.UpdateAsync(dbEntry)).Returns(Task.FromResult(IdentityResult.Success));
            mockAppUserManager.Setup(x => x.CreateAsync(It.IsAny<DBUser>(), It.IsAny<string>())).Verifiable();
            mockAppUserManager.Setup(x => x.HashPassword(It.IsAny<string>())).Returns((string s) => s + "out");

            var mockDbContextScopeFac = new Mock<IDbContextScopeFactory>();

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

            //Act   
            dbUserService.EditAsync(dbUsers).Wait();

            //Assert
            Assert.IsTrue(dbEntry.Id != dbUser.Id);
            Assert.IsTrue(dbEntry.UserName != dbUser.UserName);
            Assert.IsTrue(dbEntry.Email != dbUser.Email);
            Assert.IsTrue(dbEntry.LDAPAuthenticated_bl != dbUser.LDAPAuthenticated_bl);
            var regex = new Regex(@"\w*-\w*-\w*-\w*-\w*out\b"); Assert.IsTrue(regex.IsMatch(dbEntry.PasswordHash));
            mockAppUserManager.Verify(x => x.CreateAsync(It.IsAny<DBUser>(), It.IsAny<string>()), Times.Never);
        }
Example #3
0
        public void UserService_EditAsync_DoesNotUpdateUserifPasswordChangedAndNoPassword()
        {
            // Arrange
            var dbEntry = new DBUser { Id = "dummyEntryId1", UserName = "******", Email = "Email", LDAPAuthenticated_bl = true };
            var dbUser = new DBUser
            {
                Id = "dummyUserId1",
                UserName = "******",
                Email = "Email2",
                LDAPAuthenticated_bl = false,
                Password = "",
                ModifiedProperties = new[] { "Password" },
            };

            var dbUsers = new DBUser[] { dbUser };

            var mockAppUserManager = new Mock<IAppUserManager>();
            mockAppUserManager.Setup(x => x.FindByIdAsync(dbUser.Id)).Returns(Task.FromResult(dbEntry));
            mockAppUserManager.Setup(x => x.UpdateAsync(It.IsAny<DBUser>())).Verifiable();
            mockAppUserManager.Setup(x => x.CreateAsync(It.IsAny<DBUser>(), It.IsAny<string>())).Verifiable();

            var mockDbContextScopeFac = new Mock<IDbContextScopeFactory>();

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

            //Act   
            dbUserService.EditAsync(dbUsers).Wait();

            //Assert
            Assert.IsTrue(dbEntry.Id != dbUser.Id);
            Assert.IsTrue(dbEntry.UserName != dbUser.UserName);
            Assert.IsTrue(dbEntry.Email != dbUser.Email);
            Assert.IsTrue(dbEntry.LDAPAuthenticated_bl != dbUser.LDAPAuthenticated_bl);
            Assert.IsTrue(dbEntry.PasswordHash == null);
            mockAppUserManager.Verify(x => x.FindByIdAsync(It.IsAny<string>()), Times.Never());
            mockAppUserManager.Verify(x => x.UpdateAsync(It.IsAny<DBUser>()), Times.Never());
            mockAppUserManager.Verify(x => x.CreateAsync(It.IsAny<DBUser>(), It.IsAny<string>()), Times.Never);
        }
Example #4
0
        public void UserService_EditAsync_ReturnsErrorifNewUserwithNoPasswordAndLDAPFalse()
        {
            // Arrange
            var dbUser1 = new DBUser
            {
                Id = "dummyUserId1",
                UserName = "******",
                Email = "UserEmail1",
                LDAPAuthenticated_bl = false,
                ModifiedProperties = new[] { "UserName", "Email", "LDAPAuthenticated_bl" }
            };

            var dbUsers = new DBUser[] { dbUser1 };

            var mockAppUserManager = new Mock<IAppUserManager>();
            mockAppUserManager.Setup(x => x.FindByIdAsync(dbUser1.Id)).Returns(Task.FromResult((DBUser)null));
            mockAppUserManager.Setup(x => x.UpdateAsync(It.IsAny<DBUser>())).Verifiable();
            mockAppUserManager.Setup(x => x.CreateAsync(It.IsAny<DBUser>(), It.IsAny<string>()))
                .Returns(Task.FromResult(new IdentityResult("no password")));
            mockAppUserManager.Setup(x => x.HashPassword(It.IsAny<string>())).Returns((string s) => s + "out");

            var mockDbContextScopeFac = new Mock<IDbContextScopeFactory>();

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

            //Act   
            dbUserService.EditAsync(dbUsers).Wait();

            //Assert
            
        }
Example #5
0
        public void UserService_EditAsync_UpdateSingleUserReportErrorsFromIdentityResult()
        {
            // Arrange
            var dbEntry1 = new DBUser { Id = "dummyEntryId1", UserName = "******", Email = "EntryEmail1", LDAPAuthenticated_bl = true };
            var dbUser1 = new DBUser
            {
                Id = "dummyUserId1",
                UserName = "******",
                Email = "UserEmail1",
                LDAPAuthenticated_bl = false,
                Password = "******",
                ModifiedProperties = new[] { "UserName", "Email", "LDAPAuthenticated_bl", "Password" }
            };

            var dbUsers = new DBUser[] { dbUser1 };

            var mockAppUserManager = new Mock<IAppUserManager>();
            mockAppUserManager.Setup(x => x.FindByIdAsync(dbUser1.Id)).Returns(Task.FromResult(dbEntry1));
            mockAppUserManager.Setup(x => x.UpdateAsync(dbEntry1)).Returns(Task.FromResult(IdentityResult.Failed(new[] { "Error1", "Error2" })));
            mockAppUserManager.Setup(x => x.CreateAsync(It.IsAny<DBUser>(), It.IsAny<string>())).Verifiable();
            mockAppUserManager.Setup(x => x.HashPassword(It.IsAny<string>())).Returns((string s) => s + "out");

            var mockDbContextScopeFac = new Mock<IDbContextScopeFactory>();

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

            //Act   
            dbUserService.EditAsync(dbUsers).Wait();

            //Assert
        }
Example #6
0
        public void UserService_EditAsync_UpdateManyUsersDoUpdateModified()
        {
            // Arrange
            var dbEntry1 = new DBUser { Id = "dummyEntryId1", UserName = "******", Email = "EntryEmail1", LDAPAuthenticated_bl = true };
            var dbEntry2 = new DBUser { Id = "dummyEntryId2", UserName = "******", Email = "EntryEmail2", LDAPAuthenticated_bl = false };
            var dbUser1 = new DBUser { Id = "dummyUserId1", UserName = "******", Email = "UserEmail1", LDAPAuthenticated_bl = false, Password = "******", 
                ModifiedProperties = new[] { "UserName", "Email", "LDAPAuthenticated_bl", } };
            var dbUser2 = new DBUser { Id = "dummyUserId2", UserName = "******", Email = "UserEmail2", LDAPAuthenticated_bl = true, Password = "******" };

            var dbUsers = new DBUser[] { dbUser1, dbUser2 };

            var mockAppUserManager = new Mock<IAppUserManager>();
            mockAppUserManager.Setup(x => x.FindByIdAsync(dbUser1.Id)).Returns(Task.FromResult(dbEntry1));
            mockAppUserManager.Setup(x => x.FindByIdAsync(dbUser2.Id)).Returns(Task.FromResult(dbEntry2));
            mockAppUserManager.Setup(x => x.UpdateAsync(dbEntry1)).Returns(Task.FromResult(IdentityResult.Success));
            mockAppUserManager.Setup(x => x.UpdateAsync(dbEntry2)).Returns(Task.FromResult(IdentityResult.Success));
            mockAppUserManager.Setup(x => x.CreateAsync(It.IsAny<DBUser>(), It.IsAny<string>())).Verifiable();
            mockAppUserManager.Setup(x => x.HashPassword(It.IsAny<string>())).Returns((string s) => s + "out");

            var mockDbContextScopeFac = new Mock<IDbContextScopeFactory>();

            var dbUserService = new DBUserService(mockDbContextScopeFac.Object, mockAppUserManager.Object, true);
            
            //Act   
            dbUserService.EditAsync(dbUsers).Wait();

            //Assert
            Assert.IsTrue(dbEntry1.Id != dbUser1.Id);
            Assert.IsTrue(dbEntry1.UserName == dbUser1.UserName);
            Assert.IsTrue(dbEntry1.Email == dbUser1.Email);
            Assert.IsTrue(dbEntry1.LDAPAuthenticated_bl == dbUser1.LDAPAuthenticated_bl);
            Assert.IsTrue(dbEntry1.PasswordHash == null);
            Assert.IsTrue(dbEntry2.Id != dbUser2.Id);
            Assert.IsTrue(dbEntry2.UserName != dbUser2.UserName);
            Assert.IsTrue(dbEntry2.Email != dbUser2.Email);
            Assert.IsTrue(dbEntry2.LDAPAuthenticated_bl != dbUser2.LDAPAuthenticated_bl);
            Assert.IsTrue(dbEntry2.PasswordHash == null);
            mockAppUserManager.Verify(x => x.CreateAsync(It.IsAny<DBUser>(), It.IsAny<string>()), Times.Never);
            mockAppUserManager.Verify(x => x.UpdateAsync(It.IsAny<DBUser>()), Times.Exactly(2));
        }