Skip to content

Commit 3656e7a

Browse files
committed
Add tests to OAuth2AuthorizationServerJackson2ModuleTests
1 parent 1cca9c5 commit 3656e7a

File tree

1 file changed

+58
-30
lines changed

1 file changed

+58
-30
lines changed

oauth2/oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/jackson2/OAuth2AuthorizationServerJackson2ModuleTests.java

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,73 +16,101 @@
1616

1717
package org.springframework.security.oauth2.server.authorization.jackson2;
1818

19-
import java.util.Arrays;
20-
import java.util.Collections;
21-
import java.util.HashMap;
22-
import java.util.HashSet;
23-
import java.util.LinkedHashSet;
19+
import java.security.Principal;
20+
import java.util.List;
2421
import java.util.Map;
25-
import java.util.Set;
2622

2723
import com.fasterxml.jackson.core.type.TypeReference;
24+
import com.fasterxml.jackson.databind.Module;
2825
import com.fasterxml.jackson.databind.ObjectMapper;
2926
import org.junit.jupiter.api.BeforeEach;
3027
import org.junit.jupiter.api.Test;
3128

29+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
30+
import org.springframework.security.core.Authentication;
31+
import org.springframework.security.jackson2.SecurityJackson2Modules;
32+
import org.springframework.security.oauth2.jose.jws.MacAlgorithm;
33+
import org.springframework.security.oauth2.jwt.JwtClaimNames;
34+
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
35+
import org.springframework.security.oauth2.server.authorization.TestOAuth2Authorizations;
36+
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2TokenExchangeActor;
37+
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2TokenExchangeCompositeAuthenticationToken;
38+
import org.springframework.security.oauth2.server.authorization.settings.ClientSettings;
39+
import org.springframework.security.oauth2.server.authorization.settings.TokenSettings;
40+
3241
import static org.assertj.core.api.Assertions.assertThat;
3342

