public void ConfigurationManagerUsingCustomClass()
        {
            var docRetriever  = new FileDocumentRetriever();
            var configManager = new ConfigurationManager <IssuerMetadata>("IssuerMetadata.json", new IssuerConfigurationRetriever(), docRetriever);
            var context       = new CompareContext($"{this}.GetConfiguration");

            var configuration = configManager.GetConfigurationAsync().Result;

            configManager.MetadataAddress = "IssuerMetadata.json";
            var configuration2 = configManager.GetConfigurationAsync().Result;

            if (!IdentityComparer.AreEqual(configuration.Issuer, configuration2.Issuer))
            {
                context.Diffs.Add("!IdentityComparer.AreEqual(configuration, configuration2)");
            }

            // AutomaticRefreshInterval should pick up new bits.
            configManager = new ConfigurationManager <IssuerMetadata>("IssuerMetadata.json", new IssuerConfigurationRetriever(), docRetriever);
            configManager.RequestRefresh();
            configuration = configManager.GetConfigurationAsync().Result;
            TestUtilities.SetField(configManager, "_lastRefresh", DateTimeOffset.UtcNow - TimeSpan.FromHours(1));
            configManager.MetadataAddress = "IssuerMetadata2.json";
            configManager.RequestRefresh();
            configuration2 = configManager.GetConfigurationAsync().Result;
            if (IdentityComparer.AreEqual(configuration.Issuer, configuration2.Issuer))
            {
                context.Diffs.Add("IdentityComparer.AreEqual(configuration, configuration2)");
            }

            TestUtilities.AssertFailIfErrors(context);
        }
        public void GetDocuments()
        {
            FileDocumentRetriever docRetriever = new FileDocumentRetriever();

            GetDocument(null, docRetriever, CancellationToken.None, ExpectedException.ArgumentNullException());
            GetDocument("OpenIdConnectMetadata.json", docRetriever, CancellationToken.None, ExpectedException.IOException("IDX10804:", typeof(FileNotFoundException), "IDX10814:"));
            GetDocument("ValidJson.json", docRetriever, CancellationToken.None, ExpectedException.NoExceptionExpected);
        }
Пример #3
0
        public void GetSets()
        {
            FileDocumentRetriever docRetriever = new FileDocumentRetriever();
            ConfigurationManager <OpenIdConnectConfiguration> configManager = new ConfigurationManager <OpenIdConnectConfiguration>("OpenIdConnectMetadata.json", new OpenIdConnectConfigurationRetriever(), docRetriever);
            Type type = typeof(ConfigurationManager <OpenIdConnectConfiguration>);

            PropertyInfo[] properties = type.GetProperties();
            if (properties.Length != 2)
            {
                Assert.True(false, "Number of properties has changed from 2 to: " + properties.Length + ", adjust tests");
            }

            TimeSpan defaultAutomaticRefreshInterval = ConfigurationManager <OpenIdConnectConfiguration> .DefaultAutomaticRefreshInterval;
            TimeSpan defaultRefreshInterval          = ConfigurationManager <OpenIdConnectConfiguration> .DefaultRefreshInterval;

            GetSetContext context =
                new GetSetContext
            {
                PropertyNamesAndSetGetValue = new List <KeyValuePair <string, List <object> > >
                {
                    new KeyValuePair <string, List <object> >("AutomaticRefreshInterval", new List <object> {
                        defaultAutomaticRefreshInterval, TimeSpan.FromHours(1), TimeSpan.FromHours(10)
                    }),
                    new KeyValuePair <string, List <object> >("RefreshInterval", new List <object> {
                        defaultRefreshInterval, TimeSpan.FromHours(1), TimeSpan.FromHours(10)
                    }),
                },
                Object = configManager,
            };

            TestUtilities.GetSet(context);
            TestUtilities.AssertFailIfErrors("ConfigurationManager_GetSets", context.Errors);

            TestUtilities.SetGet(configManager, "AutomaticRefreshInterval", TimeSpan.FromMilliseconds(1), ExpectedException.ArgumentOutOfRangeException(substringExpected: "IDX10107:"));
            TestUtilities.SetGet(configManager, "RefreshInterval", TimeSpan.FromMilliseconds(1), ExpectedException.ArgumentOutOfRangeException(substringExpected: "IDX10106:"));
            TestUtilities.SetGet(configManager, "RefreshInterval", Timeout.InfiniteTimeSpan, ExpectedException.ArgumentOutOfRangeException(substringExpected: "IDX10106:"));
        }
