Пример #1
0
        public void Provider_IdContainsPath()
        {
            var pluginResult = CreatePluginDiscoveryResult();
            var provider     = new SecurePluginCredentialProvider(CreateDefaultPluginManager(), pluginResult, NullLogger.Instance);

            Assert.Contains(pluginResult.PluginFile.Path, provider.Id);
        }
        public async Task GetAsync_WithValidArguments_ReturnsValidCredentials()
        {
            var expectation = new TestExpectation(
                operationClaims: new[] { OperationClaim.Authentication },
                connectionOptions: ConnectionOptions.CreateDefault(),
                pluginVersion: ProtocolConstants.CurrentVersion,
                uri: _uri,
                authenticationUsername: _username,
                authenticationPassword: _password,
                success: true);

            using (var test = new PluginManagerMock(
                       pluginFilePath: "a",
                       pluginFileState: PluginFileState.Valid,
                       expectations: expectation))
            {
                var discoveryResult = new PluginDiscoveryResult(new PluginFile("a", new Lazy <PluginFileState>(() => PluginFileState.Valid)));
                var provider        = new SecurePluginCredentialProvider(test.PluginManager, discoveryResult, canShowDialog: true, logger: NullLogger.Instance);

                IWebProxy proxy              = null;
                var       credType           = CredentialRequestType.Unauthorized;
                var       message            = "nothing";
                var       isRetry            = false;
                var       isInteractive      = false;
                var       token              = CancellationToken.None;
                var       credentialResponse = await provider.GetAsync(_uri, proxy, credType, message, isRetry, isInteractive, token);

                Assert.True(credentialResponse.Status == CredentialStatus.Success);
                Assert.NotNull(credentialResponse.Credentials);
                Assert.Equal(_username, credentialResponse.Credentials.GetCredential(_uri, authType: null).UserName);
                Assert.Equal(_password, credentialResponse.Credentials.GetCredential(_uri, authType: null).Password);
            }
        }
        public void Id_WithValidArguments_ContainsPluginFilePath()
        {
            var pluginResult = CreatePluginDiscoveryResult();
            var provider     = new SecurePluginCredentialProvider(CreateDefaultPluginManager(), pluginResult, canShowDialog: true, logger: NullLogger.Instance);

            Assert.Contains(pluginResult.PluginFile.Path, provider.Id);
        }
        public async Task GetAsync_WhenPluginManagerReturnsException_ExceptionIsPropagated()
        {
            var expectedMessage      = "a";
            var expectedException    = CreateExceptionWithCallstack("b");
            var pluginCreationResult = new PluginCreationResult(expectedMessage, expectedException);
            var result        = new Tuple <bool, PluginCreationResult>(true, pluginCreationResult);
            var pluginManager = new Mock <IPluginManager>(MockBehavior.Strict);

            pluginManager.Setup(x => x.TryGetSourceAgnosticPluginAsync(
                                    It.IsNotNull <PluginDiscoveryResult>(),
                                    It.Is <OperationClaim>(claim => claim == OperationClaim.Authentication),
                                    It.IsAny <CancellationToken>()))
            .ReturnsAsync(result);

            var pluginDiscoveryResult = new PluginDiscoveryResult(new PluginFile("c", new Lazy <PluginFileState>(() => PluginFileState.Valid)));
            var logger = new Mock <ILogger>(MockBehavior.Strict);

            logger.Setup(x => x.LogError(It.Is <string>(data => data == expectedMessage)));
            logger.Setup(x => x.LogDebug(It.Is <string>(data => data == expectedException.ToString())));

            var provider = new SecurePluginCredentialProvider(pluginManager.Object, pluginDiscoveryResult, canShowDialog: false, logger: logger.Object);

            var exception = await Assert.ThrowsAsync <PluginException>(
                () => provider.GetAsync(_uri, proxy: null, type: CredentialRequestType.Forbidden, message: null, isRetry: false, nonInteractive: true, cancellationToken: CancellationToken.None));

            Assert.Same(expectedException, exception.InnerException);

            pluginManager.Verify();
            logger.Verify();
        }
