222c83c639f441e774e9c1fa3f4e4963cfbe9ba0
[aai/sparky-be.git] / src / test / java / org / onap / aai / sparky / security / portal / TestPortalRestAPIServiceImpl.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25
26 package org.onap.aai.sparky.security.portal;
27
28 import static org.hamcrest.Matchers.empty;
29 import static org.hamcrest.Matchers.is;
30 import static org.hamcrest.Matchers.nullValue;
31 import static org.junit.Assert.assertThat;
32 import static org.mockito.Mockito.when;
33
34 import java.io.File;
35 import java.nio.file.Files;
36 import java.nio.file.Paths;
37 import java.util.ArrayList;
38 import java.util.HashSet;
39 import java.util.LinkedHashSet;
40 import java.util.List;
41 import java.util.Set;
42
43 import javax.servlet.http.HttpServletRequest;
44
45 import org.junit.After;
46 import org.junit.AfterClass;
47 import org.junit.Before;
48 import org.junit.BeforeClass;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.mockito.InjectMocks;
52 import org.mockito.Mock;
53 import org.mockito.Mockito;
54 import org.onap.aai.sparky.security.portal.PortalRestAPIServiceImpl;
55 import org.onap.aai.sparky.security.portal.UserManager;
56 import org.onap.aai.sparky.security.portal.config.PortalAuthenticationConfig;
57 import org.onap.aai.sparky.security.portal.config.RolesConfig;
58 import org.openecomp.portalsdk.core.onboarding.exception.PortalAPIException;
59 import org.openecomp.portalsdk.core.restful.domain.EcompRole;
60 import org.openecomp.portalsdk.core.restful.domain.EcompUser;
61 import org.powermock.core.classloader.annotations.PowerMockIgnore;
62 import org.powermock.core.classloader.annotations.PrepareForTest;
63 import org.powermock.modules.junit4.PowerMockRunner;
64 import org.powermock.reflect.Whitebox;
65
66 @PowerMockIgnore({ "javax.crypto.*" })
67 @RunWith(PowerMockRunner.class)
68 @PrepareForTest({ PortalAuthenticationConfig.class, RolesConfig.class })
69 public class TestPortalRestAPIServiceImpl {
70
71   private static File testUsersFile;
72   private static final String LOGINID_1 = "200";
73   private static final String LOGINID_2 = "201";
74   private static final String VIEW_ROLE = "View";
75
76   enum TestData {
77     // @formatter:off
78     TEST_USERS                       ("src/test/resources/portal/test-users.config"),
79     PORTAL_AUTHENTICATION_PROPERTIES ("src/test/resources/portal/portal-authentication.properties"),
80     ROLES_CONFIG_FILE                ("src/test/resources/portal/roles.config");
81
82     private String filename;
83     TestData(String filename) {this.filename = filename;}
84     public String getFilename() {return this.filename;}
85     // @formatter:on
86   }
87
88   @Mock
89   private UserManager userManager = new UserManager(testUsersFile);
90
91   @InjectMocks
92   private PortalRestAPIServiceImpl portalApi = new PortalRestAPIServiceImpl();
93
94   @BeforeClass
95   public static void setUpBeforeClass() throws Exception {
96     testUsersFile = Paths.get(TestData.TEST_USERS.getFilename()).toFile();
97   }
98
99   @AfterClass
100   public static void tearDownAfterClass() throws Exception {
101     Files.deleteIfExists(testUsersFile.toPath());
102   }
103
104   @Before
105   public void setUp() throws Exception {
106     Whitebox.setInternalState(RolesConfig.class, "ROLES_CONFIG_FILE",
107         TestData.ROLES_CONFIG_FILE.getFilename());
108   }
109
110   @After
111   public void tearDown() throws Exception {
112     Files.deleteIfExists(testUsersFile.toPath());
113   }
114
115   @Test
116   public void testPushAndGetUser() throws Exception {
117     EcompUser user = new EcompUser();
118     user.setLoginId(LOGINID_1);
119
120     portalApi.pushUser(user);
121     EcompUser storedUser = portalApi.getUser(user.getLoginId());
122
123     assertThat(storedUser.getLoginId(), is(user.getLoginId()));
124   }
125
126   @Test(expected = PortalAPIException.class)
127   public void testCannotPushUserTwice() throws Exception {
128     EcompUser user = new EcompUser();
129     user.setLoginId(LOGINID_1);
130
131     portalApi.pushUser(user);
132     portalApi.pushUser(user);
133   }
134
135   @Test(expected = PortalAPIException.class)
136   public void testGetUnknownUser() throws Exception {
137     EcompUser user = new EcompUser();
138     user.setLoginId(LOGINID_1);
139     portalApi.pushUser(user);
140
141     portalApi.getUser("does-not-exist");
142   }
143
144   @Test
145   public void testGetUsers() throws Exception {
146     EcompUser user = new EcompUser();
147     user.setLoginId(LOGINID_1);
148
149     EcompUser user2 = new EcompUser();
150     user2.setLoginId(LOGINID_2);
151
152     portalApi.pushUser(user);
153     portalApi.pushUser(user2);
154
155     List<EcompUser> users = portalApi.getUsers();
156
157     assertThat(users.size(), is(2));
158     assertThat(users.get(0).getLoginId(), is(LOGINID_1));
159     assertThat(users.get(1).getLoginId(), is(LOGINID_2));
160   }
161
162   @Test
163   public void testEditUser() throws Exception {
164     EcompUser user = new EcompUser();
165     user.setLoginId(LOGINID_1);
166     user.setFirstName("Bob");
167
168     portalApi.pushUser(user);
169
170     user.setFirstName("Jen");
171     portalApi.editUser(LOGINID_1, user);
172
173     assertThat(portalApi.getUser(LOGINID_1).getFirstName(), is("Jen"));
174   }
175
176   @Test(expected = PortalAPIException.class)
177   public void testEditUnknowUser() throws Exception {
178     EcompUser user = new EcompUser();
179     user.setLoginId(LOGINID_1);
180     portalApi.pushUser(user);
181
182     portalApi.editUser("does-no-exist", new EcompUser());
183   }
184
185   @Test
186   public void testGetRoles() throws Exception {
187     EcompUser user = new EcompUser();
188     user.setLoginId(LOGINID_1);
189     user.setRoles(new HashSet<>(portalApi.getAvailableRoles()));
190
191     portalApi.pushUser(user);
192
193     List<EcompRole> userRoles = portalApi.getUserRoles(LOGINID_1);
194
195     assertThat(userRoles.size(), is(1));
196     assertThat(userRoles.get(0).getId(), is(1L));
197     assertThat(userRoles.get(0).getName(), is(VIEW_ROLE));
198   }
199
200   @Test
201   public void testPushUserRoles() throws Exception {
202     EcompUser user = new EcompUser();
203     user.setLoginId(LOGINID_1);
204     portalApi.pushUser(user);
205
206     EcompUser storedUser = portalApi.getUser(LOGINID_1);
207     assertThat(storedUser.getRoles(), nullValue());
208
209     portalApi.pushUserRole(LOGINID_1, UserManager.getRoles());
210
211     Set<EcompRole> storedUserRoles = portalApi.getUser(LOGINID_1).getRoles();
212     ArrayList<EcompRole> rolesList = new ArrayList<>(storedUserRoles);
213
214     assertThat(rolesList.size(), is(1));
215     assertThat(rolesList.get(0).getId(), is(1L));
216     assertThat(rolesList.get(0).getName(), is(VIEW_ROLE));
217   }
218
219   @Test
220   public void testCannotPushRoleTwice() throws Exception {
221     EcompUser user = new EcompUser();
222     user.setLoginId(LOGINID_1);
223     portalApi.pushUser(user);
224
225     EcompUser storedUser = portalApi.getUser(LOGINID_1);
226     assertThat(storedUser.getRoles(), nullValue());
227
228     portalApi.pushUserRole(LOGINID_1, UserManager.getRoles());
229     portalApi.pushUserRole(LOGINID_1, UserManager.getRoles());
230
231     Set<EcompRole> storedUserRoles = portalApi.getUser(LOGINID_1).getRoles();
232     ArrayList<EcompRole> rolesList = new ArrayList<>(storedUserRoles);
233
234     assertThat(rolesList.size(), is(1));
235     assertThat(rolesList.get(0).getId(), is(1L));
236     assertThat(rolesList.get(0).getName(), is(VIEW_ROLE));
237   }
238
239   @Test
240   public void testDeleteUserRoles() throws Exception {
241     EcompUser user = new EcompUser();
242     user.setLoginId(LOGINID_1);
243     user.setFirstName("Bob");
244     List<EcompRole> availableRoles = portalApi.getAvailableRoles();
245     user.setRoles(new LinkedHashSet<EcompRole>(availableRoles));
246
247     portalApi.pushUser(user);
248
249     portalApi.pushUserRole(LOGINID_1, new ArrayList<EcompRole>());
250
251     EcompUser userWithNoRoles = portalApi.getUser(LOGINID_1);
252
253     assertThat(userWithNoRoles.getRoles(), empty());
254   }
255
256   @Test
257   public void testPushNullRoles() throws Exception {
258     EcompUser user = new EcompUser();
259     user.setLoginId(LOGINID_1);
260     user.setFirstName("Bob");
261     List<EcompRole> availableRoles = portalApi.getAvailableRoles();
262     user.setRoles(new LinkedHashSet<EcompRole>(availableRoles));
263
264     portalApi.pushUser(user);
265     portalApi.pushUserRole(LOGINID_1, null);
266
267     EcompUser userWithNoRoles = portalApi.getUser(LOGINID_1);
268
269     assertThat(userWithNoRoles.getRoles(), empty());
270   }
271
272   @Test
273   public void testIsAppAuthenticated() throws Exception {
274     Whitebox.setInternalState(PortalAuthenticationConfig.class, "AUTHENTICATION_CONFIG_FILE",
275         TestData.PORTAL_AUTHENTICATION_PROPERTIES.getFilename());
276
277     HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
278     when(request.getHeader("username")).thenReturn("testuser");
279     when(request.getHeader("password")).thenReturn("testpassword");
280
281     assertThat(portalApi.isAppAuthenticated(request), is(true));
282   }
283 }