Skip to content

Commit

Permalink
Merge pull request #86 from NuGet/dev
Browse files Browse the repository at this point in the history
Merge branch 'dev' into master
  • Loading branch information
joelverhagen authored Aug 7, 2019
2 parents 97e3d99 + cfe584f commit 03597a6
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ param (
[string]$SemanticVersion = '1.0.0-zlocal',
[string]$Branch,
[string]$CommitSHA,
[string]$BuildBranch = '2d8feecabe3aeaed7f5b4d50b9be78c94faf39ec'
[string]$BuildBranch = 'cb2b9e41b18cb77ee644a51951d8c8f24cde9adf'
)

$msBuildVersion = 15;
Expand Down
32 changes: 32 additions & 0 deletions src/NuGet.Server.Core/Core/DuplicatePackageException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace NuGet.Server.Core
{
/// <summary>
/// This exception is thrown when trying to add a package in a version that already exists on the server
/// and <see cref="NuGet.Server.Core.Infrastructure.ServerPackageRepository.AllowOverrideExistingPackageOnPush"/> is set to false.
/// </summary>
public class DuplicatePackageException : Exception
{
public DuplicatePackageException()
{
}

public DuplicatePackageException(string message) : base(message)
{
}

public DuplicatePackageException(string message, Exception innerException) : base(message, innerException)
{
}

protected DuplicatePackageException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ private bool CanPackageBeAddedWithoutLocking(IPackage package, bool shouldThrow)

if (shouldThrow)
{
throw new InvalidOperationException(message);
throw new DuplicatePackageException(message);
}

return false;
Expand Down
1 change: 1 addition & 0 deletions src/NuGet.Server.Core/NuGet.Server.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<Link>HashCodeCombiner.cs</Link>
</Compile>
<Compile Include="Core\Constants.cs" />
<Compile Include="Core\DuplicatePackageException.cs" />
<Compile Include="Core\FrameworkNameExtensions.cs" />
<Compile Include="Core\NullFileSystem.cs" />
<Compile Include="Core\PackageExtensions.cs" />
Expand Down
11 changes: 9 additions & 2 deletions src/NuGet.Server.V2/Controllers/NuGetODataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,15 @@ public virtual async Task<HttpResponseMessage> UploadPackage(CancellationToken t
HttpResponseMessage retValue;
if (_authenticationService.IsAuthenticated(User, apiKey, package.Id))
{
await _serverRepository.AddPackageAsync(package, token);
retValue = Request.CreateResponse(HttpStatusCode.Created);
try
{
await _serverRepository.AddPackageAsync(package, token);
retValue = Request.CreateResponse(HttpStatusCode.Created);
}
catch (DuplicatePackageException ex)
{
retValue = CreateStringResponse(HttpStatusCode.Conflict, ex.Message);
}
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions test/NuGet.Server.Core.Tests/ServerPackageRepositoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public async Task ServerPackageRepository_DuplicateAddAfterClearObservesOverride
}
else
{
await Assert.ThrowsAsync<InvalidOperationException>(async () =>
await Assert.ThrowsAsync<DuplicatePackageException>(async () =>
await serverRepository.AddPackageAsync(CreatePackage("test", "1.2"), Token));
}
}
Expand Down Expand Up @@ -895,7 +895,7 @@ public async Task ServerPackageRepositoryAddPackageRejectsDuplicatesWithSemVer2(
await serverRepository.AddPackageAsync(CreatePackage("Foo", "1.0.0-beta.1+foo"), Token);

// Act & Assert
var actual = await Assert.ThrowsAsync<InvalidOperationException>(async () =>
var actual = await Assert.ThrowsAsync<DuplicatePackageException>(async () =>
await serverRepository.AddPackageAsync(CreatePackage("Foo", "1.0.0-beta.1+bar"), Token));
Assert.Equal(
"Package Foo 1.0.0-beta.1 already exists. The server is configured to not allow overwriting packages that already exist.",
Expand Down
27 changes: 24 additions & 3 deletions test/NuGet.Server.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -135,6 +135,27 @@ public async Task FilterOnFramework()
}
}

[Fact]
public async Task PushDuplicatePackage()
{
// Arrange
using (var tc = new TestContext(_output))
{
string apiKey = "foobar";
tc.SetApiKey(apiKey);

var packagePath = Path.Combine(tc.TemporaryDirectory, "package.nupkg");
TestData.CopyResourceToPath(TestData.PackageResource, packagePath);

// Act & Assert
// 1. Push the package.
await tc.PushPackageAsync(apiKey, packagePath);

// 2. Push the package again expecting a 409 as the Package already exists.
await tc.PushPackageAsync(apiKey, packagePath, excepectedStatusCode: HttpStatusCode.Conflict);
}
}

[Fact]
public async Task PushPackageThenReadPackages()
{
Expand Down Expand Up @@ -462,7 +483,7 @@ public MultipartContent GetFileUploadContent(params string[] paths)
return content;
}

public async Task PushPackageAsync(string apiKey, string packagePath, string pushUrl = "/nuget")
public async Task PushPackageAsync(string apiKey, string packagePath, string pushUrl = "/nuget", HttpStatusCode excepectedStatusCode = HttpStatusCode.Created)
{
using (var request = new HttpRequestMessage(HttpMethod.Put, pushUrl)
{
Expand All @@ -474,7 +495,7 @@ public async Task PushPackageAsync(string apiKey, string packagePath, string pus
})
using (var response = await Client.SendAsync(request))
{
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
Assert.Equal(excepectedStatusCode, response.StatusCode);
}
}

Expand Down

0 comments on commit 03597a6

Please sign in to comment.