public async Task WhenAuthorizingAdditionalRecordsBeforeFirstAuth_ThenInvalidOperationExceptionThrown()
        {
            SetupEmptyLocalStore();

            HealthVaultSodaConnection healthVaultSodaConnection = CreateHealthVaultSodaConnection();
            await healthVaultSodaConnection.AuthorizeAdditionalRecordsAsync();
        }
        public async Task WhenAuthenticateCalledWithStoredInfo_ThenSessionCredentialPopulated()
        {
            SetupLocalStore();

            HealthVaultSodaConnection healthVaultSodaConnection = CreateHealthVaultSodaConnection();
            await healthVaultSodaConnection.AuthenticateAsync();

            Assert.IsNotNull(healthVaultSodaConnection.SessionCredential);
        }
        public void WhenMessageHandlerCreateCalled_ThenFactoryIsInvoked()
        {
            var handler = new HttpClientHandler();

            _subMessageHandlerFactory.Create().Returns(handler);

            HealthVaultSodaConnection healthVaultSodaConnection = CreateHealthVaultSodaConnection();
            IMessageHandlerFactory    factory = healthVaultSodaConnection;
            var handlerResult = factory.Create();

            Assert.AreEqual(handler, handlerResult);
        }
        public async Task WhenAuthorizeRestRequestInvoked_ThenHeadersArePopulated()
        {
            SetupLocalStore();

            HealthVaultSodaConnection healthVaultSodaConnection = CreateHealthVaultSodaConnection();

            HttpRequestMessage message = new HttpRequestMessage();

            await healthVaultSodaConnection.AuthorizeRestRequestAsync(message, RecordId);

            Assert.AreEqual("MSH-V1", message.Headers.Authorization.Scheme);

            string        authParameters     = message.Headers.Authorization.Parameter;
            List <string> authParametersList = authParameters.Split(',').ToList();

            Assert.IsTrue(authParametersList.Contains("app-token=" + SessionToken));
            Assert.IsTrue(authParametersList.Contains("offline-person-id=" + PersonId));
            Assert.IsTrue(authParametersList.Contains("record-id=" + RecordId));
        }
        public async Task WhenAuthorizingAdditionalRecords_ThenShellAuthServiceInvoked()
        {
            SetupLocalStore();

            var responseMessage = GenerateResponseMessage("GetAuthorizedPeopleResult.xml");

            _subHealthWebRequestClient
            .SendAsync(
                new Uri("https://platform.healthvault-ppe.com/platform/wildcat.ashx"),
                Arg.Any <byte[]>(),
                Arg.Any <int>(),
                Arg.Any <IDictionary <string, string> >(),
                Arg.Any <CancellationToken>())
            .Returns(responseMessage);

            HealthVaultSodaConnection healthVaultSodaConnection = CreateHealthVaultSodaConnection();
            await healthVaultSodaConnection.AuthorizeAdditionalRecordsAsync();

            await _subShellAuthService.Received()
            .AuthorizeAdditionalRecordsAsync(new Uri("https://account.healthvault-ppe.com/"), new Guid(ApplicationInstanceId));

            await _subLocalObjectStore.Received()
            .WriteAsync(HealthVaultSodaConnection.PersonInfoKey, Arg.Any <object>());
        }
        public async Task WhenAuthenticateCalledWithNoStoredInfo_ThenInfoIsFetchedAndStored()
        {
            SetupEmptyLocalStore();

            _healthVaultConfiguration.MasterApplicationId = s_masterApplicationId;

            var responseMessage1 = GenerateResponseMessage("NewApplicationCreationInfoResult.xml");
            var responseMessage2 = GenerateResponseMessage("GetServiceDefinitionResult.xml");

            // #3 is CAST call - but goes through IClientSessionCredentialClient and not HealthWebRequestClient
            var responseMessage4 = GenerateResponseMessage("GetAuthorizedPeopleResult.xml");

            // The first few calls use the default endpoint
            _subHealthWebRequestClient
            .SendAsync(
                new Uri("https://platform2.healthvault.com/platform/wildcat.ashx"),
                Arg.Any <byte[]>(),
                Arg.Any <int>(),
                Arg.Any <IDictionary <string, string> >(),
                Arg.Any <CancellationToken>())
            .Returns(responseMessage1, responseMessage2);

            // After GetServiceDefinition called, we are calling new endpoint
            _subHealthWebRequestClient
            .SendAsync(
                new Uri("https://platform.healthvault-ppe.com/platform/wildcat.ashx"),
                Arg.Any <byte[]>(),
                Arg.Any <int>(),
                Arg.Any <IDictionary <string, string> >(),
                Arg.Any <CancellationToken>())
            .Returns(responseMessage4);

            var sessionCredential = new SessionCredential
            {
                Token        = SessionToken,
                SharedSecret = SessionSharedSecret
            };

            _subClientSessionCredentialClient
            .GetSessionCredentialAsync(Arg.Any <CancellationToken>())
            .Returns(sessionCredential);

            _subServiceLocator
            .GetInstance <IClientSessionCredentialClient>()
            .Returns(_subClientSessionCredentialClient);

            // These values match the values in NewApplicationCreationInfoResult.xml
            _subShellAuthService
            .ProvisionApplicationAsync(
                new Uri("https://account.healthvault.com"),
                s_masterApplicationId,
                ApplicationCreationToken,
                ApplicationInstanceId)
            .Returns("1");

            HealthVaultSodaConnection healthVaultSodaConnection = CreateHealthVaultSodaConnection();
            await healthVaultSodaConnection.AuthenticateAsync();

            _subClientSessionCredentialClient.Received().AppSharedSecret = ApplicationSharedSecret;
            _subClientSessionCredentialClient.Received().Connection      = healthVaultSodaConnection;

            await _subLocalObjectStore.Received()
            .WriteAsync(
                HealthVaultSodaConnection.ServiceInstanceKey,
                Arg.Is <object>(o => ((HealthServiceInstance)o).HealthServiceUrl == new Uri("https://platform.healthvault-ppe.com/platform/wildcat.ashx")));

            await _subLocalObjectStore.Received()
            .WriteAsync(
                HealthVaultSodaConnection.ApplicationCreationInfoKey,
                Arg.Is <object>(o => ((ApplicationCreationInfo)o).AppInstanceId == new Guid("b5c5593b-afb4-466d-88f2-31707fb8634b")));

            await _subLocalObjectStore.Received()
            .WriteAsync(
                HealthVaultSodaConnection.SessionCredentialKey,
                Arg.Is <object>(o => ((SessionCredential)o).SharedSecret == SessionSharedSecret));

            await _subLocalObjectStore.Received()
            .WriteAsync(
                HealthVaultSodaConnection.PersonInfoKey,
                Arg.Is <object>(o => ((PersonInfo)o).Name == "David Rickard"));
        }