public void LogUserOutIfConditionsAreMet_throws_exception_when_sender_is_null()
        {
            Exception expectedException = null;
            const string expectedExceptionMessage = "Parameter is null\r\nParameter name: sender";

            // Given a null for sender
            const object sender = null;

            var wasShouldLogUserOutCalled = false;

            using (ShimsContext.Create())
            {
                // And ShouldLogUserOut is not called
                ShimSingleConcurrentLoginManager.AllInstances.ShouldLogUserOutStringString = (method, usernameValue, sessionIdValue) =>
                {
                    wasShouldLogUserOutCalled = true;
                    return true;
                };

                // When calling LogUserOutIfConditionsAreMet
                try
                {
                    _mySingleConcurrentLoginManager.LogUserOutIfConditionsAreMet(sender);
                }
                catch (Exception ex)
                {
                    // Then an exception is thrown
                    expectedException = ex;
                }
            }

            Assert.IsNotNull(expectedException);
            Assert.AreEqual(expectedExceptionMessage, expectedException.Message, "Exception Message doesn't match expected value");

            var myMicrosoftFakesTestHelper = new MicrosoftFakesTestHelper();

            // And IsCheckEnabled is not called
            var wasIsCheckEnabledCalled = myMicrosoftFakesTestHelper.WasMethodCalled(_stubForSingleConcurrentLoginRules, "IsCheckEnabled");
            Assert.IsFalse(wasIsCheckEnabledCalled, "wasIsCheckEnabledCalled is true");

            // And ShouldLogUserOut is not called
            Assert.IsFalse(wasShouldLogUserOutCalled, "wasShouldLogUserOutCalled is true");
        }
        public void LogUserOutIfConditionsAreMet_returns_false_when_LogUserOutIfConditionsAreMetActor_returns_false()
        {
            // Given a sender that is HttpApplication but has null Context
            var sender = new HttpApplication();

            // LogUserOutIfConditionsAreMetActor returns false
            var wasLogUserOutIfConditionsAreMetActorCalled = false;

            // And ShouldLogUserOut is not called
            var wasShouldLogUserOutCalled = false;

            var shouldLogUserOut = false;

            using (ShimsContext.Create())
            {
                ShimSingleConcurrentLoginManager.AllInstances.LogUserOutIfConditionsAreMetActorHttpApplication = (app, resultValue) =>
                {
                    wasLogUserOutIfConditionsAreMetActorCalled = true;
                    return false;
                };

                ShimSingleConcurrentLoginManager.AllInstances.ShouldLogUserOutStringString = (method, usernameValue, sessionIdValue) =>
                {
                    wasShouldLogUserOutCalled = true;
                    return true;
                };

                // When calling LogUserOutIfConditionsAreMet
                shouldLogUserOut = _mySingleConcurrentLoginManager.LogUserOutIfConditionsAreMet(sender);
            }

            Assert.IsFalse(shouldLogUserOut, "shouldLogUserOut is true");

            // Then LogUserOutIfConditionsAreMetActor returns false
            Assert.IsTrue(wasLogUserOutIfConditionsAreMetActorCalled, "wasLogUserOutIfConditionsAreMetActorCalled is false");

            Assert.IsFalse(shouldLogUserOut, "shouldLogUserOut is true");

            var myMicrosoftFakesTestHelper = new MicrosoftFakesTestHelper();

            // And IsCheckEnabled is not called
            var wasIsCheckEnabledCalled = myMicrosoftFakesTestHelper.WasMethodCalled(_stubForSingleConcurrentLoginRules, "IsCheckEnabled");
            Assert.IsFalse(wasIsCheckEnabledCalled, "wasIsCheckEnabledCalled is true");

            // And ShouldLogUserOut is not called
            Assert.IsFalse(wasShouldLogUserOutCalled, "wasShouldLogUserOutCalled is true");
        }
        public void LogUserOutIfConditionsAreMet_returns_false_when_sender_is_not_HttpApplication()
        {
            // Given a non-null for sender but is not HttpApplication
            const string sender = "not HttpApplication";

            // And ShouldLogUserOut is not called
            var wasShouldLogUserOutCalled = false;

            var shouldLogUserOut = false;

            using (ShimsContext.Create())
            {
                ShimSingleConcurrentLoginManager.AllInstances.ShouldLogUserOutStringString = (method, usernameValue, sessionIdValue) =>
                {
                    wasShouldLogUserOutCalled = true;
                    return true;
                };

                // When calling LogUserOutIfConditionsAreMet
                shouldLogUserOut = _mySingleConcurrentLoginManager.LogUserOutIfConditionsAreMet(sender);
            }

            // Then false is returned
            Assert.IsFalse(shouldLogUserOut, "shouldLogUserOut is true");

            var myMicrosoftFakesTestHelper = new MicrosoftFakesTestHelper();

            // And IsCheckEnabled is not called
            var wasIsCheckEnabledCalled = myMicrosoftFakesTestHelper.WasMethodCalled(_stubForSingleConcurrentLoginRules, "IsCheckEnabled");
            Assert.IsFalse(wasIsCheckEnabledCalled, "wasIsCheckEnabledCalled is true");

            // And ShouldLogUserOut is not called
            Assert.IsFalse(wasShouldLogUserOutCalled, "wasShouldLogUserOutCalled is true");
        }
        public void CreateLoginRecord_returns_when_IsCheckEnabled_returns_false()
        {
            _stubForSingleConcurrentLoginRules.IsCheckEnabled = () => false;

            // Given non-null and non-whitespace arguments
            const string userName = "******";
            const string sessionId = "doesNotMatter";

            var wasAddSIMPLLoginTrackerRecordToCacheCalled = false;
            bool wasLoginRecordCreated;

            using (ShimsContext.Create())
            {
                // And AddSIMPLLoginTrackerRecordToCache is not called
                ShimSIMPLSessionEntitiesCache.AddSIMPLLoginTrackerRecordToCacheStringSIMPLLoginTracker = (userNameValue, myLoginTrackerValue) =>
                {
                    wasAddSIMPLLoginTrackerRecordToCacheCalled = true;
                    return false;
                };

                wasLoginRecordCreated = _mySingleConcurrentLoginManager.CreateLoginRecord(userName, sessionId);
            }

            Assert.IsFalse(wasLoginRecordCreated, "wasLoginRecordCreated is true");

            var myMicrosoftFakesTestHelper = new MicrosoftFakesTestHelper();

            // And IsCheckEnabled is called
            var wasIsCheckEnabledCalled = myMicrosoftFakesTestHelper.WasMethodCalled(_stubForSingleConcurrentLoginRules, "IsCheckEnabled");
            Assert.IsTrue(wasIsCheckEnabledCalled, "wasIsCheckEnabledCalled is false");

            // And CreateSIMPLLoginTrackerRecord is not called
            var wasCreateSIMPLLoginTrackerRecordCalled = myMicrosoftFakesTestHelper.WasMethodCalled(_stubForISIMPLSessionEntitiesRepository, "CreateSIMPLLoginTrackerRecord");
            Assert.IsFalse(wasCreateSIMPLLoginTrackerRecordCalled, "wasShouldLogUserOutCalled is true");

            // And AddSIMPLLoginTrackerRecordToCache is not called
            Assert.IsFalse(wasAddSIMPLLoginTrackerRecordToCacheCalled, "wasAddSIMPLLoginTrackerRecordToCacheCalled is true");
        }
        public void LogUserOutIfConditionsAreMetActor_returns_true_when_ShouldLogUserOut_returns_true()
        {
            // Set up
            _stubForSingleConcurrentLoginRules.IsCheckEnabled = () => true;

            // And ShouldLogUserOut is not called
            var wasShouldLogUserOutCalled = false;
            bool result;

            using (ShimsContext.Create())
            {
                // Given an application that is HttpApplication with Context and Session with username
                var session = new ShimHttpSessionState
                {
                    ItemGetString = s =>
                    {
                        if (s == "username")
                        {
                            return "usernameValue";
                        }
                        return null;
                    },

                    SessionIDGet = () => "sessionIdValue"
                };

                var context = new ShimHttpContext { SessionGet = () => session };
                var app = new ShimHttpApplication { ContextGet = () => context };

                ShimSingleConcurrentLoginManager.AllInstances.ShouldLogUserOutStringString = (method, usernameValue, sessionIdValue) =>
                {
                    wasShouldLogUserOutCalled = true;
                    return true;
                };

                // When calling LogUserOutIfConditionsAreMetActor
                result = _mySingleConcurrentLoginManager.LogUserOutIfConditionsAreMetActor(app);
            }

            // Then there is a return of true
            Assert.IsTrue(result, "LogUserOutIfConditionsAreMetActor returned false");

            var myMicrosoftFakesTestHelper = new MicrosoftFakesTestHelper();

            // And IsCheckEnabled is not called
            var wasIsCheckEnabledCalled = myMicrosoftFakesTestHelper.WasMethodCalled(_stubForSingleConcurrentLoginRules, "IsCheckEnabled");
            Assert.IsTrue(wasIsCheckEnabledCalled, "wasIsCheckEnabledCalled is false");

            // And ShouldLogUserOut is not called
            Assert.IsTrue(wasShouldLogUserOutCalled, "wasShouldLogUserOutCalled is false");
        }
        public void LogUserOutIfConditionsAreMetActor_returns_false_when_Session_does_not_have_username()
        {
            // Set up
            // And ShouldLogUserOut is not called
            var wasShouldLogUserOutCalled = false;

            var result = false;

            using (ShimsContext.Create())
            {
                // Given an application that is HttpApplication with Context and Session (just enough Session to move forward)
                var session = new ShimHttpSessionState();
                var context = new ShimHttpContext { SessionGet = () => session };
                var app = new ShimHttpApplication { ContextGet = () => context };

                ShimSingleConcurrentLoginManager.AllInstances.ShouldLogUserOutStringString = (method, usernameValue, sessionIdValue) =>
                {
                    wasShouldLogUserOutCalled = true;
                    return true;
                };

                // When calling LogUserOutIfConditionsAreMetActor
                result = _mySingleConcurrentLoginManager.LogUserOutIfConditionsAreMetActor(app);
            }

            // Then there is a return of false
            Assert.IsFalse(result, "LogUserOutIfConditionsAreMetActor returned true");

            var myMicrosoftFakesTestHelper = new MicrosoftFakesTestHelper();

            // And IsCheckEnabled is not called
            var wasIsCheckEnabledCalled = myMicrosoftFakesTestHelper.WasMethodCalled(_stubForSingleConcurrentLoginRules, "IsCheckEnabled");
            Assert.IsTrue(wasIsCheckEnabledCalled, "wasIsCheckEnabledCalled is false");

            // And ShouldLogUserOut is not called
            Assert.IsFalse(wasShouldLogUserOutCalled, "wasShouldLogUserOutCalled is true");
        }
        public void CreateLoginRecord_throws_exception_when_userName_argument_is_whitespace()
        {
            Exception expectedException = null;
            const string expectedExceptionMessage = "Parameter is null or whitespace\r\nParameter name: userName";

            // Given whitespace for userName argument
            const string userName = "******";
            const string sessionId = "doesNotMatter";

            var wasAddSIMPLLoginTrackerRecordToCacheCalled = false;

            // As all of the methods in the SIMPLSessionEntitiesCache class are static, don't have an interface and cannot use stubs - so we have to use shims here
            using (ShimsContext.Create())
            {
                // And AddSIMPLLoginTrackerRecordToCache is not called
                ShimSIMPLSessionEntitiesCache.AddSIMPLLoginTrackerRecordToCacheStringSIMPLLoginTracker = (userNameValue, myLoginTrackerValue) =>
                {
                    wasAddSIMPLLoginTrackerRecordToCacheCalled = true;
                    return false;
                };

                // When calling CreateLoginRecord
                try
                {
                    _mySingleConcurrentLoginManager.CreateLoginRecord(userName, sessionId);
                }
                catch (Exception ex)
                {
                    // Then an exception is thrown
                    expectedException = ex;
                }
            }

            Assert.IsNotNull(expectedException);
            Assert.IsTrue(expectedException is ArgumentException, "expectedException is not an ArgumentException");
            Assert.AreEqual(expectedExceptionMessage, expectedException.Message, "Exception Message doesn't match expected value");

            var myMicrosoftFakesTestHelper = new MicrosoftFakesTestHelper();

            // And IsCheckEnabled is not called
            var wasIsCheckEnabledCalled = myMicrosoftFakesTestHelper.WasMethodCalled(_stubForSingleConcurrentLoginRules, "IsCheckEnabled");
            Assert.IsFalse(wasIsCheckEnabledCalled, "wasIsCheckEnabledCalled is true");

            // And CreateSIMPLLoginTrackerRecord is not called
            var wasCreateSIMPLLoginTrackerRecordCalled = myMicrosoftFakesTestHelper.WasMethodCalled(_stubForISIMPLSessionEntitiesRepository, "CreateSIMPLLoginTrackerRecord");
            Assert.IsFalse(wasCreateSIMPLLoginTrackerRecordCalled, "wasCreateSIMPLLoginTrackerRecordCalled is true");

            // And AddSIMPLLoginTrackerRecordToCache is not called
            Assert.IsFalse(wasAddSIMPLLoginTrackerRecordToCacheCalled, "wasAddSIMPLLoginTrackerRecordToCacheCalled is true");
        }
        public void CreateLoginRecord_throws_exception_when_mySIMPLSessionEntitiesRepository_argument_in_constructor_is_null()
        {
            Exception expectedException = null;
            const string expectedExceptionMessage = "Parameter is null\r\nParameter name: _mySIMPLSessionEntitiesRepository";
            const string userName = "******";
            const string sessionId = "doesNotMatter";

            // Given a null for _mySIMPLSessionEntitiesRepository
            SIMPLSessionEntitiesRepository _mySIMPLSessionEntitiesRepository = null;

            // overriding the _mySingleConcurrentLoginManager with a specific case for this test
            _mySingleConcurrentLoginManager = new SingleConcurrentLoginManager(_stubForSingleConcurrentLoginRules, _stubForIErrorLoggingService, _mySIMPLSessionEntitiesRepository);

            var wasCreateSIMPLLoginTrackerRecordCalled = false;
            var wasAddSIMPLLoginTrackerRecordToCacheCalled = false;

            // As all of the methods in the SIMPLSessionEntitiesCache class are static, don't have an interface and cannot use stubs - so we have to use shims here
            // Also, since we are not calling the stub for SIMPLSessionEntitiesRepository, need to shim that too
            using (ShimsContext.Create())
            {
                // And AddSIMPLLoginTrackerRecordToCache is not called
                ShimSIMPLSessionEntitiesCache.AddSIMPLLoginTrackerRecordToCacheStringSIMPLLoginTracker = (userNameValue, myLoginTrackerValue) =>
                {
                    wasAddSIMPLLoginTrackerRecordToCacheCalled = true;
                    return false;
                };

                ShimSIMPLSessionEntitiesRepository.AllInstances.CreateSIMPLLoginTrackerRecordStringString = (methodName, userNameValue, sessionIdValue) =>
                {
                    wasCreateSIMPLLoginTrackerRecordCalled = true;
                    return null;
                };

                // When calling CreateLoginRecord
                try
                {
                    _mySingleConcurrentLoginManager.CreateLoginRecord(userName, sessionId);
                }
                catch (Exception ex)
                {
                    // Then an exception is thrown
                    expectedException = ex;
                }
            }

            Assert.IsNotNull(expectedException);
            Assert.IsTrue(expectedException is ArgumentException, "expectedException is not an ArgumentException");
            Assert.AreEqual(expectedExceptionMessage, expectedException.Message, "Exception Message doesn't match expected value");

            var myMicrosoftFakesTestHelper = new MicrosoftFakesTestHelper();

            // And IsCheckEnabled is not called
            var wasIsCheckEnabledCalled = myMicrosoftFakesTestHelper.WasMethodCalled(_stubForSingleConcurrentLoginRules, "IsCheckEnabled");
            Assert.IsFalse(wasIsCheckEnabledCalled, "wasIsCheckEnabledCalled is true");

            // And CreateSIMPLLoginTrackerRecord is not called
            Assert.IsFalse(wasCreateSIMPLLoginTrackerRecordCalled, "wasCreateSIMPLLoginTrackerRecordCalled is true");

            // And AddSIMPLLoginTrackerRecordToCache is not called
            Assert.IsFalse(wasAddSIMPLLoginTrackerRecordToCacheCalled, "wasAddSIMPLLoginTrackerRecordToCacheCalled is true");
        }