Пример #4
0
        public void GetConfiguration()
        {
            FileDocumentRetriever docRetriever = new FileDocumentRetriever();
            ConfigurationManager <OpenIdConnectConfiguration> configManager = new ConfigurationManager <OpenIdConnectConfiguration>("OpenIdConnectMetadata.json", new OpenIdConnectConfigurationRetriever(), docRetriever);

            // AutomaticRefreshInterval interval should return same config.
            OpenIdConnectConfiguration configuration = configManager.GetConfigurationAsync().Result;

            TestUtilities.SetField(configManager, "_metadataAddress", "OpenIdConnectMetadata2.json");
            OpenIdConnectConfiguration configuration2 = configManager.GetConfigurationAsync().Result;

            Assert.True(IdentityComparer.AreEqual(configuration, configuration2));
            Assert.True(object.ReferenceEquals(configuration, configuration2));

            // AutomaticRefreshInterval should pick up new bits.
            configManager = new ConfigurationManager <OpenIdConnectConfiguration>("OpenIdConnectMetadata.json", new OpenIdConnectConfigurationRetriever(), docRetriever);
            TestUtilities.SetField(configManager, "_automaticRefreshInterval", TimeSpan.FromMilliseconds(1));
            configuration = configManager.GetConfigurationAsync().Result;
            TestUtilities.SetField(configManager, "_lastRefresh", DateTimeOffset.UtcNow - TimeSpan.FromHours(1));
            TestUtilities.SetField(configManager, "_metadataAddress", "OpenIdConnectMetadata2.json");
            configManager.RequestRefresh();
            configuration2 = configManager.GetConfigurationAsync().Result;
            Assert.False(IdentityComparer.AreEqual(configuration, configuration2));
            Assert.False(object.ReferenceEquals(configuration, configuration2));

            // RefreshInterval is set to MaxValue
            configManager = new ConfigurationManager <OpenIdConnectConfiguration>("OpenIdConnectMetadata.json", new OpenIdConnectConfigurationRetriever(), docRetriever);
            configuration = configManager.GetConfigurationAsync().Result;
            configManager.RefreshInterval = TimeSpan.MaxValue;
            TestUtilities.SetField(configManager, "_metadataAddress", "OpenIdConnectMetadata2.json");
            configuration2 = configManager.GetConfigurationAsync().Result;
            Assert.True(IdentityComparer.AreEqual(configuration, configuration2));
            Assert.True(object.ReferenceEquals(configuration, configuration2));

            // Refresh should have no effect
            configManager = new ConfigurationManager <OpenIdConnectConfiguration>("OpenIdConnectMetadata.json", new OpenIdConnectConfigurationRetriever(), docRetriever);
            configuration = configManager.GetConfigurationAsync().Result;
            configManager.RefreshInterval = TimeSpan.FromHours(10);
            configManager.RequestRefresh();
            configuration2 = configManager.GetConfigurationAsync().Result;
            Assert.True(IdentityComparer.AreEqual(configuration, configuration2));
            Assert.True(object.ReferenceEquals(configuration, configuration2));

            // Refresh should force pickup of new config
            configManager = new ConfigurationManager <OpenIdConnectConfiguration>("OpenIdConnectMetadata.json", new OpenIdConnectConfigurationRetriever(), docRetriever);
            configuration = configManager.GetConfigurationAsync().Result;
            TestUtilities.SetField(configManager, "_lastRefresh", DateTimeOffset.UtcNow - TimeSpan.FromHours(1));
            configManager.RequestRefresh();
            TestUtilities.SetField(configManager, "_metadataAddress", "OpenIdConnectMetadata2.json");
            configuration2 = configManager.GetConfigurationAsync().Result;
            Assert.False(object.ReferenceEquals(configuration, configuration2));
            Assert.False(IdentityComparer.AreEqual(configuration, configuration2));

            // Refresh set to MaxValue
            configManager.RefreshInterval = TimeSpan.MaxValue;
            configuration = configManager.GetConfigurationAsync().Result;
            Assert.True(object.ReferenceEquals(configuration, configuration2));
            Assert.True(IdentityComparer.AreEqual(configuration, configuration2));

            // get configuration from http address, should throw
            configManager = new ConfigurationManager <OpenIdConnectConfiguration>("http://someaddress.com", new OpenIdConnectConfigurationRetriever());
            ExpectedException ee = new ExpectedException(typeof(InvalidOperationException), "IDX10803:", typeof(ArgumentException));

            try
            {
                configuration = configManager.GetConfigurationAsync().Result;
                ee.ProcessNoException();
            }
            catch (AggregateException ex)
            {
                // this should throw, because last configuration retrived was null
                Assert.Throws <AggregateException>(() => configuration = configManager.GetConfigurationAsync().Result);

                ex.Handle((x) =>
                {
                    ee.ProcessException(x);
                    return(true);
                });
            }
        }
