public async Task SuccessfullSignIn_RaisesUserChangedEvent()
        {
            bool userChangedFired = false;

            var identityService = new MockIdentityService();
            var sessionStateService = new MockSessionStateService();
            identityService.LogOnAsyncDelegate = (userId, password) =>
                {
                    return Task.FromResult(new LogOnResult { UserInfo = new UserInfo{UserName = userId} });
                };

            var target = new AccountService(identityService, sessionStateService, null);
            target.UserChanged += (sender, userInfo) => { userChangedFired = true; }; 

            var retVal = await target.SignInUserAsync("TestUserName", "TestPassword", false);
            Assert.IsTrue(retVal);
            Assert.IsTrue(userChangedFired);
        }
        public async Task GetSignedInUserAsync_SignsInUsingCredentialStore_IfNoActiveSession()
        {
            var sessionStateService = new MockSessionStateService();
            var identityService = new MockIdentityService()
            {
                LogOnAsyncDelegate = (userId, password) => Task.FromResult(new LogOnResult { UserInfo = new UserInfo { UserName = userId } }),
                VerifyActiveSessionDelegate = (userName) => Task.FromResult(false)
            };
            var credentialStore = new MockCredentialStore()
                {
                    GetSavedCredentialsDelegate = (s) => new PasswordCredential(AccountService.PasswordVaultResourceName, "TestUserName", "TestPassword"),
                    SaveCredentialsDelegate = (a, b, c) => Task.Delay(0)
                };

            var target = new AccountService(identityService, sessionStateService, credentialStore);
            await target.SignInUserAsync("TestUserName", "TestPassword", true);

            var user = await target.VerifyUserAuthenticationAsync();

            Assert.IsNotNull(user);
            Assert.IsTrue(user.UserName == "TestUserName");
        }
        public async Task GetSignedInUserAsync_Calls_VerifyActiveSessionAsync()
        {
            bool verifyActiveSessionCalled = false;
            var sessionStateService = new MockSessionStateService();
            var identityService = new MockIdentityService()
                {
                    LogOnAsyncDelegate = (userId, password) => Task.FromResult(new LogOnResult { UserInfo = new UserInfo { UserName = userId } }),
                    VerifyActiveSessionDelegate = (userName) =>
                        {
                            verifyActiveSessionCalled = true;
                            return Task.FromResult(true);
                        }
                };

            var target = new AccountService(identityService, sessionStateService, null);
            await target.SignInUserAsync("TestUserName", "TestPassword", false);
            var user = await target.VerifyUserAuthenticationAsync();

            Assert.IsTrue(verifyActiveSessionCalled);
            Assert.IsNotNull(user);
            Assert.IsTrue(user.UserName == "TestUserName");
        }
        public async Task FailedSignIn_DoesNotRaiseUserChangedEvent()
        {
            bool userChangedFired = false;

            var identityService = new MockIdentityService();
            var sessionStateService = new MockSessionStateService();
            identityService.LogOnAsyncDelegate = (userId, password) =>
            {
                throw new Exception();
            };

            var target = new AccountService(identityService, sessionStateService, null);
            target.UserChanged += (sender, userInfo) => { userChangedFired = true; };

            var retVal = await target.SignInUserAsync("TestUserName", "TestPassword", false);
            Assert.IsFalse(retVal);
            Assert.IsFalse(userChangedFired);
        }
        public async Task CheckIfUserSignedIn_ReturnsNull_IfSessionIsStillInactiveAndHasInvalidSavedCredentials()
        {
            var sessionStateService = new MockSessionStateService();
            var identityService = new MockIdentityService();
            identityService.VerifyActiveSessionDelegate = (userName) => Task.FromResult(false);
            identityService.LogOnAsyncDelegate =
                (userName, password) =>
                {
                    Assert.AreEqual("TestUserName", userName);
                    Assert.AreEqual("TestPassword", password);
                    throw new Exception();
                };
            var credentialStore = new MockCredentialStore();
            credentialStore.GetSavedCredentialsDelegate = s => new PasswordCredential(AccountService.PasswordVaultResourceName, "TestUserName", "TestPassword");
            var target = new AccountService(identityService, sessionStateService, credentialStore);

            var userInfo = await target.VerifyUserAuthenticationAsync();

            Assert.IsNull(userInfo);
        }
        public async Task CheckIfUserSignedIn_ReturnsNull_IfSessionIsStillInactiveAndNoSavedCredentials()
        {
            var identityService = new MockIdentityService();
            identityService.VerifyActiveSessionDelegate = (userName) => Task.FromResult(false);
            var credentialStore = new MockCredentialStore();
            credentialStore.GetSavedCredentialsDelegate = s => null;
            var sessionStateService = new MockSessionStateService();
            var target = new AccountService(identityService, sessionStateService, credentialStore);

            var userInfo = await target.VerifyUserAuthenticationAsync(); 
            
            Assert.IsNull(userInfo);
        }
        public async Task CheckIfUserSignedIn_ReturnsUserInfo_IfSessionIsStillLive()
        {
            var sessionStateService = new MockSessionStateService();
            var identityService = new MockIdentityService();
            identityService.VerifyActiveSessionDelegate = (userName) => Task.FromResult(true);
            identityService.LogOnAsyncDelegate = (userName, password) =>
                {
                    return Task.FromResult(new LogOnResult()
                        {
                            UserInfo = new UserInfo() {UserName = "TestUsername"}
                        });
                };

            var target = new AccountService(identityService, sessionStateService, null);
            bool userSignedIn = await target.SignInUserAsync("TestUsername", "password", false);

            Assert.IsTrue(userSignedIn);

            var userInfo = await target.VerifyUserAuthenticationAsync();

            Assert.IsNotNull(userInfo);
        }