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 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 SignOut_RaisesUserChangedEvent()
        {
            bool userChangedRaised = false;
            var sessionStateService = new MockSessionStateService();
            var credentialStore = new MockCredentialStore
                {
                    GetSavedCredentialsDelegate = s => null,
                    RemoveSavedCredentialsDelegate = s => Task.Delay(0)
                };

            var target = new AccountService(null, sessionStateService, credentialStore);
            target.UserChanged += (sender, args) =>
                {
                    userChangedRaised = true;
                    Assert.IsNull(args.NewUserInfo);
                };

            target.SignOut();

            Assert.IsTrue(userChangedRaised);

            var signedInUser = await target.VerifyUserAuthenticationAsync();

            Assert.IsNull(signedInUser);
        }
        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);
        }