コード例 #1
0
        public void CallAndReturnResultFromUserManagerUpdateAsyncMethod()
        {
            // Arrange
            var expectedResult = IdentityResult.Failed();
            var userId         = this.Fixture.Create <string>();
            var email          = this.Fixture.Create <string>();
            var username       = this.Fixture.Create <string>();
            var isConfirmed    = this.Fixture.Create <bool>();
            var mockedUser     = new MockedApplicationUser();

            var userManager = new Mock <IApplicationUserManager>();

            userManager.Setup(f => f.FindById(userId))
            .Returns(mockedUser);
            userManager.Setup(m => m.UpdateAsync(mockedUser))
            .Returns(Task.FromResult(expectedResult));

            var signInManager = new Mock <IApplicationSignInManager>();
            var modelFactory  = new Mock <IModelFactory>();

            var service = new UserService(userManager.Object,
                                          signInManager.Object,
                                          modelFactory.Object);

            // Act
            var result = service.UpdateUser(userId, email, username, isConfirmed);

            // Assert
            Assert.AreSame(expectedResult, result);
            userManager.Verify(m => m.UpdateAsync(mockedUser), Times.Once);
        }
コード例 #2
0
        public void SetUserPropertiesCorrectly()
        {
            // Arrange
            var expectedResult = IdentityResult.Failed();
            var userId         = this.Fixture.Create <string>();
            var email          = this.Fixture.Create <string>();
            var username       = this.Fixture.Create <string>();
            var isConfirmed    = this.Fixture.Create <bool>();
            var mockedUser     = new MockedApplicationUser()
            {
                EmailConfirmed = !isConfirmed
            };

            var userManager = new Mock <IApplicationUserManager>();

            userManager.Setup(f => f.FindById(userId))
            .Returns(mockedUser);
            userManager.Setup(m => m.UpdateAsync(mockedUser))
            .Returns(Task.FromResult(expectedResult));

            var signInManager = new Mock <IApplicationSignInManager>();
            var modelFactory  = new Mock <IModelFactory>();

            var service = new UserService(userManager.Object,
                                          signInManager.Object,
                                          modelFactory.Object);

            // Act
            service.UpdateUser(userId, email, username, isConfirmed);

            // Assert
            Assert.AreSame(email, mockedUser.Email);
            Assert.AreSame(username, mockedUser.UserName);
            Assert.AreEqual(isConfirmed, mockedUser.EmailConfirmed);
        }
コード例 #3
0
        public void ReturnResultFromUserManager()
        {
            // Arrange
            var userName       = this.Fixture.Create <string>();
            var email          = this.Fixture.Create <string>();
            var password       = this.Fixture.Create <string>();
            var mockedUser     = new MockedApplicationUser();
            var expectedResult = IdentityResult.Failed();

            var userManager = new Mock <IApplicationUserManager>();

            userManager.Setup(m => m.Create(It.IsAny <ApplicationUser>(), It.IsAny <string>()))
            .Returns(expectedResult);

            var signInManager = new Mock <IApplicationSignInManager>();
            var modelFactory  = new Mock <IModelFactory>();

            modelFactory.Setup(f => f.CreateApplicationUser(userName, email))
            .Returns(mockedUser);

            var service = new UserService(userManager.Object,
                                          signInManager.Object,
                                          modelFactory.Object);

            // Act
            var result = service.RegisterUser(userName, email, password);

            // Assert
            Assert.AreSame(expectedResult, result);
        }
コード例 #4
0
        public void Call_UserManagerFindByIdMethodOnceWithCorrectParams()
        {
            // Arrange
            var userId      = this.Fixture.Create <string>();
            var email       = this.Fixture.Create <string>();
            var username    = this.Fixture.Create <string>();
            var isConfirmed = this.Fixture.Create <bool>();
            var mockedUser  = new MockedApplicationUser();

            var userManager = new Mock <IApplicationUserManager>();

            userManager.Setup(f => f.FindById(userId))
            .Returns(mockedUser);

            var signInManager = new Mock <IApplicationSignInManager>();
            var modelFactory  = new Mock <IModelFactory>();

            var service = new UserService(userManager.Object,
                                          signInManager.Object,
                                          modelFactory.Object);

            // Act
            service.UpdateUser(userId, email, username, isConfirmed);

            // Assert
            userManager.Verify(m => m.FindById(userId), Times.Once);
        }
コード例 #5
0
        public void CallUserManagerCreateMethodOnce()
        {
            // Arrange
            var userName   = this.Fixture.Create <string>();
            var email      = this.Fixture.Create <string>();
            var password   = this.Fixture.Create <string>();
            var mockedUser = new MockedApplicationUser();

            var userManager   = new Mock <IApplicationUserManager>();
            var signInManager = new Mock <IApplicationSignInManager>();
            var modelFactory  = new Mock <IModelFactory>();

            modelFactory.Setup(f => f.CreateApplicationUser(userName, email))
            .Returns(mockedUser);

            var service = new UserService(userManager.Object,
                                          signInManager.Object,
                                          modelFactory.Object);

            // Act
            var result = service.RegisterUser(userName, email, password);

            // Assert
            userManager.Verify(m => m.Create(mockedUser, password), Times.Once);
        }