3443
/**
3544
* Tests for {@link OAuth2AuthorizationServerJackson2Module}.
3645
*
3746
* @author Steve Riesenberg
47+
* @author Joe Grandja
3848
*/
3949
public class OAuth2AuthorizationServerJackson2ModuleTests {
4050

4151
private static final TypeReference<Map<String, Object>> STRING_OBJECT_MAP = new TypeReference<>() {
4252
};
4353

44-
private static final TypeReference<Set<String>> STRING_SET = new TypeReference<>() {
45-
};
46-
47-
private static final TypeReference<String[]> STRING_ARRAY = new TypeReference<>() {
48-
};
49-
5054
private ObjectMapper objectMapper;
5155

5256
@BeforeEach
5357
public void setup() {
5458
this.objectMapper = new ObjectMapper();
59+
ClassLoader classLoader = OAuth2AuthorizationServerJackson2Module.class.getClassLoader();
60+
List<Module> securityModules = SecurityJackson2Modules.getModules(classLoader);
61+
this.objectMapper.registerModules(securityModules);
5562
this.objectMapper.registerModule(new OAuth2AuthorizationServerJackson2Module());
5663
}
5764

5865
@Test
59-
public void readValueWhenUnmodifiableMapThenSuccess() throws Exception {
60-
Map<String, Object> map = Collections.unmodifiableMap(new HashMap<>(Collections.singletonMap("key", "value")));
61-
String json = this.objectMapper.writeValueAsString(map);
62-
assertThat(this.objectMapper.readValue(json, STRING_OBJECT_MAP)).isEqualTo(map);
66+
public void readValueWhenOAuth2AuthorizationAttributesThenSuccess() throws Exception {
67+
Authentication principal = new UsernamePasswordAuthenticationToken("principal", "credentials");
68+
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization()
69+
.attributes(attrs -> attrs.put(Principal.class.getName(), principal))
70+
.build();
71+
Map<String, Object> attributes = authorization.getAttributes();
72+
String json = this.objectMapper.writeValueAsString(attributes);
73+
assertThat(this.objectMapper.readValue(json, STRING_OBJECT_MAP)).isEqualTo(attributes);
74+
}
75+
76+
@Test
77+
public void readValueWhenOAuth2AccessTokenMetadataThenSuccess() throws Exception {
78+
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization().build();
79+
Map<String, Object> metadata = authorization.getAccessToken().getMetadata();
80+
String json = this.objectMapper.writeValueAsString(metadata);
81+
assertThat(this.objectMapper.readValue(json, STRING_OBJECT_MAP)).isEqualTo(metadata);
6382
}
6483

6584
@Test
66-
public void readValueWhenHashSetThenSuccess() throws Exception {
67-
Set<String> set = new HashSet<>(Arrays.asList("one", "two"));
68-
String json = this.objectMapper.writeValueAsString(set);
69-
assertThat(this.objectMapper.readValue(json, STRING_SET)).isEqualTo(set);
85+
public void readValueWhenClientSettingsThenSuccess() throws Exception {
86+
ClientSettings clientSettings = ClientSettings.builder()
87+
.tokenEndpointAuthenticationSigningAlgorithm(MacAlgorithm.HS256)
88+
.build();
89+
Map<String, Object> clientSettingsMap = clientSettings.getSettings();
90+
String json = this.objectMapper.writeValueAsString(clientSettingsMap);
91+
assertThat(this.objectMapper.readValue(json, STRING_OBJECT_MAP)).isEqualTo(clientSettingsMap);
7092
}
7193

72-
// gh-457
7394
@Test
74-
public void readValueWhenLinkedHashSetThenSuccess() throws Exception {
75-
Set<String> set = new LinkedHashSet<>(Arrays.asList("one", "two"));
76-
String json = this.objectMapper.writeValueAsString(set);
77-
assertThat(this.objectMapper.readValue(json, STRING_SET)).isEqualTo(set);
95+
public void readValueWhenTokenSettingsThenSuccess() throws Exception {
96+
TokenSettings tokenSettings = TokenSettings.builder().build();
97+
Map<String, Object> tokenSettingsMap = tokenSettings.getSettings();
98+
String json = this.objectMapper.writeValueAsString(tokenSettingsMap);
99+
assertThat(this.objectMapper.readValue(json, STRING_OBJECT_MAP)).isEqualTo(tokenSettingsMap);
78100
}
79101

80-
// gh-1666
81102
@Test
82-
public void readValueWhenStringArrayThenSuccess() throws Exception {
83-
String[] array = new String[] { "one", "two" };
84-
String json = this.objectMapper.writeValueAsString(array);
85-
assertThat(this.objectMapper.readValue(json, STRING_ARRAY)).isEqualTo(array);
103+
public void readValueWhenOAuth2TokenExchangeCompositeAuthenticationTokenThenSuccess() throws Exception {
104+
Authentication subject = new UsernamePasswordAuthenticationToken("principal", "credentials");
105+
OAuth2TokenExchangeActor actor1 = new OAuth2TokenExchangeActor(
106+
Map.of(JwtClaimNames.ISS, "issuer-1", JwtClaimNames.SUB, "actor1"));
107+
OAuth2TokenExchangeActor actor2 = new OAuth2TokenExchangeActor(
108+
Map.of(JwtClaimNames.ISS, "issuer-2", JwtClaimNames.SUB, "actor2"));
109+
OAuth2TokenExchangeCompositeAuthenticationToken authentication = new OAuth2TokenExchangeCompositeAuthenticationToken(
110+
subject, List.of(actor1, actor2));
111+
String json = this.objectMapper.writeValueAsString(authentication);
112+
assertThat(this.objectMapper.readValue(json, OAuth2TokenExchangeCompositeAuthenticationToken.class))
113+
.isEqualTo(authentication);
86114
}
87115

88116
}

0 commit comments

Comments
 (0)