2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.portal.authentication;
40 import static org.junit.Assert.*;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
45 import org.onap.portalapp.authentication.OpenIdConnectLoginStrategy;
46 import org.onap.portalapp.portal.framework.MockitoTestSuite;
47 import org.onap.portalapp.portal.utils.EPSystemProperties;
48 import org.onap.portalapp.util.EPUserUtils;
49 import org.onap.portalapp.util.SessionCookieUtil;
50 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
51 import org.onap.portalsdk.core.util.SystemProperties;
53 import org.junit.Before;
54 import org.junit.Test;
55 import org.junit.runner.RunWith;
56 import org.mitre.openid.connect.model.DefaultUserInfo;
57 import org.mitre.openid.connect.model.UserInfo;
58 import org.mockito.InjectMocks;
59 import org.mockito.Mockito;
60 import org.mockito.MockitoAnnotations;
61 import org.powermock.api.mockito.PowerMockito;
62 import org.powermock.core.classloader.annotations.PrepareForTest;
63 import org.powermock.modules.junit4.PowerMockRunner;
64 import org.springframework.util.StringUtils;
66 @RunWith(PowerMockRunner.class)
67 @PrepareForTest({StringUtils.class, EPUserUtils.class , SessionCookieUtil.class,SystemProperties.class})
68 public class OpenIdConnectLoginStrategyTest {
71 OpenIdConnectLoginStrategy OpenIdConnectLoginStrategy = new OpenIdConnectLoginStrategy();
75 MockitoAnnotations.initMocks(this);
78 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
80 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
81 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
83 NullPointerException nullPointerException = new NullPointerException();
86 public void loginTest() throws Exception
88 PowerMockito.mockStatic(StringUtils.class);
89 PowerMockito.mockStatic(EPUserUtils.class);
90 PowerMockito.mockStatic(SessionCookieUtil.class);
92 UserInfo userInfo = new DefaultUserInfo();
93 userInfo.setPreferredUsername("Test");
94 userInfo.setEmail("test@gmail.com");
95 userInfo.setName("first_name");
96 userInfo.setFamilyName("last_name");
97 Mockito.when(mockedRequest.getAttribute("userInfo")).thenReturn(userInfo);
98 assertTrue(OpenIdConnectLoginStrategy.login(mockedRequest, mockedResponse));
102 public void loginIfUserNullTest() throws Exception
104 PowerMockito.mockStatic(SystemProperties.class);
105 UserInfo userInfo = null;
106 Mockito.when(mockedRequest.getAttribute("userInfo")).thenReturn(userInfo);
107 // Mockito.when(SystemProperties.getProperty("authentication_mechanism")).thenReturn("auth");
108 assertFalse(OpenIdConnectLoginStrategy.login(mockedRequest, mockedResponse));
112 public void loginIfUserIfAuthIsOIDCTest() throws Exception
114 PowerMockito.mockStatic(SystemProperties.class);
115 UserInfo userInfo = null;
116 Mockito.when(mockedRequest.getAttribute("userInfo")).thenReturn(userInfo);
117 Mockito.when(SystemProperties.getProperty("authentication_mechanism")).thenReturn("OIDC");
118 Mockito.when(SystemProperties.getProperty(EPSystemProperties.LOGIN_URL_NO_RET_VAL)).thenReturn("login_url");
119 assertFalse(OpenIdConnectLoginStrategy.login(mockedRequest, mockedResponse));
123 public void loginIfUserIfAuthNotNullTest() throws Exception
125 PowerMockito.mockStatic(SystemProperties.class);
126 UserInfo userInfo = null;
127 Mockito.when(mockedRequest.getAttribute("userInfo")).thenReturn(userInfo);
128 Mockito.when(SystemProperties.getProperty("authentication_mechanism")).thenReturn("test");
129 Mockito.when(SystemProperties.getProperty(EPSystemProperties.LOGIN_URL_NO_RET_VAL)).thenReturn("login_url");
130 assertFalse(OpenIdConnectLoginStrategy.login(mockedRequest, mockedResponse));
134 public void loginIfUserExceptionest() throws Exception
136 PowerMockito.mockStatic(SystemProperties.class);
137 UserInfo userInfo = null;
138 Mockito.when(mockedRequest.getAttribute("userInfo")).thenReturn(userInfo);
139 Mockito.when(SystemProperties.getProperty("authentication_mechanism")).thenThrow(nullPointerException);
140 Mockito.when(SystemProperties.getProperty(EPSystemProperties.LOGIN_URL_NO_RET_VAL)).thenReturn("login_url");
141 assertFalse(OpenIdConnectLoginStrategy.login(mockedRequest, mockedResponse));
143 @Test(expected = Exception.class)
144 public void doLoginTest() throws Exception
146 OpenIdConnectLoginStrategy.doLogin(mockedRequest, mockedResponse);
149 @Test(expected = PortalAPIException.class)
150 public void getUserIdTest() throws Exception
152 OpenIdConnectLoginStrategy.getUserId(mockedRequest);