예제 #1
0
        public void A_user_logs_in_when_Single_Concurrent_Login_Feature_is_enabled_but_error_occurs_when_saving_the_username_and_session_data()
        {
            using (ShimsContext.Create())
            {
                // Given a known user that is not logged in yet
                const string userName = "******";
                const string password = "******";
                const string returnUrl = "doesNotMatter";

                var requestStub = new StubHttpRequestBase();
                var contextStub = new StubHttpContextBase { RequestGet = () => requestStub };

                LoginControllerForTests.ControllerContext = new ControllerContext()
                {
                    HttpContext = contextStub
                };

                #endregion LoginModel class, AttemptToLogUserIn method

                // And the Single Concurrent Login Feature is enabled

                // As there are unit tests for IsCheckEnabledActor, I am mocking the reference to IsCheckEnabled
                var wasIsCheckEnabledCalled = false;
                ShimSingleConcurrentLoginRules.AllInstances.IsCheckEnabled = (resultValue) =>
                {
                    wasIsCheckEnabledCalled = true;
                    return true;
                };

                // When that user is trying to log in

                #region LoginModel class, AttemptToLogUserIn method

                var wasASPPAuthenticateCalled = false;
                ShimUserManagement.AllInstances.AuthenticateStringString = (userNameValue, passwordValue, results) =>
                {
                    wasASPPAuthenticateCalled = true;
                    return UserManagement.AuthenticationResults.Success;
                };

                var wasASPPGetUserDetailsCalled = false;
                ShimUserManagement.AllInstances.GetUserDetailsString = (userNameValue, results) =>
                {
                    wasASPPGetUserDetailsCalled = true;
                    return new ASPP_Users();
                };

                var wasASPPGetUserGroupsCalled = false;
                ShimUserManagement.AllInstances.GetUserGroupsString = (userNameValue, results) =>
                {
                    wasASPPGetUserGroupsCalled = true;

                    // need to add at least one group - doing just enough to get beyond the guard in LoginModel.cs
                    var myASPP_Group = new ASPP_Groups
                    {
                        Group_ID = 1
                    };

                    return new List<ASPP_Groups>
                    {
                        myASPP_Group
                    };
                };

                // Then the data is not saved correctly

                // Note: the CreateSIMPLLoginTrackerRecord method should be called

                // When the CreateSIMPLLoginTrackerRecord method is called,
                // the SIMPLLoginTrackers.Add line will throw an exception because
                // it cannot find SIMPLEntities connection string in the config;
                // this is fine for our purposes as it throws as exception.

                // And the data is not added to the cache

                // Note: the AddSIMPLLoginTrackerRecordToCache method should not be called
                var wasAddSIMPLLoginTrackerRecordToCacheCalled = false;
                ShimSIMPLSessionEntitiesCache.AddSIMPLLoginTrackerRecordToCacheStringSIMPLLoginTracker = (userNameValue, myLoginTrackerValue) =>
                {
                    wasAddSIMPLLoginTrackerRecordToCacheCalled = true;
                    return true;
                };

                // And the user is not logged in

                #region back in the LoginViewModel class, AttemptToLogUserIn method

                var session = new ShimHttpSessionState { SessionIDGet = () => "doesNotMatter"};
                var context = new ShimHttpContext();
                var applicationShim = new ShimHttpApplicationState();
                context.ApplicationGet = () => applicationShim;
                ShimHttpContext.CurrentGet = () => context;
                ShimHttpContext.AllInstances.SessionGet = (o) => session;

                // Note: the CurrentUser.SetInstance method should not be called
                var wasCurrentUserSetInstanceStringCalled = false;
                var currentUserSetInstanceCount = 0;
                ShimCurrentUser.SetInstanceString = (uniqueId) =>
                {
                    wasCurrentUserSetInstanceStringCalled = true;
                    currentUserSetInstanceCount++;
                };

                // Note: the CurrentUser.SessionInstance method should not be called
                var wasCurrentUserSessionInstanceGetCalled = false;
                ShimCurrentUser.SessionInstanceGet = () =>
                {
                    wasCurrentUserSessionInstanceGetCalled = true;
                    return new ShimCurrentUser();
                };

                // Note: the SyncWithOrImportDataFromASPP method should not be called
                var wasLoginModelSyncWithOrImportDataFromASPPCalled = false;
                ShimLoginModel.AllInstances.SyncWithOrImportDataFromASPPIErrorLoggingServiceString = (loginModel, errorLoggingServiceValue, userNameSentToLoginModelValue) =>
                {
                    wasLoginModelSyncWithOrImportDataFromASPPCalled = true;
                };

                // Note: the CurrentUser.Clear method should not be called
                var wasCurrentUserClearCalled = false;
                ShimCurrentUser.Clear = () =>
                {
                    wasCurrentUserClearCalled = true;
                };

                // Note: the UserEvents.InitializeSession method should not be called
                var wasUserEventsInitializeSessionCalled = false;
                ShimUserEvents.AllInstances.InitializeSessionStringString = (userEvents, uniqueIdValue, userHostAddressValue) =>
                {
                    wasUserEventsInitializeSessionCalled = true;
                };

                // Note: the FormsAuthentication.SetAuthCookie method should not be called
                var wasFormsAuthenticationSetAuthCookieCalled = false;
                ShimFormsAuthentication.SetAuthCookieStringBoolean = (userNameValue, createPersistentCookieValue) =>
                {
                    wasFormsAuthenticationSetAuthCookieCalled = true;
                };

                ShimErrorLoggingService.AllInstances.LogErrorException = (method, myException) =>
                {
                    // Not returning anything, just redirecting the call
                };

                #endregion back in the LoginViewModel class, AttemptToLogUserIn method

                var result = LoginControllerForTests.UserLogin(userName, password, returnUrl);
                Assert.IsNotNull(result, "LoginController returned null");
                Assert.IsTrue(result is ViewResult, "LoginController didn't return a ViewResult");

                var resultViewResult = result as ViewResult;
                Assert.IsNotNull(resultViewResult.Model, "resultViewResult.Model is null");
                Assert.IsTrue(resultViewResult.Model is LoginViewModel, "resultViewResult.Model is not LoginViewModel");

                var resultLoginViewModel = resultViewResult.Model as LoginViewModel;
                Assert.AreEqual("Unable to save the Login Record, please contact support (Single Concurrent Login Enabled)", resultLoginViewModel.Message, "resultLoginViewModel.Message did not have the expected value");

                Assert.IsTrue(wasASPPAuthenticateCalled, "wasASPPAuthenticateCalled is false");
                Assert.IsTrue(wasASPPGetUserDetailsCalled, "wasASPPGetUserDetailsCalled is false");
                Assert.IsTrue(wasASPPGetUserGroupsCalled, "wasASPPGetUserDetailsCalled is false");
                Assert.IsTrue(wasIsCheckEnabledCalled, "wasIsCheckEnabledCalled is false");

                // Not able to check if the CreateSIMPLLoginTrackerRecord method was called because
                // we are not using a Shim for that (as we are calling the live method)

                // These are negative checks to make sure that the other code was not called
                Assert.IsFalse(wasAddSIMPLLoginTrackerRecordToCacheCalled, "wasAddSIMPLLoginTrackerRecordToCacheCalled is true");
                Assert.IsFalse(wasCurrentUserSetInstanceStringCalled, "wasCurrentUserSetInstanceStringCalled is true");
                Assert.AreEqual(0, currentUserSetInstanceCount, "currentUserSetInstanceCount did not match expected value");
                Assert.IsFalse(wasCurrentUserSessionInstanceGetCalled, "wasCurrentUserSessionInstanceGetCalled is true");
                Assert.IsFalse(wasCurrentUserClearCalled, "wasCurrentUserClearCalled is true");
                Assert.IsFalse(wasLoginModelSyncWithOrImportDataFromASPPCalled, "wasLoginModelSyncWithOrImportDataFromASPPCalled is true");
                Assert.IsFalse(wasUserEventsInitializeSessionCalled, "wasUserEventsInitializeSessionCalled is true");
                Assert.IsFalse(wasFormsAuthenticationSetAuthCookieCalled, "wasFormsAuthenticationSetAuthCookieCalled is true");
            }
        }
