Skip to content

Commit

Permalink
Permissions tests for API Key list and create actions (#5153)
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Bommarito committed Jan 2, 2018
1 parent 67726f0 commit 47072cb
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 4 deletions.
126 changes: 122 additions & 4 deletions tests/NuGetGallery.Facts/Controllers/UsersControllerFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,95 @@ public async Task DoesntSendAccountChangedEmailsIfConfirmationTokenDoesntMatch()
}
}

public class TheApiKeysAction
: TestContainer
{
public static IEnumerable<object[]> CurrentUserIsInPackageOwnersWithPushNew_Data
{
get
{
foreach (var currentUser in
new[]
{
TestUtility.FakeUser,
TestUtility.FakeAdminUser,
TestUtility.FakeOrganizationAdmin,
TestUtility.FakeOrganizationCollaborator
})
{
yield return MemberDataHelper.AsData(currentUser);
}
}
}

[Theory]
[MemberData(nameof(CurrentUserIsInPackageOwnersWithPushNew_Data))]
public void CurrentUserIsFirstInPackageOwnersWithPushNew(User currentUser)
{
var model = GetModelForApiKeys(currentUser);

var firstPackageOwner = model.PackageOwners.First();
Assert.True(firstPackageOwner.Owner == currentUser.Username);
Assert.True(firstPackageOwner.CanPushNew);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void OrganizationIsInPackageOwnersIfMember(bool isAdmin)
{
var currentUser = isAdmin ? TestUtility.FakeOrganizationAdmin : TestUtility.FakeOrganizationCollaborator;
var organization = TestUtility.FakeOrganization;

var model = GetModelForApiKeys(currentUser);

Assert.Equal(1, model.PackageOwners.Count(o => o.Owner == organization.Username && o.CanPushNew == isAdmin));
}

public static IEnumerable<object[]> OrganizationIsNotInPackageOwnersIfNotMember_Data
{
get
{
foreach (var currentUser in
new[]
{
TestUtility.FakeUser,
TestUtility.FakeAdminUser
})
{
yield return MemberDataHelper.AsData(currentUser);
}
}
}

[Theory]
[MemberData(nameof(OrganizationIsNotInPackageOwnersIfNotMember_Data))]
public void OrganizationIsNotInPackageOwnersIfNotMember(User currentUser)
{
var organization = TestUtility.FakeOrganization;

var model = GetModelForApiKeys(currentUser);

Assert.Equal(0, model.PackageOwners.Count(o => o.Owner == organization.Username));
}

private ApiKeyListViewModel GetModelForApiKeys(User currentUser)
{
var controller = GetController<UsersController>();
controller.SetCurrentUser(currentUser);

// Act
var result = controller.ApiKeys();

// Assert
Assert.IsType<ViewResult>(result);
var viewResult = result as ViewResult;

Assert.IsType<ApiKeyListViewModel>(viewResult.Model);
return viewResult.Model as ApiKeyListViewModel;
}
}

public class TheGenerateApiKeyAction : TestContainer
{
[InlineData(null)]
Expand All @@ -647,13 +736,42 @@ public async Task WhenEmptyDescriptionProvidedRedirectsToAccountPageWithError(st
Assert.True(string.Compare((string)result.Data, Strings.ApiKeyDescriptionRequired) == 0);
}

[Fact]
public async Task WhenScopeOwnerDoesNotMatch_ReturnsBadRequest()
public static IEnumerable<object[]> WhenScopeOwnerDoesNotMatch_ReturnsBadRequest_Data
{
get
{
foreach (var getCurrentUser in
new Func<Fakes, User>[]
{
(fakes) => fakes.User,
(fakes) => fakes.Admin
})
{
yield return new object[]
{
getCurrentUser
};
}
}
}

[Theory]
[MemberData(nameof(WhenScopeOwnerDoesNotMatch_ReturnsBadRequest_Data))]
public Task WhenScopeOwnerDoesNotMatch_ReturnsBadRequest(Func<Fakes, User> getCurrentUser)
{
// Arrange
var fakes = new Fakes();
var user = fakes.User;
var otherUser = fakes.ShaUser;
var currentUser = getCurrentUser(fakes);
var userInOwnerScope = fakes.ShaUser;

return WhenScopeOwnerDoesNotMatch_ReturnsBadRequest(currentUser, userInOwnerScope);
}

private async Task WhenScopeOwnerDoesNotMatch_ReturnsBadRequest(User currentUser, User userInOwnerScope)
{
// Arrange
var user = currentUser;
var otherUser = userInOwnerScope;
GetMock<IUserService>()
.Setup(u => u.FindByUsername(otherUser.Username))
.Returns(otherUser);
Expand Down
13 changes: 13 additions & 0 deletions tests/NuGetGallery.Facts/Framework/MemberDataHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace NuGetGallery.Framework
{
public static class MemberDataHelper
{
public static object[] AsData(params object[] data)
{
return data;
}
}
}
1 change: 1 addition & 0 deletions tests/NuGetGallery.Facts/NuGetGallery.Facts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@
<Compile Include="Authentication\AuthenticationServiceFacts.cs" />
<Compile Include="Authentication\TestCredentialHelper.cs" />
<Compile Include="Controllers\SupportControllerFacts.cs" />
<Compile Include="Framework\MemberDataHelper.cs" />
<Compile Include="Infrastructure\Authentication\ApiKeyV4Facts.cs" />
<Compile Include="Infrastructure\Authentication\V3HasherTests.cs" />
<Compile Include="AutocompleteServicePackageIdsQueryFacts.cs" />
Expand Down

0 comments on commit 47072cb

Please sign in to comment.