Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Organization membership preview API changes - Review user permission #1633

Merged
merged 12 commits into from
Jul 29, 2017
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