Skip to content

Commit

Permalink
Organization membership preview API changes - Review user permission (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
hnrkndrssn authored and ryangribble committed Jul 29, 2017
1 parent db74c3b commit cda714b
Show file tree
Hide file tree
Showing 12 changed files with 597 additions and 2 deletions.
23 changes: 23 additions & 0 deletions Octokit.Reactive/Clients/IObservableRepoCollaboratorsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,29 @@ public interface IObservableRepoCollaboratorsClient
/// <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>
Expand Down
35 changes: 35 additions & 0 deletions Octokit.Reactive/Clients/ObservableRepoCollaboratorsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,41 @@ public IObservable<bool> IsCollaborator(long repositoryId, string user)
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>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using Octokit;
using Octokit.Tests.Integration;
using Xunit;
Expand Down Expand Up @@ -260,6 +261,153 @@ public async Task ReturnsTrueIfUserIsCollaboratorWithRepositoryId()
}
}

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]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reactive.Linq;
using System;
using System.Reactive.Linq;
using System.Threading.Tasks;
using Octokit;
using Octokit.Reactive;
Expand Down Expand Up @@ -138,4 +139,151 @@ public async Task ReturnsTrueIfUserIsCollaborator()
}
}
}
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);
}
}
}
}
Loading

0 comments on commit cda714b

Please sign in to comment.