Authentication and assertThat issues

Fixed some problems with tests trying to authenticate when you are not actually signed in. This hit rate API limiting which caused tests to hang/fail

Also fixed assertThat getting deprecated from junit
This commit is contained in:
Alex Taylor
2020-01-27 14:29:46 -05:00
parent a9bb9302bc
commit 12c3a0b1fa
30 changed files with 129 additions and 10 deletions

View File

@@ -3,7 +3,6 @@ package org.kohsuke.github;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.kohsuke.github.junit.GitHubWireMockRule;
@@ -13,13 +12,15 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeTrue;
/**
* @author Liam Newman
*/
public abstract class AbstractGitHubWireMockTest extends Assert {
public abstract class AbstractGitHubWireMockTest {
private final GitHubBuilder githubBuilder = createGitHubBuilder();
@@ -126,6 +127,13 @@ public abstract class AbstractGitHubWireMockTest extends Assert {
mockGitHub.isUseProxy());
}
protected void verifyAuthenticated() {
assertThat(
"GitHub connection believes it is anonymous. Make sure you set GITHUB_OAUTH or both GITHUB_USER and GITHUB_PASSWORD environment variables",
gitHub.isAnonymous(),
is(false));
}
protected GHUser getUser() {
return getUser(gitHub);
}
@@ -163,6 +171,10 @@ public abstract class AbstractGitHubWireMockTest extends Assert {
protected GHRepository getTempRepository(String name) throws IOException {
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)

View File

@@ -17,8 +17,15 @@ import java.util.Map.Entry;
import java.util.regex.Pattern;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasProperty;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Unit test for simple App.

View File

@@ -5,6 +5,10 @@ import org.junit.Test;
import java.io.IOException;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* @author Kohsuke Kawaguchi
*/

View File

@@ -9,7 +9,10 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
/**
* Tests for the GitHub App API methods

View File

@@ -6,6 +6,13 @@ import org.kohsuke.github.GHBranchProtection.EnforceAdmins;
import org.kohsuke.github.GHBranchProtection.RequiredReviews;
import org.kohsuke.github.GHBranchProtection.RequiredStatusChecks;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.contains;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class GHBranchProtectionTest extends AbstractGitHubWireMockTest {
private static final String BRANCH = "master";
private static final String BRANCH_REF = "heads/" + BRANCH;

View File

@@ -10,6 +10,12 @@ import java.io.InputStreamReader;
import java.util.List;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Integration test for {@link GHContent}.

View File

@@ -5,7 +5,8 @@ import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
public class GHEventPayloadTest {

View File

@@ -3,8 +3,16 @@ package org.kohsuke.github;
import org.junit.Test;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author Kohsuke Kawaguchi

View File

@@ -5,6 +5,10 @@ import org.junit.Test;
import java.io.IOException;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* @author Martin van Zijl
*/

View File

@@ -30,6 +30,11 @@ import org.junit.Test;
import java.io.IOException;
import java.net.URL;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author Duncan Dickinson
*/

View File

@@ -5,6 +5,11 @@ import org.junit.Test;
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;

View File

@@ -7,6 +7,8 @@ import org.junit.Test;
import java.io.IOException;
import java.util.Date;
import static org.junit.Assert.assertEquals;
/**
* @author Martin van Zijl
*/

View File

@@ -2,6 +2,7 @@ package org.kohsuke.github;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
public class GHObjectTest extends org.kohsuke.github.AbstractGitHubWireMockTest {
@@ -13,4 +14,4 @@ public class GHObjectTest extends org.kohsuke.github.AbstractGitHubWireMockTest
containsString(
"login=github-api-test-org,location=<null>,blog=<null>,email=<null>,name=<null>,company=<null>,type=Organization,followers=0,following=0"));
}
}
}

View File

@@ -4,6 +4,9 @@ import org.junit.Test;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* @author Martin van Zijl
*/

View File

@@ -10,6 +10,12 @@ import java.util.Collections;
import java.util.List;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author Kohsuke Kawaguchi

View File

@@ -4,13 +4,14 @@ import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
import org.kohsuke.github.extras.okhttp3.OkHttpConnector;
import java.io.IOException;
import java.util.Date;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
/**
* Test showing the behavior of OkHttpConnector with and without cache.

View File

@@ -6,6 +6,10 @@ import java.io.IOException;
import java.util.List;
import java.util.NoSuchElementException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class GHRepositoryStatisticsTest extends AbstractGitHubWireMockTest {
public static int MAX_ITERATIONS = 3;

View File

@@ -10,8 +10,15 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author Liam Newman

View File

@@ -4,6 +4,8 @@ import org.junit.Test;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
public class GHTeamTest extends AbstractGitHubWireMockTest {
@Test

View File

@@ -8,6 +8,8 @@ import org.junit.Test;
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";

View File

@@ -6,6 +6,11 @@ import java.io.IOException;
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

View File

@@ -6,6 +6,10 @@ import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Unit test for {@link GitHub}.
*/

View File

@@ -8,6 +8,13 @@ import java.io.IOException;
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.assertFalse;
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.GHMarketplaceAccountType.ORGANIZATION;
/**

View File

@@ -6,6 +6,9 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* @author Kevin Harrington mad.hephaestus@gmail.com
*/

View File

@@ -9,7 +9,11 @@ import java.io.PrintWriter;
import java.util.List;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class LifecycleTest extends AbstractGitHubWireMockTest {
@Test

View File

@@ -4,7 +4,10 @@ import org.hamcrest.Matchers;
import org.junit.Ignore;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeTrue;
@@ -22,10 +25,7 @@ public class WireMockStatusReporterTest extends AbstractGitHubWireMockTest {
snapshotNotAllowed();
requireProxy("Tests proper configuration when proxying.");
assertThat(
"GitHub connection believes it is anonymous. Make sure you set GITHUB_OAUTH or both GITHUB_USER and GITHUB_PASSWORD environment variables",
gitHub.isAnonymous(),
is(false));
verifyAuthenticated();
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());
assertThat(gitHub.isAnonymous(), is(false));
verifyAuthenticated();
assertThat(gitHub.login, equalTo(STUBBED_USER_LOGIN));
GHUser user = gitHub.getMyself();

View File

@@ -19,6 +19,8 @@ import org.kohsuke.github.GitHub;
import java.io.File;
import java.io.IOException;
import static org.junit.Assert.fail;
/**
* Test showing the behavior of OkHttpConnector cache with GitHub 404 responses.
*

View File

@@ -14,6 +14,7 @@ import org.kohsuke.github.*;
import java.io.File;
import java.io.IOException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.core.Is.is;
import static org.junit.Assume.assumeFalse;

View File

@@ -18,6 +18,8 @@ import org.kohsuke.github.GitHub;
import java.io.File;
import java.io.IOException;
import static org.junit.Assert.fail;
/**
* Test showing the behavior of OkHttpConnector cache with GitHub 404 responses.
*

View File

@@ -16,6 +16,7 @@ import org.kohsuke.github.GitHub;
import java.io.File;
import java.io.IOException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.Is.is;