added Authentication Check

Added additional authentication checks on gitHubBeforeAfter so that cleanup is done with a user logged in
This commit is contained in:
Alex Taylor
2020-01-27 15:24:28 -05:00
parent 7c495c2177
commit 4c3a0d329b
16 changed files with 39 additions and 37 deletions

View File

@@ -38,11 +38,7 @@ public abstract class AbstractGitHubWireMockTest {
*/
protected GitHub gitHub;
/**
* {@link GitHub} instance for use before/after test. Traffic will not be part of snapshot when taken. Should only
* be used when isUseProxy() or isTakeSnapShot().
*/
protected GitHub gitHubBeforeAfter;
private GitHub gitHubBeforeAfter;
protected final String baseFilesClassPath = this.getClass().getName().replace('.', '/');
protected final String baseRecordPath = "src/test/resources/" + baseFilesClassPath + "/wiremock";
@@ -127,10 +123,10 @@ public abstract class AbstractGitHubWireMockTest {
mockGitHub.isUseProxy());
}
protected void verifyAuthenticated() {
protected void verifyAuthenticated(GitHub instance) {
assertThat(
"GitHub connection believes it is anonymous. Make sure you set GITHUB_OAUTH or both GITHUB_USER and GITHUB_PASSWORD environment variables",
gitHub.isAnonymous(),
instance.isAnonymous(),
is(false));
}
@@ -172,12 +168,9 @@ public abstract class AbstractGitHubWireMockTest {
String fullName = GITHUB_API_TEST_ORG + '/' + name;
if (mockGitHub.isUseProxy()) {
// Needs to check if you are authenticated before doing this cleanup and repo creation
verifyAuthenticated();
cleanupRepository(fullName);
GHRepository repository = gitHubBeforeAfter.getOrganization(GITHUB_API_TEST_ORG)
GHRepository repository = getGitHubBeforeAfter().getOrganization(GITHUB_API_TEST_ORG)
.createRepository(name)
.description("A test repository for testing the github-api project: " + name)
.homepage("http://github-api.kohsuke.org/")
@@ -211,7 +204,7 @@ public abstract class AbstractGitHubWireMockTest {
if (mockGitHub.isUseProxy()) {
tempGitHubRepositories.add(fullName);
try {
GHRepository repository = gitHubBeforeAfter.getRepository(fullName);
GHRepository repository = getGitHubBeforeAfter().getRepository(fullName);
if (repository != null) {
repository.delete();
}
@@ -222,6 +215,15 @@ public abstract class AbstractGitHubWireMockTest {
}
}
/**
* {@link GitHub} instance for use before/after test. Traffic will not be part of snapshot when taken. Should only
* be used when isUseProxy() or isTakeSnapShot().
*/
public GitHub getGitHubBeforeAfter() {
verifyAuthenticated(gitHubBeforeAfter);
return gitHubBeforeAfter;
}
protected void kohsuke() {
// No-op for now
// Generally this means the test is doing something that requires additional access rights

View File

@@ -74,7 +74,7 @@ public class AppTest extends AbstractGitHubWireMockTest {
private void cleanupUserRepository(final String name) throws IOException {
if (mockGitHub.isUseProxy()) {
cleanupRepository(getUser(gitHubBeforeAfter).getLogin() + "/" + name);
cleanupRepository(getUser(getGitHubBeforeAfter()).getLogin() + "/" + name);
}
}
@@ -437,7 +437,7 @@ public class AppTest extends AbstractGitHubWireMockTest {
// System.out.println(hook);
if (mockGitHub.isUseProxy()) {
r = gitHubBeforeAfter.getOrganization(GITHUB_API_TEST_ORG).getRepository("github-api");
r = getGitHubBeforeAfter().getOrganization(GITHUB_API_TEST_ORG).getRepository("github-api");
for (GHHook h : r.getHooks()) {
h.delete();
}
@@ -826,7 +826,7 @@ public class AppTest extends AbstractGitHubWireMockTest {
void cleanupLabel(String name) {
if (mockGitHub.isUseProxy()) {
try {
GHLabel t = gitHubBeforeAfter.getRepository("github-api-test-org/test-labels").getLabel("test");
GHLabel t = getGitHubBeforeAfter().getRepository("github-api-test-org/test-labels").getLabel("test");
t.delete();
} catch (IOException e) {

View File

@@ -32,7 +32,7 @@ public class GHContentIntegrationTest extends AbstractGitHubWireMockTest {
@After
public void cleanup() throws Exception {
if (mockGitHub.isUseProxy()) {
repo = gitHubBeforeAfter.getRepository("github-api-test-org/GHContentIntegrationTest");
repo = getGitHubBeforeAfter().getRepository("github-api-test-org/GHContentIntegrationTest");
try {
GHContent content = repo.getFileContent(createdFilename);
if (content != null) {

View File

@@ -22,7 +22,7 @@ public class GHMilestoneTest extends AbstractGitHubWireMockTest {
return;
}
for (GHMilestone milestone : getRepository(gitHubBeforeAfter).listMilestones(GHIssueState.ALL)) {
for (GHMilestone milestone : getRepository(getGitHubBeforeAfter()).listMilestones(GHIssueState.ALL)) {
if ("Original Title".equals(milestone.getTitle()) || "Updated Title".equals(milestone.getTitle())) {
milestone.delete();
}

View File

@@ -23,7 +23,7 @@ public class GHOrganizationTest extends AbstractGitHubWireMockTest {
return;
}
GHTeam team = gitHubBeforeAfter.getOrganization(GITHUB_API_TEST_ORG).getTeamByName(TEAM_NAME_CREATE);
GHTeam team = getGitHubBeforeAfter().getOrganization(GITHUB_API_TEST_ORG).getTeamByName(TEAM_NAME_CREATE);
if (team != null) {
team.delete();
}

View File

@@ -74,7 +74,7 @@ public class GHProjectCardTest extends AbstractGitHubWireMockTest {
public void after() throws IOException {
if (mockGitHub.isUseProxy()) {
if (card != null) {
card = gitHubBeforeAfter.getProjectCard(card.getId());
card = getGitHubBeforeAfter().getProjectCard(card.getId());
try {
card.delete();
card = null;
@@ -83,7 +83,7 @@ public class GHProjectCardTest extends AbstractGitHubWireMockTest {
}
}
if (column != null) {
column = gitHubBeforeAfter.getProjectColumn(column.getId());
column = getGitHubBeforeAfter().getProjectColumn(column.getId());
try {
column.delete();
column = null;
@@ -92,7 +92,7 @@ public class GHProjectCardTest extends AbstractGitHubWireMockTest {
}
}
if (project != null) {
project = gitHubBeforeAfter.getProject(project.getId());
project = getGitHubBeforeAfter().getProject(project.getId());
try {
project.delete();
project = null;

View File

@@ -48,7 +48,7 @@ public class GHProjectColumnTest extends AbstractGitHubWireMockTest {
public void after() throws IOException {
if (mockGitHub.isUseProxy()) {
if (column != null) {
column = gitHubBeforeAfter.getProjectColumn(column.getId());
column = getGitHubBeforeAfter().getProjectColumn(column.getId());
try {
column.delete();
column = null;
@@ -57,7 +57,7 @@ public class GHProjectColumnTest extends AbstractGitHubWireMockTest {
}
}
if (project != null) {
project = gitHubBeforeAfter.getProject(project.getId());
project = getGitHubBeforeAfter().getProject(project.getId());
try {
project.delete();
project = null;

View File

@@ -69,7 +69,7 @@ public class GHProjectTest extends AbstractGitHubWireMockTest {
public void after() throws IOException {
if (mockGitHub.isUseProxy()) {
if (project != null) {
project = gitHubBeforeAfter.getProject(project.getId());
project = getGitHubBeforeAfter().getProject(project.getId());
try {
project.delete();
project = null;

View File

@@ -30,7 +30,7 @@ public class GHPullRequestTest extends AbstractGitHubWireMockTest {
return;
}
for (GHPullRequest pr : getRepository(this.gitHubBeforeAfter).getPullRequests(GHIssueState.OPEN)) {
for (GHPullRequest pr : getRepository(this.getGitHubBeforeAfter()).getPullRequests(GHIssueState.OPEN)) {
pr.close();
}
}

View File

@@ -24,7 +24,7 @@ public class GHTagTest extends AbstractGitHubWireMockTest {
}
try {
GHRef ref = getRepository(this.gitHubBeforeAfter).getRef("tags/create_tag_test");
GHRef ref = getRepository(this.getGitHubBeforeAfter()).getRef("tags/create_tag_test");
if (ref != null) {
ref.delete();
}

View File

@@ -33,7 +33,7 @@ public class GHTreeBuilderTest extends AbstractGitHubWireMockTest {
@After
public void cleanup() throws Exception {
if (mockGitHub.isUseProxy()) {
repo = gitHubBeforeAfter.getRepository(REPO_NAME);
repo = getGitHubBeforeAfter().getRepository(REPO_NAME);
Arrays.asList(PATH_SCRIPT, PATH_README, PATH_DATA1, PATH_DATA2).forEach(path -> {
try {
GHContent content = repo.getFileContent(path);

View File

@@ -25,7 +25,7 @@ public class WireMockStatusReporterTest extends AbstractGitHubWireMockTest {
snapshotNotAllowed();
requireProxy("Tests proper configuration when proxying.");
verifyAuthenticated();
verifyAuthenticated(gitHub);
assertThat(gitHub.login, not(equalTo(STUBBED_USER_LOGIN)));
@@ -47,7 +47,7 @@ public class WireMockStatusReporterTest extends AbstractGitHubWireMockTest {
assumeFalse("Test only valid when not proxying", mockGitHub.isUseProxy());
verifyAuthenticated();
verifyAuthenticated(gitHub);
assertThat(gitHub.login, equalTo(STUBBED_USER_LOGIN));
GHUser user = gitHub.getMyself();

View File

@@ -43,11 +43,11 @@ public class GitHubCachingTest extends AbstractGitHubWireMockTest {
@Before
public void setupRepo() throws Exception {
if (mockGitHub.isUseProxy()) {
for (GHPullRequest pr : getRepository(this.gitHubBeforeAfter).getPullRequests(GHIssueState.OPEN)) {
for (GHPullRequest pr : getRepository(this.getGitHubBeforeAfter()).getPullRequests(GHIssueState.OPEN)) {
pr.close();
}
try {
GHRef ref = getRepository(this.gitHubBeforeAfter).getRef(testRefName);
GHRef ref = getRepository(this.getGitHubBeforeAfter()).getRef(testRefName);
ref.delete();
} catch (IOException e) {
}

View File

@@ -75,7 +75,7 @@ public class OkHttpConnectorTest extends AbstractGitHubWireMockTest {
@Before
public void setupRepo() throws Exception {
if (mockGitHub.isUseProxy()) {
GHRepository repo = getRepository(gitHubBeforeAfter);
GHRepository repo = getRepository(getGitHubBeforeAfter());
repo.setDescription("Resetting");
// Let things settle a bit between tests when working against the live site
@@ -254,7 +254,7 @@ public class OkHttpConnectorTest extends AbstractGitHubWireMockTest {
// Get Tricky - make a change via a different client
if (mockGitHub.isUseProxy()) {
GHRepository altRepo = getRepository(gitHubBeforeAfter);
GHRepository altRepo = getRepository(getGitHubBeforeAfter());
altRepo.setDescription("Tricky");
}

View File

@@ -44,11 +44,11 @@ public class GitHubCachingTest extends AbstractGitHubWireMockTest {
@Before
public void setupRepo() throws Exception {
if (mockGitHub.isUseProxy()) {
for (GHPullRequest pr : getRepository(this.gitHubBeforeAfter).getPullRequests(GHIssueState.OPEN)) {
for (GHPullRequest pr : getRepository(this.getGitHubBeforeAfter()).getPullRequests(GHIssueState.OPEN)) {
pr.close();
}
try {
GHRef ref = getRepository(this.gitHubBeforeAfter).getRef(testRefName);
GHRef ref = getRepository(this.getGitHubBeforeAfter()).getRef(testRefName);
ref.delete();
} catch (IOException e) {
}

View File

@@ -80,7 +80,7 @@ public class OkHttpConnectorTest extends AbstractGitHubWireMockTest {
@Before
public void setupRepo() throws Exception {
if (mockGitHub.isUseProxy()) {
GHRepository repo = getRepository(gitHubBeforeAfter);
GHRepository repo = getRepository(getGitHubBeforeAfter());
repo.setDescription("Resetting");
// Let things settle a bit between tests when working against the live site
@@ -262,7 +262,7 @@ public class OkHttpConnectorTest extends AbstractGitHubWireMockTest {
// Get Tricky - make a change via a different client
if (mockGitHub.isUseProxy()) {
GHRepository altRepo = getRepository(gitHubBeforeAfter);
GHRepository altRepo = getRepository(getGitHubBeforeAfter());
altRepo.setDescription("Tricky");
}