Skip to content

Commit

Permalink
Better support for GitHub Enterprise integration tests
Browse files Browse the repository at this point in the history
- Remove EnterpriseUrl in integration test Helper class, but leave ability to override custom URL (to allow specific use case of targetting regular integration tests at a custom URL)
- Move GitHub Enterprise explicit support to a new integration helper class using new OCTOKIT_GHE_ environment variables for GHE
- Change existing GitHub Enterprise integration tests and EnterpriseTestAttribute to use the new EnterpriseHelper methods
- Enhance configure-intergration-tests.ps1 script to cater for environment variable changes
  • Loading branch information
ryangribble committed Feb 1, 2016
1 parent 5f2cc4c commit 535709c
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class EnterpriseAdminStatsClientTests

public EnterpriseAdminStatsClientTests()
{
_github = Helper.GetAuthenticatedClient();
_github = EnterpriseHelper.GetAuthenticatedClient();
}

[GitHubEnterpriseTest]
Expand Down
164 changes: 164 additions & 0 deletions Octokit.Tests.Integration/EnterpriseHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
using System;
using System.Diagnostics;
using System.IO;

namespace Octokit.Tests.Integration
{
public static class EnterpriseHelper
{
static readonly Lazy<Credentials> _credentialsThunk = new Lazy<Credentials>(() =>
{
var githubUsername = Environment.GetEnvironmentVariable("OCTOKIT_GHE_USERNAME");
GHEUserName = githubUsername;
GHEOrganization = Environment.GetEnvironmentVariable("OCTOKIT_GHE_ORGANIZATION");

var githubToken = Environment.GetEnvironmentVariable("OCTOKIT_GHE_OAUTHTOKEN");

if (githubToken != null)
return new Credentials(githubToken);

var githubPassword = Environment.GetEnvironmentVariable("OCTOKIT_GHE_PASSWORD");

if (githubUsername == null || githubPassword == null)
return null;

return new Credentials(githubUsername, githubPassword);
});

static readonly Lazy<Credentials> _oauthApplicationCredentials = new Lazy<Credentials>(() =>
{
var applicationClientId = ClientId;
var applicationClientSecret = ClientSecret;

if (applicationClientId == null || applicationClientSecret == null)
return null;

return new Credentials(applicationClientId, applicationClientSecret);
});

static readonly Lazy<Credentials> _basicAuthCredentials = new Lazy<Credentials>(() =>
{
var githubUsername = Environment.GetEnvironmentVariable("OCTOKIT_GHE_USERNAME");
GHEUserName = githubUsername;
GHEOrganization = Environment.GetEnvironmentVariable("OCTOKIT_GHE_ORGANIZATION");

var githubPassword = Environment.GetEnvironmentVariable("OCTOKIT_GHE_PASSWORD");

if (githubUsername == null || githubPassword == null)
return null;

return new Credentials(githubUsername, githubPassword);
});

static readonly Lazy<bool> _gitHubEnterpriseEnabled = new Lazy<bool>(() =>
{
string enabled = Environment.GetEnvironmentVariable("OCTOKIT_GHE_ENABLED");
return !String.IsNullOrWhiteSpace(enabled);
});

static readonly Lazy<Uri> _gitHubEnterpriseUrl = new Lazy<Uri>(() =>
{
string uri = Environment.GetEnvironmentVariable("OCTOKIT_GHE_URL");

if (uri != null)
return new Uri(uri);

return null;
});

static EnterpriseHelper()
{
// Force reading of environment variables.
// This wasn't happening if UserName/Organization were
// retrieved before Credentials.
Debug.WriteIf(GHECredentials == null, "No credentials specified.");
}

public static string GHEUserName { get; private set; }
public static string GHEOrganization { get; private set; }

/// <summary>
/// These credentials should be set to a test GitHub account using the powershell script configure-integration-tests.ps1
/// </summary>
public static Credentials GHECredentials { get { return _credentialsThunk.Value; } }

public static Credentials GHEApplicationCredentials { get { return _oauthApplicationCredentials.Value; } }

public static Credentials GHEBasicAuthCredentials { get { return _basicAuthCredentials.Value; } }

public static bool IsGitHubEnterpriseEnabled { get { return _gitHubEnterpriseEnabled.Value; } }

public static Uri GitHubEnterpriseUrl { get { return _gitHubEnterpriseUrl.Value; } }

public static bool IsUsingToken
{
get
{
return !String.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("OCTOKIT_GHE_OAUTHTOKEN"));
}
}

public static string ClientId
{
get { return Environment.GetEnvironmentVariable("OCTOKIT_GHE_CLIENTID"); }
}

public static string ClientSecret
{
get { return Environment.GetEnvironmentVariable("OCTOKIT_GHE_CLIENTSECRET"); }
}

public static void DeleteRepo(Repository repository)
{
if (repository != null)
DeleteRepo(repository.Owner.Login, repository.Name);
}

public static void DeleteRepo(string owner, string name)
{
var api = GetAuthenticatedClient();
try
{
api.Repository.Delete(owner, name).Wait(TimeSpan.FromSeconds(15));
}
catch { }
}

public static IGitHubClient GetAuthenticatedClient()
{
return new GitHubClient(new ProductHeaderValue("OctokitEnterpriseTests"), GitHubEnterpriseUrl)
{
Credentials = GHECredentials
};
}

public static IGitHubClient GetBasicAuthClient()
{
return new GitHubClient(new ProductHeaderValue("OctokitEnterpriseTests"), GitHubEnterpriseUrl)
{
Credentials = GHEBasicAuthCredentials
};
}

public static GitHubClient GetAuthenticatedApplicationClient()
{
return new GitHubClient(new ProductHeaderValue("OctokitEnterpriseTests"), GitHubEnterpriseUrl)
{
Credentials = GHEApplicationCredentials
};
}

public static IGitHubClient GetAnonymousClient()
{
return new GitHubClient(new ProductHeaderValue("OctokitEnterpriseTests"), GitHubEnterpriseUrl);
}

public static IGitHubClient GetBadCredentialsClient()
{
return new GitHubClient(new ProductHeaderValue("OctokitEnterpriseTests"), GitHubEnterpriseUrl)
{
Credentials = new Credentials(Guid.NewGuid().ToString(), "bad-password")
};
}
}
}
26 changes: 10 additions & 16 deletions Octokit.Tests.Integration/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public static class Helper
return new Credentials(githubUsername, githubPassword);
});

