public async Task AuthenticateFromCacheTest() { // Arrange var cloudProxy = Mock.Of <ICloudProxy>(c => c.IsActive && c.OpenAsync() == Task.FromResult(true)); var connectionManager = Mock.Of <IConnectionManager>( c => c.CreateCloudConnectionAsync(It.IsAny <IClientCredentials>()) == Task.FromResult(Try.Success(cloudProxy))); string iothubHostName = "iothub1.azure.net"; string callerProductInfo = "productInfo"; string sasToken = TokenHelper.CreateSasToken($"{iothubHostName}/devices/device1/modules/moduleId"); var identity = Mock.Of <IIdentity>(i => i.Id == "d1"); var credentials = new TokenCredentials(identity, sasToken, callerProductInfo); var storedTokenCredentials = Mock.Of <ITokenCredentials>(c => c.Token == sasToken); var credentialsStore = new Mock <ICredentialsStore>(); credentialsStore.Setup(c => c.Get(It.IsAny <IIdentity>())) .ReturnsAsync(Option.Some((IClientCredentials)storedTokenCredentials)); credentialsStore.Setup(c => c.Add(It.IsAny <IClientCredentials>())) .Returns(Task.CompletedTask); var tokenCredentialsAuthenticator = new TokenCacheAuthenticator(new CloudTokenAuthenticator(connectionManager), credentialsStore.Object, iothubHostName); // Act bool isAuthenticated = await tokenCredentialsAuthenticator.AuthenticateAsync(credentials); // assert Assert.True(isAuthenticated); Mock.Verify(credentialsStore); Mock.Get(connectionManager).Verify(c => c.CreateCloudConnectionAsync(It.IsAny <IClientCredentials>()), Times.Never); Mock.Get(cloudProxy).Verify(c => c.OpenAsync(), Times.Never); }
public async Task NotAuthenticatedTest() { // Arrange var connectionManager = Mock.Of <IConnectionManager>( c => c.CreateCloudConnectionAsync(It.IsAny <IClientCredentials>()) == Task.FromResult(Try <ICloudProxy> .Failure(new UnauthorizedException("Not authorized")))); string iothubHostName = "iothub1.azure.net"; string callerProductInfo = "productInfo"; string sasToken = TokenHelper.CreateSasToken($"{iothubHostName}/devices/device1/modules/moduleId"); var identity = Mock.Of <IIdentity>(i => i.Id == "d1"); var credentials = new TokenCredentials(identity, sasToken, callerProductInfo, Option.None <string>(), false); string sasToken2 = TokenHelper.CreateSasToken($"{iothubHostName}/devices/device1/modules/moduleId") + "a"; var storedTokenCredentials = Mock.Of <ITokenCredentials>(c => c.Token == sasToken2); var credentialsStore = new Mock <ICredentialsCache>(); credentialsStore.Setup(c => c.Get(It.IsAny <IIdentity>())) .ReturnsAsync(Option.Some((IClientCredentials)storedTokenCredentials)); credentialsStore.Setup(c => c.Add(It.IsAny <IClientCredentials>())) .Returns(Task.CompletedTask); var tokenCredentialsAuthenticator = new TokenCacheAuthenticator(new CloudTokenAuthenticator(connectionManager, iothubHostName), credentialsStore.Object, iothubHostName); // Act bool isAuthenticated = await tokenCredentialsAuthenticator.AuthenticateAsync(credentials); // assert Assert.False(isAuthenticated); Mock.Verify(credentialsStore); Mock.Verify(Mock.Get(connectionManager)); }