Skip to content

Commit

Permalink
Merge pull request #1099 from TattsGroup/enterprise-ldap-api
Browse files Browse the repository at this point in the history
Implement GitHub Enterprise LDAP API
  • Loading branch information
shiftkey committed Feb 22, 2016
2 parents 42d8473 + 6a61ec7 commit 307b1b8
Show file tree
Hide file tree
Showing 36 changed files with 966 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ public interface IObservableEnterpriseClient
///</remarks>
IObservableEnterpriseAdminStatsClient AdminStats { get; }

/// <summary>
/// A client for GitHub's Enterprise LDAP API
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/enterprise/ldap/">Enterprise LDAP API documentation</a> for more information.
///</remarks>
IObservableEnterpriseLdapClient Ldap { get; }

/// <summary>
/// A client for GitHub's Enterprise License API
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Reactive;
using System.Reactive.Threading.Tasks;

namespace Octokit.Reactive
{
/// <summary>
/// A client for GitHub's Enterprise LDAP API
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/enterprise/ldap/">Enterprise LDAP API documentation</a> for more information.
///</remarks>
public interface IObservableEnterpriseLdapClient
{
/// <summary>
/// Update the LDAP mapping for a user on a GitHub Enterprise appliance (must be Site Admin user).
/// </summary>
/// <remarks>
/// https://developer.github.com/v3/enterprise/ldap/#update-ldap-mapping-for-a-user
/// </remarks>
/// <param name="userName">The username to update LDAP mapping</param>
/// <param name="newLdapMapping">The <see cref="NewLdapMapping"/></param>
/// <returns>The <see cref="User"/> object.</returns>
IObservable<User> UpdateUserMapping(string userName, NewLdapMapping newLdapMapping);

/// <summary>
/// Queue an LDAP Sync job for a user on a GitHub Enterprise appliance (must be Site Admin user).
/// </summary>
/// <remarks>
/// https://developer.github.com/v3/enterprise/ldap/#sync-ldap-mapping-for-a-user
/// </remarks>
/// <param name="userName">The username to sync LDAP mapping</param>
/// <returns>The <see cref="LdapSyncResponse"/> of the queue request.</returns>
IObservable<LdapSyncResponse> QueueSyncUserMapping(string userName);

/// <summary>
/// Update the LDAP mapping for a team on a GitHub Enterprise appliance (must be Site Admin user).
/// </summary>
/// <remarks>
/// https://developer.github.com/v3/enterprise/ldap/#update-ldap-mapping-for-a-team
/// </remarks>
/// <param name="teamId">The teamId to update LDAP mapping</param>
/// <param name="newLdapMapping">The <see cref="NewLdapMapping"/></param>
/// <returns>The <see cref="Team"/> object.</returns>
IObservable<Team> UpdateTeamMapping(int teamId, NewLdapMapping newLdapMapping);

/// <summary>
/// Queue an LDAP Sync job for a team on a GitHub Enterprise appliance (must be Site Admin user).
/// </summary>
/// <remarks>
/// https://developer.github.com/v3/enterprise/ldap/#sync-ldap-mapping-for-a-team
/// </remarks>
/// <param name="teamId">The teamId to update LDAP mapping</param>
/// <returns>The <see cref="LdapSyncResponse"/> of the queue request.</returns>
IObservable<LdapSyncResponse> QueueSyncTeamMapping(int teamId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public ObservableEnterpriseClient(IGitHubClient client)
Ensure.ArgumentNotNull(client, "client");

AdminStats = new ObservableEnterpriseAdminStatsClient(client);
Ldap = new ObservableEnterpriseLdapClient(client);
License = new ObservableEnterpriseLicenseClient(client);
Organization = new ObservableEnterpriseOrganizationClient(client);
SearchIndexing = new ObservableEnterpriseSearchIndexingClient(client);
Expand All @@ -26,6 +27,14 @@ public ObservableEnterpriseClient(IGitHubClient client)
///</remarks>
public IObservableEnterpriseAdminStatsClient AdminStats { get; private set; }

/// <summary>
/// A client for GitHub's Enterprise LDAP API
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/enterprise/ldap/">Enterprise LDAP API documentation</a> for more information.
///</remarks>
public IObservableEnterpriseLdapClient Ldap { get; private set; }

/// <summary>
/// A client for GitHub's Enterprise License API
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Reactive;
using System.Reactive.Threading.Tasks;
using Octokit;


namespace Octokit.Reactive
{
/// <summary>
/// A client for GitHub's Enterprise LDAP API
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/enterprise/ldap/">Enterprise LDAP API documentation</a> for more information.
///</remarks>
public class ObservableEnterpriseLdapClient : IObservableEnterpriseLdapClient
{
readonly IEnterpriseLdapClient _client;

public ObservableEnterpriseLdapClient(IGitHubClient client)
{
Ensure.ArgumentNotNull(client, "client");

_client = client.Enterprise.Ldap;
}

/// <summary>
/// Update the LDAP mapping for a user on a GitHub Enterprise appliance (must be Site Admin user).
/// </summary>
/// <remarks>
/// https://developer.github.com/v3/enterprise/ldap/#update-ldap-mapping-for-a-user
/// </remarks>
/// <param name="userName">The username to update LDAP mapping</param>
/// <param name="newLdapMapping">The <see cref="NewLdapMapping"/></param>
/// <returns>The <see cref="User"/> object.</returns>
public IObservable<User> UpdateUserMapping(string userName, NewLdapMapping newLdapMapping)
{
return _client.UpdateUserMapping(userName, newLdapMapping).ToObservable();
}

/// <summary>
/// Queue an LDAP Sync job for a user on a GitHub Enterprise appliance (must be Site Admin user).
/// </summary>
/// <remarks>
/// https://developer.github.com/v3/enterprise/ldap/#sync-ldap-mapping-for-a-user
/// </remarks>
/// <param name="userName">The username to sync LDAP mapping</param>
/// <returns>The <see cref="LdapSyncResponse"/> of the queue request.</returns>
public IObservable<LdapSyncResponse> QueueSyncUserMapping(string userName)
{
return _client.QueueSyncUserMapping(userName).ToObservable();
}

/// <summary>
/// Update the LDAP mapping for a team on a GitHub Enterprise appliance (must be Site Admin user).
/// </summary>
/// <remarks>
/// https://developer.github.com/v3/enterprise/ldap/#update-ldap-mapping-for-a-team
/// </remarks>
/// <param name="teamId">The teamId to update LDAP mapping</param>
/// <param name="newLdapMapping">The <see cref="NewLdapMapping"/></param>
/// <returns>The <see cref="Team"/> object.</returns>
public IObservable<Team> UpdateTeamMapping(int teamId, NewLdapMapping newLdapMapping)
{
return _client.UpdateTeamMapping(teamId, newLdapMapping).ToObservable();
}

/// <summary>
/// Queue an LDAP Sync job for a team on a GitHub Enterprise appliance (must be Site Admin user).
/// </summary>
/// <remarks>
/// https://developer.github.com/v3/enterprise/ldap/#sync-ldap-mapping-for-a-team
/// </remarks>
/// <param name="teamId">The teamId to update LDAP mapping</param>
/// <returns>The <see cref="LdapSyncResponse"/> of the queue request.</returns>
public IObservable<LdapSyncResponse> QueueSyncTeamMapping(int teamId)
{
return _client.QueueSyncTeamMapping(teamId).ToObservable();
}
}
}
2 changes: 2 additions & 0 deletions Octokit.Reactive/Octokit.Reactive-Mono.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@
<Compile Include="Clients\ObservableUserAdministrationClient.cs" />
<Compile Include="Clients\Enterprise\IObservableEnterpriseSearchIndexingClient.cs" />
<Compile Include="Clients\Enterprise\ObservableEnterpriseSearchIndexingClient.cs" />
<Compile Include="Clients\Enterprise\IObservableEnterpriseLdapClient.cs" />
<Compile Include="Clients\Enterprise\ObservableEnterpriseLdapClient.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions Octokit.Reactive/Octokit.Reactive-MonoAndroid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@
<Compile Include="Clients\ObservableUserAdministrationClient.cs" />
<Compile Include="Clients\Enterprise\IObservableEnterpriseSearchIndexingClient.cs" />
<Compile Include="Clients\Enterprise\ObservableEnterpriseSearchIndexingClient.cs" />
<Compile Include="Clients\Enterprise\IObservableEnterpriseLdapClient.cs" />
<Compile Include="Clients\Enterprise\ObservableEnterpriseLdapClient.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions Octokit.Reactive/Octokit.Reactive-Monotouch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@
<Compile Include="Clients\ObservableUserAdministrationClient.cs" />
<Compile Include="Clients\Enterprise\IObservableEnterpriseSearchIndexingClient.cs" />
<Compile Include="Clients\Enterprise\ObservableEnterpriseSearchIndexingClient.cs" />
<Compile Include="Clients\Enterprise\IObservableEnterpriseLdapClient.cs" />
<Compile Include="Clients\Enterprise\ObservableEnterpriseLdapClient.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions Octokit.Reactive/Octokit.Reactive.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,15 @@
<Compile Include="..\SolutionInfo.cs">
<Link>Properties\SolutionInfo.cs</Link>
</Compile>
<Compile Include="Clients\Enterprise\IObservableEnterpriseLdapClient.cs" />
<Compile Include="Clients\Enterprise\IObservableEnterpriseSearchIndexingClient.cs" />
<Compile Include="Clients\Enterprise\IObservableEnterpriseOrganizationClient.cs" />
<Compile Include="Clients\Enterprise\IObservableEnterpriseLicenseClient.cs" />
<Compile Include="Clients\Enterprise\ObservableEnterpriseAdminStatsClient.cs" />
<Compile Include="Clients\Enterprise\IObservableEnterpriseAdminStatsClient.cs" />
<Compile Include="Clients\Enterprise\IObservableEnterpriseClient.cs" />
<Compile Include="Clients\Enterprise\ObservableEnterpriseClient.cs" />
<Compile Include="Clients\Enterprise\ObservableEnterpriseLdapClient.cs" />
<Compile Include="Clients\Enterprise\ObservableEnterpriseSearchIndexingClient.cs" />
<Compile Include="Clients\Enterprise\ObservableEnterpriseOrganizationClient.cs" />
<Compile Include="Clients\Enterprise\ObservableEnterpriseLicenseClient.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Octokit;
using Octokit.Tests.Integration;
using Octokit.Tests.Integration.Helpers;
using Xunit;

public class EnterpriseLdapClientTests : IDisposable
{
readonly IGitHubClient _github;

readonly string _testUser = "test-user";
readonly string _distinguishedNameUser = "uid=test-user,ou=users,dc=company,dc=com";

readonly EnterpriseTeamContext _context;
readonly string _distinguishedNameTeam = "cn=test-team,ou=groups,dc=company,dc=com";

public EnterpriseLdapClientTests()
{
_github = EnterpriseHelper.GetAuthenticatedClient();

NewTeam newTeam = new NewTeam(Helper.MakeNameWithTimestamp("test-team")) { Description = "Test Team" };
_context = _github.CreateEnterpriseTeamContext(EnterpriseHelper.Organization, newTeam).Result;
}

[GitHubEnterpriseTest]
public async Task CanUpdateUserMapping()
{
var newLDAPMapping = new NewLdapMapping(_distinguishedNameUser);
var ldapUser = await
_github.Enterprise.Ldap.UpdateUserMapping(_testUser, newLDAPMapping);

Assert.NotNull(ldapUser);
Assert.NotNull(ldapUser.LdapDistinguishedName);
Assert.Equal(ldapUser.LdapDistinguishedName, _distinguishedNameUser);

// Get user and check mapping was updated
var checkUser = await _github.User.Get(_testUser);
Assert.Equal(checkUser.Login, ldapUser.Login);
Assert.Equal(checkUser.LdapDistinguishedName, _distinguishedNameUser);
}

[GitHubEnterpriseTest]
public async Task CanQueueSyncUserMapping()
{
var response = await
_github.Enterprise.Ldap.QueueSyncUserMapping(_testUser);

// Check response message indicates LDAP sync was queued
Assert.NotNull(response);
Assert.NotNull(response.Status);
Assert.True(response.Status == "queued");
}

[GitHubEnterpriseTest]
public async Task CanUpdateTeamMapping()
{
var newLDAPMapping = new NewLdapMapping(_distinguishedNameTeam);
var ldapTeam = await
_github.Enterprise.Ldap.UpdateTeamMapping(_context.TeamId, newLDAPMapping);

Assert.NotNull(ldapTeam);
Assert.NotNull(ldapTeam.LdapDistinguishedName);
Assert.Equal(ldapTeam.LdapDistinguishedName, _distinguishedNameTeam);

// Get Team and check mapping was updated
var checkTeam = await _github.Organization.Team.Get(_context.TeamId);
Assert.Equal(checkTeam.Name, ldapTeam.Name);
Assert.Equal(checkTeam.LdapDistinguishedName, _distinguishedNameTeam);
}

[GitHubEnterpriseTest]
public async Task CanQueueSyncTeamMapping()
{
var response = await
_github.Enterprise.Ldap.QueueSyncTeamMapping(_context.TeamId);

// Check response message indicates LDAP sync was queued
Assert.NotNull(response);
Assert.NotNull(response.Status);
Assert.True(response.Status == "queued");
}

public void Dispose()
{
_context.Dispose();
}
}
16 changes: 16 additions & 0 deletions Octokit.Tests.Integration/EnterpriseHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,22 @@ public static void DeleteRepo(string owner, string name)
catch { }
}

public static void DeleteTeam(Team team)
{
if (team != null)
DeleteTeam(team.Id);
}

public static void DeleteTeam(int teamId)
{
var api = GetAuthenticatedClient();
try
{
api.Organization.Team.Delete(teamId).Wait(TimeSpan.FromSeconds(15));
}
catch { }
}

public static IGitHubClient GetAuthenticatedClient()
{
return new GitHubClient(new ProductHeaderValue("OctokitEnterpriseTests"), GitHubEnterpriseUrl)
Expand Down
28 changes: 28 additions & 0 deletions Octokit.Tests.Integration/Helpers/EnterpriseTeamContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Octokit.Tests.Integration.Helpers
{
internal sealed class EnterpriseTeamContext : IDisposable
{
internal EnterpriseTeamContext(Team team)
{
Team = team;
TeamId = team.Id;
TeamName = team.Name;
}

internal int TeamId { get; private set; }
internal string TeamName { get; private set; }

internal Team Team { get; private set; }

public void Dispose()
{
EnterpriseHelper.DeleteTeam(Team);
}
}
}
7 changes: 7 additions & 0 deletions Octokit.Tests.Integration/Helpers/GithubClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,12 @@ internal async static Task<RepositoryContext> CreateRepositoryContext(this IGitH

return new RepositoryContext(repo);
}

internal async static Task<EnterpriseTeamContext> CreateEnterpriseTeamContext(this IGitHubClient client, string organization, NewTeam newTeam)
{
var team = await client.Organization.Team.Create(organization, newTeam);

return new EnterpriseTeamContext(team);
}
}
}
Loading

0 comments on commit 307b1b8

Please sign in to comment.