Skip to content

Commit

Permalink
Error message for removal of last org admin (#5490)
Browse files Browse the repository at this point in the history
chenriksson authored Feb 17, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent fb8f971 commit 3eb84de
Showing 4 changed files with 28 additions and 11 deletions.
6 changes: 2 additions & 4 deletions src/NuGetGallery/Services/UserService.cs
Original file line number Diff line number Diff line change
@@ -111,8 +111,7 @@ public async Task<Membership> UpdateMemberAsync(Organization organization, strin
// block removal of last admin
if (membership.IsAdmin && organization.Administrators.Count() == 1)
{
throw new EntityException(string.Format(CultureInfo.CurrentCulture,
Strings.UpdateOrDeleteMember_CannotRemoveLastAdmin, memberName));
throw new EntityException(Strings.UpdateMember_CannotRemoveLastAdmin);
}

membership.IsAdmin = isAdmin;
@@ -136,8 +135,7 @@ public async Task DeleteMemberAsync(Organization organization, string memberName
// block removal of last admin
if (membership.IsAdmin && organization.Administrators.Count() == 1)
{
throw new EntityException(string.Format(CultureInfo.CurrentCulture,
Strings.UpdateOrDeleteMember_CannotRemoveLastAdmin, memberName));
throw new EntityException(Strings.DeleteMember_CannotRemoveLastAdmin);
}

organization.Members.Remove(membership);
15 changes: 12 additions & 3 deletions src/NuGetGallery/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions src/NuGetGallery/Strings.resx
Original file line number Diff line number Diff line change
@@ -786,8 +786,8 @@ If you wish to update the linked Microsoft account you can do so from the accoun
<data name="DeleteMember_CannotDeleteLastAdmin" xml:space="preserve">
<value>Organization member '{0}' is the last admin and cannot be deleted.</value>
</data>
<data name="UpdateOrDeleteMember_CannotRemoveLastAdmin" xml:space="preserve">
<value>Organization member '{0}' is the last admin and cannot be changed.</value>
<data name="UpdateMember_CannotRemoveLastAdmin" xml:space="preserve">
<value>You can't change your role to collaborator. In order to change, another collaborator must be assigned as an administrator.</value>
</data>
<data name="UpdateOrDeleteMember_MemberNotFound" xml:space="preserve">
<value>Organization member '{0}' was not found.</value>
@@ -816,4 +816,7 @@ If you wish to update the linked Microsoft account you can do so from the accoun
<data name="TransformAccount_AdminAccountDoesNotHaveTenant" xml:space="preserve">
<value>Administrator account '{0}' is not linked to an AAD credential with an organization tenant.</value>
</data>
<data name="DeleteMember_CannotRemoveLastAdmin" xml:space="preserve">
<value>You can't leave the organization. In order to leave the organization, another collaborator must be assigned as an administrator.</value>
</data>
</root>
11 changes: 9 additions & 2 deletions tests/NuGetGallery.Facts/Services/UserServiceFacts.cs
Original file line number Diff line number Diff line change
@@ -146,12 +146,16 @@ public async Task WhenLastAdmin_ThrowsEntityException()
var service = new TestableUserService();

// Act & Assert
await Assert.ThrowsAsync<EntityException>(async () =>
var exception = await Assert.ThrowsAsync<EntityException>(async () =>
{
await service.DeleteMemberAsync(fakes.Organization, fakes.OrganizationAdmin.Username);
});

service.MockEntitiesContext.Verify(c => c.SaveChangesAsync(), Times.Never);

Assert.Equal(
Strings.DeleteMember_CannotRemoveLastAdmin,
exception.Message);
}

[Fact]
@@ -227,12 +231,15 @@ public async Task WhenRemovingLastAdmin_ThrowsEntityException()
var service = new TestableUserService();

// Act & Assert
await Assert.ThrowsAsync<EntityException>(async () =>
var exception = await Assert.ThrowsAsync<EntityException>(async () =>
{
await service.UpdateMemberAsync(fakes.Organization, fakes.OrganizationAdmin.Username, false);
});

service.MockEntitiesContext.Verify(c => c.SaveChangesAsync(), Times.Never);
Assert.Equal(
Strings.UpdateMember_CannotRemoveLastAdmin,
exception.Message);
}

[Fact]

0 comments on commit 3eb84de

Please sign in to comment.