Beispiel #1
0
        public static async Task <IStorageAccount> GetStorageAccountAsync(this IStorageAccountProvider provider, ParameterInfo parameter, CancellationToken cancellationToken, INameResolver nameResolver = null)
        {
            if (provider == null)
            {
                throw new ArgumentNullException("provider");
            }

            string connectionStringName = GetAccountOverrideOrNull(parameter);

            if (string.IsNullOrEmpty(connectionStringName))
            {
                connectionStringName = ConnectionStringNames.Storage;
            }

            if (nameResolver != null)
            {
                string resolved = null;
                if (nameResolver.TryResolveWholeString(connectionStringName, out resolved))
                {
                    connectionStringName = resolved;
                }
            }

            IStorageAccount account = await provider.TryGetAccountAsync(connectionStringName, cancellationToken);

            ValidateStorageAccount(account, connectionStringName);
            return(account);
        }
Beispiel #2
0
        public static Task <IStorageAccount> GetDashboardAccountAsync(this IStorageAccountProvider provider, CancellationToken cancellationToken)
        {
            if (provider == null)
            {
                throw new ArgumentNullException("provider");
            }

            return(provider.TryGetAccountAsync(ConnectionStringNames.Dashboard, cancellationToken));
        }
Beispiel #3
0
        public static async Task <IStorageAccount> GetAccountAsync(this IStorageAccountProvider provider, string connectionStringName, CancellationToken cancellationToken)
        {
            if (provider == null)
            {
                throw new ArgumentNullException("provider");
            }

            IStorageAccount account = await provider.TryGetAccountAsync(connectionStringName, cancellationToken);

            ValidateStorageAccount(account, connectionStringName);
            return(account);
        }
        public void GetAccountAsync_WhenInvalidConfig_PropagatesParserException(string connectionStringName)
        {
            string    connectionString  = "invalid-ignore";
            Exception expectedException = new InvalidOperationException();
            IConnectionStringProvider connectionStringProvider = CreateConnectionStringProvider(connectionStringName,
                                                                                                connectionString);
            Mock <IStorageAccountParser> parserMock = new Mock <IStorageAccountParser>(MockBehavior.Strict);
            IServiceProvider             services   = CreateServices();

            parserMock.Setup(p => p.ParseAccount(connectionString, connectionStringName, services))
            .Throws(expectedException);
            IStorageAccountParser parser = parserMock.Object;

            IStorageAccountProvider provider = CreateProductUnderTest(services, connectionStringProvider, parser);

            Exception actualException = Assert.Throws <InvalidOperationException>(
                () => provider.TryGetAccountAsync(connectionStringName, CancellationToken.None).GetAwaiter().GetResult());

            Assert.Same(expectedException, actualException);
        }
        public void GetAccountAsync_WhenInvalidCredentials_PropagatesValidatorException(string connectionStringName)
        {
            string                              connectionString         = "invalid-ignore";
            IStorageAccount                     parsedAccount            = Mock.Of <IStorageAccount>();
            Exception                           expectedException        = new InvalidOperationException();
            IConnectionStringProvider           connectionStringProvider = CreateConnectionStringProvider(connectionStringName, connectionString);
            IServiceProvider                    services      = CreateServices();
            IStorageAccountParser               parser        = CreateParser(services, connectionStringName, connectionString, parsedAccount);
            Mock <IStorageCredentialsValidator> validatorMock = new Mock <IStorageCredentialsValidator>(
                MockBehavior.Strict);

            validatorMock.Setup(v => v.ValidateCredentialsAsync(parsedAccount, It.IsAny <CancellationToken>()))
            .Throws(expectedException);
            IStorageCredentialsValidator validator = validatorMock.Object;
            IStorageAccountProvider      provider  = CreateProductUnderTest(services, connectionStringProvider, parser, validator);

            Exception actualException = Assert.Throws <InvalidOperationException>(
                () => provider.TryGetAccountAsync(connectionStringName, CancellationToken.None).GetAwaiter().GetResult());

            Assert.Same(expectedException, actualException);
        }
        public void GetAccountAsync_WhenReadFromConfig_ReturnsParsedAccount(string connectionStringName)
        {
            string          connectionString = "valid-ignore";
            IStorageAccount parsedAccount    = Mock.Of <IStorageAccount>();

            Mock <IConnectionStringProvider> connectionStringProviderMock = new Mock <IConnectionStringProvider>(MockBehavior.Strict);

            connectionStringProviderMock.Setup(p => p.GetConnectionString(connectionStringName))
            .Returns(connectionString)
            .Verifiable();
            IConnectionStringProvider connectionStringProvider = connectionStringProviderMock.Object;

            Mock <IStorageAccountParser> parserMock = new Mock <IStorageAccountParser>(MockBehavior.Strict);
            IServiceProvider             services   = CreateServices();

            parserMock.Setup(p => p.ParseAccount(connectionString, connectionStringName, services))
            .Returns(parsedAccount)
            .Verifiable();
            IStorageAccountParser parser = parserMock.Object;

            Mock <IStorageCredentialsValidator> validatorMock = new Mock <IStorageCredentialsValidator>(
                MockBehavior.Strict);

            validatorMock.Setup(v => v.ValidateCredentialsAsync(parsedAccount, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(0))
            .Verifiable();
            IStorageCredentialsValidator validator = validatorMock.Object;

            IStorageAccountProvider provider = CreateProductUnderTest(services, connectionStringProvider, parser, validator);

            IStorageAccount actualAccount = provider.TryGetAccountAsync(
                connectionStringName, CancellationToken.None).GetAwaiter().GetResult();

            Assert.Same(parsedAccount, actualAccount);
            connectionStringProviderMock.Verify();
            parserMock.Verify();
            validatorMock.Verify();
        }