Skip to content

Commit 5cbcbfd

Browse files
committed
improve coverage
1 parent 70e18ca commit 5cbcbfd

File tree

7 files changed

+116
-2
lines changed

7 files changed

+116
-2
lines changed

lib/src/main/java/com/auth0/jwt/JWTVerifier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final class JWTVerifier {
1616
final Map<String, Object> claims;
1717
private final Clock clock;
1818

19-
private JWTVerifier(Algorithm algorithm, Map<String, Object> claims, Clock clock) {
19+
JWTVerifier(Algorithm algorithm, Map<String, Object> claims, Clock clock) {
2020
this.algorithm = algorithm;
2121
this.claims = Collections.unmodifiableMap(claims);
2222
this.clock = clock;

lib/src/test/java/com/auth0/jwt/JWTVerifierTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.junit.rules.ExpectedException;
1010

1111
import java.util.Date;
12+
import java.util.HashMap;
13+
import java.util.Map;
1214

1315
import static org.hamcrest.Matchers.*;
1416
import static org.junit.Assert.assertThat;
@@ -170,6 +172,7 @@ public void shouldThrowOnInvalidCustomClaimValueOfTypeBoolean() throws Exception
170172
.verify(token);
171173
}
172174

175+
173176
@Test
174177
public void shouldThrowOnInvalidCustomClaimValueOfTypeDate() throws Exception {
175178
exception.expect(InvalidClaimException.class);
@@ -181,6 +184,17 @@ public void shouldThrowOnInvalidCustomClaimValueOfTypeDate() throws Exception {
181184
.verify(token);
182185
}
183186

187+
@Test
188+
public void shouldThrowOnInvalidCustomClaimValue() throws Exception {
189+
exception.expect(InvalidClaimException.class);
190+
exception.expectMessage("The Claim 'name' value doesn't match the required one.");
191+
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjpbInNvbWV0aGluZyJdfQ.3ENLez6tU_fG0SVFrGmISltZPiXLSHaz_dyn-XFTEGQ";
192+
Map<String, Object> map = new HashMap<>();
193+
map.put("name", new Object());
194+
JWTVerifier verifier = new JWTVerifier(Algorithm.HMAC256("secret"), map, new Clock());
195+
verifier.verify(token);
196+
}
197+
184198
@Test
185199
public void shouldValidateCustomClaimOfTypeString() throws Exception {
186200
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidmFsdWUifQ.Jki8pvw6KGbxpMinufrgo6RDL1cu7AtNMJYVh6t-_cE";
@@ -239,6 +253,7 @@ public void shouldValidateCustomClaimOfTypeDate() throws Exception {
239253

240254

241255
// Generic Delta
256+
@SuppressWarnings("RedundantCast")
242257
@Test
243258
public void shouldAddDefaultTimeDeltaToDateClaims() throws Exception {
244259
Algorithm algorithm = mock(Algorithm.class);
@@ -251,6 +266,7 @@ public void shouldAddDefaultTimeDeltaToDateClaims() throws Exception {
251266
assertThat(verifier.claims, hasEntry("nbf", (Object) 0L));
252267
}
253268

269+
@SuppressWarnings("RedundantCast")
254270
@Test
255271
public void shouldAddCustomTimeDeltaToDateClaims() throws Exception {
256272
Algorithm algorithm = mock(Algorithm.class);
@@ -264,6 +280,7 @@ public void shouldAddCustomTimeDeltaToDateClaims() throws Exception {
264280
assertThat(verifier.claims, hasEntry("nbf", (Object) 1234L));
265281
}
266282

283+
@SuppressWarnings("RedundantCast")
267284
@Test
268285
public void shouldOverrideDefaultIssuedAtTimeDelta() throws Exception {
269286
Algorithm algorithm = mock(Algorithm.class);
@@ -278,6 +295,7 @@ public void shouldOverrideDefaultIssuedAtTimeDelta() throws Exception {
278295
assertThat(verifier.claims, hasEntry("nbf", (Object) 1234L));
279296
}
280297

298+
@SuppressWarnings("RedundantCast")
281299
@Test
282300
public void shouldOverrideDefaultExpiresAtTimeDelta() throws Exception {
283301
Algorithm algorithm = mock(Algorithm.class);
@@ -292,6 +310,7 @@ public void shouldOverrideDefaultExpiresAtTimeDelta() throws Exception {
292310
assertThat(verifier.claims, hasEntry("nbf", (Object) 1234L));
293311
}
294312

313+
@SuppressWarnings("RedundantCast")
295314
@Test
296315
public void shouldOverrideDefaultNotBeforeTimeDelta() throws Exception {
297316
Algorithm algorithm = mock(Algorithm.class);

lib/src/test/java/com/auth0/jwt/impl/ClaimImplTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.auth0.jwt.interfaces.Claim;
66
import com.fasterxml.jackson.databind.JsonNode;
77
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.fasterxml.jackson.databind.node.MissingNode;
9+
import com.fasterxml.jackson.databind.node.NullNode;
810
import org.junit.Before;
911
import org.junit.Rule;
1012
import org.junit.Test;
@@ -202,6 +204,26 @@ public void shouldThrowIfListClassMismatch() throws Exception {
202204
claim.asList(UserPojo.class);
203205
}
204206

207+
@Test
208+
public void shouldReturnBaseClaimWhenParsingMissingNode() throws Exception {
209+
JsonNode value = MissingNode.getInstance();
210+
Claim claim = claimFromNode(value);
211+
212+
assertThat(claim, is(notNullValue()));
213+
assertThat(claim, is(instanceOf(BaseClaim.class)));
214+
assertThat(claim.isNull(), is(true));
215+
}
216+
217+
@Test
218+
public void shouldReturnBaseClaimWhenParsingNullNode() throws Exception {
219+
JsonNode value = NullNode.getInstance();
220+
Claim claim = claimFromNode(value);
221+
222+
assertThat(claim, is(notNullValue()));
223+
assertThat(claim, is(instanceOf(BaseClaim.class)));
224+
assertThat(claim.isNull(), is(true));
225+
}
226+
205227
@Test
206228
public void shouldReturnBaseClaimWhenParsingNullValue() throws Exception {
207229
JsonNode value = mapper.valueToTree(null);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.auth0.jwt.impl;
2+
3+
import org.hamcrest.collection.IsMapContaining;
4+
import org.junit.Test;
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
import static org.hamcrest.Matchers.*;
10+
import static org.junit.Assert.assertThat;
11+
12+
public class ClaimsHolderTest {
13+
14+
@SuppressWarnings("RedundantCast")
15+
@Test
16+
public void shouldGetClaims() throws Exception {
17+
HashMap<String, Object> claims = new HashMap<>();
18+
claims.put("iss", "auth0");
19+
ClaimsHolder holder = new ClaimsHolder(claims);
20+
assertThat(holder, is(notNullValue()));
21+
assertThat(holder.getClaims(), is(notNullValue()));
22+
assertThat(holder.getClaims(), is(instanceOf(Map.class)));
23+
assertThat(holder.getClaims(), is(IsMapContaining.hasEntry("iss", (Object) "auth0")));
24+
}
25+
26+
@Test
27+
public void shouldGetNotNullClaims() throws Exception {
28+
ClaimsHolder holder = new ClaimsHolder(null);
29+
assertThat(holder, is(notNullValue()));
30+
assertThat(holder.getClaims(), is(notNullValue()));
31+
assertThat(holder.getClaims(), is(instanceOf(Map.class)));
32+
}
33+
}

lib/src/test/java/com/auth0/jwt/impl/HeaderImplTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.auth0.jwt.impl;
22

33
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.node.NullNode;
45
import com.fasterxml.jackson.databind.node.TextNode;
6+
import org.hamcrest.collection.IsMapContaining;
57
import org.junit.Rule;
68
import org.junit.Test;
79
import org.junit.rules.ExpectedException;
@@ -18,12 +20,30 @@ public class HeaderImplTest {
1820

1921
@SuppressWarnings("Convert2Diamond")
2022
@Test
21-
public void shouldHaveUnmodifiableTree() throws Exception {
23+
public void shouldHaveUnmodifiableTreeWhenInstantiatedWithNonNullTree() throws Exception {
2224
exception.expect(UnsupportedOperationException.class);
2325
HeaderImpl header = new HeaderImpl(new HashMap<String, JsonNode>());
2426
header.getTree().put("something", null);
2527
}
2628

29+
@Test
30+
public void shouldHaveUnmodifiableTreeWhenInstantiatedWithNullTree() throws Exception {
31+
exception.expect(UnsupportedOperationException.class);
32+
HeaderImpl header = new HeaderImpl(null);
33+
header.getTree().put("something", null);
34+
}
35+
36+
@Test
37+
public void shouldHaveTree() throws Exception {
38+
HashMap<String, JsonNode> map = new HashMap<>();
39+
JsonNode node = NullNode.getInstance();
40+
map.put("key", node);
41+
HeaderImpl header = new HeaderImpl(map);
42+
43+
assertThat(header.getTree(), is(notNullValue()));
44+
assertThat(header.getTree(), is(IsMapContaining.hasEntry("key", node)));
45+
}
46+
2747
@Test
2848
public void shouldGetHS256Algorithm() throws Exception {
2949
JsonNode algNode = new TextNode("HS256");

lib/src/test/java/com/auth0/jwt/impl/PayloadDeserializerTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ public void shouldGetStringArrayWhenParsingTextNode() throws Exception {
137137
assertThat(values, is(arrayContaining("something")));
138138
}
139139

140+
@Test
141+
public void shouldGetEmptyStringArrayWhenParsingEmptyTextNode() throws Exception {
142+
Map<String, JsonNode> tree = new HashMap<>();
143+
TextNode textNode = new TextNode("");
144+
tree.put("key", textNode);
145+
146+
String[] values = deserializer.getStringOrArray(tree, "key");
147+
assertThat(values, is(notNullValue()));
148+
assertThat(values, is(arrayWithSize(0)));
149+
}
150+
140151
@Test
141152
public void shouldGetNullArrayWhenParsingNullNode() throws Exception {
142153
Map<String, JsonNode> tree = new HashMap<>();

lib/src/test/java/com/auth0/jwt/impl/PayloadSerializerTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ public void shouldSerializeMultipleItemsAudienceAsArray() throws Exception {
7272
assertThat(writer.toString(), is(equalTo("{\"aud\":[\"auth0\",\"auth10\"]}")));
7373
}
7474

75+
@Test
76+
public void shouldSkipSerializationOnEmptyAudience() throws Exception {
77+
ClaimsHolder holder = holderFor("aud", new String[0]);
78+
serializer.serialize(holder, jsonGenerator, serializerProvider);
79+
jsonGenerator.flush();
80+
81+
assertThat(writer.toString(), is(equalTo("{}")));
82+
}
83+
7584
@Test
7685
public void shouldSerializeNotBeforeDateInSeconds() throws Exception {
7786
ClaimsHolder holder = holderFor("nbf", new Date(1478874000));

0 commit comments

Comments
 (0)