Files
octokit.net/Octokit.Tests.Integration/Helper.cs
Travis Harris ff9521ce3d Implement Review API for Pull Requests (#1648)
* First Iteration Need to finish tests and docs

* Mostly Complete

* Fixing tests and adding review comments

* Added tests for reactive client

* Moved Reviews inside fo the Pull request client for better organization and began initial intigration testing

* Fixing bad recursive function breaking tests

* test fixes

* Add paging support to review comments call

* Fixing recursive function

* Addressing comments from PR

* fixing CI break

* Typo build break

* Fixing Convention Tests

* Adding correct nameof() usage in Ensure

* Small consitancy changes

* Trigger build

* Address PR Comments

* Fixup test naming

* Fix sub client ordering and incorrect URL

* Tidy up comments and remove StringEnum wrapper from Request models as it is only for Response models

* Rename GetReview to Get

* tweak debugger display

* Rework integration tests - implement the easy Get/GetAll ones first...

* Implement integration tests for Create method.
Move helpers to create PR/review into SetupHelper class
Fixed up review status enum to contain correct values
Tests for Approve/RequestChanges currently failing as a user cant approve/request changes on their own PR

* Implement secondary account settings for integration tests and a new [DualAccountTest] attribute for discovery when configured
Change integration test to create PR from the 2nd account, so the main test account is able to perform review actions on the PR

* Add integration tests for Delete, Dismiss and Submit methods
Fixed up API client implementation for delete (was looking for incorrect 201 http status)
Removed unnecessary await/async calls from client implementations that dont need to do anything with the result

* Attempting to add comments as part of a review revealed that we cant use the existing PullRequestReviewCommentCreate class as the API throws a validation error due to the CommitId field
These newer review APIs need a DraftPullRequestReviewComment (that doesnt have a commitId) instead

* add second test account user/password to configure-integration-tests script
2017-08-16 20:50:25 +10:00

272 lines
9.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
namespace Octokit.Tests.Integration
{
public static class Helper
{
static readonly Lazy<Credentials> _credentialsThunk = new Lazy<Credentials>(() =>
{
var githubUsername = Environment.GetEnvironmentVariable("OCTOKIT_GITHUBUSERNAME");
UserName = githubUsername;
Organization = Environment.GetEnvironmentVariable("OCTOKIT_GITHUBORGANIZATION");
var githubToken = Environment.GetEnvironmentVariable("OCTOKIT_OAUTHTOKEN");
if (githubToken != null)
return new Credentials(githubToken);
var githubPassword = Environment.GetEnvironmentVariable("OCTOKIT_GITHUBPASSWORD");
if (githubUsername == null || githubPassword == null)
return null;
return new Credentials(githubUsername, githubPassword);
});
static readonly Lazy<Credentials> _credentialsSecondUserThunk = new Lazy<Credentials>(() =>
{
var githubUsername = Environment.GetEnvironmentVariable("OCTOKIT_GITHUBUSERNAME_2");
var githubPassword = Environment.GetEnvironmentVariable("OCTOKIT_GITHUBPASSWORD_2");
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_GITHUBUSERNAME");
UserName = githubUsername;
Organization = Environment.GetEnvironmentVariable("OCTOKIT_GITHUBORGANIZATION");
var githubPassword = Environment.GetEnvironmentVariable("OCTOKIT_GITHUBPASSWORD");
if (githubUsername == null || githubPassword == null)
return null;
return new Credentials(githubUsername, githubPassword);
});
static readonly Lazy<Uri> _customUrl = new Lazy<Uri>(() =>
{
string uri = Environment.GetEnvironmentVariable("OCTOKIT_CUSTOMURL");
if (uri != null)
return new Uri(uri);
return null;
});
static Helper()
{
// Force reading of environment variables.
// This wasn't happening if UserName/Organization were
// retrieved before Credentials.
Debug.WriteIf(Credentials == null, "No credentials specified.");
}
public static string UserName { get; private set; }
public static string Organization { 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 Credentials { get { return _credentialsThunk.Value; } }
public static Credentials CredentialsSecondUser { get { return _credentialsSecondUserThunk.Value; } }
public static Credentials ApplicationCredentials { get { return _oauthApplicationCredentials.Value; } }
public static Credentials BasicAuthCredentials { get { return _basicAuthCredentials.Value; } }
public static Uri CustomUrl { get { return _customUrl.Value; } }
public static Uri TargetUrl { get { return CustomUrl ?? GitHubClient.GitHubApiUrl; } }
public static bool IsUsingToken
{
get
{
return !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("OCTOKIT_OAUTHTOKEN"));
}
}
public static bool IsPaidAccount
{
get
{
return !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("OCTOKIT_PRIVATEREPOSITORIES"));
}
}
public static string ClientId
{
get { return Environment.GetEnvironmentVariable("OCTOKIT_CLIENTID"); }
}
public static string ClientSecret
{
get { return Environment.GetEnvironmentVariable("OCTOKIT_CLIENTSECRET"); }
}
public static void DeleteRepo(IConnection connection, Repository repository)
{
if (repository != null)
DeleteRepo(connection, repository.Owner.Login, repository.Name);
}
public static void DeleteRepo(IConnection connection, string owner, string name)
{
try
{
var client = new GitHubClient(connection);
client.Repository.Delete(owner, name).Wait(TimeSpan.FromSeconds(15));
}
catch { }
}
public static void DeleteTeam(IConnection connection, Team team)
{
if (team != null)
DeleteTeam(connection, team.Id);
}
public static void DeleteTeam(IConnection connection, int teamId)
{
try
{
var client = new GitHubClient(connection);
client.Organization.Team.Delete(teamId).Wait(TimeSpan.FromSeconds(15));
}
catch { }
}
public static void DeleteKey(IConnection connection, PublicKey key)
{
if (key != null)
DeleteKey(connection, key.Id);
}
public static void DeleteKey(IConnection connection, int keyId)
{
try
{
var client = new GitHubClient(connection);
client.User.GitSshKey.Delete(keyId).Wait(TimeSpan.FromSeconds(15));
}
catch { }
}
public static void DeleteGpgKey(IConnection connection, GpgKey key)
{
if (key != null)
DeleteGpgKey(connection, key.Id);
}
public static void DeleteGpgKey(IConnection connection, int keyId)
{
try
{
var client = new GitHubClient(connection);
client.User.GpgKey.Delete(keyId).Wait(TimeSpan.FromSeconds(15));
}
catch { }
}
public static string MakeNameWithTimestamp(string name)
{
return string.Concat(name, "-", DateTime.UtcNow.ToString("yyyyMMddhhmmssfff"));
}
public static Stream LoadFixture(string fileName)
{
var key = "Octokit.Tests.Integration.fixtures." + fileName;
var stream = typeof(Helper).GetTypeInfo().Assembly.GetManifestResourceStream(key);
if (stream == null)
{
throw new InvalidOperationException(
"The file '" + fileName + "' was not found as an embedded resource in the assembly. Failing the test...");
}
return stream;
}
public static IGitHubClient GetAuthenticatedClient(bool useSecondUser = false)
{
return new GitHubClient(new ProductHeaderValue("OctokitTests"), TargetUrl)
{
Credentials = useSecondUser ? CredentialsSecondUser : Credentials
};
}
public static IGitHubClient GetBasicAuthClient()
{
return new GitHubClient(new ProductHeaderValue("OctokitTests"), TargetUrl)
{
Credentials = BasicAuthCredentials
};
}
public static GitHubClient GetAuthenticatedApplicationClient()
{
return new GitHubClient(new ProductHeaderValue("OctokitTests"), TargetUrl)
{
Credentials = ApplicationCredentials
};
}
public static IGitHubClient GetAnonymousClient()
{
return new GitHubClient(new ProductHeaderValue("OctokitTests"), TargetUrl);
}
public static IGitHubClient GetBadCredentialsClient()
{
return new GitHubClient(new ProductHeaderValue("OctokitTests"), TargetUrl)
{
Credentials = new Credentials(Guid.NewGuid().ToString(), "bad-password")
};
}
public static void DeleteInvitations(IConnection connection, List<string> invitees, int teamId)
{
try
{
foreach (var invitee in invitees)
{
connection.Delete(new Uri($"orgs/{Organization}/memberships/{invitee}", UriKind.Relative), null, AcceptHeaders.OrganizationMembershipPreview).Wait(TimeSpan.FromSeconds(15));
}
}
catch { }
}
public static string InviteMemberToTeam(IConnection connection, int teamId, string login)
{
try
{
var client = new GitHubClient(connection);
client.Organization.Team.AddMembership(teamId, login).Wait(TimeSpan.FromSeconds(15));
}
catch { }
return login;
}
}
}