Skip to content

Commit

Permalink
[ELY-2693] Unit test code refactor (removed duplicates)
Browse files Browse the repository at this point in the history
  • Loading branch information
sugan0tech committed Oct 22, 2023
1 parent 5ed4c6c commit bf3530d
Showing 1 changed file with 20 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -372,22 +372,7 @@ public void testMultipleTokenTypes() throws Exception {

@Test
public void testUnsecuredJkuEndpoint() throws Exception {
BearerTokenEvidence evidence = new BearerTokenEvidence(createJwt(keyPair1, 60, -1, "1", new URI("https://localhost:50832")));

X509TrustManager tm = getTrustManager();
SSLContext sslContext = new SSLContextBuilder().setTrustManager(tm).setClientMode(true).setSessionTimeout(10).build().create();

TokenSecurityRealm securityRealm = TokenSecurityRealm.builder()
.principalClaimName("sub")
.validator(JwtValidator.builder()
.issuer("elytron-oauth2-realm")
.audience("my-app-valid")
.useSslContext(sslContext)
.useSslHostnameVerifier((a,b) -> true).build())
.build();

assertIdentityNotExist(securityRealm, evidence);

testTokenSecurityRealm("1", 50832);
}

@Test
Expand Down Expand Up @@ -467,41 +452,12 @@ public void testJkuMultipleKeys() throws Exception {

@Test
public void testInvalidJku() throws Exception {
BearerTokenEvidence evidence = new BearerTokenEvidence(createJwt(keyPair1, 60, -1, "1", new URI("https://localhost:80")));

X509TrustManager tm = getTrustManager();
SSLContext sslContext = new SSLContextBuilder().setTrustManager(tm).setClientMode(true).setSessionTimeout(10).build().create();

TokenSecurityRealm securityRealm = TokenSecurityRealm.builder()
.principalClaimName("sub")
.validator(JwtValidator.builder()
.issuer("elytron-oauth2-realm")
.audience("my-app-valid")
.useSslContext(sslContext).useSslHostnameVerifier((a,b) -> true).build())
.build();

assertIdentityNotExist(securityRealm, evidence);

testTokenSecurityRealm("1", 80);
}

@Test
public void testInvalidKid() throws Exception {
BearerTokenEvidence evidence = new BearerTokenEvidence(createJwt(keyPair1, 60, -1, "badkid", new URI("https://localhost:50831")));

X509TrustManager tm = getTrustManager();
SSLContext sslContext = new SSLContextBuilder().setTrustManager(tm).setClientMode(true).setSessionTimeout(10).build().create();

TokenSecurityRealm securityRealm = TokenSecurityRealm.builder()
.principalClaimName("sub")
.validator(JwtValidator.builder()
.issuer("elytron-oauth2-realm")
.audience("my-app-valid")
.useSslContext(sslContext)
.useSslHostnameVerifier((a,b) -> true).build())
.build();

assertIdentityNotExist(securityRealm, evidence);

testTokenSecurityRealm("badkid", 50831);
}

@Test
Expand Down Expand Up @@ -845,4 +801,21 @@ public MockResponse dispatch(RecordedRequest recordedRequest) {
}
};
}

private void testTokenSecurityRealm(String kid, int port) throws Exception {
BearerTokenEvidence evidence = new BearerTokenEvidence(createJwt(keyPair1, 60, -1, kid, new URI("https://localhost:" + port)));

X509TrustManager tm = getTrustManager();
SSLContext sslContext = new SSLContextBuilder().setTrustManager(tm).setClientMode(true).setSessionTimeout(10).build().create();

TokenSecurityRealm securityRealm = TokenSecurityRealm.builder()
.principalClaimName("sub")
.validator(JwtValidator.builder()
.issuer("elytron-oauth2-realm")
.audience("my-app-valid")
.useSslContext(sslContext).useSslHostnameVerifier((a,b) -> true).build())
.build();

assertIdentityNotExist(securityRealm, evidence);
}
}

0 comments on commit bf3530d

Please sign in to comment.