Skip to content

Commit

Permalink
Merge pull request #2 from TattsGroup/enh-gpgkeysclient
Browse files Browse the repository at this point in the history
Context helper tweaks and fix integration test
  • Loading branch information
hnrkndrssn committed Jun 8, 2016
2 parents 9e594a6 + 511e3f9 commit 990ba69
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Octokit.Reactive/Clients/ObservableTeamsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public IObservable<Repository> GetAllRepositories(int id, ApiOptions options)
{
Ensure.ArgumentNotNull(options, "options");

return _connection.GetAndFlattenAllPages<Repository>(ApiUrls.TeamRepositories(id), options);
return _connection.GetAndFlattenAllPages<Repository>(ApiUrls.TeamRepositories(id), null, AcceptHeaders.OrganizationPermissionsPreview, options);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public async Task ReturnsAllCollaborators()
var collaborators = await fixture.GetAll(context.RepositoryOwner, context.RepositoryName);
Assert.NotNull(collaborators);
Assert.Equal(2, collaborators.Count);
Assert.NotNull(collaborators[0].Permissions);
Assert.NotNull(collaborators[1].Permissions);
}
}

Expand Down
32 changes: 30 additions & 2 deletions Octokit.Tests.Integration/Clients/TeamsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ public async Task GetsIsMemberFalseForNonMemberWhenAuthenticated()
}
}

public class TheGetMembersMethod
public class TheGetAllMembersMethod
{
readonly Team team;

public TheGetMembersMethod()
public TheGetAllMembersMethod()
{
var github = Helper.GetAuthenticatedClient();

Expand All @@ -121,6 +121,34 @@ public async Task GetsAllMembersWhenAuthenticated()
}
}

public class TheGetAllRepositoriesMethod
{
readonly Team _team;

public TheGetAllRepositoriesMethod()
{
var github = Helper.GetAuthenticatedClient();

_team = github.Organization.Team.GetAll(Helper.Organization).Result.First();
}

[OrganizationTest]
public async Task GetsAllRepositories()
{
var github = Helper.GetAuthenticatedClient();

using (var repositoryContext = await github.CreateRepositoryContext(Helper.Organization, new NewRepository(Helper.MakeNameWithTimestamp("teamrepo"))))
{
github.Organization.Team.AddRepository(_team.Id, Helper.Organization, repositoryContext.RepositoryName);

var repos = await github.Organization.Team.GetAllRepositories(_team.Id);

Assert.True(repos.Count > 0);
Assert.NotNull(repos[0].Permissions);
}
}
}

public class TheAddOrUpdateTeamRepositoryMethod
{
private readonly IGitHubClient _github;
Expand Down
16 changes: 16 additions & 0 deletions Octokit.Tests.Integration/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,22 @@ public static void DeleteKey(IConnection connection, int keyId)
catch { }
}

public static void DeleteGpgKey(IConnection connection, GpgKey key)
{
if (key != null)
DeleteGpgKey(connection, key.Id);
}

public static void DeleteGpgKey(IConnection connection, int keyId)
{
try
{
var client = new GitHubClient(connection);
client.User.GpgKey.Delete(keyId).Wait(TimeSpan.FromSeconds(15));
}
catch { }
}