Пример #5
0
        public void GetConfiguration()
        {
            var docRetriever  = new FileDocumentRetriever();
            var configManager = new ConfigurationManager <OpenIdConnectConfiguration>("OpenIdConnectMetadata.json", new OpenIdConnectConfigurationRetriever(), docRetriever);
            var context       = new CompareContext($"{this}.GetConfiguration");

            // AutomaticRefreshInterval interval should return same config.
            var configuration = configManager.GetConfigurationAsync().Result;

            TestUtilities.SetField(configManager, "_metadataAddress", "OpenIdConnectMetadata2.json");
            var configuration2 = configManager.GetConfigurationAsync().Result;

            IdentityComparer.AreEqual(configuration, configuration2, context);
            if (!object.ReferenceEquals(configuration, configuration2))
            {
                context.Diffs.Add("!object.ReferenceEquals(configuration, configuration2)");
            }

            // AutomaticRefreshInterval should pick up new bits.
            configManager = new ConfigurationManager <OpenIdConnectConfiguration>("OpenIdConnectMetadata.json", new OpenIdConnectConfigurationRetriever(), docRetriever);
            TestUtilities.SetField(configManager, "_automaticRefreshInterval", TimeSpan.FromMilliseconds(1));
            configuration = configManager.GetConfigurationAsync().Result;
            TestUtilities.SetField(configManager, "_lastRefresh", DateTimeOffset.UtcNow - TimeSpan.FromHours(1));
            TestUtilities.SetField(configManager, "_metadataAddress", "OpenIdConnectMetadata2.json");
            configManager.RequestRefresh();
            configuration2 = configManager.GetConfigurationAsync().Result;
            if (IdentityComparer.AreEqual(configuration, configuration2))
            {
                context.Diffs.Add("IdentityComparer.AreEqual(configuration, configuration2)");
            }

            if (object.ReferenceEquals(configuration, configuration2))
            {
                context.Diffs.Add("object.ReferenceEquals(configuration, configuration2) (2)");
            }

            // RefreshInterval is set to MaxValue
            configManager = new ConfigurationManager <OpenIdConnectConfiguration>("OpenIdConnectMetadata.json", new OpenIdConnectConfigurationRetriever(), docRetriever);
            configuration = configManager.GetConfigurationAsync().Result;
            configManager.RefreshInterval = TimeSpan.MaxValue;
            TestUtilities.SetField(configManager, "_metadataAddress", "OpenIdConnectMetadata2.json");
            configuration2 = configManager.GetConfigurationAsync().Result;
            IdentityComparer.AreEqual(configuration, configuration2, context);
            if (!object.ReferenceEquals(configuration, configuration2))
            {
                context.Diffs.Add("!object.ReferenceEquals(configuration, configuration2) (3)");
            }

            // Refresh should have no effect
            configManager = new ConfigurationManager <OpenIdConnectConfiguration>("OpenIdConnectMetadata.json", new OpenIdConnectConfigurationRetriever(), docRetriever);
            configuration = configManager.GetConfigurationAsync().Result;
            configManager.RefreshInterval = TimeSpan.FromHours(10);
            configManager.RequestRefresh();
            configuration2 = configManager.GetConfigurationAsync().Result;
            IdentityComparer.AreEqual(configuration, configuration2, context);
            if (!object.ReferenceEquals(configuration, configuration2))
            {
                context.Diffs.Add("!object.ReferenceEquals(configuration, configuration2) (4)");
            }

            // Refresh should force pickup of new config
            configManager = new ConfigurationManager <OpenIdConnectConfiguration>("OpenIdConnectMetadata.json", new OpenIdConnectConfigurationRetriever(), docRetriever);
            configuration = configManager.GetConfigurationAsync().Result;
            TestUtilities.SetField(configManager, "_lastRefresh", DateTimeOffset.UtcNow - TimeSpan.FromHours(1));
            configManager.RequestRefresh();
            TestUtilities.SetField(configManager, "_metadataAddress", "OpenIdConnectMetadata2.json");
            configuration2 = configManager.GetConfigurationAsync().Result;
            if (IdentityComparer.AreEqual(configuration, configuration2))
            {
                context.Diffs.Add("IdentityComparer.AreEqual(configuration, configuration2), should be different");
            }

            if (object.ReferenceEquals(configuration, configuration2))
            {
                context.Diffs.Add("object.ReferenceEquals(configuration, configuration2)");
            }

            // Refresh set to MaxValue
            configManager.RefreshInterval = TimeSpan.MaxValue;
            configuration = configManager.GetConfigurationAsync().Result;
            IdentityComparer.AreEqual(configuration, configuration2, context);
            if (!object.ReferenceEquals(configuration, configuration2))
            {
                context.Diffs.Add("!object.ReferenceEquals(configuration, configuration2)");
            }

            // get configuration from http address, should throw
            configManager = new ConfigurationManager <OpenIdConnectConfiguration>("http://someaddress.com", new OpenIdConnectConfigurationRetriever());
            var ee = new ExpectedException(typeof(InvalidOperationException), "IDX20803:", typeof(ArgumentException));

            try
            {
                configuration = configManager.GetConfigurationAsync().Result;
                ee.ProcessNoException(context);
            }
            catch (AggregateException ex)
            {
                // this should throw, because last configuration retrived was null
                Assert.Throws <AggregateException>(() => configuration = configManager.GetConfigurationAsync().Result);

                ex.Handle((x) =>
                {
                    ee.ProcessException(x, context);
                    return(true);
                });
            }

            // get configuration from https address, should throw
            configManager = new ConfigurationManager <OpenIdConnectConfiguration>("https://someaddress.com", new OpenIdConnectConfigurationRetriever());
            ee            = new ExpectedException(typeof(InvalidOperationException), "IDX20803:", typeof(IOException));
            try
            {
                configuration = configManager.GetConfigurationAsync().Result;
                ee.ProcessNoException(context);
            }
            catch (AggregateException ex)
            {
                // this should throw, because last configuration retrived was null
                Assert.Throws <AggregateException>(() => configuration = configManager.GetConfigurationAsync().Result);

                ex.Handle((x) =>
                {
                    ee.ProcessException(x, context);
                    return(true);
                });
            }

            // get configuration with unsuccessful HTTP response status code
            configManager = new ConfigurationManager <OpenIdConnectConfiguration>("https://httpstat.us/429", new OpenIdConnectConfigurationRetriever());
            ee            = new ExpectedException(typeof(InvalidOperationException), "IDX20803:", typeof(IOException));
            try
            {
                configuration = configManager.GetConfigurationAsync().Result;
                ee.ProcessNoException(context);
            }
            catch (AggregateException ex)
            {
                // this should throw, because last configuration retrived was null
                Assert.Throws <AggregateException>(() => configuration = configManager.GetConfigurationAsync().Result);

                ex.Handle((x) =>
                {
                    ee.ProcessException(x, context);
                    return(true);
                });
            }

            // Unable to obtain a new configuration, but _currentConfiguration is not null so it should be returned.
            configManager = new ConfigurationManager <OpenIdConnectConfiguration>("OpenIdConnectMetadata.json", new OpenIdConnectConfigurationRetriever(), docRetriever);
            configuration = configManager.GetConfigurationAsync().Result;
            TestUtilities.SetField(configManager, "_lastRefresh", DateTimeOffset.UtcNow - TimeSpan.FromHours(1));
            configManager.RequestRefresh();
            TestUtilities.SetField(configManager, "_metadataAddress", "http://someaddress.com");
            configuration2 = configManager.GetConfigurationAsync().Result;
            IdentityComparer.AreEqual(configuration, configuration2, context);
            if (!object.ReferenceEquals(configuration, configuration2))
            {
                context.Diffs.Add("!object.ReferenceEquals(configuration, configuration2)");
            }

            TestUtilities.AssertFailIfErrors(context);
        }