Skip to content

Commit e5551c6

Browse files
rwinchDave Syer
authored andcommitted
SECOAUTH-178: Added unit tests for OAuth2AccessToken Serializer/Deserializer
1 parent a1f270e commit e5551c6

File tree

3 files changed

+274
-0
lines changed

3 files changed

+274
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2006-2010 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package org.springframework.security.oauth2.common;
14+
15+
import static org.mockito.Matchers.any;
16+
import static org.mockito.Mockito.when;
17+
import static org.powermock.api.mockito.PowerMockito.mockStatic;
18+
19+
import java.util.Date;
20+
import java.util.Set;
21+
import java.util.TreeSet;
22+
23+
import org.codehaus.jackson.map.ObjectMapper;
24+
import org.junit.Before;
25+
import org.junit.Rule;
26+
import org.junit.rules.ExpectedException;
27+
import org.junit.runner.RunWith;
28+
import org.mockito.Mock;
29+
import org.powermock.core.classloader.annotations.PrepareForTest;
30+
import org.powermock.modules.junit4.PowerMockRunner;
31+
import org.springframework.security.oauth2.common.OAuth2AccessToken;
32+
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
33+
34+
/**
35+
* Base class for testing Jackson serialization and deserialization of {@link OAuth2AccessToken}.
36+
*
37+
* @author Rob Winch
38+
*/
39+
@RunWith(PowerMockRunner.class)
40+
@PrepareForTest({ System.class })
41+
abstract class BaseOAuth2AccessTokenJacksonTest {
42+
protected static final String ACCESS_TOKEN_EMPTYSCOPE = "{\"access_token\":\"token-value\",\"token_type\":\"bearer\",\"refresh_token\":\"refresh-value\",\"expires_in\":10,\"scope\":\"\"}";
43+
44+
protected static final String ACCESS_TOKEN_MULTISCOPE = "{\"access_token\":\"token-value\",\"token_type\":\"bearer\",\"refresh_token\":\"refresh-value\",\"expires_in\":10,\"scope\":\"read write\"}";
45+
46+
protected static final String ACCESS_TOKEN_NOSCOPE = "{\"access_token\":\"token-value\",\"token_type\":\"bearer\",\"refresh_token\":\"refresh-value\",\"expires_in\":10}";
47+
48+
protected static final String ACCESS_TOKEN_NOREFRESH = "{\"access_token\":\"token-value\",\"token_type\":\"bearer\",\"expires_in\":10}";
49+
50+
protected static final String ACCESS_TOKEN_SINGLESCOPE = "{\"access_token\":\"token-value\",\"token_type\":\"bearer\",\"refresh_token\":\"refresh-value\",\"expires_in\":10,\"scope\":\"write\"}";
51+
52+
@Rule
53+
public ExpectedException thrown = ExpectedException.none();
54+
55+
@Mock
56+
protected Date expiration;
57+
58+
protected OAuth2AccessToken accessToken;
59+
60+
protected ObjectMapper mapper;
61+
62+
public BaseOAuth2AccessTokenJacksonTest() {
63+
super();
64+
}
65+
66+
@Before
67+
public void setUp() {
68+
mockStatic(System.class);
69+
long now = 1323123715041L;
70+
when(System.currentTimeMillis()).thenReturn(now);
71+
when(expiration.before(any(Date.class))).thenReturn(false);
72+
when(expiration.getTime()).thenReturn(now + 10000);
73+
74+
accessToken = new OAuth2AccessToken("token-value");
75+
accessToken.setExpiration(expiration);
76+
mapper = new ObjectMapper();
77+
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken("refresh-value");
78+
accessToken.setRefreshToken(refreshToken);
79+
Set<String> scope = new TreeSet<String>();
80+
scope.add("read");
81+
scope.add("write");
82+
accessToken.setScope(scope);
83+
}
84+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2011 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package org.springframework.security.oauth2.common;
14+
15+
import static org.junit.Assert.assertEquals;
16+
import static org.junit.Assert.assertNull;
17+
18+
import java.io.IOException;
19+
import java.util.Date;
20+
import java.util.HashSet;
21+
22+
import org.codehaus.jackson.JsonGenerationException;
23+
import org.codehaus.jackson.map.JsonMappingException;
24+
import org.junit.Test;
25+
import org.powermock.core.classloader.annotations.PrepareForTest;
26+
27+
/**
28+
* Tests deserialization of an {@link OAuth2AccessToken} using jackson.
29+
*
30+
* @author Rob Winch
31+
*/
32+
@PrepareForTest(OAuth2AccessTokenDeserializer.class)
33+
public class TestOAuth2AccessTokenDeserializer extends BaseOAuth2AccessTokenJacksonTest {
34+
35+
@Test
36+
public void readValueNoRefresh() throws JsonGenerationException, JsonMappingException, IOException {
37+
accessToken.setRefreshToken(null);
38+
accessToken.setScope(null);
39+
OAuth2AccessToken actual = mapper.readValue(ACCESS_TOKEN_NOREFRESH, OAuth2AccessToken.class);
40+
assertTokenEquals(accessToken,actual);
41+
}
42+
43+
@Test
44+
public void readValueWithRefresh() throws JsonGenerationException, JsonMappingException, IOException {
45+
accessToken.setScope(null);
46+
OAuth2AccessToken actual = mapper.readValue(ACCESS_TOKEN_NOSCOPE, OAuth2AccessToken.class);
47+
assertTokenEquals(accessToken,actual);
48+
}
49+
50+
@Test
51+
public void readValueWithSingleScopes() throws JsonGenerationException, JsonMappingException, IOException {
52+
accessToken.getScope().remove(accessToken.getScope().iterator().next());
53+
OAuth2AccessToken actual = mapper.readValue(ACCESS_TOKEN_SINGLESCOPE, OAuth2AccessToken.class);
54+
assertTokenEquals(accessToken,actual);
55+
}
56+
57+
@Test
58+
public void readValueWithEmptyStringScope() throws JsonGenerationException, JsonMappingException, IOException {
59+
accessToken.setScope(new HashSet<String>());
60+
OAuth2AccessToken actual = mapper.readValue(ACCESS_TOKEN_EMPTYSCOPE, OAuth2AccessToken.class);
61+
assertTokenEquals(accessToken,actual);
62+
}
63+
64+
@Test
65+
public void readValueWithMultiScopes() throws Exception {
66+
OAuth2AccessToken actual = mapper.readValue(ACCESS_TOKEN_MULTISCOPE, OAuth2AccessToken.class);
67+
assertTokenEquals(accessToken,actual);
68+
}
69+
70+
@Test
71+
public void readValueWithMac() throws Exception {
72+
accessToken.setTokenType("mac");
73+
String encodedToken = ACCESS_TOKEN_MULTISCOPE.replace("bearer", accessToken.getTokenType());
74+
OAuth2AccessToken actual = mapper.readValue(encodedToken, OAuth2AccessToken.class);
75+
assertTokenEquals(accessToken,actual);
76+
}
77+
78+
private static void assertTokenEquals(OAuth2AccessToken expected, OAuth2AccessToken actual) {
79+
assertEquals(expected.getTokenType(), actual.getTokenType());
80+
assertEquals(expected.getValue(), actual.getValue());
81+
82+
OAuth2RefreshToken expectedRefreshToken = expected.getRefreshToken();
83+
if (expectedRefreshToken == null) {
84+
assertNull(actual.getRefreshToken());
85+
}
86+
else {
87+
assertEquals(expectedRefreshToken.getValue(), actual.getRefreshToken().getValue());
88+
}
89+
assertEquals(expected.getScope(), actual.getScope());
90+
Date expectedExpiration = expected.getExpiration();
91+
if (expectedExpiration == null) {
92+
assertNull(actual.getExpiration());
93+
}
94+
else {
95+
assertEquals(expectedExpiration.getTime(), actual.getExpiration().getTime());
96+
}
97+
}
98+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package org.springframework.security.oauth2.common;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.IOException;
6+
7+
import org.codehaus.jackson.JsonGenerationException;
8+
import org.codehaus.jackson.map.JsonMappingException;
9+
import org.junit.Test;
10+
import org.powermock.core.classloader.annotations.PrepareForTest;
11+
12+
/**
13+
* Tests serialization of an {@link OAuth2AccessToken} using jackson.
14+
*
15+
* @author Rob Winch
16+
*/
17+
@PrepareForTest(OAuth2AccessTokenSerializer.class)
18+
public class TestOAuth2AccessTokenSerializer extends BaseOAuth2AccessTokenJacksonTest {
19+
20+
@Test
21+
public void writeValueAsStringNoRefresh() throws JsonGenerationException, JsonMappingException, IOException {
22+
accessToken.setRefreshToken(null);
23+
accessToken.setScope(null);
24+
String encodedAccessToken = mapper.writeValueAsString(accessToken);
25+
assertEquals(BaseOAuth2AccessTokenJacksonTest.ACCESS_TOKEN_NOREFRESH, encodedAccessToken);
26+
}
27+
28+
@Test
29+
public void writeValueAsStringWithRefresh() throws JsonGenerationException, JsonMappingException, IOException {
30+
accessToken.setScope(null);
31+
String encodedAccessToken = mapper.writeValueAsString(accessToken);
32+
assertEquals(BaseOAuth2AccessTokenJacksonTest.ACCESS_TOKEN_NOSCOPE, encodedAccessToken);
33+
}
34+
35+
@Test
36+
public void writeValueAsStringWithEmptyScope() throws JsonGenerationException, JsonMappingException, IOException {
37+
accessToken.getScope().clear();
38+
String encodedAccessToken = mapper.writeValueAsString(accessToken);
39+
assertEquals(BaseOAuth2AccessTokenJacksonTest.ACCESS_TOKEN_NOSCOPE, encodedAccessToken);
40+
}
41+
42+
@Test
43+
public void writeValueAsStringWithSingleScopes() throws JsonGenerationException, JsonMappingException, IOException {
44+
accessToken.getScope().remove(accessToken.getScope().iterator().next());
45+
String encodedAccessToken = mapper.writeValueAsString(accessToken);
46+
assertEquals(BaseOAuth2AccessTokenJacksonTest.ACCESS_TOKEN_SINGLESCOPE, encodedAccessToken);
47+
}
48+
49+
@Test
50+
public void writeValueAsStringWithNullScope() throws JsonGenerationException, JsonMappingException, IOException {
51+
thrown.expect(JsonMappingException.class);
52+
thrown.expectMessage("Scopes cannot be null or empty. Got [null]");
53+
54+
accessToken.getScope().clear();
55+
accessToken.getScope().add(null);
56+
mapper.writeValueAsString(accessToken);
57+
}
58+
59+
@Test
60+
public void writeValueAsStringWithEmptyStringScope() throws JsonGenerationException, JsonMappingException,
61+
IOException {
62+
thrown.expect(JsonMappingException.class);
63+
thrown.expectMessage("Scopes cannot be null or empty. Got []");
64+
65+
accessToken.getScope().clear();
66+
accessToken.getScope().add("");
67+
mapper.writeValueAsString(accessToken);
68+
}
69+
70+
@Test
71+
public void writeValueAsStringWithQuoteInScope() throws JsonGenerationException, JsonMappingException, IOException {
72+
accessToken.getScope().add("\"");
73+
String encodedAccessToken = mapper.writeValueAsString(accessToken);
74+
assertEquals(
75+
"{\"access_token\":\"token-value\",\"token_type\":\"bearer\",\"refresh_token\":\"refresh-value\",\"expires_in\":10,\"scope\":\"\\\" read write\"}",
76+
encodedAccessToken);
77+
}
78+
79+
@Test
80+
public void writeValueAsStringWithMultiScopes() throws JsonGenerationException, JsonMappingException, IOException {
81+
String encodedAccessToken = mapper.writeValueAsString(accessToken);
82+
assertEquals(ACCESS_TOKEN_MULTISCOPE, encodedAccessToken);
83+
}
84+
85+
@Test
86+
public void writeValueAsStringWithMac() throws Exception {
87+
accessToken.setTokenType("mac");
88+
String expectedEncodedAccessToken = ACCESS_TOKEN_MULTISCOPE.replace("bearer", accessToken.getTokenType());
89+
String encodedAccessToken = mapper.writeValueAsString(accessToken);
90+
assertEquals(expectedEncodedAccessToken, encodedAccessToken);
91+
}
92+
}

0 commit comments

Comments
 (0)