static readonly Lazy<Uri> _gitHubEnterpriseUrl = new Lazy<Uri>(() =>
static readonly Lazy<Uri> _customUrl = new Lazy<Uri>(() =>
{
string uri = Environment.GetEnvironmentVariable("OCTOKIT_GITHUBENTERPRISEURL");
string uri = Environment.GetEnvironmentVariable("OCTOKIT_CUSTOMURL");

if (uri != null)
return new Uri(uri);
Expand Down Expand Up @@ -80,7 +80,9 @@ static Helper()

public static Credentials BasicAuthCredentials { get { return _basicAuthCredentials.Value; } }

public static Uri GitHubEnterpriseUrl { get { return _gitHubEnterpriseUrl.Value; } }
public static Uri CustomUrl { get { return _customUrl.Value; } }

public static Uri TargetUrl { get { return CustomUrl ?? GitHubClient.GitHubApiUrl; } }

public static bool IsUsingToken
{
Expand All @@ -98,14 +100,6 @@ public static bool IsPaidAccount
}
}

public static bool IsGitHubEnterprise
{
get
{
return GitHubEnterpriseUrl != null;
}
}

public static string ClientId
{
get { return Environment.GetEnvironmentVariable("OCTOKIT_CLIENTID"); }
Expand Down Expand Up @@ -151,36 +145,36 @@ public static Stream LoadFixture(string fileName)

public static IGitHubClient GetAuthenticatedClient()
{
return new GitHubClient(new ProductHeaderValue("OctokitTests"), GitHubEnterpriseUrl ?? GitHubClient.GitHubApiUrl)
return new GitHubClient(new ProductHeaderValue("OctokitTests"), TargetUrl)
{
Credentials = Credentials
};
}

public static IGitHubClient GetBasicAuthClient()
{
return new GitHubClient(new ProductHeaderValue("OctokitTests"), GitHubEnterpriseUrl ?? GitHubClient.GitHubApiUrl)
return new GitHubClient(new ProductHeaderValue("OctokitTests"), TargetUrl)
{
Credentials = BasicAuthCredentials
};
}

public static GitHubClient GetAuthenticatedApplicationClient()
{
return new GitHubClient(new ProductHeaderValue("OctokitTests"), GitHubEnterpriseUrl ?? GitHubClient.GitHubApiUrl)
return new GitHubClient(new ProductHeaderValue("OctokitTests"), TargetUrl)
{
Credentials = ApplicationCredentials
};
}

public static IGitHubClient GetAnonymousClient()
{
return new GitHubClient(new ProductHeaderValue("OctokitTests"), GitHubEnterpriseUrl ?? GitHubClient.GitHubApiUrl);
return new GitHubClient(new ProductHeaderValue("OctokitTests"), TargetUrl);
}

public static IGitHubClient GetBadCredentialsClient()
{
return new GitHubClient(new ProductHeaderValue("OctokitTests"), GitHubEnterpriseUrl ?? GitHubClient.GitHubApiUrl)
return new GitHubClient(new ProductHeaderValue("OctokitTests"), TargetUrl)
{
Credentials = new Credentials(Guid.NewGuid().ToString(), "bad-password")
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public IEnumerable<IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions disco
if (Helper.Credentials == null)
return Enumerable.Empty<IXunitTestCase>();

if (!Helper.IsGitHubEnterprise)
if (!EnterpriseHelper.IsGitHubEnterpriseEnabled)
return Enumerable.Empty<IXunitTestCase>();

return new[] { new XunitTestCase(diagnosticMessageSink, discoveryOptions.MethodDisplayOrDefault(), testMethod) };
Expand Down
1 change: 1 addition & 0 deletions Octokit.Tests.Integration/Octokit.Tests.Integration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
<Compile Include="Clients\UserKeysClientTests.cs" />
<Compile Include="fixtures\RepositoriesHooksCollection.cs" />
<Compile Include="fixtures\RepositoriesHooksFixture.cs" />
<Compile Include="EnterpriseHelper.cs" />
<Compile Include="Helpers\ApplicationTestAttribute.cs" />
<Compile Include="Helpers\GithubClientExtensions.cs" />
<Compile Include="Helpers\BasicAuthenticationTestAttribute.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ObservableEnterpriseAdminStatsClientTests

public ObservableEnterpriseAdminStatsClientTests()
{
_github = new ObservableGitHubClient(Helper.GetAuthenticatedClient());
_github = new ObservableGitHubClient(EnterpriseHelper.GetAuthenticatedClient());
}

[GitHubEnterpriseTest]
Expand Down
23 changes: 20 additions & 3 deletions script/configure-integration-tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ function AskYesNoQuestion([string]$question, [string]$key)
}

Write-Host

return ($answer -eq "Y")
}

function VerifyEnvironmentVariable([string]$friendlyName, [string]$key, [bool]$optional = $false)
Expand Down Expand Up @@ -71,11 +73,26 @@ VerifyEnvironmentVariable "account name" "OCTOKIT_GITHUBUSERNAME"
VerifyEnvironmentVariable "account password" "OCTOKIT_GITHUBPASSWORD" $true
VerifyEnvironmentVariable "OAuth token" "OCTOKIT_OAUTHTOKEN"

AskYesNoQuestion "Do you have private repositories associated with your test account?" "OCTOKIT_PRIVATEREPOSITORIES"
AskYesNoQuestion "Do you have private repositories associated with your test account?" "OCTOKIT_PRIVATEREPOSITORIES" | Out-Null

VerifyEnvironmentVariable "organization name" "OCTOKIT_GITHUBORGANIZATION" $true

VerifyEnvironmentVariable "GitHub Enterprise Server URL" "OCTOKIT_GITHUBENTERPRISEURL" $true
VerifyEnvironmentVariable "Override GitHub URL" "OCTOKIT_CUSTOMURL" $true

VerifyEnvironmentVariable "application ClientID" "OCTOKIT_CLIENTID" $true
VerifyEnvironmentVariable "application Secret" "OCTOKIT_CLIENTSECRET" $true
VerifyEnvironmentVariable "application Secret" "OCTOKIT_CLIENTSECRET" $true


if (AskYesNoQuestion "Do you wish to enable GitHub Enterprise (GHE) Integration Tests?" "OCTOKIT_GHE_ENABLED")
{
VerifyEnvironmentVariable "GitHub Enterprise account name" "OCTOKIT_GHE_USERNAME"
VerifyEnvironmentVariable "GitHub Enterprise account password" "OCTOKIT_GHE_PASSWORD" $true
VerifyEnvironmentVariable "GitHub Enterprise OAuth token" "OCTOKIT_GHE_OAUTHTOKEN"

VerifyEnvironmentVariable "GitHub Enterprise organization name" "OCTOKIT_GHE_ORGANIZATION" $true

VerifyEnvironmentVariable "GitHub Enterprise URL" "OCTOKIT_GHE_URL" $true

VerifyEnvironmentVariable "GitHub Enterprise application ClientID" "OCTOKIT_GHE_CLIENTID" $true
VerifyEnvironmentVariable "GitHub Enterprise application Secret" "OCTOKIT_GHE_CLIENTSECRET" $true
}

0 comments on commit 535709c

Please sign in to comment.