public void GetCredentialQueriesReturnsCredentialsFromSourceProviderIfAvailable()
        {
            // Arrange
            string userName = "******";
            string password = "******";

            var sourceProvider = new Mock <IPackageSourceProvider>(MockBehavior.Strict);

            sourceProvider.Setup(s => s.LoadPackageSources())
            .Returns(new[] { new PackageSource("https://nuget.org")
                             {
                                 UserName = userName, Password = password
                             } })
            .Verifiable();
            var underlyingProvider         = new Mock <ICredentialProvider>(MockBehavior.Strict);
            var settingsCredentialProvider = new SettingsCredentialProvider(underlyingProvider.Object, sourceProvider.Object);

            // Act
            var value = settingsCredentialProvider.GetCredentials(new Uri("https://nuget.org"), new Mock <IWebProxy>().Object, CredentialType.RequestCredentials, false);

            // Assert
            sourceProvider.Verify();
            Assert.IsType <NetworkCredential>(value);
            var networkCredential = (NetworkCredential)value;

            Assert.Equal(userName, networkCredential.UserName);
            Assert.Equal(password, networkCredential.Password);
        }
        public void GetCredentialQueriesUnderlyingProviderIfCredentialsAreNotAvailableInSettings()
        {
            // Arrange
            var packageSourceProvider = new Mock <IPackageSourceProvider>(MockBehavior.Strict);

            packageSourceProvider.Setup(s => s.LoadPackageSources())
            .Returns(new[] { new PackageSource("https://not-nuget.org", "Official")
                             {
                                 UserName = "******", Password = "******"
                             } })
            .Verifiable();

            var underlyingProvider = new Mock <ICredentialProvider>(MockBehavior.Strict);

            underlyingProvider.Setup(p => p.GetCredentials(It.IsAny <Uri>(), It.IsAny <IWebProxy>(), CredentialType.RequestCredentials, false))
            .Returns <ICredentials>(null).Verifiable();


            var settingsCredentialProvider = new SettingsCredentialProvider(underlyingProvider.Object, packageSourceProvider.Object);

            // Act
            var value = settingsCredentialProvider.GetCredentials(new Uri("https://nuget.org"), new Mock <IWebProxy>().Object, CredentialType.RequestCredentials, false);

            // Assert
            Assert.Null(value);
            underlyingProvider.Verify();
            packageSourceProvider.Verify();
        }
        public void GetCredentialQueriesUnderlyingProviderIfProxyCredentialsAreRequested()
        {
            // Arrange
            var underlyingProvider = new Mock<ICredentialProvider>(MockBehavior.Strict);
            underlyingProvider.Setup(p => p.GetCredentials(It.IsAny<Uri>(), It.IsAny<IWebProxy>(), CredentialType.ProxyCredentials, false))
                              .Returns<ICredentials>(null).Verifiable();

            var packageSourceProvider = new PackageSourceProvider(NullSettings.Instance);
            var settingsCredentialProvider = new SettingsCredentialProvider(underlyingProvider.Object, packageSourceProvider);

            // Act
            var value = settingsCredentialProvider.GetCredentials(new Uri("http://nuget.org"), new Mock<IWebProxy>().Object, CredentialType.ProxyCredentials, false);

            // Assert
            Assert.Null(value);
            underlyingProvider.Verify();
        }
        public void GetCredentialQueriesUnderlyingProviderIfProxyCredentialsAreRequested()
        {
            // Arrange
            var underlyingProvider = new Mock <ICredentialProvider>(MockBehavior.Strict);

            underlyingProvider.Setup(p => p.GetCredentials(It.IsAny <Uri>(), It.IsAny <IWebProxy>(), CredentialType.ProxyCredentials, false))
            .Returns <ICredentials>(null).Verifiable();

            var packageSourceProvider      = new PackageSourceProvider(NullSettings.Instance, null, null, null, null);
            var settingsCredentialProvider = new SettingsCredentialProvider(underlyingProvider.Object, packageSourceProvider);

            // Act
            var value = settingsCredentialProvider.GetCredentials(new Uri("http://nuget.org"), new Mock <IWebProxy>().Object, CredentialType.ProxyCredentials, false);

            // Assert
            Assert.Null(value);
            underlyingProvider.Verify();
        }
        public void GetCredentialQueriesReturnsCredentialsFromSourceProviderIfAvailable()
        {
            // Arrange
            string userName = "******";
            string password = "******";

            var sourceProvider = new Mock<IPackageSourceProvider>(MockBehavior.Strict);
            sourceProvider.Setup(s => s.LoadPackageSources())
                          .Returns(new[] { new PackageSource("https://nuget.org") { UserName = userName, Password = password } })
                          .Verifiable();
            var underlyingProvider = new Mock<ICredentialProvider>(MockBehavior.Strict);
            var settingsCredentialProvider = new SettingsCredentialProvider(underlyingProvider.Object, sourceProvider.Object);

            // Act
            var value = settingsCredentialProvider.GetCredentials(new Uri("https://nuget.org"), new Mock<IWebProxy>().Object, CredentialType.RequestCredentials, false);

            // Assert
            sourceProvider.Verify();
            Assert.IsType<NetworkCredential>(value);
            var networkCredential = (NetworkCredential)value;
            Assert.Equal(userName, networkCredential.UserName);
            Assert.Equal(password, networkCredential.Password);
        }
        public void GetCredentialQueriesUnderlyingProviderIfCredentialsAreNotAvailableInSettings()
        {
            // Arrange
            var packageSourceProvider = new Mock<IPackageSourceProvider>(MockBehavior.Strict);
            packageSourceProvider.Setup(s => s.LoadPackageSources())
                                 .Returns(new[] { new PackageSource("https://not-nuget.org", "Official") { UserName = "******", Password = "******" } })
                                 .Verifiable();

            var underlyingProvider = new Mock<ICredentialProvider>(MockBehavior.Strict);
            underlyingProvider.Setup(p => p.GetCredentials(It.IsAny<Uri>(), It.IsAny<IWebProxy>(), CredentialType.RequestCredentials, false))
                              .Returns<ICredentials>(null).Verifiable();


            var settingsCredentialProvider = new SettingsCredentialProvider(underlyingProvider.Object, packageSourceProvider.Object);

            // Act
            var value = settingsCredentialProvider.GetCredentials(new Uri("https://nuget.org"), new Mock<IWebProxy>().Object, CredentialType.RequestCredentials, false);

            // Assert
            Assert.Null(value);
            underlyingProvider.Verify();
            packageSourceProvider.Verify();
        }