public async void TestBadInputsToAuthenticateAsync() { var authenticator = Mock.Of <IAuthenticator>(); var identityFactory = Mock.Of <IClientCredentialsFactory>(); var saslAuthenticator = new EdgeHubSaslPlainAuthenticator(authenticator, identityFactory, "iothub"); await Assert.ThrowsAsync <ArgumentException>(() => saslAuthenticator.AuthenticateAsync(null, "pwd")); await Assert.ThrowsAsync <ArgumentException>(() => saslAuthenticator.AuthenticateAsync("uid", null)); }
public async void TestAuthSucceeds() { var authenticator = Mock.Of <IAuthenticator>(); var clientCredentialsFactory = Mock.Of <IClientCredentialsFactory>(); var saslAuthenticator = new EdgeHubSaslPlainAuthenticator(authenticator, clientCredentialsFactory, "hub1"); var identity = new ModuleIdentity("hub1", "dev1", "mod1"); var clientCredentials = Mock.Of <IClientCredentials>(c => c.Identity == identity); const string UserId = "dev1/modules/[email protected]"; const string Password = "******"; Mock.Get(clientCredentialsFactory).Setup(f => f.GetWithSasToken("dev1", "mod1", string.Empty, Password, false)) .Returns(clientCredentials); Mock.Get(authenticator).Setup(a => a.AuthenticateAsync(clientCredentials)) .ReturnsAsync(true); IPrincipal principal = await saslAuthenticator.AuthenticateAsync(UserId, Password); Assert.NotNull(principal); var amqpAuthenticator = principal as IAmqpAuthenticator; Assert.NotNull(amqpAuthenticator); bool isAuthenticated = await amqpAuthenticator.AuthenticateAsync("dev1/mod1"); Assert.True(isAuthenticated); }
public async void TestNoDeviceId() { var authenticator = Mock.Of <IAuthenticator>(); var identityFactory = Mock.Of <IClientCredentialsFactory>(); var saslAuthenticator = new EdgeHubSaslPlainAuthenticator(authenticator, identityFactory, "iothub"); const string UserId = "[email protected]"; await Assert.ThrowsAsync <EdgeHubConnectionException>(() => saslAuthenticator.AuthenticateAsync(UserId, "pwd")); }
public async void TestGetSasTokenFailed() { var authenticator = Mock.Of <IAuthenticator>(); var clientCredentialsFactory = Mock.Of <IClientCredentialsFactory>(); var saslAuthenticator = new EdgeHubSaslPlainAuthenticator(authenticator, clientCredentialsFactory, "iothub"); const string UserId = "dev1/modules/[email protected]"; const string Password = "******"; Mock.Get(clientCredentialsFactory).Setup(f => f.GetWithSasToken("dev1", "mod1", string.Empty, Password)) .Throws(new ApplicationException("Bad donut")); await Assert.ThrowsAsync <EdgeHubConnectionException>(() => saslAuthenticator.AuthenticateAsync(UserId, Password)); }
public async void TestAuthSucceeds() { var authenticator = Mock.Of <IAuthenticator>(); var clientCredentialsFactory = Mock.Of <IClientCredentialsFactory>(); var saslAuthenticator = new EdgeHubSaslPlainAuthenticator(authenticator, clientCredentialsFactory, "hub1"); var identity = new ModuleIdentity("hub1", "dev1", "mod1"); var clientCredentials = Mock.Of <IClientCredentials>(c => c.Identity == identity); const string UserId = "dev1/modules/[email protected]"; const string Password = "******"; Mock.Get(clientCredentialsFactory).Setup(f => f.GetWithSasToken("dev1", "mod1", string.Empty, Password)) .Returns(clientCredentials); Mock.Get(authenticator).Setup(a => a.AuthenticateAsync(clientCredentials)) .ReturnsAsync(true); var principal = await saslAuthenticator.AuthenticateAsync(UserId, Password) as SaslPrincipal; Assert.NotNull(principal); Assert.NotNull(principal.Identity); Assert.NotNull(principal.AmqpAuthentication); Assert.Equal(identity, principal.AmqpAuthentication.ClientCredentials.OrDefault().Identity); }