Better support for GitHub Enterprise integration tests

- 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
This commit is contained in:
Ryan Gribble
2016-01-27 00:12:22 +10:00
parent 5f2cc4cb4e
commit 535709c368
7 changed files with 198 additions and 22 deletions
@@ -9,7 +9,7 @@ public class EnterpriseAdminStatsClientTests
public EnterpriseAdminStatsClientTests()
{
_github = Helper.GetAuthenticatedClient();
_github = EnterpriseHelper.GetAuthenticatedClient();
}
[GitHubEnterpriseTest]
@@ -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")
};
}
}
}
+10 -16
View File
@@ -50,9 +50,9 @@ namespace Octokit.Tests.Integration
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);
@@ -80,7 +80,9 @@ namespace Octokit.Tests.Integration
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
{
@@ -98,14 +100,6 @@ namespace Octokit.Tests.Integration
}
}
public static bool IsGitHubEnterprise
{
get
{
return GitHubEnterpriseUrl != null;
}
}
public static string ClientId
{
get { return Environment.GetEnvironmentVariable("OCTOKIT_CLIENTID"); }
@@ -151,7 +145,7 @@ namespace Octokit.Tests.Integration
public static IGitHubClient GetAuthenticatedClient()
{
return new GitHubClient(new ProductHeaderValue("OctokitTests"), GitHubEnterpriseUrl ?? GitHubClient.GitHubApiUrl)
return new GitHubClient(new ProductHeaderValue("OctokitTests"), TargetUrl)
{
Credentials = Credentials
};
@@ -159,7 +153,7 @@ namespace Octokit.Tests.Integration
public static IGitHubClient GetBasicAuthClient()
{
return new GitHubClient(new ProductHeaderValue("OctokitTests"), GitHubEnterpriseUrl ?? GitHubClient.GitHubApiUrl)
return new GitHubClient(new ProductHeaderValue("OctokitTests"), TargetUrl)
{
Credentials = BasicAuthCredentials
};
@@ -167,7 +161,7 @@ namespace Octokit.Tests.Integration
public static GitHubClient GetAuthenticatedApplicationClient()
{
return new GitHubClient(new ProductHeaderValue("OctokitTests"), GitHubEnterpriseUrl ?? GitHubClient.GitHubApiUrl)
return new GitHubClient(new ProductHeaderValue("OctokitTests"), TargetUrl)
{
Credentials = ApplicationCredentials
};
@@ -175,12 +169,12 @@ namespace Octokit.Tests.Integration
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")
};
@@ -20,7 +20,7 @@ namespace Octokit.Tests.Integration
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) };
@@ -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" />
@@ -11,7 +11,7 @@ namespace Octokit.Tests.Integration
public ObservableEnterpriseAdminStatsClientTests()
{
_github = new ObservableGitHubClient(Helper.GetAuthenticatedClient());
_github = new ObservableGitHubClient(EnterpriseHelper.GetAuthenticatedClient());
}
[GitHubEnterpriseTest]
+20 -3
View File
@@ -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)
@@ -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
}