From beb18d7213c2f68a4238bebe3f5b4c20891f4b72 Mon Sep 17 00:00:00 2001 From: half-ogre Date: Mon, 16 Sep 2013 19:50:22 -0700 Subject: [PATCH 1/8] keep automation settings in one place --- .../AutomationSettings.cs | 55 +++++++++++++++++++ .../Octokit.Tests.Integration.csproj | 1 + 2 files changed, 56 insertions(+) create mode 100644 Octokit.Tests.Integration/AutomationSettings.cs diff --git a/Octokit.Tests.Integration/AutomationSettings.cs b/Octokit.Tests.Integration/AutomationSettings.cs new file mode 100644 index 00000000..e80774fa --- /dev/null +++ b/Octokit.Tests.Integration/AutomationSettings.cs @@ -0,0 +1,55 @@ +using System; +using Octokit.Http; + +namespace Octokit.Tests.Integration +{ + /// + /// Settings for executing automated tests. + /// + public class AutomationSettings + { + static readonly Lazy automationSettingsThunk = new Lazy(() => + new AutomationSettings("xapitestaccountx", "octocat11")); + + /// + /// The current automation settings. + /// + public static AutomationSettings Current + { + get + { + return automationSettingsThunk.Value; + } + } + + /// + /// Creates a new instance of settings for executing automated tests. + /// + /// Username of a GitHub test account (DO NOT USE A "REAL" ACCOUNT) + /// Password for a GitHub test account (DO NOT USE A "REAL" ACCOUNT) + public AutomationSettings(string githubUsername, string githubPassword) + { + GitHubUsername = githubUsername; + GitHubPassword = githubPassword; + + GitHubCredentials = new Credentials( + GitHubUsername, + GitHubPassword); + } + + /// + /// for a GitHub test account (DO NOT USE A "REAL" ACCOUNT). + /// + public Credentials GitHubCredentials { get; private set; } + + /// + /// Password for a GitHub test account (DO NOT USE A "REAL" ACCOUNT). + /// + public string GitHubPassword { get; private set; } + + /// + /// Username of a GitHub test account (DO NOT USE A "REAL" ACCOUNT). + /// + public string GitHubUsername { get; private set; } + } +} diff --git a/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj b/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj index 68b7cf4f..b97fb8bd 100644 --- a/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj +++ b/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj @@ -50,6 +50,7 @@ + From 577095df37f8cb151ee8b61fd69774c3a23bf458 Mon Sep 17 00:00:00 2001 From: half-ogre Date: Mon, 16 Sep 2013 20:06:30 -0700 Subject: [PATCH 2/8] use automation settings in integration tests --- .../AutoCompleteClientTests.cs | 3 +-- .../RepositoriesClientTests.cs | 7 +++---- Octokit.Tests.Integration/UsersClientTests.cs | 20 +++++++++---------- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/Octokit.Tests.Integration/AutoCompleteClientTests.cs b/Octokit.Tests.Integration/AutoCompleteClientTests.cs index 9b32b1ab..a1a7b548 100644 --- a/Octokit.Tests.Integration/AutoCompleteClientTests.cs +++ b/Octokit.Tests.Integration/AutoCompleteClientTests.cs @@ -1,6 +1,5 @@ using System.Threading.Tasks; using FluentAssertions; -using Octokit.Http; using Xunit; namespace Octokit.Tests.Integration @@ -14,7 +13,7 @@ namespace Octokit.Tests.Integration { var github = new GitHubClient { - Credentials = new Credentials("xapitestaccountx", "octocat11") + Credentials = AutomationSettings.Current.GitHubCredentials }; var emojis = await github.AutoComplete.GetEmojis(); diff --git a/Octokit.Tests.Integration/RepositoriesClientTests.cs b/Octokit.Tests.Integration/RepositoriesClientTests.cs index 7c8c5aed..81b6d48a 100644 --- a/Octokit.Tests.Integration/RepositoriesClientTests.cs +++ b/Octokit.Tests.Integration/RepositoriesClientTests.cs @@ -1,6 +1,5 @@ using System.Threading.Tasks; using FluentAssertions; -using Octokit.Http; using Xunit; namespace Octokit.Tests.Integration @@ -14,7 +13,7 @@ namespace Octokit.Tests.Integration { var github = new GitHubClient { - Credentials = new Credentials("xapitestaccountx", "octocat11") + Credentials = AutomationSettings.Current.GitHubCredentials }; var repository = await github.Repository.Get("ReactiveCocoa", "ReactiveCocoa"); @@ -30,7 +29,7 @@ namespace Octokit.Tests.Integration { var github = new GitHubClient { - Credentials = new Credentials("xapitestaccountx", "octocat11") + Credentials = AutomationSettings.Current.GitHubCredentials }; var repositories = await github.Repository.GetAllForOrg("github"); @@ -46,7 +45,7 @@ namespace Octokit.Tests.Integration { var github = new GitHubClient { - Credentials = new Credentials("xapitestaccountx", "octocat11") + Credentials = AutomationSettings.Current.GitHubCredentials }; // TODO: Change this to request github/Octokit.net once we make this OSS. diff --git a/Octokit.Tests.Integration/UsersClientTests.cs b/Octokit.Tests.Integration/UsersClientTests.cs index ec441011..4a9a5ec6 100644 --- a/Octokit.Tests.Integration/UsersClientTests.cs +++ b/Octokit.Tests.Integration/UsersClientTests.cs @@ -1,6 +1,4 @@ -using System; -using System.Net; -using System.Net.Http; +using System.Net; using System.Threading.Tasks; using FluentAssertions; using Octokit.Http; @@ -18,7 +16,7 @@ namespace Octokit.Tests.Integration { var github = new GitHubClient { - Credentials = new Credentials("xapitestaccountx", "octocat11") + Credentials = AutomationSettings.Current.GitHubCredentials }; // Get a user by username @@ -35,12 +33,12 @@ namespace Octokit.Tests.Integration { var github = new GitHubClient { - Credentials = new Credentials("xapitestaccountx", "octocat11") + Credentials = AutomationSettings.Current.GitHubCredentials }; var user = await github.User.Current(); - user.Login.Should().Be("xapitestaccountx"); + user.Login.Should().Be(AutomationSettings.Current.GitHubUsername); } } @@ -51,8 +49,8 @@ namespace Octokit.Tests.Integration { var github = new GitHubClient(); var userUpdate = new UserUpdate - { - Name = "xapitestaccountx", + { + Name = AutomationSettings.Current.GitHubUsername, Bio = "UPDATED BIO" }; @@ -66,11 +64,11 @@ namespace Octokit.Tests.Integration { var github = new GitHubClient { - Credentials = new Credentials("xapitestaccountx", "bad-password") + Credentials = new Credentials(AutomationSettings.Current.GitHubUsername, "bad-password") }; var userUpdate = new UserUpdate - { - Name = "xapitestaccountx", + { + Name = AutomationSettings.Current.GitHubUsername, Bio = "UPDATED BIO" }; From a21ddd90e884c1f034febc72717a69d07f6b90da Mon Sep 17 00:00:00 2001 From: half-ogre Date: Mon, 16 Sep 2013 20:14:33 -0700 Subject: [PATCH 3/8] get the test account from the environment --- Octokit.Tests.Integration/AutomationSettings.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Octokit.Tests.Integration/AutomationSettings.cs b/Octokit.Tests.Integration/AutomationSettings.cs index e80774fa..7f938271 100644 --- a/Octokit.Tests.Integration/AutomationSettings.cs +++ b/Octokit.Tests.Integration/AutomationSettings.cs @@ -8,8 +8,18 @@ namespace Octokit.Tests.Integration /// public class AutomationSettings { - static readonly Lazy automationSettingsThunk = new Lazy(() => - new AutomationSettings("xapitestaccountx", "octocat11")); + static readonly Lazy automationSettingsThunk = new Lazy(() => + { + var githubUsername = Environment.GetEnvironmentVariable("Octokit.GitHubUsername"); + if (githubUsername == null) + throw new InvalidOperationException("The \"Octokit.GitHubUsername\" environment variable must be set. Please use a test account (i.e, DO NOT USE A \"REAL\" ACCOUNT)."); + + var githubPassword = Environment.GetEnvironmentVariable("Octokit.GitHubPassword"); + if (githubPassword == null) + throw new InvalidOperationException("The \"Octokit.GitHubPassword\" environment variable must be set. Please use a test account (i.e, DO NOT USE A \"REAL\" ACCOUNT)."); + + return new AutomationSettings(githubUsername, githubPassword); + }); /// /// The current automation settings. From d281eaaf50afe1628ed1422bd520fbf514bd95cb Mon Sep 17 00:00:00 2001 From: half-ogre Date: Mon, 16 Sep 2013 20:24:03 -0700 Subject: [PATCH 4/8] don't use dot notation in environment variables It doesn't play nice with MSBuild (which wants to treat it like a property access operator). --- Octokit.Tests.Integration/AutomationSettings.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Octokit.Tests.Integration/AutomationSettings.cs b/Octokit.Tests.Integration/AutomationSettings.cs index 7f938271..a5d5c48e 100644 --- a/Octokit.Tests.Integration/AutomationSettings.cs +++ b/Octokit.Tests.Integration/AutomationSettings.cs @@ -10,11 +10,11 @@ namespace Octokit.Tests.Integration { static readonly Lazy automationSettingsThunk = new Lazy(() => { - var githubUsername = Environment.GetEnvironmentVariable("Octokit.GitHubUsername"); + var githubUsername = Environment.GetEnvironmentVariable("OCTOKIT_GITHUBUSERNAME"); if (githubUsername == null) throw new InvalidOperationException("The \"Octokit.GitHubUsername\" environment variable must be set. Please use a test account (i.e, DO NOT USE A \"REAL\" ACCOUNT)."); - var githubPassword = Environment.GetEnvironmentVariable("Octokit.GitHubPassword"); + var githubPassword = Environment.GetEnvironmentVariable("OCTOKIT_GITHUBPASSWORD"); if (githubPassword == null) throw new InvalidOperationException("The \"Octokit.GitHubPassword\" environment variable must be set. Please use a test account (i.e, DO NOT USE A \"REAL\" ACCOUNT)."); From 064fc2c05d26c4d7f732d99165e92173f453aab5 Mon Sep 17 00:00:00 2001 From: half-ogre Date: Mon, 16 Sep 2013 20:24:47 -0700 Subject: [PATCH 5/8] only do integration tests when environment set up --- Octokit.msbuild | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Octokit.msbuild b/Octokit.msbuild index e16c92ab..6d0b218b 100644 --- a/Octokit.msbuild +++ b/Octokit.msbuild @@ -17,7 +17,7 @@ - + From 99e195faf4838ce8353028da00b13e190e99cb01 Mon Sep 17 00:00:00 2001 From: half-ogre Date: Mon, 16 Sep 2013 20:41:40 -0700 Subject: [PATCH 6/8] show warning when integration tests are skipped --- Octokit.msbuild | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Octokit.msbuild b/Octokit.msbuild index 6d0b218b..2b40f814 100644 --- a/Octokit.msbuild +++ b/Octokit.msbuild @@ -17,9 +17,15 @@ - + + + + + + + From 6352cc8b24a9283c6d103b75ba5f710f06245f23 Mon Sep 17 00:00:00 2001 From: half-ogre Date: Mon, 16 Sep 2013 21:03:11 -0700 Subject: [PATCH 7/8] skip integration tests in xunit when not set up --- .../AutoCompleteClientTests.cs | 2 +- Octokit.Tests.Integration/AutomationSettings.cs | 12 ++++-------- .../IntegrationTestAttribute.cs | 17 +++++++++++++++++ .../Octokit.Tests.Integration.csproj | 1 + .../RepositoriesClientTests.cs | 6 +++--- Octokit.Tests.Integration/UsersClientTests.cs | 8 ++++---- 6 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 Octokit.Tests.Integration/IntegrationTestAttribute.cs diff --git a/Octokit.Tests.Integration/AutoCompleteClientTests.cs b/Octokit.Tests.Integration/AutoCompleteClientTests.cs index a1a7b548..26197315 100644 --- a/Octokit.Tests.Integration/AutoCompleteClientTests.cs +++ b/Octokit.Tests.Integration/AutoCompleteClientTests.cs @@ -8,7 +8,7 @@ namespace Octokit.Tests.Integration { public class TheGetEmojisMethod { - [Fact] + [IntegrationTest] public async Task GetsAllTheEmojis() { var github = new GitHubClient diff --git a/Octokit.Tests.Integration/AutomationSettings.cs b/Octokit.Tests.Integration/AutomationSettings.cs index a5d5c48e..fca5ac97 100644 --- a/Octokit.Tests.Integration/AutomationSettings.cs +++ b/Octokit.Tests.Integration/AutomationSettings.cs @@ -11,12 +11,7 @@ namespace Octokit.Tests.Integration static readonly Lazy automationSettingsThunk = new Lazy(() => { var githubUsername = Environment.GetEnvironmentVariable("OCTOKIT_GITHUBUSERNAME"); - if (githubUsername == null) - throw new InvalidOperationException("The \"Octokit.GitHubUsername\" environment variable must be set. Please use a test account (i.e, DO NOT USE A \"REAL\" ACCOUNT)."); - var githubPassword = Environment.GetEnvironmentVariable("OCTOKIT_GITHUBPASSWORD"); - if (githubPassword == null) - throw new InvalidOperationException("The \"Octokit.GitHubPassword\" environment variable must be set. Please use a test account (i.e, DO NOT USE A \"REAL\" ACCOUNT)."); return new AutomationSettings(githubUsername, githubPassword); }); @@ -42,9 +37,10 @@ namespace Octokit.Tests.Integration GitHubUsername = githubUsername; GitHubPassword = githubPassword; - GitHubCredentials = new Credentials( - GitHubUsername, - GitHubPassword); + if (GitHubUsername != null && GitHubPassword != null) + GitHubCredentials = new Credentials( + GitHubUsername, + GitHubPassword); } /// diff --git a/Octokit.Tests.Integration/IntegrationTestAttribute.cs b/Octokit.Tests.Integration/IntegrationTestAttribute.cs new file mode 100644 index 00000000..a86d957a --- /dev/null +++ b/Octokit.Tests.Integration/IntegrationTestAttribute.cs @@ -0,0 +1,17 @@ +using System.Collections.Generic; +using Xunit; +using Xunit.Sdk; + +namespace Octokit.Tests.Integration +{ + public class IntegrationTestAttribute : FactAttribute + { + protected override IEnumerable EnumerateTestCommands(IMethodInfo testMethod) + { + if (AutomationSettings.Current.GitHubCredentials == null) + yield return new SkipCommand(testMethod, MethodUtility.GetDisplayName(testMethod), "Automation settings not configured. Please set the OCTOKIT_GITHUBUSERNAME and OCTOKIT_GITHUBPASSWORD environment variables to a GitHub test account (i.e, DO NOT USE A \"REAL\" ACCOUNT)."); + else + yield return new FactCommand(testMethod); + } + } +} diff --git a/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj b/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj index b97fb8bd..6d4f7ffb 100644 --- a/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj +++ b/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj @@ -51,6 +51,7 @@ + diff --git a/Octokit.Tests.Integration/RepositoriesClientTests.cs b/Octokit.Tests.Integration/RepositoriesClientTests.cs index 81b6d48a..21110194 100644 --- a/Octokit.Tests.Integration/RepositoriesClientTests.cs +++ b/Octokit.Tests.Integration/RepositoriesClientTests.cs @@ -8,7 +8,7 @@ namespace Octokit.Tests.Integration { public class TheGetAsyncMethod { - [Fact] + [IntegrationTest] public async Task ReturnsSpecifiedUser() { var github = new GitHubClient @@ -24,7 +24,7 @@ namespace Octokit.Tests.Integration public class TheGetAllForOrgMethod { - [Fact] + [IntegrationTest] public async Task ReturnsAllRepositoriesForOrganization() { var github = new GitHubClient @@ -40,7 +40,7 @@ namespace Octokit.Tests.Integration public class TheGetReadmeMethod { - [Fact] + [IntegrationTest] public async Task ReturnsReadmeForOctokit() { var github = new GitHubClient diff --git a/Octokit.Tests.Integration/UsersClientTests.cs b/Octokit.Tests.Integration/UsersClientTests.cs index 4a9a5ec6..b06c517c 100644 --- a/Octokit.Tests.Integration/UsersClientTests.cs +++ b/Octokit.Tests.Integration/UsersClientTests.cs @@ -11,7 +11,7 @@ namespace Octokit.Tests.Integration { public class TheGetMethod { - [Fact] + [IntegrationTest] public async Task ReturnsSpecifiedUser() { var github = new GitHubClient @@ -28,7 +28,7 @@ namespace Octokit.Tests.Integration public class TheCurrentMethod { - [Fact] + [IntegrationTest] public async Task ReturnsSpecifiedUser() { var github = new GitHubClient @@ -44,7 +44,7 @@ namespace Octokit.Tests.Integration public class TheUpdateMethod { - [Fact] + [IntegrationTest] public async Task FailsWhenNotAuthenticated() { var github = new GitHubClient(); @@ -59,7 +59,7 @@ namespace Octokit.Tests.Integration e.StatusCode.Should().Be(HttpStatusCode.Unauthorized); } - [Fact] + [IntegrationTest] public async Task FailsWhenAuthenticatedWithBadCredentials() { var github = new GitHubClient From 0798649a18a1e6c452eb94b39154de06383f602d Mon Sep 17 00:00:00 2001 From: half-ogre Date: Mon, 16 Sep 2013 21:09:09 -0700 Subject: [PATCH 8/8] update README for setting up integration tests --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index b9d1a761..8f970c77 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,18 @@ cd Octokit .\build.cmd ``` +## Integration Tests + +Octokit has integration tests that access the GitHub API, but they must be configured before they will be executed. +To configure the tests, create a test GitHub account (i.e., **don't use your real GitHub account**) and then set +the following two environment variables: + +- `OCTOKIT_GITHUBUSERNAME` (set this to the test account's username) +- `OCTOKIT_GITHUBPASSWORD` (set this to the test account's password) + +Once both of these are set, the integration tests will be executed both when running the `FullBuild` MSBuild target, +and when running the `Octokit.Tests.Integration` assembly through an xUnit.net-friendly test runner. + ## Problems? Octokit is 100% certified to be bug free. If you find an issue with our