Skip to content

Commit 4c3a0d3

Browse files
author
Alex Taylor
committed
added Authentication Check
Added additional authentication checks on gitHubBeforeAfter so that cleanup is done with a user logged in
1 parent 7c495c2 commit 4c3a0d3

16 files changed

Lines changed: 39 additions & 37 deletions

src/test/java/org/kohsuke/github/AbstractGitHubWireMockTest.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,7 @@ public abstract class AbstractGitHubWireMockTest {
3838
*/
3939
protected GitHub gitHub;
4040

41-
/**
42-
* {@link GitHub} instance for use before/after test. Traffic will not be part of snapshot when taken. Should only
43-
* be used when isUseProxy() or isTakeSnapShot().
44-
*/
45-
protected GitHub gitHubBeforeAfter;
41+
private GitHub gitHubBeforeAfter;
4642

4743
protected final String baseFilesClassPath = this.getClass().getName().replace('.', '/');
4844
protected final String baseRecordPath = "src/test/resources/" + baseFilesClassPath + "/wiremock";
@@ -127,10 +123,10 @@ protected void requireProxy(String reason) {
127123
mockGitHub.isUseProxy());
128124
}
129125

130-
protected void verifyAuthenticated() {
126+
protected void verifyAuthenticated(GitHub instance) {
131127
assertThat(
132128
"GitHub connection believes it is anonymous. Make sure you set GITHUB_OAUTH or both GITHUB_USER and GITHUB_PASSWORD environment variables",
133-
gitHub.isAnonymous(),
129+
instance.isAnonymous(),
134130
is(false));
135131
}
136132

@@ -172,12 +168,9 @@ protected GHRepository getTempRepository(String name) throws IOException {
172168
String fullName = GITHUB_API_TEST_ORG + '/' + name;
173169
if (mockGitHub.isUseProxy()) {
174170

175-
// Needs to check if you are authenticated before doing this cleanup and repo creation
176-
verifyAuthenticated();
177-
178171
cleanupRepository(fullName);
179172

180-
GHRepository repository = gitHubBeforeAfter.getOrganization(GITHUB_API_TEST_ORG)
173+
GHRepository repository = getGitHubBeforeAfter().getOrganization(GITHUB_API_TEST_ORG)
181174
.createRepository(name)
182175
.description("A test repository for testing the github-api project: " + name)
183176
.homepage("http://github-api.kohsuke.org/")
@@ -211,7 +204,7 @@ protected void cleanupRepository(String fullName) throws IOException {
211204
if (mockGitHub.isUseProxy()) {
212205
tempGitHubRepositories.add(fullName);
213206
try {
214-
GHRepository repository = gitHubBeforeAfter.getRepository(fullName);
207+
GHRepository repository = getGitHubBeforeAfter().getRepository(fullName);
215208
if (repository != null) {
216209
repository.delete();
217210
}
@@ -222,6 +215,15 @@ protected void cleanupRepository(String fullName) throws IOException {
222215
}
223216
}
224217

218+
/**
219+
* {@link GitHub} instance for use before/after test. Traffic will not be part of snapshot when taken. Should only
220+
* be used when isUseProxy() or isTakeSnapShot().
221+
*/
222+
public GitHub getGitHubBeforeAfter() {
223+
verifyAuthenticated(gitHubBeforeAfter);
224+
return gitHubBeforeAfter;
225+
}
226+
225227
protected void kohsuke() {
226228
// No-op for now
227229
// Generally this means the test is doing something that requires additional access rights

src/test/java/org/kohsuke/github/AppTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void testRepositoryWithAutoInitializationCRUD() throws Exception {
7474

7575
private void cleanupUserRepository(final String name) throws IOException {
7676
if (mockGitHub.isUseProxy()) {
77-
cleanupRepository(getUser(gitHubBeforeAfter).getLogin() + "/" + name);
77+
cleanupRepository(getUser(getGitHubBeforeAfter()).getLogin() + "/" + name);
7878
}
7979
}
8080

@@ -437,7 +437,7 @@ public void tryHook() throws Exception {
437437
// System.out.println(hook);
438438

439439
if (mockGitHub.isUseProxy()) {
440-
r = gitHubBeforeAfter.getOrganization(GITHUB_API_TEST_ORG).getRepository("github-api");
440+
r = getGitHubBeforeAfter().getOrganization(GITHUB_API_TEST_ORG).getRepository("github-api");
441441
for (GHHook h : r.getHooks()) {
442442
h.delete();
443443
}
@@ -826,7 +826,7 @@ public void testRepoLabel() throws IOException {
826826
void cleanupLabel(String name) {
827827
if (mockGitHub.isUseProxy()) {
828828
try {
829-
GHLabel t = gitHubBeforeAfter.getRepository("github-api-test-org/test-labels").getLabel("test");
829+
GHLabel t = getGitHubBeforeAfter().getRepository("github-api-test-org/test-labels").getLabel("test");
830830
t.delete();
831831
} catch (IOException e) {
832832

src/test/java/org/kohsuke/github/GHContentIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class GHContentIntegrationTest extends AbstractGitHubWireMockTest {
3232
@After
3333
public void cleanup() throws Exception {
3434
if (mockGitHub.isUseProxy()) {
35-
repo = gitHubBeforeAfter.getRepository("github-api-test-org/GHContentIntegrationTest");
35+
repo = getGitHubBeforeAfter().getRepository("github-api-test-org/GHContentIntegrationTest");
3636
try {
3737
GHContent content = repo.getFileContent(createdFilename);
3838
if (content != null) {

src/test/java/org/kohsuke/github/GHMilestoneTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void cleanUp() throws Exception {
2222
return;
2323
}
2424

25-
for (GHMilestone milestone : getRepository(gitHubBeforeAfter).listMilestones(GHIssueState.ALL)) {
25+
for (GHMilestone milestone : getRepository(getGitHubBeforeAfter()).listMilestones(GHIssueState.ALL)) {
2626
if ("Original Title".equals(milestone.getTitle()) || "Updated Title".equals(milestone.getTitle())) {
2727
milestone.delete();
2828
}

src/test/java/org/kohsuke/github/GHOrganizationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void cleanUpTeam() throws IOException {
2323
return;
2424
}
2525

26-
GHTeam team = gitHubBeforeAfter.getOrganization(GITHUB_API_TEST_ORG).getTeamByName(TEAM_NAME_CREATE);
26+
GHTeam team = getGitHubBeforeAfter().getOrganization(GITHUB_API_TEST_ORG).getTeamByName(TEAM_NAME_CREATE);
2727
if (team != null) {
2828
team.delete();
2929
}

src/test/java/org/kohsuke/github/GHProjectCardTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void testDeleteCard() throws IOException {
7474
public void after() throws IOException {
7575
if (mockGitHub.isUseProxy()) {
7676
if (card != null) {
77-
card = gitHubBeforeAfter.getProjectCard(card.getId());
77+
card = getGitHubBeforeAfter().getProjectCard(card.getId());
7878
try {
7979
card.delete();
8080
card = null;
@@ -83,7 +83,7 @@ public void after() throws IOException {
8383
}
8484
}
8585
if (column != null) {
86-
column = gitHubBeforeAfter.getProjectColumn(column.getId());
86+
column = getGitHubBeforeAfter().getProjectColumn(column.getId());
8787
try {
8888
column.delete();
8989
column = null;
@@ -92,7 +92,7 @@ public void after() throws IOException {
9292
}
9393
}
9494
if (project != null) {
95-
project = gitHubBeforeAfter.getProject(project.getId());
95+
project = getGitHubBeforeAfter().getProject(project.getId());
9696
try {
9797
project.delete();
9898
project = null;

src/test/java/org/kohsuke/github/GHProjectColumnTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void testDeleteColumn() throws IOException {
4848
public void after() throws IOException {
4949
if (mockGitHub.isUseProxy()) {
5050
if (column != null) {
51-
column = gitHubBeforeAfter.getProjectColumn(column.getId());
51+
column = getGitHubBeforeAfter().getProjectColumn(column.getId());
5252
try {
5353
column.delete();
5454
column = null;
@@ -57,7 +57,7 @@ public void after() throws IOException {
5757
}
5858
}
5959
if (project != null) {
60-
project = gitHubBeforeAfter.getProject(project.getId());
60+
project = getGitHubBeforeAfter().getProject(project.getId());
6161
try {
6262
project.delete();
6363
project = null;

src/test/java/org/kohsuke/github/GHProjectTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void testDeleteProject() throws IOException {
6969
public void after() throws IOException {
7070
if (mockGitHub.isUseProxy()) {
7171
if (project != null) {
72-
project = gitHubBeforeAfter.getProject(project.getId());
72+
project = getGitHubBeforeAfter().getProject(project.getId());
7373
try {
7474
project.delete();
7575
project = null;

src/test/java/org/kohsuke/github/GHPullRequestTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void cleanUp() throws Exception {
3030
return;
3131
}
3232

33-
for (GHPullRequest pr : getRepository(this.gitHubBeforeAfter).getPullRequests(GHIssueState.OPEN)) {
33+
for (GHPullRequest pr : getRepository(this.getGitHubBeforeAfter()).getPullRequests(GHIssueState.OPEN)) {
3434
pr.close();
3535
}
3636
}

src/test/java/org/kohsuke/github/GHTagTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void cleanUpTags() throws Exception {
2424
}
2525

2626
try {
27-
GHRef ref = getRepository(this.gitHubBeforeAfter).getRef("tags/create_tag_test");
27+
GHRef ref = getRepository(this.getGitHubBeforeAfter()).getRef("tags/create_tag_test");
2828
if (ref != null) {
2929
ref.delete();
3030
}

0 commit comments

Comments
 (0)