Пример #5
0
        public void TryCreate_SetsProxyCredentials()
        {
            var uri           = new Uri("https://api.nuget.org/v3/index.json");
            var authUsername  = "******";
            var authPassword  = "******";
            var proxyUsername = "******";
            var proxyPassword = "******";

            var expectation = new TestExpectation(
                serviceIndexJson: null,
                sourceUri: null,
                operationClaims: new[] { OperationClaim.Authentication },
                connectionOptions: ConnectionOptions.CreateDefault(),
                pluginVersion: ProtocolConstants.CurrentVersion,
                uri: uri,
                authenticationUsername: authUsername,
                authenticationPassword: authPassword,
                success: true,
                proxyUsername: proxyUsername,
                proxyPassword: proxyPassword
                );

            using (var test = new PluginManagerMock(
                       pluginFilePath: "a",
                       pluginFileState: PluginFileState.Valid,
                       expectations: expectation))
            {
                var discoveryResult = new PluginDiscoveryResult(new PluginFile("a", new Lazy <PluginFileState>(() => PluginFileState.Valid)));
                var provider        = new SecurePluginCredentialProvider(test.PluginManager, discoveryResult, NullLogger.Instance);
                var proxy           = new System.Net.WebProxy()
                {
                    Credentials = new NetworkCredential(proxyUsername, proxyPassword)
                };
                var credType           = CredentialRequestType.Unauthorized;
                var message            = "nothing";
                var isRetry            = false;
                var isInteractive      = false;
                var token              = CancellationToken.None;
                var credentialResponse = provider.GetAsync(uri, proxy, credType, message, isRetry, isInteractive, token).Result;

                Assert.True(credentialResponse.Status == CredentialStatus.Success);
                Assert.NotNull(credentialResponse.Credentials);
                Assert.Equal(authUsername, credentialResponse.Credentials.GetCredential(uri, null).UserName);
                Assert.Equal(authPassword, credentialResponse.Credentials.GetCredential(uri, null).Password);
            }
        }
        public void TryCreate_DoesNotCreateNonCredentialsPluginTwice()
        {
            var uri          = new Uri("https://api.nuget.org/v3/index.json");
            var authUsername = "******";
            var authPassword = "******";

            var expectation = new TestExpectation(
                serviceIndexJson: null,
                sourceUri: null,
                operationClaims: new[] { OperationClaim.DownloadPackage },
                connectionOptions: ConnectionOptions.CreateDefault(),
                pluginVersion: ProtocolConstants.CurrentVersion,
                uri: uri,
                authenticationUsername: authUsername,
                authenticationPassword: authPassword,
                success: false
                );

            using (var test = new PluginManagerMock(
                       pluginFilePath: "a",
                       pluginFileState: PluginFileState.Valid,
                       expectations: expectation))
            {
                var discoveryResult = new PluginDiscoveryResult(new PluginFile("a", PluginFileState.Valid));
                var provider        = new SecurePluginCredentialProvider(test.PluginManager, discoveryResult, NullLogger.Instance);

                System.Net.IWebProxy proxy = null;
                var credType           = CredentialRequestType.Unauthorized;
                var message            = "nothing";
                var isRetry            = false;
                var isInteractive      = false;
                var token              = CancellationToken.None;
                var credentialResponse = provider.GetAsync(uri, proxy, credType, message, isRetry, isInteractive, token).Result;

                Assert.True(credentialResponse.Status == CredentialStatus.ProviderNotApplicable);
                Assert.Null(credentialResponse.Credentials);

                var credentialResponse2 = provider.GetAsync(uri, proxy, credType, message, isRetry, isInteractive, token).Result;
                Assert.True(credentialResponse2.Status == CredentialStatus.ProviderNotApplicable);
                Assert.Null(credentialResponse2.Credentials);
            }
        }
        public async Task GetAsync_WhenCalledMultipleTimes_DoesNotCreateMultipleInstancesOfANonCredentialsPlugin()
        {
            var expectation = new TestExpectation(
                operationClaims: new[] { OperationClaim.DownloadPackage },
                connectionOptions: ConnectionOptions.CreateDefault(),
                pluginVersion: ProtocolConstants.CurrentVersion,
                uri: _uri,
                authenticationUsername: _username,
                authenticationPassword: _password,
                success: false);

            using (var test = new PluginManagerMock(
                       pluginFilePath: "a",
                       pluginFileState: PluginFileState.Valid,
                       expectations: expectation))
            {
                var discoveryResult = new PluginDiscoveryResult(new PluginFile("a", new Lazy <PluginFileState>(() => PluginFileState.Valid)));
                var provider        = new SecurePluginCredentialProvider(test.PluginManager, discoveryResult, canShowDialog: true, logger: NullLogger.Instance);

                IWebProxy proxy              = null;
                var       credType           = CredentialRequestType.Unauthorized;
                var       message            = "nothing";
                var       isRetry            = false;
                var       isInteractive      = false;
                var       token              = CancellationToken.None;
                var       credentialResponse = await provider.GetAsync(_uri, proxy, credType, message, isRetry, isInteractive, token);

                Assert.True(credentialResponse.Status == CredentialStatus.ProviderNotApplicable);
                Assert.Null(credentialResponse.Credentials);

                var credentialResponse2 = await provider.GetAsync(_uri, proxy, credType, message, isRetry, isInteractive, token);

                Assert.True(credentialResponse2.Status == CredentialStatus.ProviderNotApplicable);
                Assert.Null(credentialResponse2.Credentials);
            }
        }
Пример #8
0
        public void Type_IsICredentialProvider()
        {
            var provider = new SecurePluginCredentialProvider(CreateDefaultPluginManager(), CreatePluginDiscoveryResult(), NullLogger.Instance);

            Assert.True(provider is ICredentialProvider);
        }