Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix push package API to return conlfict for existing ID #3224

Merged
merged 2 commits into from
Sep 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/NuGetGallery/Controllers/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,10 @@ await AuditingService.SaveAuditRecord(
attemptedPackage: new AuditedPackageIdentifier(
nuspec.GetId(), nuspec.GetVersion().ToNormalizedStringSafe())));

// User can not push this package
return new HttpStatusCodeWithBodyResult(HttpStatusCode.Forbidden,
Strings.ApiKeyNotAuthorized);
// User cannot push a package to an ID owned by another user.
return new HttpStatusCodeWithBodyResult(HttpStatusCode.Conflict,
String.Format(CultureInfo.CurrentCulture, Strings.PackageIdNotAvailable,
nuspec.GetId()));
}

// Check if a particular Id-Version combination already exists. We eventually need to remove this check.
Expand Down
32 changes: 32 additions & 0 deletions tests/NuGetGallery.Facts/Controllers/ApiControllerFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,38 @@ public async Task WillReturnConflictIfAPackageWithTheIdAndSameNormalizedVersionA
String.Format(Strings.PackageExistsAndCannotBeModified, "theId", "1.0.42"));
}

[Fact]
public async Task WillReturnConflictIfAPackageWithTheIdExistsBelongingToAnotherUser()
{
// Arrange
var user = new User { EmailAddress = "confirmed@email.com" };
var packageId = "theId";
var packageRegistration = new PackageRegistration();
packageRegistration.Id = packageId;
var package = new Package();
package.PackageRegistration = packageRegistration;
package.Version = "1.0.42";
packageRegistration.Packages.Add(package);

var controller = new TestableApiController();
controller.SetCurrentUser(user);
controller.MockPackageService.Setup(p => p.FindPackageRegistrationById(It.IsAny<string>()))
.Returns(packageRegistration);

var nuGetPackage = TestPackage.CreateTestPackageStream(packageId, "1.0.42");
controller.SetCurrentUser(new User());
controller.SetupPackageFromInputStream(nuGetPackage);

// Act
var result = await controller.CreatePackagePut();

// Assert
ResultAssert.IsStatusCode(
result,
HttpStatusCode.Conflict,
String.Format(Strings.PackageIdNotAvailable, packageId));
}

[Fact]
public void WillCreateAPackageFromTheNuGetPackage()
{
Expand Down