From 535709c3689eaa6b6a4556757c11d0d50b0aae0e Mon Sep 17 00:00:00 2001 From: Ryan Gribble Date: Wed, 27 Jan 2016 00:12:22 +1000 Subject: [PATCH] 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 --- .../EnterpriseAdminStatsClientTests.cs | 2 +- Octokit.Tests.Integration/EnterpriseHelper.cs | 164 ++++++++++++++++++ Octokit.Tests.Integration/Helper.cs | 26 ++- .../Helpers/GitHubEnterpriseTestAttribute.cs | 2 +- .../Octokit.Tests.Integration.csproj | 1 + ...servableEnterpriseAdminStatsClientTests.cs | 2 +- script/configure-integration-tests.ps1 | 23 ++- 7 files changed, 198 insertions(+), 22 deletions(-) create mode 100644 Octokit.Tests.Integration/EnterpriseHelper.cs diff --git a/Octokit.Tests.Integration/Clients/Enterprise/EnterpriseAdminStatsClientTests.cs b/Octokit.Tests.Integration/Clients/Enterprise/EnterpriseAdminStatsClientTests.cs index 12dc638a..2c8b1cf6 100644 --- a/Octokit.Tests.Integration/Clients/Enterprise/EnterpriseAdminStatsClientTests.cs +++ b/Octokit.Tests.Integration/Clients/Enterprise/EnterpriseAdminStatsClientTests.cs @@ -9,7 +9,7 @@ public class EnterpriseAdminStatsClientTests public EnterpriseAdminStatsClientTests() { - _github = Helper.GetAuthenticatedClient(); + _github = EnterpriseHelper.GetAuthenticatedClient(); } [GitHubEnterpriseTest] diff --git a/Octokit.Tests.Integration/EnterpriseHelper.cs b/Octokit.Tests.Integration/EnterpriseHelper.cs new file mode 100644 index 00000000..27f237d0 --- /dev/null +++ b/Octokit.Tests.Integration/EnterpriseHelper.cs @@ -0,0 +1,164 @@ +using System; +using System.Diagnostics; +using System.IO; + +namespace Octokit.Tests.Integration +{ + public static class EnterpriseHelper + { + static readonly Lazy _credentialsThunk = new Lazy(() => + { + 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 _oauthApplicationCredentials = new Lazy(() => + { + var applicationClientId = ClientId; + var applicationClientSecret = ClientSecret; + + if (applicationClientId == null || applicationClientSecret == null) + return null; + + return new Credentials(applicationClientId, applicationClientSecret); + }); + + static readonly Lazy _basicAuthCredentials = new Lazy(() => + { + 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 _gitHubEnterpriseEnabled = new Lazy(() => + { + string enabled = Environment.GetEnvironmentVariable("OCTOKIT_GHE_ENABLED"); + return !String.IsNullOrWhiteSpace(enabled); + }); + + static readonly Lazy _gitHubEnterpriseUrl = new Lazy(() => + { + 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; } + + /// + /// These credentials should be set to a test GitHub account using the powershell script configure-integration-tests.ps1 + /// + 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") + }; + } + } +} diff --git a/Octokit.Tests.Integration/Helper.cs b/Octokit.Tests.Integration/Helper.cs index bd118521..79a249a9 100644 --- a/Octokit.Tests.Integration/Helper.cs +++ b/Octokit.Tests.Integration/Helper.cs @@ -50,9 +50,9 @@ namespace Octokit.Tests.Integration return new Credentials(githubUsername, githubPassword); }); - static readonly Lazy _gitHubEnterpriseUrl = new Lazy(() => + static readonly Lazy _customUrl = new Lazy(() => { - 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") }; diff --git a/Octokit.Tests.Integration/Helpers/GitHubEnterpriseTestAttribute.cs b/Octokit.Tests.Integration/Helpers/GitHubEnterpriseTestAttribute.cs index 80c1df66..938bfb46 100644 --- a/Octokit.Tests.Integration/Helpers/GitHubEnterpriseTestAttribute.cs +++ b/Octokit.Tests.Integration/Helpers/GitHubEnterpriseTestAttribute.cs @@ -20,7 +20,7 @@ namespace Octokit.Tests.Integration if (Helper.Credentials == null) return Enumerable.Empty(); - if (!Helper.IsGitHubEnterprise) + if (!EnterpriseHelper.IsGitHubEnterpriseEnabled) return Enumerable.Empty(); return new[] { new XunitTestCase(diagnosticMessageSink, discoveryOptions.MethodDisplayOrDefault(), testMethod) }; diff --git a/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj b/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj index 3cbb9462..2be66be3 100644 --- a/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj +++ b/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj @@ -106,6 +106,7 @@ + diff --git a/Octokit.Tests.Integration/Reactive/Enterprise/ObservableEnterpriseAdminStatsClientTests.cs b/Octokit.Tests.Integration/Reactive/Enterprise/ObservableEnterpriseAdminStatsClientTests.cs index 2cb61234..182ad393 100644 --- a/Octokit.Tests.Integration/Reactive/Enterprise/ObservableEnterpriseAdminStatsClientTests.cs +++ b/Octokit.Tests.Integration/Reactive/Enterprise/ObservableEnterpriseAdminStatsClientTests.cs @@ -11,7 +11,7 @@ namespace Octokit.Tests.Integration public ObservableEnterpriseAdminStatsClientTests() { - _github = new ObservableGitHubClient(Helper.GetAuthenticatedClient()); + _github = new ObservableGitHubClient(EnterpriseHelper.GetAuthenticatedClient()); } [GitHubEnterpriseTest] diff --git a/script/configure-integration-tests.ps1 b/script/configure-integration-tests.ps1 index 5592c3e4..590291d9 100644 --- a/script/configure-integration-tests.ps1 +++ b/script/configure-integration-tests.ps1 @@ -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 \ No newline at end of file +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 +} \ No newline at end of file