Skip to content

Commit 63e2db7

Browse files
committed
Add tests to oauth2-jose
Fixes gh-4806
1 parent 473ac0e commit 63e2db7

File tree

2 files changed

+163
-0
lines changed

2 files changed

+163
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright 2002-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.oauth2.jwt;
17+
18+
import org.junit.Test;
19+
import org.springframework.security.oauth2.jose.jws.JwsAlgorithms;
20+
21+
import java.time.Instant;
22+
import java.util.Arrays;
23+
import java.util.Collections;
24+
import java.util.HashMap;
25+
import java.util.List;
26+
import java.util.Map;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* Tests for {@link Jwt}.
32+
*
33+
* @author Joe Grandja
34+
*/
35+
public class JwtTests {
36+
private static final String ISS_CLAIM = "iss";
37+
private static final String SUB_CLAIM = "sub";
38+
private static final String AUD_CLAIM = "aud";
39+
private static final String EXP_CLAIM = "exp";
40+
private static final String NBF_CLAIM = "nbf";
41+
private static final String IAT_CLAIM = "iat";
42+
private static final String JTI_CLAIM = "jti";
43+
44+
private static final String ISS_VALUE = "https://provider.com";
45+
private static final String SUB_VALUE = "subject1";
46+
private static final List<String> AUD_VALUE = Arrays.asList("aud1", "aud2");
47+
private static final long EXP_VALUE = Instant.now().plusSeconds(60).toEpochMilli();
48+
private static final long NBF_VALUE = Instant.now().plusSeconds(5).toEpochMilli();
49+
private static final long IAT_VALUE = Instant.now().toEpochMilli();
50+
private static final String JTI_VALUE = "jwt-id-1";
51+
52+
private static final Map<String, Object> HEADERS;
53+
private static final Map<String, Object> CLAIMS;
54+
private static final String JWT_TOKEN_VALUE = "jwt-token-value";
55+
56+
static {
57+
HEADERS = new HashMap<>();
58+
HEADERS.put("alg", JwsAlgorithms.RS256);
59+
60+
CLAIMS = new HashMap<>();
61+
CLAIMS.put(ISS_CLAIM, ISS_VALUE);
62+
CLAIMS.put(SUB_CLAIM, SUB_VALUE);
63+
CLAIMS.put(AUD_CLAIM, AUD_VALUE);
64+
CLAIMS.put(EXP_CLAIM, EXP_VALUE);
65+
CLAIMS.put(NBF_CLAIM, NBF_VALUE);
66+
CLAIMS.put(IAT_CLAIM, IAT_VALUE);
67+
CLAIMS.put(JTI_CLAIM, JTI_VALUE);
68+
}
69+
70+
@Test(expected = IllegalArgumentException.class)
71+
public void constructorWhenTokenValueIsNullThenThrowIllegalArgumentException() {
72+
new Jwt(null, Instant.ofEpochMilli(IAT_VALUE), Instant.ofEpochMilli(EXP_VALUE), HEADERS, CLAIMS);
73+
}
74+
75+
@Test(expected = IllegalArgumentException.class)
76+
public void constructorWhenIssuedAtIsNullThenThrowIllegalArgumentException() {
77+
new Jwt(JWT_TOKEN_VALUE, null, Instant.ofEpochMilli(EXP_VALUE), HEADERS, CLAIMS);
78+
}
79+
80+
@Test(expected = IllegalArgumentException.class)
81+
public void constructorWhenExpiresAtIsNullThenThrowIllegalArgumentException() {
82+
new Jwt(JWT_TOKEN_VALUE, Instant.ofEpochMilli(IAT_VALUE), null, HEADERS, CLAIMS);
83+
}
84+
85+
@Test(expected = IllegalArgumentException.class)
86+
public void constructorWhenHeadersIsEmptyThenThrowIllegalArgumentException() {
87+
new Jwt(JWT_TOKEN_VALUE, Instant.ofEpochMilli(IAT_VALUE),
88+
Instant.ofEpochMilli(EXP_VALUE), Collections.emptyMap(), CLAIMS);
89+
}
90+
91+
@Test(expected = IllegalArgumentException.class)
92+
public void constructorWhenClaimsIsEmptyThenThrowIllegalArgumentException() {
93+
new Jwt(JWT_TOKEN_VALUE, Instant.ofEpochMilli(IAT_VALUE),
94+
Instant.ofEpochMilli(EXP_VALUE), HEADERS, Collections.emptyMap());
95+
}
96+
97+
@Test
98+
public void constructorWhenParametersProvidedAndValidThenCreated() {
99+
Jwt jwt = new Jwt(JWT_TOKEN_VALUE, Instant.ofEpochMilli(IAT_VALUE),
100+
Instant.ofEpochMilli(EXP_VALUE), HEADERS, CLAIMS);
101+
102+
assertThat(jwt.getTokenValue()).isEqualTo(JWT_TOKEN_VALUE);
103+
assertThat(jwt.getHeaders()).isEqualTo(HEADERS);
104+
assertThat(jwt.getClaims()).isEqualTo(CLAIMS);
105+
assertThat(jwt.getIssuer().toString()).isEqualTo(ISS_VALUE);
106+
assertThat(jwt.getSubject()).isEqualTo(SUB_VALUE);
107+
assertThat(jwt.getAudience()).isEqualTo(AUD_VALUE);
108+
assertThat(jwt.getExpiresAt().toEpochMilli()).isEqualTo(EXP_VALUE);
109+
assertThat(jwt.getNotBefore().toEpochMilli()).isEqualTo(NBF_VALUE);
110+
assertThat(jwt.getIssuedAt().toEpochMilli()).isEqualTo(IAT_VALUE);
111+
assertThat(jwt.getId()).isEqualTo(JTI_VALUE);
112+
}
113+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2002-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.oauth2.jwt;
17+
18+
import org.junit.Test;
19+
import org.springframework.security.oauth2.jose.jws.JwsAlgorithms;
20+
21+
/**
22+
* Tests for {@link NimbusJwtDecoderJwkSupport}.
23+
*
24+
* @author Joe Grandja
25+
*/
26+
public class NimbusJwtDecoderJwkSupportTests {
27+
private static final String JWK_SET_URL = "https://provider.com/oauth2/keys";
28+
private static final String JWS_ALGORITHM = JwsAlgorithms.RS256;
29+
30+
@Test(expected = IllegalArgumentException.class)
31+
public void constructorWhenJwkSetUrlIsNullThenThrowIllegalArgumentException() {
32+
new NimbusJwtDecoderJwkSupport(null);
33+
}
34+
35+
@Test(expected = IllegalArgumentException.class)
36+
public void constructorWhenJwkSetUrlInvalidThenThrowIllegalArgumentException() {
37+
new NimbusJwtDecoderJwkSupport("invalid.com");
38+
}
39+
40+
@Test(expected = IllegalArgumentException.class)
41+
public void constructorWhenJwsAlgorithmIsNullThenThrowIllegalArgumentException() {
42+
new NimbusJwtDecoderJwkSupport(JWK_SET_URL, null);
43+
}
44+
45+
@Test(expected = JwtException.class)
46+
public void decodeWhenJwtInvalidThenThrowJwtException() {
47+
NimbusJwtDecoderJwkSupport jwtDecoder = new NimbusJwtDecoderJwkSupport(JWK_SET_URL, JWS_ALGORITHM);
48+
jwtDecoder.decode("invalid");
49+
}
50+
}

0 commit comments

Comments
 (0)