Skip to content

Commit

Permalink
Merge pull request #684 from alexanderrtaylor/assertThatIssues
Browse files Browse the repository at this point in the history
Authentication and assertThat issues
  • Loading branch information
bitwiseman authored Jan 30, 2020
2 parents 9b3fe3b + 303aef3 commit 79fb343
Show file tree
Hide file tree
Showing 28 changed files with 105 additions and 48 deletions.
57 changes: 50 additions & 7 deletions src/test/java/org/kohsuke/github/AbstractGitHubWireMockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import org.apache.commons.io.IOUtils;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.StringDescription;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
Expand All @@ -13,6 +16,7 @@
import java.io.IOException;
import java.util.*;

import static org.hamcrest.Matchers.is;
import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeTrue;

Expand All @@ -37,11 +41,7 @@ public abstract class AbstractGitHubWireMockTest extends Assert {
*/
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";
Expand Down Expand Up @@ -126,6 +126,13 @@ protected void requireProxy(String reason) {
mockGitHub.isUseProxy());
}

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",
instance.isAnonymous(),
is(false));
}

protected GHUser getUser() {
return getUser(gitHub);
}
Expand Down Expand Up @@ -163,9 +170,10 @@ protected GHRepository getTempRepository() throws IOException {
protected GHRepository getTempRepository(String name) throws IOException {
String fullName = GITHUB_API_TEST_ORG + '/' + name;
if (mockGitHub.isUseProxy()) {

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/")
Expand Down Expand Up @@ -199,7 +207,7 @@ protected void cleanupRepository(String fullName) throws IOException {
if (mockGitHub.isUseProxy()) {
tempGitHubRepositories.add(fullName);
try {
GHRepository repository = gitHubBeforeAfter.getRepository(fullName);
GHRepository repository = getGitHubBeforeAfter().getRepository(fullName);
if (repository != null) {
repository.delete();
}
Expand All @@ -210,6 +218,17 @@ protected void cleanupRepository(String fullName) throws IOException {
}
}

/**
* {@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().
*
* @return a github instance after checking Authentication
*/
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
Expand All @@ -219,4 +238,28 @@ protected void kohsuke() {
// assumeTrue(login.equals("kohsuke") || login.equals("kohsuke2"));
}

public static <T> void assertThat(T actual, Matcher<? super T> matcher) {
assertThat("", actual, matcher);
}

public static <T> void assertThat(String reason, T actual, Matcher<? super T> matcher) {
if (!matcher.matches(actual)) {
Description description = new StringDescription();
description.appendText(reason)
.appendText(System.lineSeparator())
.appendText("Expected: ")
.appendDescriptionOf(matcher)
.appendText(System.lineSeparator())
.appendText(" but: ");
matcher.describeMismatch(actual, description);
throw new AssertionError(description.toString());
}
}

public static void assertThat(String reason, boolean assertion) {
if (!assertion) {
throw new AssertionError(reason);
}
}

}
7 changes: 3 additions & 4 deletions src/test/java/org/kohsuke/github/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.regex.Pattern;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasProperty;

/**
Expand Down Expand Up @@ -69,7 +68,7 @@ public void testRepositoryWithAutoInitializationCRUD() throws Exception {

private void cleanupUserRepository(final String name) throws IOException {
if (mockGitHub.isUseProxy()) {
cleanupRepository(getUser(gitHubBeforeAfter).getLogin() + "/" + name);
cleanupRepository(getUser(getGitHubBeforeAfter()).getLogin() + "/" + name);
}
}

Expand Down Expand Up @@ -432,7 +431,7 @@ public void tryHook() throws Exception {
// 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();
}
Expand Down Expand Up @@ -821,7 +820,7 @@ public void testRepoLabel() throws IOException {
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) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,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) {
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/org/kohsuke/github/GHDeploymentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import java.io.IOException;

import static org.junit.Assert.assertNotNull;

/**
* @author Martin van Zijl
*/
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/kohsuke/github/GHEventPayloadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;

public class GHEventPayloadTest {

Expand Down
1 change: 0 additions & 1 deletion src/test/java/org/kohsuke/github/GHGistTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.junit.Test;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.core.Is.is;

Expand Down
4 changes: 0 additions & 4 deletions src/test/java/org/kohsuke/github/GHGistUpdaterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
import java.io.IOException;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* @author Martin van Zijl
*/
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/kohsuke/github/GHHookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertThat;

/**
* @author Kanstantsin Shautsou
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/org/kohsuke/github/GHMarketplacePlanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
import java.io.IOException;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.kohsuke.github.GHDirection.DESC;
import static org.kohsuke.github.GHMarketplaceAccountType.ORGANIZATION;
import static org.kohsuke.github.GHMarketplaceListAccountBuilder.Sort.UPDATED;
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/org/kohsuke/github/GHMilestoneTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.io.IOException;
import java.util.Date;

import static org.junit.Assert.assertEquals;

/**
* @author Martin van Zijl
*/
Expand All @@ -20,7 +22,7 @@ public void cleanUp() throws Exception {
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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/kohsuke/github/GHObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ public void test_toString() throws Exception {
containsString(
"login=github-api-test-org,location=<null>,blog=<null>,email=<null>,name=<null>,company=<null>,type=Organization,followers=0,following=0"));
}
}
}
2 changes: 1 addition & 1 deletion src/test/java/org/kohsuke/github/GHOrganizationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void cleanUpTeam() throws IOException {
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();
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/kohsuke/github/GHProjectCardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void testDeleteCard() throws IOException {
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;
Expand All @@ -83,7 +83,7 @@ public void after() throws IOException {
}
}
if (column != null) {
column = gitHubBeforeAfter.getProjectColumn(column.getId());
column = getGitHubBeforeAfter().getProjectColumn(column.getId());
try {
column.delete();
column = null;
Expand All @@ -92,7 +92,7 @@ public void after() throws IOException {
}
}
if (project != null) {
project = gitHubBeforeAfter.getProject(project.getId());
project = getGitHubBeforeAfter().getProject(project.getId());
try {
project.delete();
project = null;
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/kohsuke/github/GHProjectColumnTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void testDeleteColumn() throws IOException {
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;
Expand All @@ -57,7 +57,7 @@ public void after() throws IOException {
}
}
if (project != null) {
project = gitHubBeforeAfter.getProject(project.getId());
project = getGitHubBeforeAfter().getProject(project.getId());
try {
project.delete();
project = null;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/kohsuke/github/GHProjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void testDeleteProject() throws IOException {
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;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/kohsuke/github/GHPullRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void cleanUp() throws Exception {
return;
}

for (GHPullRequest pr : getRepository(this.gitHubBeforeAfter).getPullRequests(GHIssueState.OPEN)) {
for (GHPullRequest pr : getRepository(this.getGitHubBeforeAfter()).getPullRequests(GHIssueState.OPEN)) {
pr.close();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/kohsuke/github/GHRateLimitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.core.IsInstanceOf.instanceOf;

/**
* Test showing the behavior of OkHttpConnector with and without cache.
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/kohsuke/github/GHTagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void cleanUpTags() throws Exception {
}

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();
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/org/kohsuke/github/GHTeamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import java.io.IOException;

import static org.junit.Assert.assertEquals;

public class GHTeamTest extends AbstractGitHubWireMockTest {

@Test
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/org/kohsuke/github/GHTreeBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.io.IOException;
import java.util.Arrays;

import static org.junit.Assert.assertEquals;

public class GHTreeBuilderTest extends AbstractGitHubWireMockTest {
private static String REPO_NAME = "github-api-test-org/GHTreeBuilderTest";

Expand All @@ -31,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);
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/org/kohsuke/github/GHUserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
import java.util.*;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class GHUserTest extends AbstractGitHubWireMockTest {
@Test
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/org/kohsuke/github/GitHubStaticTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.kohsuke.github;

import org.junit.Assert;
import org.junit.Test;

import java.text.SimpleDateFormat;
Expand All @@ -18,7 +17,7 @@
*
* @author Liam Newman
*/
public class GitHubStaticTest extends Assert {
public class GitHubStaticTest extends AbstractGitHubWireMockTest {

@Test
public void timeRoundTrip() throws Exception {
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/org/kohsuke/github/Github2faTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* @author Kevin Harrington [email protected]
*/
Expand Down
Loading

0 comments on commit 79fb343

Please sign in to comment.