Adding UI extensibility
[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"), PORTAL_AUTHENTICATION_PROPERTIES(
79         "src/test/resources/portal/portal-authentication.properties"), ROLES_CONFIG_FILE(
80             "src/test/resources/portal/roles.config");
81
82     private String filename;
83
84     TestData(String filename) {
85       this.filename = filename;
86     }
87
88     public String getFilename() {
89       return this.filename;
90     }
91     // @formatter:on
92   }
93
94   @Mock
95   private UserManager userManager = new UserManager(testUsersFile);
96
97   @InjectMocks
98   private PortalRestAPIServiceImpl portalApi = new PortalRestAPIServiceImpl();
99
100   @BeforeClass
101   public static void setUpBeforeClass() throws Exception {
102     testUsersFile = Paths.get(TestData.TEST_USERS.getFilename()).toFile();
103   }
104
105   @AfterClass
106   public static void tearDownAfterClass() throws Exception {
107     Files.deleteIfExists(testUsersFile.toPath());
108   }
109
110   @Before
111   public void setUp() throws Exception {
112     Whitebox.setInternalState(RolesConfig.class, "ROLES_CONFIG_FILE",
113         TestData.ROLES_CONFIG_FILE.getFilename());
114   }
115
116   @After
117   public void tearDown() throws Exception {
118     Files.deleteIfExists(testUsersFile.toPath());
119   }
120
121   @Test
122   public void testPushAndGetUser() throws Exception {
123     EcompUser user = new EcompUser();
124     user.setLoginId(LOGINID_1);
125
126     portalApi.pushUser(user);
127     EcompUser storedUser = portalApi.getUser(user.getLoginId());
128
129     assertThat(storedUser.getLoginId(), is(user.getLoginId()));
130   }
131
132   @Test(expected = PortalAPIException.class)
133   public void testCannotPushUserTwice() throws Exception {
134     EcompUser user = new EcompUser();
135     user.setLoginId(LOGINID_1);
136
137     portalApi.pushUser(user);
138     portalApi.pushUser(user);
139   }
140
141   @Test(expected = PortalAPIException.class)
142   public void testGetUnknownUser() throws Exception {
143     EcompUser user = new EcompUser();
144     user.setLoginId(LOGINID_1);
145     portalApi.pushUser(user);
146
147     portalApi.getUser("does-not-exist");
148   }
149
150   @Test
151   public void testGetUsers() throws Exception {
152     EcompUser user = new EcompUser();
153     user.setLoginId(LOGINID_1);
154
155     EcompUser user2 = new EcompUser();
156     user2.setLoginId(LOGINID_2);
157
158     portalApi.pushUser(user);
159     portalApi.pushUser(user2);
160
161     List<EcompUser> users = portalApi.getUsers();
162
163     assertThat(users.size(), is(2));
164     assertThat(users.get(0).getLoginId(), is(LOGINID_1));
165     assertThat(users.get(1).getLoginId(), is(LOGINID_2));
166   }
167
168   @Test
169   public void testEditUser() throws Exception {
170     EcompUser user = new EcompUser();
171     user.setLoginId(LOGINID_1);
172     user.setFirstName("Bob");
173
174     portalApi.pushUser(user);
175
176     user.setFirstName("Jen");
177     portalApi.editUser(LOGINID_1, user);
178
179     assertThat(portalApi.getUser(LOGINID_1).getFirstName(), is("Jen"));
180   }
181
182   @Test(expected = PortalAPIException.class)
183   public void testEditUnknowUser() throws Exception {
184     EcompUser user = new EcompUser();
185     user.setLoginId(LOGINID_1);
186     portalApi.pushUser(user);
187
188     portalApi.editUser("does-no-exist", new EcompUser());
189   }
190
191   @Test
192   public void testGetRoles() throws Exception {
193     EcompUser user = new EcompUser();
194     user.setLoginId(LOGINID_1);
195     user.setRoles(new HashSet<>(portalApi.getAvailableRoles()));
196
197     portalApi.pushUser(user);
198
199     List<EcompRole> userRoles = portalApi.getUserRoles(LOGINID_1);
200
201     assertThat(userRoles.size(), is(1));
202     assertThat(userRoles.get(0).getId(), is(1L));
203     assertThat(userRoles.get(0).getName(), is(VIEW_ROLE));
204   }
205
206   @Test
207   public void testPushUserRoles() throws Exception {
208     EcompUser user = new EcompUser();
209     user.setLoginId(LOGINID_1);
210     portalApi.pushUser(user);
211
212     EcompUser storedUser = portalApi.getUser(LOGINID_1);
213     assertThat(storedUser.getRoles(), nullValue());
214
215     portalApi.pushUserRole(LOGINID_1, UserManager.getRoles());
216
217     Set<EcompRole> storedUserRoles = portalApi.getUser(LOGINID_1).getRoles();
218     ArrayList<EcompRole> rolesList = new ArrayList<>(storedUserRoles);
219
220     assertThat(rolesList.size(), is(1));
221     assertThat(rolesList.get(0).getId(), is(1L));
222     assertThat(rolesList.get(0).getName(), is(VIEW_ROLE));
223   }
224
225   @Test
226   public void testCannotPushRoleTwice() throws Exception {
227     EcompUser user = new EcompUser();
228     user.setLoginId(LOGINID_1);
229     portalApi.pushUser(user);
230
231     EcompUser storedUser = portalApi.getUser(LOGINID_1);
232     assertThat(storedUser.getRoles(), nullValue());
233
234     portalApi.pushUserRole(LOGINID_1, UserManager.getRoles());
235     portalApi.pushUserRole(LOGINID_1, UserManager.getRoles());
236
237     Set<EcompRole> storedUserRoles = portalApi.getUser(LOGINID_1).getRoles();
238     ArrayList<EcompRole> rolesList = new ArrayList<>(storedUserRoles);
239
240     assertThat(rolesList.size(), is(1));
241     assertThat(rolesList.get(0).getId(), is(1L));
242     assertThat(rolesList.get(0).getName(), is(VIEW_ROLE));
243   }
244
245   @Test
246   public void testDeleteUserRoles() throws Exception {
247     EcompUser user = new EcompUser();
248     user.setLoginId(LOGINID_1);
249     user.setFirstName("Bob");
250     List<EcompRole> availableRoles = portalApi.getAvailableRoles();
251     user.setRoles(new LinkedHashSet<EcompRole>(availableRoles));
252
253     portalApi.pushUser(user);
254
255     portalApi.pushUserRole(LOGINID_1, new ArrayList<EcompRole>());
256
257     EcompUser userWithNoRoles = portalApi.getUser(LOGINID_1);
258
259     assertThat(userWithNoRoles.getRoles(), empty());
260   }
261
262   @Test
263   public void testPushNullRoles() throws Exception {
264     EcompUser user = new EcompUser();
265     user.setLoginId(LOGINID_1);
266     user.setFirstName("Bob");
267     List<EcompRole> availableRoles = portalApi.getAvailableRoles();
268     user.setRoles(new LinkedHashSet<EcompRole>(availableRoles));
269
270     portalApi.pushUser(user);
271     portalApi.pushUserRole(LOGINID_1, null);
272
273     EcompUser userWithNoRoles = portalApi.getUser(LOGINID_1);
274
275     assertThat(userWithNoRoles.getRoles(), empty());
276   }
277
278   @Test
279   public void testIsAppAuthenticated() throws Exception {
280     Whitebox.setInternalState(PortalAuthenticationConfig.class, "AUTHENTICATION_CONFIG_FILE",
281         TestData.PORTAL_AUTHENTICATION_PROPERTIES.getFilename());
282
283     HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
284     when(request.getHeader("username")).thenReturn("testuser");
285     when(request.getHeader("password")).thenReturn("testpassword");
286
287     assertThat(portalApi.isAppAuthenticated(request), is(true));
288   }
289 }