2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.auth.oauth;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.mockito.Mockito.when;
28 import static org.mockito.MockitoAnnotations.initMocks;
31 import java.util.Properties;
32 import java.util.TreeMap;
34 import javax.servlet.http.HttpServletRequest;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mock;
39 import org.onap.aaf.auth.env.AuthzEnv;
40 import org.onap.aaf.auth.env.AuthzTrans;
41 import org.onap.aaf.auth.layer.Result;
42 import org.onap.aaf.auth.oauth.facade.DirectIntrospect;
43 import org.onap.aaf.auth.rserv.TransFilter;
44 import org.onap.aaf.cadi.CadiException;
45 import org.onap.aaf.cadi.PropAccess;
46 import org.onap.aaf.cadi.taf.TafResp;
47 import org.onap.aaf.misc.env.APIException;
49 import aafoauth.v2_0.Introspect;
51 public class JU_DirectOAuthTAF {
57 private PropAccess access;
59 private Properties props = new Properties();
62 private HttpServletRequest req;
64 private Map<String, String[]> parameterMap;
66 private DirectIntrospect<Introspect> facade;
68 private AuthzTrans trans;
70 private Result<Introspect> ri;
75 parameterMap = new TreeMap<String, String[]>();
80 public void testValidateWithoutSecret() throws APIException, CadiException {
81 parameterMap.put("client_id", new String[] { "Client1" });
82 // parameterMap.put("client_secret", new String[] { "Secret1" });
83 parameterMap.put("username", new String[] { "User1" });
84 parameterMap.put("password", new String[] { "Pass1" });
85 parameterMap.put("token", new String[] { "token1" });
86 when(env.access()).thenReturn(access);
87 when(access.getProperties()).thenReturn(props);
88 when(req.getContentType()).thenReturn("application/x-www-form-urlencoded");
89 when(req.getParameterMap()).thenReturn(parameterMap);
91 DirectOAuthTAF oAuthTaf = new DirectOAuthTAF(env, null, null);
93 TafResp validate = oAuthTaf.validate(null, req, null);
95 assertNotNull(validate);
96 assertEquals(validate.getAccess(), access);
97 assertEquals(validate.desc(), "client_id and client_secret required");
101 public void testValidateWithSecret() throws APIException, CadiException {
102 parameterMap.put("client_id", new String[] { "Client1" });
103 parameterMap.put("client_secret", new String[] { "Secret1" });
104 parameterMap.put("username", new String[] { "User1" });
105 parameterMap.put("password", new String[] { "Pass1" });
106 parameterMap.put("token", new String[] { "token1" });
108 when(env.access()).thenReturn(access);
109 when(access.getProperties()).thenReturn(props);
110 when(req.getContentType()).thenReturn("application/x-www-form-urlencoded");
111 when(req.getParameterMap()).thenReturn(parameterMap);
112 when(req.getAttribute(TransFilter.TRANS_TAG)).thenReturn(trans);
113 when(facade.mappedIntrospect(trans, "token1")).thenReturn(ri);
115 DirectOAuthTAF oAuthTaf = new DirectOAuthTAF(env, null, facade);
117 TafResp validate = oAuthTaf.validate(null, req, null);
119 assertNotNull(validate);
120 assertEquals(validate.getAccess(), access);
121 assertEquals(validate.desc(), ri.errorString());
123 assertNull(oAuthTaf.revalidate(null, null));
124 assertNotNull(oAuthTaf.directUserPass());