Skip to content

Commit 63d2463

Browse files
rwinchDave Syer
authored andcommitted
SECOAUTH-178: Refined OAuth2Exception Serialize/Deserialize tests
1 parent e5551c6 commit 63d2463

File tree

2 files changed

+81
-27
lines changed

2 files changed

+81
-27
lines changed

spring-security-oauth2/src/test/java/org/springframework/security/oauth2/common/exception/TestOAuth2ExceptionDeserializer.java

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
*/
1313
package org.springframework.security.oauth2.common.exception;
1414

15+
import static org.junit.Assert.*;
16+
17+
import java.util.Collections;
18+
1519
import org.codehaus.jackson.map.ObjectMapper;
1620
import org.junit.BeforeClass;
1721
import org.junit.Test;
@@ -26,9 +30,14 @@
2630
import org.springframework.security.oauth2.common.exceptions.UnsupportedGrantTypeException;
2731
import org.springframework.security.oauth2.common.exceptions.UserDeniedAuthorizationException;
2832

33+
/**
34+
*
35+
* @author Rob Winch
36+
*
37+
*/
2938
@SuppressWarnings("unused")
30-
public class TestOAuth2ExceptionDeserializer {
31-
39+
public class TestOAuth2ExceptionDeserializer {
40+
private static final String DETAILS = "some detail";
3241
private static ObjectMapper mapper;
3342

3443
@BeforeClass
@@ -37,67 +46,99 @@ public static void setUpClass() {
3746
}
3847

3948
@Test
40-
public void deserializeInvalidGrant() throws Exception {
49+
public void readValueInvalidGrant() throws Exception {
4150
String accessToken = createResponse(OAuth2Exception.INVALID_GRANT);
4251
InvalidGrantException result = (InvalidGrantException) mapper.readValue(accessToken, OAuth2Exception.class);
52+
assertEquals(DETAILS,result.getMessage());
53+
assertEquals(null,result.getAdditionalInformation());
4354
}
4455

4556
@Test
46-
public void deserializeInvalidRequest() throws Exception {
57+
public void readValueInvalidRequest() throws Exception {
4758
String accessToken = createResponse(OAuth2Exception.INVALID_REQUEST);
4859
InvalidRequestException result = (InvalidRequestException) mapper.readValue(accessToken, OAuth2Exception.class);
60+
assertEquals(DETAILS,result.getMessage());
61+
assertEquals(null,result.getAdditionalInformation());
4962
}
5063

5164
@Test
52-
public void deserializeInvalidScope() throws Exception {
65+
public void readValueInvalidScope() throws Exception {
5366
String accessToken = createResponse(OAuth2Exception.INVALID_SCOPE);
5467
InvalidScopeException result = (InvalidScopeException) mapper.readValue(accessToken, OAuth2Exception.class);
68+
assertEquals(DETAILS,result.getMessage());
69+
assertEquals(null,result.getAdditionalInformation());
5570
}
5671

5772
@Test
58-
public void deserializeUnsupportedGrantType() throws Exception {
73+
public void readValueUnsupportedGrantType() throws Exception {
5974
String accessToken = createResponse(OAuth2Exception.UNSUPPORTED_GRANT_TYPE);
60-
UnsupportedGrantTypeException result = (UnsupportedGrantTypeException) mapper.readValue(accessToken, OAuth2Exception.class);
75+
UnsupportedGrantTypeException result = (UnsupportedGrantTypeException) mapper.readValue(accessToken,
76+
OAuth2Exception.class);
77+
assertEquals(DETAILS,result.getMessage());
78+
assertEquals(null,result.getAdditionalInformation());
6179
}
6280

6381
@Test
64-
public void deserializeUnauthorizedClient() throws Exception {
82+
public void readValueUnauthorizedClient() throws Exception {
6583
String accessToken = createResponse(OAuth2Exception.UNAUTHORIZED_CLIENT);
66-
UnauthorizedClientException result = (UnauthorizedClientException) mapper.readValue(accessToken, OAuth2Exception.class);
84+
UnauthorizedClientException result = (UnauthorizedClientException) mapper.readValue(accessToken,
85+
OAuth2Exception.class);
86+
assertEquals(DETAILS,result.getMessage());
87+
assertEquals(null,result.getAdditionalInformation());
6788
}
6889

6990
@Test
70-
public void deserializeAccessDenied() throws Exception {
91+
public void readValueAccessDenied() throws Exception {
7192
String accessToken = createResponse(OAuth2Exception.ACCESS_DENIED);
72-
UserDeniedAuthorizationException result = (UserDeniedAuthorizationException) mapper.readValue(accessToken, OAuth2Exception.class);
93+
UserDeniedAuthorizationException result = (UserDeniedAuthorizationException) mapper.readValue(accessToken,
94+
OAuth2Exception.class);
95+
assertEquals(DETAILS,result.getMessage());
96+
assertEquals(null,result.getAdditionalInformation());
7397
}
7498

7599
@Test
76-
public void deserializeRedirectUriMismatch() throws Exception {
100+
public void readValueRedirectUriMismatch() throws Exception {
77101
String accessToken = createResponse(OAuth2Exception.REDIRECT_URI_MISMATCH);
78-
RedirectMismatchException result = (RedirectMismatchException)mapper.readValue(accessToken, OAuth2Exception.class);
102+
RedirectMismatchException result = (RedirectMismatchException) mapper.readValue(accessToken,
103+
OAuth2Exception.class);
104+
assertEquals(DETAILS,result.getMessage());
105+
assertEquals(null,result.getAdditionalInformation());
79106
}
80107

81108
@Test
82-
public void deserializeInvalidToken() throws Exception {
109+
public void readValueInvalidToken() throws Exception {
83110
String accessToken = createResponse(OAuth2Exception.INVALID_TOKEN);
84111
InvalidTokenException result = (InvalidTokenException) mapper.readValue(accessToken, OAuth2Exception.class);
112+
assertEquals(DETAILS,result.getMessage());
113+
assertEquals(null,result.getAdditionalInformation());
85114
}
86115

87116
@Test
88-
public void deserializeUndefinedException() throws Exception {
117+
public void readValueUndefinedException() throws Exception {
89118
String accessToken = createResponse("notdefinedcode");
90119
OAuth2Exception result = mapper.readValue(accessToken, OAuth2Exception.class);
120+
assertEquals(DETAILS,result.getMessage());
121+
assertEquals(null,result.getAdditionalInformation());
91122
}
92123

93124
@Test
94-
public void deserializeInvalidClient() throws Exception {
125+
public void readValueInvalidClient() throws Exception {
95126
String accessToken = createResponse(OAuth2Exception.INVALID_CLIENT);
96127
InvalidClientException result = (InvalidClientException) mapper.readValue(accessToken, OAuth2Exception.class);
128+
assertEquals(DETAILS,result.getMessage());
129+
assertEquals(null,result.getAdditionalInformation());
130+
}
131+
132+
@Test
133+
public void readValueWithAdditionalDetails() throws Exception {
134+
String accessToken = "{\"error\": \"invalid_client\", \"error_description\": \"some detail\", \"foo\": \"bar\"}";
135+
InvalidClientException result = (InvalidClientException) mapper.readValue(accessToken, OAuth2Exception.class);
136+
assertEquals(DETAILS,result.getMessage());
137+
assertEquals("{foo=bar}",result.getAdditionalInformation().toString());
97138
}
98139

99140
private String createResponse(String error) {
100-
return "{\"error\":\""+error+"\",\"error_description\":\"some detail\"}";
141+
return "{\"error\":\"" + error + "\",\"error_description\":\"some detail\"}";
101142
}
102143

103144
}

spring-security-oauth2/src/test/java/org/springframework/security/oauth2/common/exception/TestOAuth2ExceptionSerializer.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
import org.springframework.security.oauth2.common.exceptions.UnsupportedGrantTypeException;
3030
import org.springframework.security.oauth2.common.exceptions.UserDeniedAuthorizationException;
3131

32+
/**
33+
*
34+
* @author Rob Winch
35+
*
36+
*/
3237
public class TestOAuth2ExceptionSerializer {
3338

3439
private static final String DETAILS = "some detail";
@@ -46,75 +51,83 @@ public void tearDown() {
4651
}
4752

4853
@Test
49-
public void serializeInvalidClient() throws Exception {
54+
public void writeValueAsStringInvalidClient() throws Exception {
5055
oauthException = new InvalidClientException(DETAILS);
5156
String expected = createResponse(oauthException.getOAuth2ErrorCode());
5257
assertEquals(expected,mapper.writeValueAsString(oauthException));
5358
}
5459

5560
@Test
56-
public void serializeInvalidGrant() throws Exception {
61+
public void writeValueAsStringInvalidGrant() throws Exception {
5762
oauthException = new InvalidGrantException(DETAILS);
5863
String expected = createResponse(oauthException.getOAuth2ErrorCode());
5964
assertEquals(expected,mapper.writeValueAsString(oauthException));
6065
}
6166

6267
@Test
63-
public void serializeInvalidRequest() throws Exception {
68+
public void writeValueAsStringInvalidRequest() throws Exception {
6469
oauthException = new InvalidRequestException(DETAILS);
6570
String expected = createResponse(oauthException.getOAuth2ErrorCode());
6671
assertEquals(expected,mapper.writeValueAsString(oauthException));
6772
}
6873

6974
@Test
70-
public void serializeInvalidScope() throws Exception {
75+
public void writeValueAsStringInvalidScope() throws Exception {
7176
oauthException = new InvalidScopeException(DETAILS);
7277
String expected = createResponse(oauthException.getOAuth2ErrorCode());
7378
assertEquals(expected,mapper.writeValueAsString(oauthException));
7479
}
7580

7681
@Test
77-
public void serializeUnsupportedGrantType() throws Exception {
82+
public void writeValueAsStringUnsupportedGrantType() throws Exception {
7883
oauthException = new UnsupportedGrantTypeException(DETAILS);
7984
String expected = createResponse(oauthException.getOAuth2ErrorCode());
8085
assertEquals(expected,mapper.writeValueAsString(oauthException));
8186
}
8287

8388
@Test
84-
public void serializeUnauthorizedClient() throws Exception {
89+
public void writeValueAsStringUnauthorizedClient() throws Exception {
8590
oauthException = new UnauthorizedClientException(DETAILS);
8691
String expected = createResponse(oauthException.getOAuth2ErrorCode());
8792
assertEquals(expected,mapper.writeValueAsString(oauthException));
8893
}
8994

9095
@Test
91-
public void serializeAccessDenied() throws Exception {
96+
public void writeValueAsStringAccessDenied() throws Exception {
9297
oauthException = new UserDeniedAuthorizationException(DETAILS);
9398
String expected = createResponse(oauthException.getOAuth2ErrorCode());
9499
assertEquals(expected,mapper.writeValueAsString(oauthException));
95100
}
96101

97102
@Test
98-
public void serializeRedirectUriMismatch() throws Exception {
103+
public void writeValueAsStringRedirectUriMismatch() throws Exception {
99104
oauthException = new RedirectMismatchException(DETAILS);
100105
String expected = createResponse(oauthException.getOAuth2ErrorCode());
101106
assertEquals(expected,mapper.writeValueAsString(oauthException));
102107
}
103108

104109
@Test
105-
public void serializeInvalidToken() throws Exception {
110+
public void writeValueAsStringInvalidToken() throws Exception {
106111
oauthException = new InvalidTokenException(DETAILS);
107112
String expected = createResponse(oauthException.getOAuth2ErrorCode());
108113
assertEquals(expected,mapper.writeValueAsString(oauthException));
109114
}
110115

111116
@Test
112-
public void serializeOAuth2Exception() throws Exception {
117+
public void writeValueAsStringOAuth2Exception() throws Exception {
113118
oauthException = new OAuth2Exception(DETAILS);
114119
String expected = createResponse(oauthException.getOAuth2ErrorCode());
115120
assertEquals(expected,mapper.writeValueAsString(oauthException));
116121
}
117122

123+
@Test
124+
public void writeValueAsStringWithAdditionalDetails() throws Exception {
125+
oauthException = new InvalidClientException(DETAILS);
126+
oauthException.addAdditionalInformation("foo", "bar");
127+
String expected = "{\"error\":\"invalid_client\",\"error_description\":\"some detail\",\"foo\":\"bar\"}";
128+
assertEquals(expected,mapper.writeValueAsString(oauthException));
129+
}
130+
118131
private String createResponse(String error) {
119132
return "{\"error\":\""+error+"\",\"error_description\":\"some detail\"}";
120133
}

0 commit comments

Comments
 (0)