public void TestInitialize() { // Arrange Ioc.Container = new DependencyInjectionContainer(); IServiceLocator serviceLocator = Substitute.For <IServiceLocator>(); serviceLocator.GetInstance <WebHealthVaultConfiguration>().Returns(new WebHealthVaultConfiguration { DefaultHealthVaultUrl = new Uri("http://www.bing.com"), DefaultHealthVaultShellUrl = new Uri("http://www.bing.com") }); HealthServiceInstance healthServiceInstance = Substitute.For <HealthServiceInstance>(); SessionCredential sessionCredential = new SessionCredential { ExpirationUtc = DateTimeOffset.UtcNow.AddHours(4), SharedSecret = SessionSharedSecret, Token = SessionToken }; _userAuthToken = "someToken"; _webHealthVaultConnection = new WebHealthVaultConnection(serviceLocator) { UserAuthToken = _userAuthToken, ServiceInstance = healthServiceInstance, SessionCredential = sessionCredential }; }
public async Task <WebConnectionInfo> CreateWebConnectionInfoAsync(string token, string instanceId) { IServiceInstanceProvider serviceInstanceProvider = _serviceLocator.GetInstance <IServiceInstanceProvider>(); HealthServiceInstance serviceInstance = await serviceInstanceProvider.GetHealthServiceInstanceAsync(instanceId); IWebHealthVaultConnection connection = Ioc.Container.Locate <IWebHealthVaultConnection>( extraData: new { serviceLocator = _serviceLocator }); WebHealthVaultConnection webHealthVaultConnection = connection as WebHealthVaultConnection; webHealthVaultConnection.ServiceInstance = serviceInstance; webHealthVaultConnection.UserAuthToken = token; IPersonClient personClient = webHealthVaultConnection.CreatePersonClient(); var personInfo = await personClient.GetPersonInfoAsync(); WebConnectionInfo webConnectionInfo = new WebConnectionInfo { PersonInfo = personInfo, ServiceInstanceId = instanceId, SessionCredential = webHealthVaultConnection.SessionCredential, UserAuthToken = token }; return(webConnectionInfo); }
public async Task WhenTokenAndInstanceId() { // Arrange IServiceLocator serviceLocator = Substitute.For <IServiceLocator>(); WebHealthVaultConfiguration webHealthVaultConfiguration = Substitute.For <WebHealthVaultConfiguration>(); ICookieDataManager cookieDataManager = Substitute.For <ICookieDataManager>(); serviceLocator.GetInstance <WebHealthVaultConfiguration>().Returns(webHealthVaultConfiguration); serviceLocator.GetInstance <ICookieDataManager>().Returns(cookieDataManager); WebHealthVaultConnection webHealthVaultConnection = Substitute.For <WebHealthVaultConnection>(serviceLocator); Ioc.Container.Configure(c => c.ExportInstance(webHealthVaultConnection).As <IWebHealthVaultConnection>()); IPersonClient mockedPersonClient = Substitute.For <IPersonClient>(); mockedPersonClient.GetPersonInfoAsync().Returns(Task.FromResult(new PersonInfo())); Ioc.Container.Configure(c => c.ExportInstance(mockedPersonClient).As <IPersonClient>()); // Act IWebConnectionInfoProvider webConnectionInfoProvider = new WebConnectionInfoProvider(serviceLocator); WebConnectionInfo webConnectionInfo = await webConnectionInfoProvider.CreateWebConnectionInfoAsync("someToken", "1"); // Assert Assert.AreEqual("someToken", webConnectionInfo.UserAuthToken); }
public async Task WhenUserIdentityHasWebConnectionInfo() { // Arrange var webConnectionInfo = new WebConnectionInfo { ServiceInstanceId = "1", PersonInfo = new PersonInfo(), SessionCredential = new SessionCredential(), UserAuthToken = "some" }; // Mock HealthVaultIdentityProvider IHealthVaultIdentityProvider healthVaultIdentityProvider = Substitute.For <IHealthVaultIdentityProvider>(); healthVaultIdentityProvider.TryGetIdentity().Returns(new HealthVaultIdentity { WebConnectionInfo = webConnectionInfo }); Ioc.Container.Configure(c => c.ExportInstance(healthVaultIdentityProvider).As <IHealthVaultIdentityProvider>()); // Mock HealthVaultConnection WebHealthVaultConfiguration webHealthVaultConfiguration = new WebHealthVaultConfiguration(); webHealthVaultConfiguration.DefaultHealthVaultUrl = new Uri("http://www.bing.com"); webHealthVaultConfiguration.DefaultHealthVaultShellUrl = new Uri("http://www.bing.com"); IServiceLocator serviceLocator = Substitute.For <IServiceLocator>(); serviceLocator.GetInstance <WebHealthVaultConfiguration>().Returns(webHealthVaultConfiguration); serviceLocator.GetInstance <IHealthWebRequestClient>().Returns(Substitute.For <IHealthWebRequestClient>()); serviceLocator .GetInstance <IHealthServiceResponseParser>() .Returns(Substitute.For <IHealthServiceResponseParser>()); WebHealthVaultConnection webHealthVaultConnection = Substitute.For <WebHealthVaultConnection>(serviceLocator); Ioc.Container.Configure(c => c.ExportInstance(webHealthVaultConnection).As <IWebHealthVaultConnection>()); // Mock ServiceInstanceProvider IServiceInstanceProvider serviceInstanceProvider = Substitute.For <IServiceInstanceProvider>(); serviceInstanceProvider .GetHealthServiceInstanceAsync(Arg.Any <string>()) .Returns(Task.FromResult(new HealthServiceInstance())); Ioc.Container.Configure(c => c.ExportInstance(serviceInstanceProvider).As <IServiceInstanceProvider>()); WebHealthVaultFactory factory = new WebHealthVaultFactory(); // Act IWebHealthVaultConnection resultWebHealthVaultConnection = await factory.CreateWebConnectionInternalAsync(); // Assert Assert.AreEqual(webConnectionInfo.UserAuthToken, resultWebHealthVaultConnection.UserAuthToken); }
// Enables unit test internal async Task <IWebHealthVaultConnection> CreateWebConnectionInternalAsync() { IHealthVaultIdentityProvider healthVaultIdentityProvider = Ioc.Container.Locate <IHealthVaultIdentityProvider>(); HealthVaultIdentity identity = healthVaultIdentityProvider.TryGetIdentity(); IServiceLocator serviceLocator = new ServiceLocator(); if (identity == null) { IWebHealthVaultConnection anonymousWebConnection = serviceLocator.GetInstance <IWebHealthVaultConnection>(); return(anonymousWebConnection); } var webConnectionInfo = identity.WebConnectionInfo; if (webConnectionInfo == null) { throw new NotSupportedException("WebConnectionInfo is expected for authenticated connections"); } // Get ServiceInstance IServiceInstanceProvider serviceInstanceProvider = Ioc.Container.Locate <IServiceInstanceProvider>(); HealthServiceInstance serviceInstance = await serviceInstanceProvider.GetHealthServiceInstanceAsync(webConnectionInfo.ServiceInstanceId); // Get AuthInformation SessionCredential sessionCredentialToken = webConnectionInfo.SessionCredential; string token = webConnectionInfo.UserAuthToken; IWebHealthVaultConnection webConnection = Ioc.Container.Locate <IWebHealthVaultConnection>(extraData: new { serviceLocator = serviceLocator }); WebHealthVaultConnection connection = webConnection as WebHealthVaultConnection; connection.UserAuthToken = token; connection.ServiceInstance = serviceInstance; connection.SessionCredential = sessionCredentialToken; return(webConnection); }