Esempio n. 1
0
        public void ParentNetworkAuthOkTest()
        {
            var deviceOperations  = Substitute.For <IDeviceOperations>();
            var networkOperations = Substitute.For <INetworkOperations>();
            var serviceOperations = Substitute.For <IServiceOperations>();

            var deviceId  = Identity.Next();
            var deviceKey = Crypto.GenerateSafeRandomToken();

            var networkId  = Identity.Next();
            var networkKey = Crypto.GenerateSafeRandomToken();

            var parentNetworkId  = Identity.Next();
            var parentNetworkKey = Crypto.GenerateSafeRandomToken();

            var serviceId = Identity.Next();
            var apiKey    = Crypto.GenerateSafeRandomToken();

            deviceOperations.Get(deviceId).Returns(TestDataCreator.Device(deviceId, deviceKey, networkId, serviceId, null, 0));
            networkOperations.Get(networkId).Returns(TestDataCreator.Network(networkId, networkKey, parentNetworkId, serviceId, null, null));
            networkOperations.Get(parentNetworkId).Returns(TestDataCreator.Network(parentNetworkId, parentNetworkKey, null, serviceId, null, null));
            serviceOperations.Get(serviceId).Returns(TestDataCreator.Service(serviceId, apiKey, null, null));

            var deviceAuthenticator = new DeviceAuthenticator(deviceOperations, networkOperations, serviceOperations);

            Assert.IsTrue(deviceAuthenticator.Authenticate(new AuthenticationParameters(deviceId, parentNetworkKey)));
        }
        public void ResolveIncomingNullAllTest()
        {
            var deviceOperations   = Substitute.For <IDeviceOperations>();
            var networkeOperations = Substitute.For <INetworkOperations>();
            var serviceOperations  = Substitute.For <IServiceOperations>();
            var companyOperations  = Substitute.For <ICompanyOperations>();
            var telemetryDataSinkMetadataRegistry = Substitute.For <ITelemetryDataSinkMetadataRegistry>();

            var deviceId = Identity.Next();

            var serviceId = Identity.Next();
            var networkId = Identity.Next();
            var companyId = Identity.Next();

            deviceOperations.Get(deviceId).Returns(TestDataCreator.Device(deviceId, null, networkId, serviceId, companyId, 0));
            serviceOperations.Get(serviceId)
            .Returns(TestDataCreator.Service(serviceId, null,
                                             companyId, new TelemetryDataSinkSettings {
                Incoming = null
            }));
            companyOperations.Get(companyId)
            .Returns(TestDataCreator.Company(companyId, new TelemetryDataSinkSettings {
                Incoming = null
            }));
            networkeOperations.Get(networkId)
            .Returns(TestDataCreator.Network(networkId, "key", null, serviceId, companyId, new TelemetryDataSinkSettings {
                Incoming = new List <TelemetryDataSinkParameters>()
            }));

            var telemetryDataSinkResolver = new TelemetryDataSinkResolver(deviceOperations, networkeOperations, serviceOperations, companyOperations,
                                                                          telemetryDataSinkMetadataRegistry, new DynamicConnectionStringResolver(null));

            Assert.AreEqual(0, telemetryDataSinkResolver.ResolveIncoming(deviceId).Count());
        }
        public void ResolveIncomingCompanyTest()
        {
            var deviceOperations   = Substitute.For <IDeviceOperations>();
            var networkeOperations = Substitute.For <INetworkOperations>();
            var serviceOperations  = Substitute.For <IServiceOperations>();
            var companyOperations  = Substitute.For <ICompanyOperations>();
            var telemetryDataSinkMetadataRegistry = Substitute.For <ITelemetryDataSinkMetadataRegistry>();

            var deviceId = Identity.Next();

            var serviceId = Identity.Next();
            var networkId = Identity.Next();
            var companyId = Identity.Next();

            telemetryDataSinkMetadataRegistry.Incoming.Returns(new List <TelemetryDataSinkMetadata>
            {
                new TelemetryDataSinkMetadata("azureData", "data", typeof(IncomingStubs.CurrentDataStub),
                                              new[] { "ConnectionString", "Table" }, new Dictionary <string, string>())
            });

            deviceOperations.Get(deviceId).Returns(TestDataCreator.Device(deviceId, null, networkId, serviceId, companyId, 0));
            serviceOperations.Get(serviceId)
            .Returns(TestDataCreator.Service(serviceId, null,
                                             companyId, new TelemetryDataSinkSettings {
                Incoming = new List <TelemetryDataSinkParameters>()
            }));

            companyOperations.Get(companyId)
            .Returns(TestDataCreator.Company(companyId, new TelemetryDataSinkSettings
            {
                Incoming =
                    new List <TelemetryDataSinkParameters>
                {
                    new TelemetryDataSinkParameters()
                    {
                        SinkName   = "azureData",
                        Parameters =
                            new Dictionary <string, string>()
                        {
                            { "ConnectionString", "that" },
                            { "Table", "t" }
                        }
                    }
                }
            }));
            networkeOperations.Get(networkId)
            .Returns(TestDataCreator.Network(networkId, "key", null, serviceId, companyId, new TelemetryDataSinkSettings {
                Incoming = new List <TelemetryDataSinkParameters>()
            }));

            var telemetryDataSinkResolver = new TelemetryDataSinkResolver(deviceOperations, networkeOperations, serviceOperations, companyOperations,
                                                                          telemetryDataSinkMetadataRegistry, new DynamicConnectionStringResolver(null));

            Assert.AreEqual(1, telemetryDataSinkResolver.ResolveIncoming(deviceId).Count());
            Assert.IsInstanceOfType(telemetryDataSinkResolver.ResolveIncoming(deviceId).First(), typeof(IncomingStubs.CurrentDataStub));
        }
Esempio n. 4
0
        public void ServiceAuthFailTest()
        {
            var networkOperations = Substitute.For <INetworkOperations>();
            var serviceOperations = Substitute.For <IServiceOperations>();

            var networkId  = Identity.Next();
            var networkKey = Crypto.GenerateSafeRandomToken();

            var serviceId = Identity.Next();
            var apiKey    = Crypto.GenerateSafeRandomToken();

            networkOperations.Get(networkId).Returns(TestDataCreator.Network(networkId, networkKey, null, serviceId, null, null));
            serviceOperations.Get(serviceId).Returns(TestDataCreator.Service(serviceId, apiKey, null, null));

            var networkAuthenticator = new NetworkAuthenticator(networkOperations, serviceOperations);

            Assert.IsFalse(networkAuthenticator.Authenticate(new AuthenticationParameters(networkId, Identity.Next())));
        }
Esempio n. 5
0
        public void ServiceOperationsTest()
        {
            var serviceOperations = Substitute.For <IPersistedServiceOperations>();

            var cachingServiceOperations = new ServiceOperations(serviceOperations);

            cachingServiceOperations.Remove("1");

            serviceOperations.Get("1").Returns(TestDataCreator.Service("1", "2", "3", null));

            var service = cachingServiceOperations.Get("1");

            Assert.AreEqual("2", service.ApiKey);

            serviceOperations.Received().Get("1");

            serviceOperations.ClearReceivedCalls();

            var service2 = cachingServiceOperations.Get("1");

            Assert.AreEqual("2", service2.ApiKey);

            serviceOperations.DidNotReceive().Get("1");
        }