Skip to content

Commit 4a46fa0

Browse files
author
Scott Bommarito
authored
Properly return a 400 JSON result instead of 500 when trying to remove the last owner of a package (#6715)
1 parent e42da10 commit 4a46fa0

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

src/NuGetGallery/Controllers/JsonApiController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public async Task<JsonResult> RemovePackageOwner(string id, string username)
206206
{
207207
if (model.Package.Owners.Count == 1 && model.User == model.Package.Owners.Single())
208208
{
209-
throw new InvalidOperationException("You can't remove the only owner from a package.");
209+
return Json(new { success = false, message = "You can't remove the only owner from a package." }, JsonRequestBehavior.AllowGet);
210210
}
211211

212212
await _packageOwnershipManagementService.RemovePackageOwnerAsync(model.Package, model.CurrentUser, model.User, commitAsTransaction: true);

tests/NuGetGallery.Facts/Controllers/JsonApiControllerFacts.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,54 @@ public async Task RemovesPackageOwnerRequest(Func<Fakes, User> getCurrentUser, F
588588
false));
589589
}
590590

591+
[Theory]
592+
[MemberData(nameof(AllCanManagePackageOwnersPairedWithCanBeRemoved_Data))]
593+
public async Task FailsIfAttemptingToRemoveLastOwner(Func<Fakes, User> getCurrentUser, Func<Fakes, User> getUserToRemove)
594+
{
595+
// Arrange
596+
var fakes = Get<Fakes>();
597+
var currentUser = getCurrentUser(fakes);
598+
var userToRemove = getUserToRemove(fakes);
599+
var package = fakes.Package;
600+
var controller = GetController<JsonApiController>();
601+
controller.SetCurrentUser(currentUser);
602+
603+
foreach (var owner in package.Owners.Where(o => !o.MatchesUser(userToRemove)).ToList())
604+
{
605+
package.Owners.Remove(owner);
606+
}
607+
608+
var packageOwnershipManagementService = GetMock<IPackageOwnershipManagementService>();
609+
610+
packageOwnershipManagementService
611+
.Setup(x => x.GetPackageOwnershipRequests(package, null, userToRemove))
612+
.Returns(Enumerable.Empty<PackageOwnerRequest>());
613+
614+
// Act
615+
var result = await controller.RemovePackageOwner(package.Id, userToRemove.Username);
616+
dynamic data = result.Data;
617+
618+
// Assert
619+
Assert.False(data.success);
620+
621+
packageOwnershipManagementService
622+
.Verify(
623+
x => x.RemovePackageOwnerAsync(package, currentUser, userToRemove, It.IsAny<bool>()),
624+
Times.Never());
625+
626+
GetMock<IMessageService>()
627+
.Verify(
628+
x => x.SendMessageAsync(
629+
It.Is<PackageOwnerRemovedMessage>(
630+
msg =>
631+
msg.FromUser == currentUser
632+
&& msg.ToUser == userToRemove
633+
&& msg.PackageRegistration == package),
634+
false,
635+
false),
636+
Times.Never());
637+
}
638+
591639
[Theory]
592640
[MemberData(nameof(AllCanManagePackageOwnersPairedWithCanBeRemoved_Data))]
593641
public async Task RemovesExistingOwner(Func<Fakes, User> getCurrentUser, Func<Fakes, User> getUserToRemove)

0 commit comments

Comments
 (0)