Skip to content

Commit

Permalink
Fix plugin package download in dotnet.exe.
Browse files Browse the repository at this point in the history
  • Loading branch information
dtivel committed May 24, 2017
1 parent 8ce25a5 commit 4727daa
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@ public sealed class PluginCredentialsProvider : IRequestHandler, IDisposable
/// <param name="plugin">A plugin.</param>
/// <param name="packageSource">A package source.</param>
/// <param name="proxy">A web proxy.</param>
/// <param name="credentialService">A credential service.</param>
/// <param name="credentialService">An optional credential service.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="plugin" />
/// is <c>null</c>.</exception>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="packageSource" />
/// is <c>null</c>.</exception>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="credentialService" />
/// is <c>null</c>.</exception>
public PluginCredentialsProvider(
IPlugin plugin,
PackageSource packageSource,
Expand All @@ -57,11 +55,6 @@ public PluginCredentialsProvider(
throw new ArgumentNullException(nameof(packageSource));
}

if (credentialService == null)
{
throw new ArgumentNullException(nameof(credentialService));
}

_plugin = plugin;
_packageSource = packageSource;
_proxy = proxy;
Expand Down Expand Up @@ -208,6 +201,11 @@ private async Task<NetworkCredential> GetPackageSourceCredential(
return new NetworkCredential(_packageSource.Credentials.Username, _packageSource.Credentials.Password);
}

if (_credentialService == null)
{
return null;
}

string message;
if (requestType == CredentialRequestType.Unauthorized)
{
Expand Down Expand Up @@ -237,7 +235,7 @@ private async Task<NetworkCredential> GetPackageSourceCredential(

private async Task<NetworkCredential> GetProxyCredentialAsync(CancellationToken cancellationToken)
{
if (_proxy != null)
if (_proxy != null && _credentialService != null)
{
var sourceUri = _packageSource.SourceUri;
var proxyUri = _proxy.GetProxy(sourceUri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,6 @@ public void Constructor_ThrowsForNullPackageSource()
Assert.Equal("packageSource", exception.ParamName);
}

[Fact]
public void Constructor_ThrowsForNullCredentialService()
{
var exception = Assert.Throws<ArgumentNullException>(
() => new PluginCredentialsProvider(
Mock.Of<IPlugin>(),
_packageSource,
Mock.Of<IWebProxy>(),
credentialService: null));

Assert.Equal("credentialService", exception.ParamName);
}

[Fact]
public void CancellationToken_IsNone()
{
Expand Down Expand Up @@ -335,6 +322,37 @@ await provider.HandleResponseAsync(
}
}

[Fact]
public async Task HandleResponseAsync_ReturnsNullPackageSourceCredentialsIfPackageSourceCredentialsAreInvalidAndCredentialServiceIsNull()
{
using (var provider = new PluginCredentialsProvider(
Mock.Of<IPlugin>(),
_packageSource,
Mock.Of<IWebProxy>(),
credentialService: null))
{
var request = CreateRequest(
MessageType.Request,
new GetCredentialsRequest(_packageSource.Source, HttpStatusCode.Unauthorized));
var responseHandler = new Mock<IResponseHandler>(MockBehavior.Strict);

responseHandler.Setup(x => x.SendResponseAsync(
It.Is<Message>(r => r == request),
It.Is<GetCredentialsResponse>(r => r.ResponseCode == MessageResponseCode.NotFound
&& r.Username == null && r.Password == null),
It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(0));

await provider.HandleResponseAsync(
Mock.Of<IConnection>(),
request,
responseHandler.Object,
CancellationToken.None);

responseHandler.Verify();
}
}

[Fact]
public async Task HandleResponseAsync_ReturnsNullPackageSourceCredentialsIfNoCredentials()
{
Expand Down Expand Up @@ -436,6 +454,37 @@ await provider.HandleResponseAsync(
}
}

[Fact]
public async Task HandleResponseAsync_ReturnsNullProxyCredentialsIfCredentialServiceIsNull()
{
using (var provider = new PluginCredentialsProvider(
Mock.Of<IPlugin>(),
_packageSource,
Mock.Of<IWebProxy>(),
credentialService: null))
{
var request = CreateRequest(
MessageType.Request,
new GetCredentialsRequest(_packageSource.Source, HttpStatusCode.ProxyAuthenticationRequired));
var responseHandler = new Mock<IResponseHandler>(MockBehavior.Strict);

responseHandler.Setup(x => x.SendResponseAsync(
It.Is<Message>(r => r == request),
It.Is<GetCredentialsResponse>(r => r.ResponseCode == MessageResponseCode.NotFound
&& r.Username == null && r.Password == null),
It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(0));

await provider.HandleResponseAsync(
Mock.Of<IConnection>(),
request,
responseHandler.Object,
CancellationToken.None);

responseHandler.Verify();
}
}

[Fact]
public async Task HandleResponseAsync_ReturnsNullProxyCredentialsIfNoCredentials()
{
Expand Down

0 comments on commit 4727daa

Please sign in to comment.