Skip to content

Commit 7e574fc

Browse files
authored
Fix bugs - issue 6422 (#6512)
* Fix issue 6422 * Refactor in a simpler way * add unit test * Update
1 parent ff4e85c commit 7e574fc

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

src/NuGetGallery/Controllers/UsersController.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,10 +990,12 @@ private async Task<JsonResult> RemoveApiKeyCredential(User user, Credential cred
990990
return Json(Strings.CredentialNotFound);
991991
}
992992

993+
var credDescription = AuthenticationService.DescribeCredential(cred);
994+
993995
await AuthenticationService.RemoveCredential(user, cred);
994996

995997
// Notify the user of the change
996-
await MessageService.SendCredentialRemovedNoticeAsync(user, AuthenticationService.DescribeCredential(cred));
998+
await MessageService.SendCredentialRemovedNoticeAsync(user, credDescription);
997999

9981000
return Json(Strings.CredentialRemoved);
9991001
}

tests/NuGetGallery.Facts/Controllers/UsersControllerFacts.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,6 +1642,60 @@ public async Task GivenValidRequest_ItRemovesCredAndSendsNotificationToUser()
16421642
GetMock<IMessageService>().VerifyAll();
16431643
}
16441644

1645+
[Theory]
1646+
[MemberData(nameof(GivenValidRequest_ItRemovesAPIKeyWithDifferentScopesAndSendsCorrectMessageToUser_Input))]
1647+
public async Task GivenValidRequest_ItRemovesAPIKeyWithDifferentScopesAndSendsCorrectMessageToUser(ICollection<Scope> scopes, string expectedDescription)
1648+
{
1649+
// Arrange
1650+
var fakes = Get<Fakes>();
1651+
var apiKey = new CredentialBuilder().CreateApiKey(TimeSpan.FromHours(1), out string plaintextApiKey);
1652+
apiKey.Description = "theApiKey";
1653+
apiKey.Scopes = scopes;
1654+
apiKey.Expires -= TimeSpan.FromDays(1);
1655+
1656+
var user = fakes.CreateUser("test", apiKey);
1657+
var cred = user.Credentials.First();
1658+
cred.Key = CredentialKey;
1659+
1660+
var controller = GetController<UsersController>();
1661+
controller.SetCurrentUser(user);
1662+
1663+
// Act
1664+
var result = await controller.RemoveCredential(
1665+
credentialType: apiKey.Type,
1666+
credentialKey: CredentialKey);
1667+
1668+
// Assert
1669+
GetMock<IMessageService>()
1670+
.Verify(m =>
1671+
m.SendCredentialRemovedNoticeAsync(
1672+
user,
1673+
It.Is<CredentialViewModel>(c => c.Description == expectedDescription)));
1674+
}
1675+
1676+
public static IEnumerable<object[]> GivenValidRequest_ItRemovesAPIKeyWithDifferentScopesAndSendsCorrectMessageToUser_Input
1677+
{
1678+
get
1679+
{
1680+
return new[]
1681+
{
1682+
new object[]
1683+
{
1684+
new []
1685+
{
1686+
new Scope("*", NuGetScopes.All)
1687+
},
1688+
"theApiKey",
1689+
},
1690+
new object[]
1691+
{
1692+
new Scope[0]{ },
1693+
Strings.NonScopedApiKeyDescription
1694+
}
1695+
};
1696+
}
1697+
}
1698+
16451699
[Fact]
16461700
public async Task GivenValidRequest_CanDeleteMicrosoftAccountWithMultipleMicrosoftAccounts()
16471701
{

0 commit comments

Comments
 (0)