/
TokenAdminEndpointsTests.java
169 lines (145 loc) · 7.14 KB
/
TokenAdminEndpointsTests.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Cloud Foundry 2012.02.03 Beta
* Copyright (c) [2009-2012] VMware, Inc. All Rights Reserved.
*
* This product is licensed to you under the Apache License, Version 2.0 (the "License").
* You may not use this product except in compliance with the License.
*
* This product includes a number of subcomponents with
* separate copyright notices and license terms. Your use of these
* subcomponents is subject to the terms and conditions of the
* subcomponent's license, as noted in the LICENSE file.
*/
package org.cloudfoundry.identity.uaa.oauth;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.Collection;
import java.util.Collections;
import org.cloudfoundry.identity.uaa.message.SimpleMessage;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.crypto.password.StandardPasswordEncoder;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.AuthorizationRequest;
import org.springframework.security.oauth2.provider.DefaultAuthorizationRequest;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
/**
* @author Dave Syer
*
*/
public class TokenAdminEndpointsTests {
private TokenAdminEndpoints endpoints = new TokenAdminEndpoints();
private ConsumerTokenServices tokenServices = Mockito.mock(ConsumerTokenServices.class);
private AuthorizationRequest authorizationRequest = new DefaultAuthorizationRequest(Collections.singletonMap(
"client_id", "foo"));
{
endpoints.setTokenServices(tokenServices);
}
@Test
public void testListTokensForOAuth2User() throws Exception {
Mockito.when(tokenServices.findTokensByUserName("marissa")).thenReturn(
Collections.<OAuth2AccessToken> singleton(new DefaultOAuth2AccessToken("FOO")));
Collection<OAuth2AccessToken> tokens = endpoints.listTokensForUser("marissa", new OAuth2Authentication(
authorizationRequest, new TestingAuthenticationToken("marissa", "")));
assertEquals(1, tokens.size());
assertNotNull(tokens.iterator().next().getAdditionalInformation().get(JwtTokenEnhancer.TOKEN_ID));
}
@Test
public void testListTokensForOAuth2UserWithClientId() throws Exception {
Mockito.when(tokenServices.findTokensByUserName("marissa")).thenReturn(
Collections.<OAuth2AccessToken> singleton(new DefaultOAuth2AccessToken("FOO")));
Mockito.when(tokenServices.getClientId("FOO")).thenReturn("foo");
Collection<OAuth2AccessToken> tokens = endpoints.listTokensForUser("marissa", new OAuth2Authentication(
authorizationRequest, new TestingAuthenticationToken("marissa", "")));
assertEquals(1, tokens.size());
assertNotNull(tokens.iterator().next().getAdditionalInformation().get(JwtTokenEnhancer.TOKEN_ID));
}
@Test
public void testListTokensForOAuth2UserByClient() throws Exception {
Collection<OAuth2AccessToken> tokens = endpoints.listTokensForUser("marissa", new OAuth2Authentication(
authorizationRequest, null));
assertEquals(0, tokens.size());
}
@Test
public void testListTokensForUser() throws Exception {
Collection<OAuth2AccessToken> tokens = endpoints.listTokensForUser("marissa", new TestingAuthenticationToken(
"marissa", ""));
assertEquals(0, tokens.size());
}
@Test
public void testRevokeTokenForUser() throws Exception {
Mockito.when(tokenServices.findTokensByUserName("marissa")).thenReturn(
Collections.<OAuth2AccessToken> singleton(new DefaultOAuth2AccessToken("FOO")));
Mockito.when(tokenServices.revokeToken("FOO")).thenReturn(true);
SimpleMessage result = endpoints.revokeUserToken("marissa", new StandardPasswordEncoder().encode("FOO"),
new TestingAuthenticationToken("marissa", ""));
assertEquals("ok", result.getStatus());
}
@Test
public void testRevokeTokenForUserWithTokenId() throws Exception {
DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("FOO");
token.setAdditionalInformation(Collections.<String, Object> singletonMap(JwtTokenEnhancer.TOKEN_ID, "BAR"));
Mockito.when(tokenServices.findTokensByUserName("marissa")).thenReturn(
Collections.<OAuth2AccessToken> singleton(token));
Mockito.when(tokenServices.revokeToken("FOO")).thenReturn(true);
SimpleMessage result = endpoints.revokeUserToken("marissa", "BAR", new TestingAuthenticationToken(
"marissa", ""));
assertEquals("ok", result.getStatus());
}
@Test(expected=NoSuchTokenException.class)
public void testRevokeInvalidTokenForUser() throws Exception {
OAuth2AccessToken token = new DefaultOAuth2AccessToken("BAR");
Mockito.when(tokenServices.findTokensByUserName("marissa")).thenReturn(Collections.singleton(token));
SimpleMessage result = endpoints.revokeUserToken("marissa", "FOO", new TestingAuthenticationToken(
"marissa", ""));
assertEquals("ok", result.getStatus());
}
@Test(expected=NoSuchTokenException.class)
public void testRevokeNullTokenForUser() throws Exception {
SimpleMessage result = endpoints.revokeUserToken("marissa", null, new TestingAuthenticationToken("marissa",
""));
assertEquals("ok", result.getStatus());
}
@Test(expected = AccessDeniedException.class)
public void testListTokensForWrongUser() throws Exception {
Collection<OAuth2AccessToken> tokens = endpoints.listTokensForUser("barry", new TestingAuthenticationToken(
"marissa", ""));
assertEquals(0, tokens.size());
}
@Test(expected = AccessDeniedException.class)
public void testListTokensForWrongOAuth2User() throws Exception {
Collection<OAuth2AccessToken> tokens = endpoints.listTokensForUser("barry", new OAuth2Authentication(
authorizationRequest, new TestingAuthenticationToken("marissa", "")));
assertEquals(0, tokens.size());
}
@Test
public void testListTokensForClient() throws Exception {
Collection<OAuth2AccessToken> tokens = endpoints.listTokensForClient("foo", new OAuth2Authentication(
authorizationRequest, null));
assertEquals(0, tokens.size());
}
@Test(expected = AccessDeniedException.class)
public void testListTokensForWrongClient() throws Exception {
Collection<OAuth2AccessToken> tokens = endpoints.listTokensForClient("bar", new OAuth2Authentication(
authorizationRequest, null));
assertEquals(0, tokens.size());
}
@Test
public void testRevokeTokenForClient() throws Exception {
Mockito.when(tokenServices.findTokensByClientId("foo")).thenReturn(
Collections.<OAuth2AccessToken> singleton(new DefaultOAuth2AccessToken("FOO")));
Mockito.when(tokenServices.revokeToken("FOO")).thenReturn(true);
SimpleMessage result = endpoints.revokeClientToken("foo", new StandardPasswordEncoder().encode("FOO"),
new TestingAuthenticationToken("foo", ""));
assertEquals("ok", result.getStatus());
}
@Test(expected=NoSuchTokenException.class)
public void testRevokeInvalidTokenForClient() throws Exception {
SimpleMessage result = endpoints.revokeClientToken("foo", "FOO", new TestingAuthenticationToken("foo", ""));
assertEquals("ok", result.getStatus());
}
}