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);
            }
        }
Ejemplo n.º 2
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);
            }
        }