c488a9a6ca2e3a4b713cb0d9208c40d69ef62afd
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / impl / aaf / RoleAuthorizationHandlerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2020 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.impl.aaf;
22
23 import org.aspectj.lang.JoinPoint;
24 import org.aspectj.lang.Signature;
25 import org.junit.Before;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
31 import org.mockito.junit.MockitoJUnitRunner;
32 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
33 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
34 import org.openecomp.sdc.be.components.impl.aaf.RoleAuthorizationHandler;
35 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
36 import org.openecomp.sdc.be.config.ConfigurationManager;
37 import org.openecomp.sdc.be.dao.api.ActionStatus;
38 import org.openecomp.sdc.be.servlets.BeGenericServlet;
39 import org.openecomp.sdc.common.api.ConfigurationSource;
40 import org.openecomp.sdc.common.api.FilterDecisionEnum;
41 import org.openecomp.sdc.common.impl.ExternalConfiguration;
42 import org.openecomp.sdc.common.impl.FSConfigurationSource;
43 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
44 import sun.reflect.annotation.AnnotationParser;
45
46 import javax.servlet.http.HttpServletRequest;
47 import java.util.Collections;
48
49 import static org.assertj.core.api.Assertions.assertThat;
50 import static org.assertj.core.api.Assertions.catchThrowable;
51 import static org.mockito.Mockito.when;
52
53 @RunWith(MockitoJUnitRunner.Silent.class)
54 public class RoleAuthorizationHandlerTest {
55
56     private RoleAuthorizationHandler roleAuthorizationHandler;
57     @Mock
58     JoinPoint joinPoint;
59     @Mock
60     Signature signature;
61     @Mock
62     BeGenericServlet beGenericServlet;
63     @Mock
64     HttpServletRequest httpServletRequest;
65
66     @Before
67     public void setUp() {
68         MockitoAnnotations.initMocks(this);
69         when(joinPoint.getSignature()).thenReturn(signature);
70         when(signature.toShortString()).thenReturn("methodName");
71         when(joinPoint.getThis()).thenReturn(beGenericServlet);
72         when(beGenericServlet.getServletRequest()).thenReturn(httpServletRequest);
73         ThreadLocalsHolder.setApiType(FilterDecisionEnum.EXTERNAL);
74         new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be/auth"));
75         roleAuthorizationHandler = new RoleAuthorizationHandler();
76     }
77
78     @Test
79     public void testAuthorizeRoleOnePermittedRole() {
80         String[] permsAllowed = {AafPermission.PermNames.WRITE_VALUE};
81         PermissionAllowed rolesAllowed =
82                 (PermissionAllowed) AnnotationParser.annotationForMap(PermissionAllowed.class, Collections.singletonMap("value", permsAllowed));
83         when(httpServletRequest.isUserInRole(AafPermission.getEnumByString(permsAllowed[0]).getFullPermission()))
84                 .thenReturn(true);
85         roleAuthorizationHandler.authorizeRole(joinPoint, rolesAllowed);
86     }
87
88     @Test
89     public void testAuthorizeRoleTwoPermittedRole() {
90         String[] permsAllowed = {AafPermission.PermNames.WRITE_VALUE, AafPermission.PermNames.READ_VALUE};
91         PermissionAllowed rolesAllowed =
92                 (PermissionAllowed) AnnotationParser.annotationForMap(PermissionAllowed.class, Collections.singletonMap("value", permsAllowed));
93         when(httpServletRequest.isUserInRole(AafPermission.getEnumByString(permsAllowed[0]).getFullPermission()))
94                 .thenReturn(true);
95         roleAuthorizationHandler.authorizeRole(joinPoint, rolesAllowed);
96     }
97
98     @Test
99     public void testAuthorizeRoleNonPermittedRole() {
100         String[] permsAllowed = {AafPermission.PermNames.WRITE_VALUE, AafPermission.PermNames.READ_VALUE};
101         PermissionAllowed rolesAllowed =
102                 (PermissionAllowed) AnnotationParser.annotationForMap(PermissionAllowed.class, Collections.singletonMap("value", permsAllowed));
103         when(httpServletRequest.isUserInRole(AafPermission.getEnumByString(permsAllowed[0]).getFullPermission()))
104                 .thenReturn(false);
105
106         ComponentException thrown = (ComponentException) catchThrowable(()->roleAuthorizationHandler.authorizeRole(joinPoint, rolesAllowed));
107         assertThat(thrown.getActionStatus()).isEqualTo(ActionStatus.AUTH_FAILED);
108     }
109
110     @Test
111     public void testAuthorizeRoleEmptyRole() {
112         String[] permsAllowed = {};
113         PermissionAllowed rolesAllowed =
114                 (PermissionAllowed) AnnotationParser.annotationForMap(PermissionAllowed.class, Collections.singletonMap("value", permsAllowed));
115
116         ComponentException thrown = (ComponentException) catchThrowable(()->roleAuthorizationHandler.authorizeRole(joinPoint, rolesAllowed));
117         assertThat(thrown.getActionStatus()).isEqualTo(ActionStatus.AUTH_FAILED);
118     }
119 }