Example #1
0
        internal PluginManagerMock(
            string pluginFilePath,
            PluginFileState pluginFileState,
            TestExpectation expectations)
        {
            _expectations = expectations;

            _reader = new Mock <IEnvironmentVariableReader>(MockBehavior.Strict);
            EnsureAllEnvironmentVariablesAreCalled(pluginFilePath);

            _pluginDiscoverer = new Mock <IPluginDiscoverer>(MockBehavior.Strict);
            EnsureDiscovererIsCalled(pluginFilePath, pluginFileState);

            _connection = new Mock <IConnection>(MockBehavior.Strict);
            EnsureBasicPluginSetupCalls();

            _plugin = new Mock <IPlugin>(MockBehavior.Strict);
            EnsurePluginSetupCalls();

            _factory = new Mock <IPluginFactory>(MockBehavior.Strict);
            EnsureFactorySetupCalls(pluginFilePath);

            // Setup connection
            _connection.SetupGet(x => x.Options)
            .Returns(expectations.ClientConnectionOptions);

            _connection.SetupGet(x => x.ProtocolVersion)
            .Returns(expectations.PluginVersion);

            // Setup expectations
            _connection.Setup(x => x.SendRequestAndReceiveResponseAsync <GetOperationClaimsRequest, GetOperationClaimsResponse>(
                                  It.Is <MessageMethod>(m => m == MessageMethod.GetOperationClaims),
                                  It.Is <GetOperationClaimsRequest>(
                                      g => g.PackageSourceRepository == expectations.OperationClaimsSourceRepository),
                                  It.IsAny <CancellationToken>()))
            .ReturnsAsync(new GetOperationClaimsResponse(expectations.OperationClaims.ToArray()));

            if (expectations.ProxyUsername != null && expectations.ProxyPassword != null)
            {
                _connection.Setup(x => x.SendRequestAndReceiveResponseAsync <SetCredentialsRequest, SetCredentialsResponse>(
                                      It.Is <MessageMethod>(m => m == MessageMethod.SetCredentials),
                                      It.Is <SetCredentialsRequest>(e => e.PackageSourceRepository.Equals(expectations.Uri.AbsolutePath) && e.Password == null && e.Username == null && e.ProxyPassword.Equals(expectations.ProxyPassword) && e.ProxyUsername.Equals(expectations.ProxyUsername)),
                                      It.IsAny <CancellationToken>()))
                .ReturnsAsync(new SetCredentialsResponse(MessageResponseCode.Success));
            }

            if (_expectations.Success)
            {
                _connection.Setup(x => x.SendRequestAndReceiveResponseAsync <GetAuthenticationCredentialsRequest, GetAuthenticationCredentialsResponse>(
                                      It.Is <MessageMethod>(m => m == MessageMethod.GetAuthenticationCredentials),
                                      It.Is <GetAuthenticationCredentialsRequest>(e => e.Uri.Equals(expectations.Uri)),
                                      It.IsAny <CancellationToken>()))
                .ReturnsAsync(new GetAuthenticationCredentialsResponse(expectations.AuthenticationUsername, expectations.AuthenticationPassword, null, null, MessageResponseCode.Success));
            }

            PluginManager = new PluginManager(
                _reader.Object,
                new Lazy <IPluginDiscoverer>(() => _pluginDiscoverer.Object),
                (TimeSpan idleTimeout) => _factory.Object);
        }
        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);
            }
        }
Example #3
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);
            }
        }