Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #20 added url constructor to JwkProviderBuilder #22

Merged
merged 5 commits into from
Apr 26, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/test/java/com/auth0/jwk/JwkProviderBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import java.util.concurrent.TimeUnit;

import static com.auth0.jwk.UrlJwkProvider.WELL_KNOWN_JWKS_PATH;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;

public class JwkProviderBuilderTest {
Expand All @@ -26,31 +28,31 @@ public void shouldCreateForUrl() throws Exception {
}

@Test
public void shouldCreateForDomain() throws Exception {
public void shouldCreateForDomain() {
assertThat(new JwkProviderBuilder(domain).build(), notNullValue());
}

@Test
public void shouldCreateForNormalizedDomain() throws Exception {
public void shouldCreateForNormalizedDomain() {
assertThat(new JwkProviderBuilder(normalizedDomain).build(), notNullValue());
}

@Test
public void shouldFailWhenNoUrlIsProvided() throws Exception {
public void shouldFailWhenNoUrlIsProvided() {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Cannot build provider without url to jwks");
new JwkProviderBuilder((URL) null).build();
}

@Test
public void shouldFailWhenNoDomainIsProvided() throws Exception {
public void shouldFailWhenNoDomainIsProvided() {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Cannot build provider without domain");
new JwkProviderBuilder((String) null).build();
}

@Test
public void shouldCreateCachedProvider() throws Exception {
public void shouldCreateCachedProvider() {
JwkProvider provider = new JwkProviderBuilder(domain)
.rateLimited(false)
.cached(true)
Expand All @@ -61,7 +63,7 @@ public void shouldCreateCachedProvider() throws Exception {
}

@Test
public void shouldCreateCachedProviderWithCustomValues() throws Exception {
public void shouldCreateCachedProviderWithCustomValues() {
JwkProvider provider = new JwkProviderBuilder(domain)
.rateLimited(false)
.cached(10, 24, TimeUnit.HOURS)
Expand All @@ -72,7 +74,7 @@ public void shouldCreateCachedProviderWithCustomValues() throws Exception {
}

@Test
public void shouldCreateRateLimitedProvider() throws Exception {
public void shouldCreateRateLimitedProvider() {
JwkProvider provider = new JwkProviderBuilder(domain)
.cached(false)
.rateLimited(true)
Expand All @@ -83,7 +85,7 @@ public void shouldCreateRateLimitedProvider() throws Exception {
}

@Test
public void shouldCreateRateLimitedProviderWithCustomValues() throws Exception {
public void shouldCreateRateLimitedProviderWithCustomValues() {
JwkProvider provider = new JwkProviderBuilder(domain)
.cached(false)
.rateLimited(10, 24, TimeUnit.HOURS)
Expand All @@ -94,7 +96,7 @@ public void shouldCreateRateLimitedProviderWithCustomValues() throws Exception {
}

@Test
public void shouldCreateCachedAndRateLimitedProvider() throws Exception {
public void shouldCreateCachedAndRateLimitedProvider() {
JwkProvider provider = new JwkProviderBuilder(domain)
.cached(true)
.rateLimited(true)
Expand All @@ -107,7 +109,7 @@ public void shouldCreateCachedAndRateLimitedProvider() throws Exception {
}

@Test
public void shouldCreateCachedAndRateLimitedProviderWithCustomValues() throws Exception {
public void shouldCreateCachedAndRateLimitedProviderWithCustomValues() {
JwkProvider provider = new JwkProviderBuilder(domain)
.cached(10, 24, TimeUnit.HOURS)
.rateLimited(10, 24, TimeUnit.HOURS)
Expand All @@ -120,7 +122,7 @@ public void shouldCreateCachedAndRateLimitedProviderWithCustomValues() throws Ex
}

@Test
public void shouldCreateCachedAndRateLimitedProviderByDefault() throws Exception {
public void shouldCreateCachedAndRateLimitedProviderByDefault() {
JwkProvider provider = new JwkProviderBuilder(domain).build();
assertThat(provider, notNullValue());
assertThat(provider, instanceOf(GuavaCachedJwkProvider.class));
Expand Down
15 changes: 10 additions & 5 deletions src/test/java/com/auth0/jwk/UrlJwkProviderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
public class UrlJwkProviderTest {

private static final String KID = "NkJCQzIyQzRBMEU4NjhGNUU4MzU4RkY0M0ZDQzkwOUQ0Q0VGNUMwQg";
private UrlJwkProvider provider;

@Rule
public ExpectedException expectedException = ExpectedException.none();

@Before
public void setUp() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole "before" method is no longer being used generically. It's only used in shouldReturnSingleJwkById. Feel free to remove this and the provider field.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made some cleanup

provider = new UrlJwkProvider(getClass().getResource("/jwks.json"));
}

@Test
Expand All @@ -45,28 +43,29 @@ public void shouldFailToCreateWithEmptyDomain() {

@Test
public void shouldReturnSingleJwkById() throws Exception {
UrlJwkProvider provider = new UrlJwkProvider(getClass().getResource("/jwks.json"));
assertThat(provider.get(KID), notNullValue());
}

@Test
public void shouldFailToLoadSingleWhenUrlHasNothing() throws Exception {
expectedException.expect(SigningKeyNotFoundException.class);
provider = new UrlJwkProvider(new URL("file:///not_found.file"));
UrlJwkProvider provider = new UrlJwkProvider(new URL("file:///not_found.file"));
provider.get(KID);
}

@Test
public void shouldFailToLoadSingleWhenKeysIsEmpty() throws Exception {
expectedException.expect(SigningKeyNotFoundException.class);
provider = new UrlJwkProvider(getClass().getResource("/empty-jwks.json"));
UrlJwkProvider provider = new UrlJwkProvider(getClass().getResource("/empty-jwks.json"));
provider.get(KID);
}


@Test
public void shouldFailToLoadSingleWhenJsonIsInvalid() throws Exception {
expectedException.expect(SigningKeyNotFoundException.class);
provider = new UrlJwkProvider(getClass().getResource("/invalid-jwks.json"));
UrlJwkProvider provider = new UrlJwkProvider(getClass().getResource("/invalid-jwks.json"));
provider.get(KID);
}

Expand Down Expand Up @@ -122,4 +121,10 @@ public void shouldUseOnlyDomain() {
String actualJwksUrl = new UrlJwkProvider(domainWithSubPath).url.toString();
assertThat(actualJwksUrl, equalTo("https://" + domain + WELL_KNOWN_JWKS_PATH));
}

@Test(expected = IllegalArgumentException.class)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the other syntax so we have everything the same.

expectedException.expect(IllegalArgumentException.class);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, done

public void shouldFailOnInvalidProtocol() {
String domainWithInvalidProtocol = "httptest://samples.auth0.com";
new UrlJwkProvider(domainWithInvalidProtocol);
}
}