Integration tests

Added some helpers for getting an authenticated application GitHub
client.
This commit is contained in:
Henrik Andersson
2015-03-15 23:04:52 +10:00
parent 9faaf53304
commit 2078216f7a
2 changed files with 129 additions and 0 deletions
@@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
using Octokit.Tests.Helpers;
using Xunit;
namespace Octokit.Tests.Integration.Clients
@@ -97,5 +98,111 @@ namespace Octokit.Tests.Integration.Clients
await client.Authorization.Delete(created.Id);
}
[ApplicationTest]
public async Task CanCheckApplicationAuthentication()
{
var client = Helper.GetAuthenticatedClient();
var fingerprint = Helper.MakeNameWithTimestamp("authorization-testing");
var note = Helper.MakeNameWithTimestamp("Testing authentication");
var newAuthorization = new NewAuthorization(
note,
new[] { "user" },
fingerprint);
var created = await client.Authorization.GetOrCreateApplicationAuthentication(
Helper.ClientId,
Helper.ClientSecret,
newAuthorization);
var applicationClient = Helper.GetAuthenticatedApplicationClient();
var applicationAuthorization = await applicationClient.Authorization.CheckApplicationAuthentication(Helper.ClientId, created.Token);
Assert.NotNull(applicationAuthorization);
Assert.Equal(created.Token, applicationAuthorization.Token);
await client.Authorization.Delete(created.Id);
}
[ApplicationTest]
public async Task CanResetApplicationAuthentication()
{
var client = Helper.GetAuthenticatedClient();
var fingerprint = Helper.MakeNameWithTimestamp("authorization-testing");
var note = Helper.MakeNameWithTimestamp("Testing authentication");
var newAuthorization = new NewAuthorization(
note,
new[] { "user" },
fingerprint);
var created = await client.Authorization.GetOrCreateApplicationAuthentication(
Helper.ClientId,
Helper.ClientSecret,
newAuthorization);
var applicationClient = Helper.GetAuthenticatedApplicationClient();
var applicationAuthorization = await applicationClient.Authorization.ResetApplicationAuthentication(Helper.ClientId, created.Token);
Assert.NotNull(applicationAuthorization);
Assert.NotEqual(created.Token, applicationAuthorization.Token);
await client.Authorization.Delete(created.Id);
}
[ApplicationTest]
public async Task CanRevokeApplicationAuthentication()
{
var client = Helper.GetAuthenticatedClient();
var fingerprint = Helper.MakeNameWithTimestamp("authorization-testing");
var note = Helper.MakeNameWithTimestamp("Testing authentication");
var newAuthorization = new NewAuthorization(
note,
new[] { "user" },
fingerprint);
var created = await client.Authorization.GetOrCreateApplicationAuthentication(
Helper.ClientId,
Helper.ClientSecret,
newAuthorization);
var applicationClient = Helper.GetAuthenticatedApplicationClient();
await applicationClient.Authorization.RevokeApplicationAuthentication(Helper.ClientId, created.Token);
AssertEx.Throws<NotFoundException>(async () => await applicationClient.Authorization.CheckApplicationAuthentication(Helper.ClientId, created.Token));
}
[ApplicationTest]
public async Task CanRevokeAllApplicationAuthentications()
{
var client = Helper.GetAuthenticatedClient();
var fingerprint = Helper.MakeNameWithTimestamp("authorization-testing");
var note = Helper.MakeNameWithTimestamp("Testing authentication");
var token1 = await client.Authorization.GetOrCreateApplicationAuthentication(
Helper.ClientId,
Helper.ClientSecret,
new NewAuthorization(
note,
new[] { "user" },
fingerprint));
fingerprint = Helper.MakeNameWithTimestamp("authorization-testing-2");
note = Helper.MakeNameWithTimestamp("Testing authentication 2");
var token2 = await client.Authorization.GetOrCreateApplicationAuthentication(
Helper.ClientId,
Helper.ClientSecret,
new NewAuthorization(
note,
new[] { "user" },
fingerprint));
var applicationClient = Helper.GetAuthenticatedApplicationClient();
await applicationClient.Authorization.RevokeAllApplicationAuthentications(Helper.ClientId);
AssertEx.Throws<NotFoundException>(async () =>
await applicationClient.Authorization.CheckApplicationAuthentication(Helper.ClientId, token1.Token));
AssertEx.Throws<NotFoundException>(async () =>
await applicationClient.Authorization.CheckApplicationAuthentication(Helper.ClientId, token2.Token));
}
}
}
+22
View File
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Security.Policy;
namespace Octokit.Tests.Integration
{
@@ -25,6 +26,17 @@ namespace Octokit.Tests.Integration
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 Helper()
{
// Force reading of environment variables.
@@ -38,6 +50,8 @@ namespace Octokit.Tests.Integration
public static Credentials Credentials { get { return _credentialsThunk.Value; }}
public static Credentials ApplicationCredentials { get { return _oauthApplicationCredentials.Value; } }
public static bool IsPaidAccount
{
get
@@ -97,6 +111,14 @@ namespace Octokit.Tests.Integration
};
}
public static GitHubClient GetAuthenticatedApplicationClient()
{
return new GitHubClient(new ProductHeaderValue("OctokitTests"))
{
Credentials = ApplicationCredentials
};
}
public static IGitHubClient GetAnonymousClient()
{
return new GitHubClient(new ProductHeaderValue("OctokitTests"));