예제 #1
0
        public void AttemptToLogUserIn_ModelReturnsBadOrIncorrectUsernameOrPasswordError()
        {
            using (ShimsContext.Create())
            {
                // Arrange
                const string userName = "******";
                const string password = "******";
                const string userHostAddress = "doesNotMatter";
                const string returnUrl = "doesNotMatter";
                var wasLoginModelAttemptToLogUserInCalled = false;

                 ShimLoginModel.AllInstances.AttemptToLogUserInStringString = (loginModel, userNameSentToLoginModel, passwordSentToLoginModel) =>
                    {
                        wasLoginModelAttemptToLogUserInCalled = true;
                        var result = new LoginModel()
                        {
                            Errors = true,
                            Message = SIMPL.Models.Code.Constants.Areas.Common.LoginModel.AuthenticationResultMessages.BadOrIncorrectUsernameOrPassword
                        };
                        return result;
                    };

                // Act
                var resultAttemptToLogUserIn = LoginViewModelForTests.AttemptToLogUserIn(userName, password, userHostAddress, returnUrl);

                // Assert
                // Only testing the items that could be impacted by the logic of this test
                Assert.IsNotNull(resultAttemptToLogUserIn, "resultAttemptToLogUserIn");
                Assert.IsTrue(wasLoginModelAttemptToLogUserInCalled, "wasGetInitialLoginViewModelStringStringStringCalled");
                Assert.AreEqual(SIMPL.Models.Code.Constants.Areas.Common.LoginModel.AuthenticationResultMessages.BadOrIncorrectUsernameOrPassword, resultAttemptToLogUserIn.Message, "Message");
                Assert.AreEqual(string.Empty, resultAttemptToLogUserIn.RedirectUrl, "RedirectUrl");
            }
        }
예제 #2
0
        /// <summary>
        /// AttemptToLogUserIn method
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public LoginModel AttemptToLogUserIn(string userName, string password)
        {
            LoginModel myLoginModel = new LoginModel();

            var myUserManagement = new UserManagement();
            var authenticationResult = myUserManagement.Authenticate(userName, password);

            if ((authenticationResult == UserManagement.AuthenticationResults.InvalidPassword)
                || (authenticationResult == UserManagement.AuthenticationResults.NoMatchingUser))
            {
                myLoginModel.Message = Constants.Areas.Common.LoginModel.AuthenticationResultMessages.BadOrIncorrectUsernameOrPassword;
                myLoginModel.Errors = true;
                return myLoginModel;
            }

            if (authenticationResult == UserManagement.AuthenticationResults.ChangePassword)
            {
                myLoginModel.Message = Constants.Areas.Common.LoginModel.AuthenticationResultMessages.ChangePassword;
                myLoginModel.Errors = true;
                return myLoginModel;
            }

            if (authenticationResult != UserManagement.AuthenticationResults.Success)
            {
                myLoginModel.Message = Constants.Areas.Common.LoginModel.AuthenticationResultMessages.UnknownResult;
                myLoginModel.Errors = true;
                return myLoginModel;
            }

            myLoginModel.User = myUserManagement.GetUserDetails(userName);
            myLoginModel.Groups = myUserManagement.GetUserGroups(userName);

            if(myLoginModel.User == null || myLoginModel.Groups.Count == 0)
            {
                myLoginModel.Message = Constants.Areas.Common.LoginModel.AuthenticationResultMessages.NoUserRoles;
                myLoginModel.Errors = true;
                return myLoginModel;
            }

            return myLoginModel;
        }