コード例 #6
0
        public void CallReturnResult()
        {
            var userId          = this.Fixture.Create <string>();
            var currentPassword = this.Fixture.Create <string>();
            var newPassword     = this.Fixture.Create <string>();
            var mockedUser      = new MockedApplicationUser();
            var expectedResult  = IdentityResult.Success;

            var userManager = new Mock <IApplicationUserManager>();

            userManager.Setup(f => f.FindById(userId))
            .Returns(mockedUser);
            userManager.Setup(m => m.ChangePassword(userId, currentPassword, newPassword))
            .Returns(expectedResult);

            var signInManager = new Mock <IApplicationSignInManager>();
            var modelFactory  = new Mock <IModelFactory>();

            var service = new UserService(userManager.Object,
                                          signInManager.Object,
                                          modelFactory.Object);

            var result = service.ChangePassword(userId, currentPassword, newPassword);

            Assert.AreSame(expectedResult, result);
        }
コード例 #7
0
        public void CallFindByIdAndSignInMethodsOnce_IfResultIsSuccessfull()
        {
            var userId          = this.Fixture.Create <string>();
            var currentPassword = this.Fixture.Create <string>();
            var newPassword     = this.Fixture.Create <string>();
            var mockedUser      = new MockedApplicationUser();

            var userManager = new Mock <IApplicationUserManager>();

            userManager.Setup(f => f.FindById(userId))
            .Returns(mockedUser);
            userManager.Setup(m => m.ChangePassword(userId, currentPassword, newPassword))
            .Returns(IdentityResult.Success);

            var signInManager = new Mock <IApplicationSignInManager>();
            var modelFactory  = new Mock <IModelFactory>();

            var service = new UserService(userManager.Object,
                                          signInManager.Object,
                                          modelFactory.Object);

            var result = service.ChangePassword(userId, currentPassword, newPassword);

            userManager.Verify(m => m.FindById(userId), Times.Once);
            signInManager.Verify(m => m.SignIn(mockedUser, false, false), Times.Once);
        }
コード例 #8
0
        public void CallUserManagerChangepasswordMethodOnce()
        {
            var userId          = this.Fixture.Create <string>();
            var currentPassword = this.Fixture.Create <string>();
            var newPassword     = this.Fixture.Create <string>();
            var mockedUser      = new MockedApplicationUser();

            var userManager = new Mock <IApplicationUserManager>();

            userManager.Setup(f => f.FindById(userId))
            .Returns(mockedUser);
            userManager.Setup(m => m.ChangePassword(userId, currentPassword, newPassword))
            .Returns(IdentityResult.Failed());

            var signInManager = new Mock <IApplicationSignInManager>();
            var modelFactory  = new Mock <IModelFactory>();

            var service = new UserService(userManager.Object,
                                          signInManager.Object,
                                          modelFactory.Object);

            var result = service.ChangePassword(userId, currentPassword, newPassword);

            userManager.Verify(m => m.ChangePassword(userId, currentPassword, newPassword), Times.Once);
        }
コード例 #9
0
        public void CallAndReturnValueFromFindByIdMethodOnceWithCorrectParams()
        {
            var expectedUser = new MockedApplicationUser();
            var expectedId   = this.Fixture.Create <string>();
            var userManager  = new Mock <IApplicationUserManager>();

            userManager.Setup(m => m.FindById(expectedId))
            .Returns(expectedUser);

            var signInManager = new Mock <IApplicationSignInManager>();
            var modelFactory  = new Mock <IModelFactory>();

            var service = new UserService(userManager.Object,
                                          signInManager.Object,
                                          modelFactory.Object);

            var result = service.GetById(expectedId);

            Assert.AreSame(expectedUser, result);
            userManager.Verify(u => u.FindById(expectedId), Times.Once);
        }
コード例 #10
0
        public void ReturnValueFromUserManager_WhenUserIsFound()
        {
            var userName = this.Fixture.Create <string>();

            var mockedUser = new MockedApplicationUser();

            var userManager = new Mock <IApplicationUserManager>();

            userManager.Setup(m => m.FindByName(userName))
            .Returns(mockedUser);

            var signInManager = new Mock <IApplicationSignInManager>();
            var modelFactory  = new Mock <IModelFactory>();

            var service = new UserService(userManager.Object,
                                          signInManager.Object,
                                          modelFactory.Object);

            var result = service.FindByName(userName);

            Assert.AreSame(mockedUser, result);
        }
コード例 #11
0
        public void CallFindByNameMethodOnce()
        {
            var userName = this.Fixture.Create <string>();

            var mockedUser = new MockedApplicationUser();

            var userManager = new Mock <IApplicationUserManager>();

            userManager.Setup(m => m.FindByName(userName))
            .Returns(mockedUser);

            var signInManager = new Mock <IApplicationSignInManager>();
            var modelFactory  = new Mock <IModelFactory>();

            var service = new UserService(userManager.Object,
                                          signInManager.Object,
                                          modelFactory.Object);

            var result = service.FindByName(userName);

            userManager.Verify(m => m.FindByName(userName), Times.Once);
        }