Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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"));
        }
Ejemplo n.º 3
0
        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));
        }
Ejemplo n.º 4
0
        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));
        }
Ejemplo n.º 5
0
        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);
        }