This repository has been archived by the owner on May 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
TestOAuth2AccessTokenJackson2Serializer.java
118 lines (99 loc) · 4.53 KB
/
TestOAuth2AccessTokenJackson2Serializer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package org.springframework.security.oauth2.common;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.powermock.core.classloader.annotations.PrepareForTest;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
/**
* Tests serialization of an {@link org.springframework.security.oauth2.common.OAuth2AccessToken} using jackson.
*
* @author Rob Winch
*/
@PrepareForTest(OAuth2AccessTokenJackson2Serializer.class)
public class TestOAuth2AccessTokenJackson2Serializer extends BaseOAuth2AccessTokenJacksonTest {
protected ObjectMapper mapper;
@Before
public void createObjectMapper() {
mapper = new ObjectMapper();
}
@Test
public void writeValueAsStringNoRefresh() throws JsonGenerationException, JsonMappingException, IOException {
accessToken.setRefreshToken(null);
accessToken.setScope(null);
String encodedAccessToken = mapper.writeValueAsString(accessToken);
assertEquals(BaseOAuth2AccessTokenJacksonTest.ACCESS_TOKEN_NOREFRESH, encodedAccessToken);
}
@Test
public void writeValueAsStringWithRefresh() throws JsonGenerationException, JsonMappingException, IOException {
accessToken.setScope(null);
String encodedAccessToken = mapper.writeValueAsString(accessToken);
assertEquals(BaseOAuth2AccessTokenJacksonTest.ACCESS_TOKEN_NOSCOPE, encodedAccessToken);
}
@Test
public void writeValueAsStringWithEmptyScope() throws JsonGenerationException, JsonMappingException, IOException {
accessToken.getScope().clear();
String encodedAccessToken = mapper.writeValueAsString(accessToken);
assertEquals(BaseOAuth2AccessTokenJacksonTest.ACCESS_TOKEN_NOSCOPE, encodedAccessToken);
}
@Test
public void writeValueAsStringWithSingleScopes() throws JsonGenerationException, JsonMappingException, IOException {
accessToken.getScope().remove(accessToken.getScope().iterator().next());
String encodedAccessToken = mapper.writeValueAsString(accessToken);
assertEquals(BaseOAuth2AccessTokenJacksonTest.ACCESS_TOKEN_SINGLESCOPE, encodedAccessToken);
}
@Test
public void writeValueAsStringWithNullScope() throws JsonGenerationException, JsonMappingException, IOException {
thrown.expect(JsonMappingException.class);
thrown.expectMessage("Scopes cannot be null or empty. Got [null]");
accessToken.getScope().clear();
try {
accessToken.getScope().add(null);
}
catch (NullPointerException e) {
// short circuit NPE from Java 7 (which is correct but only relevant for this test)
throw new JsonMappingException("Scopes cannot be null or empty. Got [null]");
}
mapper.writeValueAsString(accessToken);
}
@Test
public void writeValueAsStringWithEmptyStringScope() throws JsonGenerationException, JsonMappingException,
IOException {
thrown.expect(JsonMappingException.class);
thrown.expectMessage("Scopes cannot be null or empty. Got []");
accessToken.getScope().clear();
accessToken.getScope().add("");
mapper.writeValueAsString(accessToken);
}
@Test
public void writeValueAsStringWithQuoteInScope() throws JsonGenerationException, JsonMappingException, IOException {
accessToken.getScope().add("\"");
String encodedAccessToken = mapper.writeValueAsString(accessToken);
assertEquals(
"{\"access_token\":\"token-value\",\"token_type\":\"bearer\",\"refresh_token\":\"refresh-value\",\"expires_in\":10,\"scope\":\"\\\" read write\"}",
encodedAccessToken);
}
@Test
public void writeValueAsStringWithMultiScopes() throws JsonGenerationException, JsonMappingException, IOException {
String encodedAccessToken = mapper.writeValueAsString(accessToken);
assertEquals(ACCESS_TOKEN_MULTISCOPE, encodedAccessToken);
}
@Test
public void writeValueAsStringWithMac() throws Exception {
accessToken.setTokenType("mac");
String expectedEncodedAccessToken = ACCESS_TOKEN_MULTISCOPE.replace("bearer", accessToken.getTokenType());
String encodedAccessToken = mapper.writeValueAsString(accessToken);
assertEquals(expectedEncodedAccessToken, encodedAccessToken);
}
@Test
public void writeValueWithAdditionalInformation() throws JsonGenerationException, JsonMappingException, IOException {
accessToken.setRefreshToken(null);
accessToken.setScope(null);
accessToken.setExpiration(null);
accessToken.setAdditionalInformation(additionalInformation);
String encodedAccessToken = mapper.writeValueAsString(accessToken);
assertEquals(BaseOAuth2AccessTokenJacksonTest.ACCESS_TOKEN_ADDITIONAL_INFO, encodedAccessToken);
}
}