예제 #2
0
        public void A_user_logs_in_when_Single_Concurrent_Login_Feature_is_enabled_and_is_able_to_successfully_log_in()
        {
            using (ShimsContext.Create())
            {
                // Given a known user that is not logged in yet
                const string userName = "******";
                const string password = "******";
                const string returnUrl = "doesNotMatter";

                var requestStub = new StubHttpRequestBase();
                var contextStub = new StubHttpContextBase { RequestGet = () => requestStub };

                LoginControllerForTests.ControllerContext = new ControllerContext()
                {
                    HttpContext = contextStub
                };

                #endregion LoginModel class, AttemptToLogUserIn method

                // And the Single Concurrent Login Feature is enabled

                // As there are unit tests for IsCheckEnabledActor, I am mocking the reference to IsCheckEnabled
                var wasIsCheckEnabledCalled = false;
                ShimSingleConcurrentLoginRules.AllInstances.IsCheckEnabled = (resultValue) =>
                {
                    wasIsCheckEnabledCalled = true;
                    return true;
                };

                // When that user is trying to log in

                #region LoginModel class, AttemptToLogUserIn method

                var wasASPPAuthenticateCalled = false;
                ShimUserManagement.AllInstances.AuthenticateStringString = (userNameValue, passwordValue, results) =>
                {
                    wasASPPAuthenticateCalled = true;
                    return UserManagement.AuthenticationResults.Success;
                };

                var wasASPPGetUserDetailsCalled = false;
                ShimUserManagement.AllInstances.GetUserDetailsString = (userNameValue, results) =>
                {
                    wasASPPGetUserDetailsCalled = true;
                    return new ASPP_Users();
                };

                var wasASPPGetUserGroupsCalled = false;
                ShimUserManagement.AllInstances.GetUserGroupsString = (userNameValue, results) =>
                {
                    wasASPPGetUserGroupsCalled = true;

                    // need to add at least one group - doing just enough to get beyond the guard in LoginModel.cs
                    var myASPP_Group = new ASPP_Groups
                    {
                        Group_ID = 1
                    };

                    return new List<ASPP_Groups>
                    {
                        myASPP_Group
                    };
                };

                // Then the data is saved correctly

                var wasCreateSIMPLLoginTrackerRecordCalled = false;
                ShimSIMPLSessionEntitiesRepository.AllInstances.CreateSIMPLLoginTrackerRecordStringString = (method, userNameValue, aspNetSessionIdValue) =>
                {
                    wasCreateSIMPLLoginTrackerRecordCalled = true;
                    return new SIMPLLoginTracker { SIMPLUsername = "******", ASPNETSessionId = "doesNotMatter", SIMPLLoginTrackerId = 1, SIMPLLoginTimeUtc = DateTime.UtcNow };
                };

                // And the data is added to the cache correctly

                var wasAddSIMPLLoginTrackerRecordToCacheCalled = false;
                ShimSIMPLSessionEntitiesCache.AddSIMPLLoginTrackerRecordToCacheStringSIMPLLoginTracker = (userNameValue, myLoginTrackerValue) =>
                {
                    wasAddSIMPLLoginTrackerRecordToCacheCalled = true;
                    return true;
                };

                // And the user is logged in successfully

                #region back in the LoginViewModel class, AttemptToLogUserIn method

                var session = new ShimHttpSessionState { SessionIDGet = () => "doesNotMatter"};
                var context = new ShimHttpContext();
                var applicationShim = new ShimHttpApplicationState();
                context.ApplicationGet = () => applicationShim;
                ShimHttpContext.CurrentGet = () => context;
                ShimHttpContext.AllInstances.SessionGet = (o) => session;

                var wasCurrentUserSetInstanceStringCalled = false;
                var currentUserSetInstanceCount = 0;
                ShimCurrentUser.SetInstanceString = (uniqueId) =>
                {
                    wasCurrentUserSetInstanceStringCalled = true;
                    currentUserSetInstanceCount++;
                };

                var wasCurrentUserSessionInstanceGetCalled = false;
                ShimCurrentUser.SessionInstanceGet = () =>
                {
                    wasCurrentUserSessionInstanceGetCalled = true;
                    return new ShimCurrentUser();
                };

                var wasLoginModelSyncWithOrImportDataFromASPPCalled = false;
                ShimLoginModel.AllInstances.SyncWithOrImportDataFromASPPIErrorLoggingServiceString = (loginModel, errorLoggingServiceValue, userNameSentToLoginModelValue) =>
                {
                    wasLoginModelSyncWithOrImportDataFromASPPCalled = true;
                };

                var wasCurrentUserClearCalled = false;
                ShimCurrentUser.Clear = () =>
                {
                    wasCurrentUserClearCalled = true;
                };

                var wasUserEventsInitializeSessionCalled = false;
                ShimUserEvents.AllInstances.InitializeSessionStringString = (userEvents, uniqueIdValue, userHostAddressValue) =>
                {
                    wasUserEventsInitializeSessionCalled = true;
                };

                var wasFormsAuthenticationSetAuthCookieCalled = false;
                ShimFormsAuthentication.SetAuthCookieStringBoolean = (userNameValue, createPersistentCookieValue) =>
                {
                    wasFormsAuthenticationSetAuthCookieCalled = true;
                };

                #endregion back in the LoginViewModel class, AttemptToLogUserIn method

                var result = LoginControllerForTests.UserLogin(userName, password, returnUrl);
                Assert.IsNotNull(result, "LoginController returned null");
                Assert.IsTrue(result is RedirectResult, "LoginController didn't return a RedirectResult");

                var resultRedirectResult = result as RedirectResult;
                Assert.AreEqual(returnUrl, resultRedirectResult.Url, "URL did not match expected value");

                Assert.IsTrue(wasIsCheckEnabledCalled, "wasIsCheckEnabledCalled is false");
                Assert.IsTrue(wasCreateSIMPLLoginTrackerRecordCalled, "wasCreateSIMPLLoginTrackerRecordCalled is false");
                Assert.IsTrue(wasAddSIMPLLoginTrackerRecordToCacheCalled, "wasAddSIMPLLoginTrackerRecordToCacheCalled is false");

                Assert.IsTrue(wasASPPAuthenticateCalled, "wasASPPAuthenticateCalled is false");
                Assert.IsTrue(wasASPPGetUserDetailsCalled, "wasASPPGetUserDetailsCalled is false");
                Assert.IsTrue(wasASPPGetUserGroupsCalled, "wasASPPGetUserDetailsCalled is false");
                Assert.IsTrue(wasCurrentUserSetInstanceStringCalled, "wasCurrentUserSetInstanceStringCalled is false");
                Assert.AreEqual(2, currentUserSetInstanceCount, "currentUserSetInstanceCount did not match expected value");
                Assert.IsTrue(wasCurrentUserSessionInstanceGetCalled, "wasCurrentUserSessionInstanceGetCalled is false");
                Assert.IsTrue(wasCurrentUserClearCalled, "wasCurrentUserClearCalled is false");
                Assert.IsTrue(wasLoginModelSyncWithOrImportDataFromASPPCalled, "wasLoginModelSyncWithOrImportDataFromASPPCalled is false");
                Assert.IsTrue(wasUserEventsInitializeSessionCalled, "wasUserEventsInitializeSessionCalled is false");
                Assert.IsTrue(wasFormsAuthenticationSetAuthCookieCalled, "wasFormsAuthenticationSetAuthCookieCalled is false");
            }
        }