diff --git a/src/NuGetGallery/Controllers/ApiController.cs b/src/NuGetGallery/Controllers/ApiController.cs index ef4ffc6538..b16000ba0c 100644 --- a/src/NuGetGallery/Controllers/ApiController.cs +++ b/src/NuGetGallery/Controllers/ApiController.cs @@ -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. diff --git a/tests/NuGetGallery.Facts/Controllers/ApiControllerFacts.cs b/tests/NuGetGallery.Facts/Controllers/ApiControllerFacts.cs index b9d8ef1896..b881772fc1 100644 --- a/tests/NuGetGallery.Facts/Controllers/ApiControllerFacts.cs +++ b/tests/NuGetGallery.Facts/Controllers/ApiControllerFacts.cs @@ -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())) + .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() {