public async Task SignOut_RaisesUserChangedEvent()
        {
            var userChangedRaised = false;
            var credentialStore = new MockCredentialStore();
            credentialStore.GetSavedCredentialsDelegate = s => null;
            var restorableStateService = new MockRestorableStateService();
            var target = new AccountService(null, restorableStateService, credentialStore);
            target.UserChanged += (sender, args) =>
                                      {
                                          userChangedRaised = true;
                                          Assert.IsNull(args.NewUserInfo);
                                      };

            target.SignOut();

            Assert.IsTrue(userChangedRaised);

            var signedInUser = await target.GetSignedInUserAsync();

            Assert.IsNull(signedInUser);
        }
        public async Task GetSignedInUserAsync_WhenSessionTimedOut_AttemptsToAutoSignIn()
        {
            var userChangedEventRaised = false;
            var newUserName = string.Empty;
            var identityService = new MockIdentityService();
            identityService.LogOnAsyncDelegate = (s, s1) => Task.FromResult(new LogOnResult {UserInfo = new UserInfo{UserName = s}, ServerCookieHeader = string.Empty});
            var restorableStateService = new MockRestorableStateService();
            var credentialStore = new MockCredentialStore();
            credentialStore.GetSavedCredentialsDelegate = s =>
                                                              {
                                                                  if (s == "KonaRI")
                                                                  {
                                                                      return new PasswordCredential("KonaRI", "testusername",
                                                                                                    "testpassword");
                                                                  }
                                                                  return null;
                                                              };
            var target = new AccountService(identityService, restorableStateService, credentialStore);

            target.SignInUserAsync("testusername", "testpassword");
            target.UserChanged += (sender, args) =>
                                      {
                                          userChangedEventRaised = true;
                                          newUserName = args.NewUserInfo.UserName;
                                      };
            identityService.VerifyActiveSessionDelegate = (s, s1) =>
                                                              {
                                                                  throw new SecurityException();
                                                              };
            var userInfo = await target.GetSignedInUserAsync();

            Assert.IsTrue(userChangedEventRaised);
            Assert.AreEqual("testusername", newUserName);

        }
        public async Task CheckIfUserSignedIn_ReturnsNull_IfSessionIsStillInactiveAndHasInvalidSavedCredentials()
        {
            var restorableStateService = new MockRestorableStateService();
            var identityService = new MockIdentityService();
            identityService.VerifyActiveSessionDelegate = (userName, cookieHeader) => Task.FromResult(false);
            identityService.LogOnAsyncDelegate =
                (userName, password) =>
                {
                    Assert.AreEqual("TestUserName", userName);
                    Assert.AreEqual("TestPassword", password);
                    throw new HttpRequestException();
                };
            var credentialStore = new MockCredentialStore();
            credentialStore.GetSavedCredentialsDelegate = s => new PasswordCredential("KonaRI", "TestUserName", "TestPassword");
            var target = new AccountService(identityService, restorableStateService, credentialStore);

            var userInfo = await target.GetSignedInUserAsync();

            Assert.IsNull(userInfo);
        }
        public async Task CheckIfUserSignedIn_ReturnsUserInfo_IfSessionIsStillInactiveButHasSavedCredentials()
        {
            var mockRestorableStateService = new MockRestorableStateService();
            var identityService = new MockIdentityService();
            identityService.VerifyActiveSessionDelegate = (userName, cookieHeader) => Task.FromResult(false);
            identityService.LogOnAsyncDelegate =
                (userName, password) =>
                    {
                        Assert.AreEqual("TestUserName", userName);
                        Assert.AreEqual("TestPassword", password);
                        return Task.FromResult(new LogOnResult()
                                            {
                                                UserInfo = new UserInfo(){UserName = "******"}
                                            });
                    };
            var credentialStore = new MockCredentialStore();
            credentialStore.GetSavedCredentialsDelegate = s => new PasswordCredential("KonaRI", "TestUserName", "TestPassword");
            var target = new AccountService(identityService, mockRestorableStateService, credentialStore);

            var userInfo = await target.GetSignedInUserAsync();

            Assert.IsNotNull(userInfo);
            Assert.AreEqual("ReturnedUserName", userInfo.UserName);
        }
        public async Task CheckIfUserSignedIn_ReturnsNull_IfSessionIsStillInactiveAndNoSavedCredentials()
        {
            var identityService = new MockIdentityService();
            identityService.VerifyActiveSessionDelegate = (userName, cookieHeader) => Task.FromResult(false);
            var credentialStore = new MockCredentialStore();
            credentialStore.GetSavedCredentialsDelegate = s => null;
            var restorableStateService = new MockRestorableStateService();
            var target = new AccountService(identityService, restorableStateService, credentialStore);

            var userInfo = await target.GetSignedInUserAsync(); 
            
            Assert.IsNull(userInfo);
        }
        public async Task GetSignedInUserAsync_SignsInUsingCredentialStore_IfNoActiveSession()
        {
            var suspensionManagerState = new MockSuspensionManagerState();
            suspensionManagerState.SessionState[AccountService.ServerCookieHeaderKey] = "TestServerCookieHeader";
            var identityService = new MockIdentityService()
            {
                LogOnAsyncDelegate = (userId, password) => Task.FromResult(new LogOnResult { ServerCookieHeader = "TestServerCookieHeader", UserInfo = new UserInfo { UserName = userId } }),
                VerifyActiveSessionDelegate = (userName, serverCookieHeader) => Task.FromResult(false)
            };
            var credentialStore = new MockCredentialStore()
                {
                    GetSavedCredentialsDelegate = (s) => new PasswordCredential("KonaRI", "TestUserName", "TestPassword"),
                    SaveCredentialsDelegate = (a, b, c) => Task.Delay(0)
                };

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

            Assert.IsNotNull(user);
            Assert.IsTrue(user.UserName == "TestUserName");
        }
        public async Task SignOut_RaisesUserChangedEvent()
        {
            bool userChangedRaised = false;
            var suspensionManagerState = new MockSuspensionManagerState();
            var credentialStore = new MockCredentialStore
                {
                    GetSavedCredentialsDelegate = s => null,
                    RemovedSavedCredentialsDelegate = s => Task.Delay(0)
                };

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

            target.SignOut();

            Assert.IsTrue(userChangedRaised);

            var signedInUser = await target.GetSignedInUserAsync();

            Assert.IsNull(signedInUser);
        }