public static string MakeNameWithTimestamp(string name)
{
return string.Concat(name, "-", DateTime.UtcNow.ToString("yyyyMMddhhmmssfff"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ internal static async Task<GpgKeyContext> CreateGpgKeyContext(this IGitHubClient

var key = await client.User.GpgKey.Create(new NewGpgKey(publicKey));

return new GpgKeyContext(key);
return new GpgKeyContext(client.Connection, key);
}
}
}
12 changes: 4 additions & 8 deletions Octokit.Tests.Integration/Helpers/GpgKeyContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ namespace Octokit.Tests.Integration.Helpers
{
public class GpgKeyContext : IDisposable
{
internal GpgKeyContext(GpgKey key)
internal GpgKeyContext(IConnection connection, GpgKey key)
{
_connection = connection;
Key = key;
GpgKeyId = key.Id;
KeyId = key.KeyId;
PublicKeyData = key.PublicKey;
}


private IConnection _connection;
internal int GpgKeyId { get; set; }
internal string KeyId { get; set; }
internal string PublicKeyData { get; set; }
Expand All @@ -27,12 +28,7 @@ public void Dispose()
{
if (Key != null)
{
var api = Helper.GetBasicAuthClient();
try
{
api.User.GpgKey.Delete(Key.Id).Wait(TimeSpan.FromSeconds(15));
}
catch { }
Helper.DeleteGpgKey(_connection, Key);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ internal static async Task<GpgKeyContext> CreateGpgKeyContext(this IObservableGi

var key = await client.User.GpgKey.Create(new NewGpgKey(publicKey));

return new GpgKeyContext(key);
return new GpgKeyContext(client.Connection, key);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public async Task ReturnsAllCollaborators()
var collaborators = await fixture.GetAll(context.RepositoryOwner, context.RepositoryName).ToList();
Assert.NotNull(collaborators);
Assert.Equal(2, collaborators.Count);
Assert.NotNull(collaborators[0].Permissions);
Assert.NotNull(collaborators[1].Permissions);
}
}

Expand Down
40 changes: 36 additions & 4 deletions Octokit.Tests.Integration/Reactive/ObservableTeamsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
using Octokit;
using Octokit.Reactive;
using Octokit.Tests.Integration;
using Octokit.Tests.Integration.Helpers;
using Xunit;

public class ObservableTeamsClientTests
{
public class TheGetMembersMethod
public class TheGetAllMembersMethod
{
readonly Team _team;

public TheGetMembersMethod()
public TheGetAllMembersMethod()
{
var github = Helper.GetAuthenticatedClient();

Expand All @@ -23,12 +24,43 @@ public TheGetMembersMethod()
public async Task GetsAllMembersWhenAuthenticated()
{
var github = Helper.GetAuthenticatedClient();
var client = new ObservableTeamsClient(github);

var observable = client.GetAllMembers(_team.Id, ApiOptions.None);
var members = await observable.ToList();

Assert.True(members.Count > 0);
Assert.True(members.Any(x => x.Login == Helper.UserName));
}
}

public class TheGetAllRepositoriesMethod
{
readonly Team _team;

public TheGetAllRepositoriesMethod()
{
var github = Helper.GetAuthenticatedClient();

_team = github.Organization.Team.GetAll(Helper.Organization).Result.First();
}

[OrganizationTest]
public async Task GetsAllRepositories()
{
var github = Helper.GetAuthenticatedClient();
var client = new ObservableTeamsClient(github);

var member = await client.GetAllMembers(_team.Id, ApiOptions.None);
using (var repositoryContext = await github.CreateRepositoryContext(Helper.Organization, new NewRepository(Helper.MakeNameWithTimestamp("teamrepo"))))
{
client.AddRepository(_team.Id, Helper.Organization, repositoryContext.RepositoryName);

var observable = client.GetAllRepositories(_team.Id, ApiOptions.None);
var repos = await observable.ToList();

Assert.Equal(Helper.UserName, member.Login);
Assert.True(repos.Count() > 0);
Assert.NotNull(repos[0].Permissions);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ObservableUserGpgKeysClientTests()
[IntegrationTest]
public async Task CanGetAllForCurrentUser()
{
using (var context = _gitHubClient.CreateGpgKeyContext())
using (var context = await _gitHubClient.CreateGpgKeyContext())
{
var observable = _gitHubClient.User.GpgKey.GetAllForCurrent();
var keys = await observable.ToList();
Expand Down
4 changes: 2 additions & 2 deletions Octokit.Tests/Clients/RepoCollaboratorsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void RequestsCorrectUrl()
var client = new RepoCollaboratorsClient(connection);

client.GetAll("owner", "test");
connection.Received().GetAll<User>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/test/collaborators"), Args.ApiOptions);
connection.Received().GetAll<User>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/test/collaborators"), null, "application/vnd.github.ironman-preview+json", Args.ApiOptions);
}

[Fact]
Expand All @@ -51,7 +51,7 @@ public void RequestsCorrectUrlWithApiOptions()
client.GetAll("owner", "test", options);

connection.Received()
.GetAll<User>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/test/collaborators"), options);
.GetAll<User>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/test/collaborators"), null, "application/vnd.github.ironman-preview+json", options);
}

[Fact]
Expand Down
4 changes: 3 additions & 1 deletion Octokit.Tests/Clients/TeamsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public void EnsuresNonEmptyLogin()
}
}

public class TheRRemoveMembershipMethod
public class TheRemoveMembershipMethod
{
[Fact]
public void RequestsTheCorrectUrl()
Expand Down Expand Up @@ -256,6 +256,8 @@ public void RequestsTheCorrectUrl()

connection.Received().GetAll<Repository>(
Arg.Is<Uri>(u => u.ToString() == "teams/1/repos"),
null,
"application/vnd.github.ironman-preview+json",
Args.ApiOptions);
}
}
Expand Down
2 changes: 1 addition & 1 deletion Octokit/Clients/RepoCollaboratorsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Task<IReadOnlyList<User>> GetAll(string owner, string repo, ApiOptions op
Ensure.ArgumentNotNullOrEmptyString(repo, "repo");
Ensure.ArgumentNotNull(options, "options");

return ApiConnection.GetAll<User>(ApiUrls.RepoCollaborators(owner, repo), options);
return ApiConnection.GetAll<User>(ApiUrls.RepoCollaborators(owner, repo), null, AcceptHeaders.OrganizationPermissionsPreview, options);
}

/// <summary>
Expand Down
17 changes: 1 addition & 16 deletions Octokit/Clients/TeamsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,22 +276,7 @@ public Task<IReadOnlyList<Repository>> GetAllRepositories(int id, ApiOptions opt

var endpoint = ApiUrls.TeamRepositories(id);

return ApiConnection.GetAll<Repository>(endpoint, options);
}

/// <summary>
/// Returns all <see cref="Repository"/>(ies) associated with the given team.
/// </summary>
/// <param name="id">The team identifier</param>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/orgs/teams/#list-team-repos">API documentation</a> for more information.
/// </remarks>
/// <returns>A list of the team's <see cref="Repository"/>(ies).</returns>
public Task<IReadOnlyList<Repository>> GetRepositories(int id)
{
var endpoint = ApiUrls.TeamRepositories(id);

return ApiConnection.GetAll<Repository>(endpoint);
return ApiConnection.GetAll<Repository>(endpoint, null, AcceptHeaders.OrganizationPermissionsPreview, options);
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions Octokit/Helpers/AcceptHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public static class AcceptHeaders

public const string RedirectsPreviewThenStableVersionJson = "application/vnd.github.quicksilver-preview+json; charset=utf-8, application/vnd.github.v3+json; charset=utf-8";

public const string OrganizationPermissionsPreview = "application/vnd.github.ironman-preview+json";

public const string LicensesApiPreview = "application/vnd.github.drax-preview+json";

public const string ProtectedBranchesApiPreview = "application/vnd.github.loki-preview+json";
Expand All @@ -22,9 +24,7 @@ public static class AcceptHeaders

public const string SquashCommitPreview = "application/vnd.github.polaris-preview+json";

public const string MigrationsApiPreview = " application/vnd.github.wyandotte-preview+json";

public const string OrganizationPermissionsPreview = "application/vnd.github.ironman-preview+json";
public const string MigrationsApiPreview = "application/vnd.github.wyandotte-preview+json";

[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Gpg")]
public const string GpgKeysPreview = "application/vnd.github.cryptographer-preview";
Expand Down
5 changes: 4 additions & 1 deletion Octokit/Models/Response/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ public class User : Account
{
public User() { }

public User(string avatarUrl, string bio, string blog, int collaborators, string company, DateTimeOffset createdAt, int diskUsage, string email, int followers, int following, bool? hireable, string htmlUrl, int totalPrivateRepos, int id, string location, string login, string name, int ownedPrivateRepos, Plan plan, int privateGists, int publicGists, int publicRepos, string url, bool siteAdmin, string ldapDistinguishedName, DateTimeOffset? suspendedAt)
public User(string avatarUrl, string bio, string blog, int collaborators, string company, DateTimeOffset createdAt, int diskUsage, string email, int followers, int following, bool? hireable, string htmlUrl, int totalPrivateRepos, int id, string location, string login, string name, int ownedPrivateRepos, Plan plan, int privateGists, int publicGists, int publicRepos, string url, RepositoryPermissions permissions, bool siteAdmin, string ldapDistinguishedName, DateTimeOffset? suspendedAt)
: base(avatarUrl, bio, blog, collaborators, company, createdAt, diskUsage, email, followers, following, hireable, htmlUrl, totalPrivateRepos, id, location, login, name, ownedPrivateRepos, plan, privateGists, publicGists, publicRepos, AccountType.User, url)
{
Permissions = permissions;
SiteAdmin = siteAdmin;
LdapDistinguishedName = ldapDistinguishedName;
SuspendedAt = suspendedAt;
}

public RepositoryPermissions Permissions { get; protected set; }

/// <summary>
/// Whether or not the user is an administrator of the site
/// </summary>
Expand Down

0 comments on commit 990ba69

Please sign in to comment.