Organization membership preview API changes - Review user permission (#1633)

* Add organization membership preview header

* Add API endpoints to preview a collaborators permission

* Add methods to preview a collaborators permission

* Add methods to the observable repo collaborator client

* Fix convention test failure

* Use correct API endpoint when using repository ID

* Move the helper function pair so they aren't in between another pair

* Use the correct URL for the review permission API endpoint

* Add unit tests

* Add integration tests for review permission methods

* Fix spelling mistake

* Renaming enum as per review
This commit is contained in:
Henrik Andersson
2017-07-29 15:08:44 +10:00
committed by Ryan Gribble
parent db74c3b4ad
commit cda714bef6
12 changed files with 597 additions and 2 deletions
@@ -78,6 +78,29 @@ namespace Octokit.Reactive
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
IObservable<bool> IsCollaborator(long repositoryId, string user);
/// <summary>
/// Review a user's permission level in a repository
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/collaborators/#review-a-users-permission-level">API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="user">Username of the collaborator to check permission for</param>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
IObservable<CollaboratorPermission> ReviewPermission(string owner, string name, string user);
/// <summary>
/// Review a user's permission level in a repository
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/collaborators/#review-a-users-permission-level">API documentation</a> for more information.
/// </remarks>
/// <param name="repositoryId">The id of the repository</param>
/// <param name="user">Username of the collaborator to check permission for</param>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
IObservable<CollaboratorPermission> ReviewPermission(long repositoryId, string user);
/// <summary>
/// Adds a new collaborator to the repository.
/// </summary>
@@ -128,6 +128,41 @@ namespace Octokit.Reactive
return _client.IsCollaborator(repositoryId, user).ToObservable();
}
/// <summary>
/// Review a user's permission level in a repository
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/collaborators/#review-a-users-permission-level">API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="user">Username of the collaborator to check permission for</param>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
public IObservable<CollaboratorPermission> ReviewPermission(string owner, string name, string user)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNullOrEmptyString(user, "user");
return _client.ReviewPermission(owner, name, user).ToObservable();
}
/// <summary>
/// Review a user's permission level in a repository
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/collaborators/#review-a-users-permission-level">API documentation</a> for more information.
/// </remarks>
/// <param name="repositoryId">The id of the repository</param>
/// <param name="user">Username of the collaborator to check permission for</param>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
public IObservable<CollaboratorPermission> ReviewPermission(long repositoryId, string user)
{
Ensure.ArgumentNotNullOrEmptyString(user, "user");
return _client.ReviewPermission(repositoryId, user).ToObservable();
}
/// <summary>
/// Adds a new collaborator to the repository.
/// </summary>
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using Octokit;
using Octokit.Tests.Integration;
using Xunit;
@@ -260,6 +261,153 @@ public class RepositoryCollaboratorClientTests
}
}
public class TheReviewPermissionMethod
{
[IntegrationTest]
public async Task ReturnsReadPermissionForNonCollaborator()
{
var github = Helper.GetAuthenticatedClient();
var repoName = Helper.MakeNameWithTimestamp("public-repo");
using (var context = await github.CreateRepositoryContext(new NewRepository(repoName)))
{
var fixture = github.Repository.Collaborator;
var permission = await fixture.ReviewPermission(context.RepositoryOwner, context.RepositoryName, "alfhenrik-test-2");
Assert.Equal(PermissionLevel.Read, permission.Permission);
}
}
[IntegrationTest]
public async Task ReturnsReadPermissionForNonCollaboratorWithRepositoryId()
{
var github = Helper.GetAuthenticatedClient();
var repoName = Helper.MakeNameWithTimestamp("public-repo");
using (var context = await github.CreateRepositoryContext(new NewRepository(repoName)))
{
var fixture = github.Repository.Collaborator;
var permission = await fixture.ReviewPermission(context.RepositoryId, "alfhenrik-test-2");
Assert.Equal(PermissionLevel.Read, permission.Permission);
}
}
[IntegrationTest]
public async Task ReturnsWritePermissionForCollaborator()
{
var github = Helper.GetAuthenticatedClient();
var repoName = Helper.MakeNameWithTimestamp("public-repo");
using (var context = await github.CreateRepositoryContext(new NewRepository(repoName)))
{
var fixture = github.Repository.Collaborator;
// add a collaborator
await fixture.Add(context.RepositoryOwner, context.RepositoryName, "alfhenrik-test-2");
var permission = await fixture.ReviewPermission(context.RepositoryOwner, context.RepositoryName, "alfhenrik-test-2");
Assert.Equal(PermissionLevel.Write, permission.Permission);
}
}
[IntegrationTest]
public async Task ReturnsWritePermissionForCollaboratorWithRepositoryId()
{
var github = Helper.GetAuthenticatedClient();
var repoName = Helper.MakeNameWithTimestamp("public-repo");
using (var context = await github.CreateRepositoryContext(new NewRepository(repoName)))
{
var fixture = github.Repository.Collaborator;
// add a collaborator
await fixture.Add(context.RepositoryOwner, context.RepositoryName, "alfhenrik-test-2");
var permission = await fixture.ReviewPermission(context.RepositoryId, "alfhenrik-test-2");
Assert.Equal(PermissionLevel.Write, permission.Permission);
}
}
[IntegrationTest]
public async Task ReturnsAdminPermissionForOwner()
{
var github = Helper.GetAuthenticatedClient();
var repoName = Helper.MakeNameWithTimestamp("public-repo");
using (var context = await github.CreateRepositoryContext(new NewRepository(repoName)))
{
var fixture = github.Repository.Collaborator;
var permission = await fixture.ReviewPermission(context.RepositoryOwner, context.RepositoryName, context.RepositoryOwner);
Assert.Equal(PermissionLevel.Admin, permission.Permission);
}
}
[IntegrationTest]
public async Task ReturnsAdminPermissionForOwnerWithRepositoryId()
{
var github = Helper.GetAuthenticatedClient();
var repoName = Helper.MakeNameWithTimestamp("public-repo");
using (var context = await github.CreateRepositoryContext(new NewRepository(repoName)))
{
var fixture = github.Repository.Collaborator;
var permission = await fixture.ReviewPermission(context.RepositoryId, context.RepositoryOwner);
Assert.Equal(PermissionLevel.Admin, permission.Permission);
}
}
[PaidAccountTest]
public async Task ReturnsNonePermissionForPrivateRepository()
{
var github = Helper.GetAuthenticatedClient();
var userDetails = await github.User.Current();
if (userDetails.Plan.PrivateRepos == 0)
{
throw new Exception("Test cannot complete, account is on free plan");
}
var repoName = Helper.MakeNameWithTimestamp("private-repo");
using (var context = await github.CreateRepositoryContext(new NewRepository(repoName) { Private = true }))
{
var fixture = github.Repository.Collaborator;
var permission = await fixture.ReviewPermission(context.RepositoryOwner, context.RepositoryName, "alfhenrik-test-2");
Assert.Equal(PermissionLevel.None, permission.Permission);
}
}
[PaidAccountTest]
public async Task ReturnsNonePermissionForPrivateRepositoryWithRepositoryId()
{
var github = Helper.GetAuthenticatedClient();
var userDetails = await github.User.Current();
if (userDetails.Plan.PrivateRepos == 0)
{
throw new Exception("Test cannot complete, account is on free plan");
}
var repoName = Helper.MakeNameWithTimestamp("private-repo");
using (var context = await github.CreateRepositoryContext(new NewRepository(repoName) { Private = true }))
{
var fixture = github.Repository.Collaborator;
var permission = await fixture.ReviewPermission(context.RepositoryId, "alfhenrik-test-2");
Assert.Equal(PermissionLevel.None, permission.Permission);
}
}
}
public class TheDeleteMethod
{
[IntegrationTest]
@@ -1,4 +1,5 @@
using System.Reactive.Linq;
using System;
using System.Reactive.Linq;
using System.Threading.Tasks;
using Octokit;
using Octokit.Reactive;
@@ -138,4 +139,151 @@ public class ObservableRepositoryCollaboratorClientTests
}
}
}
public class TheReviewPermissionMethod
{
[IntegrationTest]
public async Task ReturnsReadPermissionForNonCollaborator()
{
var github = Helper.GetAuthenticatedClient();
var repoName = Helper.MakeNameWithTimestamp("public-repo");
using (var context = await github.CreateRepositoryContext(new NewRepository(repoName)))
{
var fixture = new ObservableRepoCollaboratorsClient(github);
var permission = await fixture.ReviewPermission(context.RepositoryOwner, context.RepositoryName, "alfhenrik-test-2");
Assert.Equal(PermissionLevel.Read, permission.Permission);
}
}
[IntegrationTest]
public async Task ReturnsReadPermissionForNonCollaboratorWithRepositoryId()
{
var github = Helper.GetAuthenticatedClient();
var repoName = Helper.MakeNameWithTimestamp("public-repo");
using (var context = await github.CreateRepositoryContext(new NewRepository(repoName)))
{
var fixture = new ObservableRepoCollaboratorsClient(github);
var permission = await fixture.ReviewPermission(context.RepositoryId, "alfhenrik-test-2");
Assert.Equal(PermissionLevel.Read, permission.Permission);
}
}
[IntegrationTest]
public async Task ReturnsWritePermissionForCollaborator()
{
var github = Helper.GetAuthenticatedClient();
var repoName = Helper.MakeNameWithTimestamp("public-repo");
using (var context = await github.CreateRepositoryContext(new NewRepository(repoName)))
{
var fixture = new ObservableRepoCollaboratorsClient(github);
// add a collaborator
await fixture.Add(context.RepositoryOwner, context.RepositoryName, "alfhenrik-test-2");
var permission = await fixture.ReviewPermission(context.RepositoryOwner, context.RepositoryName, "alfhenrik-test-2");
Assert.Equal(PermissionLevel.Write, permission.Permission);
}
}
[IntegrationTest]
public async Task ReturnsWritePermissionForCollaboratorWithRepositoryId()
{
var github = Helper.GetAuthenticatedClient();
var repoName = Helper.MakeNameWithTimestamp("public-repo");
using (var context = await github.CreateRepositoryContext(new NewRepository(repoName)))
{
var fixture = new ObservableRepoCollaboratorsClient(github);
// add a collaborator
await fixture.Add(context.RepositoryOwner, context.RepositoryName, "alfhenrik-test-2");
var permission = await fixture.ReviewPermission(context.RepositoryId, "alfhenrik-test-2");
Assert.Equal(PermissionLevel.Write, permission.Permission);
}
}
[IntegrationTest]
public async Task ReturnsAdminPermissionForOwner()
{
var github = Helper.GetAuthenticatedClient();
var repoName = Helper.MakeNameWithTimestamp("public-repo");
using (var context = await github.CreateRepositoryContext(new NewRepository(repoName)))
{
var fixture = new ObservableRepoCollaboratorsClient(github);
var permission = await fixture.ReviewPermission(context.RepositoryOwner, context.RepositoryName, context.RepositoryOwner);
Assert.Equal(PermissionLevel.Admin, permission.Permission);
}
}
[IntegrationTest]
public async Task ReturnsAdminPermissionForOwnerWithRepositoryId()
{
var github = Helper.GetAuthenticatedClient();
var repoName = Helper.MakeNameWithTimestamp("public-repo");
using (var context = await github.CreateRepositoryContext(new NewRepository(repoName)))
{
var fixture = new ObservableRepoCollaboratorsClient(github);
var permission = await fixture.ReviewPermission(context.RepositoryId, context.RepositoryOwner);
Assert.Equal(PermissionLevel.Admin, permission.Permission);
}
}
[PaidAccountTest]
public async Task ReturnsNonePermissionForPrivateRepository()
{
var github = Helper.GetAuthenticatedClient();
var userDetails = await github.User.Current();
if (userDetails.Plan.PrivateRepos == 0)
{
throw new Exception("Test cannot complete, account is on free plan");
}
var repoName = Helper.MakeNameWithTimestamp("private-repo");
using (var context = await github.CreateRepositoryContext(new NewRepository(repoName) { Private = true }))
{
var fixture = new ObservableRepoCollaboratorsClient(github);
var permission = await fixture.ReviewPermission(context.RepositoryOwner, context.RepositoryName, "alfhenrik-test-2");
Assert.Equal(PermissionLevel.None, permission.Permission);
}
}
[PaidAccountTest]
public async Task ReturnsNonePermissionForPrivateRepositoryWithRepositoryId()
{
var github = Helper.GetAuthenticatedClient();
var userDetails = await github.User.Current();
if (userDetails.Plan.PrivateRepos == 0)
{
throw new Exception("Test cannot complete, account is on free plan");
}
var repoName = Helper.MakeNameWithTimestamp("private-repo");
using (var context = await github.CreateRepositoryContext(new NewRepository(repoName) { Private = true }))
{
var fixture = new ObservableRepoCollaboratorsClient(github);
var permission = await fixture.ReviewPermission(context.RepositoryId, "alfhenrik-test-2");
Assert.Equal(PermissionLevel.None, permission.Permission);
}
}
}
}
@@ -190,6 +190,52 @@ namespace Octokit.Tests.Clients
}
}
public class TheReviewPermissionMethod
{
[Fact]
public void RequestsCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepoCollaboratorsClient(connection);
client.ReviewPermission("owner", "test", "user1");
connection.Received().Get<CollaboratorPermission>(
Arg.Is<Uri>(u => u.ToString() == "repos/owner/test/collaborators/user1/permission"),
Arg.Any<Dictionary<string, string>>(),
"application/vnd.github.korra-preview+json");
}
[Fact]
public void RequestsCorrectUrlWithRepositoryId()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepoCollaboratorsClient(connection);
client.ReviewPermission(1L, "user1");
connection.Received().Get<CollaboratorPermission>(
Arg.Is<Uri>(u => u.ToString() == "repositories/1/collaborators/user1/permission"),
Arg.Any<Dictionary<string, string>>(),
"application/vnd.github.korra-preview+json");
}
[Fact]
public async Task EnsuresNonNullArguments()
{
var client = new RepoCollaboratorsClient(Substitute.For<IApiConnection>());
await Assert.ThrowsAsync<ArgumentNullException>(() => client.ReviewPermission(null, "test", "user1"));
await Assert.ThrowsAsync<ArgumentException>(() => client.ReviewPermission("", "test", "user1"));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.ReviewPermission("owner", null, "user1"));
await Assert.ThrowsAsync<ArgumentException>(() => client.ReviewPermission("owner", "", "user1"));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.ReviewPermission("owner", "test", null));
await Assert.ThrowsAsync<ArgumentException>(() => client.ReviewPermission("owner", "test", ""));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.ReviewPermission(1L, null));
await Assert.ThrowsAsync<ArgumentException>(() => client.ReviewPermission(1L, ""));
}
}
public class TheAddMethod
{
[Fact]
@@ -253,6 +253,92 @@ namespace Octokit.Tests.Reactive
}
}
public class TheReviewPermissionMethod
{
private readonly IGitHubClient _gitHubClient;
private IObservableRepoCollaboratorsClient _client;
public TheReviewPermissionMethod()
{
_gitHubClient = Substitute.For<IGitHubClient>();
}
private void SetupWithoutNonReactiveClient()
{
_client = new ObservableRepoCollaboratorsClient(_gitHubClient);
}
private void SetupWithNonReactiveClient()
{
var collaboratorsClient = new RepoCollaboratorsClient(Substitute.For<IApiConnection>());
_gitHubClient.Repository.Collaborator.Returns(collaboratorsClient);
_client = new ObservableRepoCollaboratorsClient(_gitHubClient);
}
[Fact]
public void EnsuresNonNullArguments()
{
SetupWithNonReactiveClient();
Assert.Throws<ArgumentNullException>(() => _client.ReviewPermission(null, "test", "user1"));
Assert.Throws<ArgumentNullException>(() => _client.ReviewPermission("owner", null, "user1"));
Assert.Throws<ArgumentNullException>(() => _client.ReviewPermission("owner", "test", null));
Assert.Throws<ArgumentNullException>(() => _client.ReviewPermission(1L, null));
}
[Fact]
public void EnsuresNonEmptyArguments()
{
SetupWithNonReactiveClient();
Assert.Throws<ArgumentException>(() => _client.ReviewPermission("", "test", "user1"));
Assert.Throws<ArgumentException>(() => _client.ReviewPermission("owner", "", "user1"));
Assert.Throws<ArgumentException>(() => _client.ReviewPermission("owner", "test", ""));
Assert.Throws<ArgumentException>(() => _client.ReviewPermission(1L, ""));
}
[Fact]
public async Task EnsuresNonWhitespaceArguments()
{
SetupWithNonReactiveClient();
await AssertEx.ThrowsWhenGivenWhitespaceArgument(
async whitespace => await _client.ReviewPermission(whitespace, "repo", "user"));
await AssertEx.ThrowsWhenGivenWhitespaceArgument(
async whitespace => await _client.ReviewPermission("owner", whitespace, "user"));
await AssertEx.ThrowsWhenGivenWhitespaceArgument(
async whitespace => await _client.ReviewPermission("owner", "repo", whitespace));
await AssertEx.ThrowsWhenGivenWhitespaceArgument(
async whitespace => await _client.ReviewPermission(1L, whitespace));
}
[Fact]
public void CallsReviewPermissionOnRegularRepoCollaboratorsClient()
{
SetupWithoutNonReactiveClient();
_client.ReviewPermission("owner", "repo", "user");
_gitHubClient.Repository.Collaborator.Received(1).ReviewPermission(
Arg.Is("owner"),
Arg.Is("repo"),
Arg.Is("user"));
}
[Fact]
public void CallsReviewPermissionOnRegularRepoCollaboratorsClientWithRepositoryId()
{
SetupWithoutNonReactiveClient();
var id = 1L;
_client.ReviewPermission(id, "user");
_gitHubClient.Repository.Collaborator.Received(1).ReviewPermission(
Arg.Is(id),
Arg.Is("user"));
}
}
public class TheAddMethod
{
private readonly IGitHubClient _githubClient;
@@ -78,6 +78,29 @@ namespace Octokit
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
Task<bool> IsCollaborator(long repositoryId, string user);
/// <summary>
/// Review a user's permission level in a repository
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/collaborators/#review-a-users-permission-level">API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="user">Username of the collaborator to check permission for</param>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
Task<CollaboratorPermission> ReviewPermission(string owner, string name, string user);
/// <summary>
/// Review a user's permission level in a repository
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/collaborators/#review-a-users-permission-level">API documentation</a> for more information.
/// </remarks>
/// <param name="repositoryId">The id of the repository</param>
/// <param name="user">Username of the collaborator to check permission for</param>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
Task<CollaboratorPermission> ReviewPermission(long repositoryId, string user);
/// <summary>
/// Adds a new collaborator to the repository.
/// </summary>
@@ -136,6 +136,43 @@ namespace Octokit
}
}
/// <summary>
/// Review a user's permission level in a repository
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/collaborators/#review-a-users-permission-level">API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="user">Username of the collaborator to check permission for</param>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
public Task<CollaboratorPermission> ReviewPermission(string owner, string name, string user)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNullOrEmptyString(user, "user");
return ApiConnection
.Get<CollaboratorPermission>(ApiUrls.RepoCollaboratorPermission(owner, name, user), null, AcceptHeaders.OrganizationMembershipPreview);
}
/// <summary>
/// Review a user's permission level in a repository
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/collaborators/#review-a-users-permission-level">API documentation</a> for more information.
/// </remarks>
/// <param name="repositoryId">The id of the repository</param>
/// <param name="user">Username of the collaborator to check permission for</param>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
public Task<CollaboratorPermission> ReviewPermission(long repositoryId, string user)
{
Ensure.ArgumentNotNullOrEmptyString(user, "user");
return ApiConnection
.Get<CollaboratorPermission>(ApiUrls.RepoCollaboratorPermission(repositoryId, user), null, AcceptHeaders.OrganizationMembershipPreview);
}
/// <summary>
/// Adds a new collaborator to the repository.
/// </summary>
+3
View File
@@ -46,5 +46,8 @@ namespace Octokit
public const string PullRequestReviewsApiPreview = "application/vnd.github.black-cat-preview+json";
public const string ProjectsApiPreview = "application/vnd.github.inertia-preview+json";
public const string OrganizationMembershipPreview = "application/vnd.github.korra-preview+json";
}
}
+17
View File
@@ -1518,6 +1518,23 @@ namespace Octokit
return "repositories/{0}/collaborators/{1}".FormatUri(repositoryId, user);
}
/// <summary>
/// Returns the <see cref="Uri"/> to review the collaborators permission
/// </summary>
/// <param name="owner">The owner of the repo</param>
/// <param name="repo">The name of the repo</param>
/// <param name="user">The name of the user</param>
/// <returns>The <see cref="Uri"/> to review the collaborators permission</returns>
public static Uri RepoCollaboratorPermission(string owner, string repo, string user)
{
return "repos/{0}/{1}/collaborators/{2}/permission".FormatUri(owner, repo, user);
}
public static Uri RepoCollaboratorPermission(long repositoryId, string user)
{
return "repositories/{0}/collaborators/{1}/permission".FormatUri(repositoryId, user);
}
/// <summary>
/// returns the <see cref="Uri"/> for branches
/// </summary>
@@ -0,0 +1,13 @@
using System.Diagnostics;
namespace Octokit
{
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class CollaboratorPermission
{
public StringEnum<PermissionLevel> Permission { get; protected set; }
public User User { get; protected set; }
internal string DebuggerDisplay => $"User: {User.Id} Permission: {Permission}";
}
}
@@ -0,0 +1,16 @@
using Octokit.Internal;
namespace Octokit
{
public enum PermissionLevel
{
[Parameter(Value = "admin")]
Admin,
[Parameter(Value = "write")]
Write,
[Parameter(Value = "read")]
Read,
[Parameter(Value = "none")]
None
}
}