2 * Copyright (c) 2014, 2015 Hewlett-Packard Development Company, L.P. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.aaa.sts;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Matchers.anyString;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.when;
18 import java.util.Arrays;
19 import org.eclipse.jetty.testing.HttpTester;
20 import org.eclipse.jetty.testing.ServletTester;
21 import org.junit.After;
22 import org.junit.AfterClass;
23 import org.junit.Before;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.opendaylight.aaa.AuthenticationBuilder;
27 import org.opendaylight.aaa.ClaimBuilder;
28 import org.opendaylight.aaa.api.AuthenticationService;
29 import org.opendaylight.aaa.api.Claim;
30 import org.opendaylight.aaa.api.ClientService;
31 import org.opendaylight.aaa.api.CredentialAuth;
32 import org.opendaylight.aaa.api.IdMService;
33 import org.opendaylight.aaa.api.PasswordCredentials;
34 import org.opendaylight.aaa.api.TokenAuth;
35 import org.opendaylight.aaa.api.TokenStore;
38 * A unit test for token endpoint.
43 public class TokenEndpointTest {
44 private static final long TOKEN_TIMEOUT_SECS = 10;
45 private static final String CONTEXT = "/oauth2";
46 private static final String DIRECT_AUTH = "grant_type=password&username=admin&password=admin&scope=pepsi&client_id=dlux&client_secret=secrete";
47 private static final String REFRESH_TOKEN = "grant_type=refresh_token&refresh_token=whateverisgood&scope=pepsi";
49 private static final Claim claim = new ClaimBuilder().setUser("bob").setUserId("1234")
50 .addRole("admin").build();
51 private final static ServletTester server = new ServletTester();
54 public static void init() throws Exception {
56 server.setContextPath(CONTEXT);
58 // Add our servlet under test
59 server.addServlet(TokenEndpoint.class, "/revoke");
60 server.addServlet(TokenEndpoint.class, "/token");
67 public static void shutdown() throws Exception {
74 when(ServiceLocator.getInstance().getTokenStore().tokenExpiration()).thenReturn(
79 public void teardown() {
80 ServiceLocator.getInstance().getTokenAuthCollection().clear();
84 public void testCreateToken401() throws Exception {
85 HttpTester req = new HttpTester();
86 req.setMethod("POST");
87 req.setHeader("Content-Type", "application/x-www-form-urlencoded");
88 req.setContent(DIRECT_AUTH);
89 req.setURI(CONTEXT + TokenEndpoint.TOKEN_GRANT_ENDPOINT);
90 req.setVersion("HTTP/1.0");
92 HttpTester resp = new HttpTester();
93 resp.parse(server.getResponses(req.generate()));
94 assertEquals(401, resp.getStatus());
98 public void testCreateTokenWithPassword() throws Exception {
100 ServiceLocator.getInstance().getCredentialAuth()
101 .authenticate(any(PasswordCredentials.class))).thenReturn(claim);
103 HttpTester req = new HttpTester();
104 req.setMethod("POST");
105 req.setHeader("Content-Type", "application/x-www-form-urlencoded");
106 req.setContent(DIRECT_AUTH);
107 req.setURI(CONTEXT + TokenEndpoint.TOKEN_GRANT_ENDPOINT);
108 req.setVersion("HTTP/1.0");
110 HttpTester resp = new HttpTester();
111 resp.parse(server.getResponses(req.generate()));
112 assertEquals(201, resp.getStatus());
113 assertTrue(resp.getContent().contains("expires_in\":10"));
114 assertTrue(resp.getContent().contains("Bearer"));
118 public void testCreateTokenWithRefreshToken() throws Exception {
119 when(ServiceLocator.getInstance().getTokenStore().get(anyString())).thenReturn(
120 new AuthenticationBuilder(claim).build());
121 when(ServiceLocator.getInstance().getIdmService().listRoles(anyString(), anyString())).thenReturn(
122 Arrays.asList("admin", "user"));
124 HttpTester req = new HttpTester();
125 req.setMethod("POST");
126 req.setHeader("Content-Type", "application/x-www-form-urlencoded");
127 req.setContent(REFRESH_TOKEN);
128 req.setURI(CONTEXT + TokenEndpoint.TOKEN_GRANT_ENDPOINT);
129 req.setVersion("HTTP/1.0");
131 HttpTester resp = new HttpTester();
132 resp.parse(server.getResponses(req.generate()));
133 assertEquals(201, resp.getStatus());
134 assertTrue(resp.getContent().contains("expires_in\":10"));
135 assertTrue(resp.getContent().contains("Bearer"));
139 public void testDeleteToken() throws Exception {
140 when(ServiceLocator.getInstance().getTokenStore().delete("token_to_be_deleted")).thenReturn(
143 HttpTester req = new HttpTester();
144 req.setMethod("POST");
145 req.setHeader("Content-Type", "application/x-www-form-urlencoded");
146 req.setContent("token_to_be_deleted");
147 req.setURI(CONTEXT + TokenEndpoint.TOKEN_REVOKE_ENDPOINT);
148 req.setVersion("HTTP/1.0");
150 HttpTester resp = new HttpTester();
151 resp.parse(server.getResponses(req.generate()));
152 assertEquals(204, resp.getStatus());
155 @SuppressWarnings("unchecked")
156 private static void mockServiceLocator() {
157 ServiceLocator.getInstance().setClientService(mock(ClientService.class));
158 ServiceLocator.getInstance().setIdmService(mock(IdMService.class));
159 ServiceLocator.getInstance().setAuthenticationService(mock(AuthenticationService.class));
160 ServiceLocator.getInstance().setTokenStore(mock(TokenStore.class));
161 ServiceLocator.getInstance().setCredentialAuth(mock(CredentialAuth.class));
162 ServiceLocator.getInstance().getTokenAuthCollection().add(mock(TokenAuth.class));