d9f249a42ad56cbe2080bacf66675c81c9341335
[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 static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.catchThrowable;
25 import static org.mockito.Mockito.when;
26
27 import java.util.Collections;
28 import javax.servlet.http.HttpServletRequest;
29 import org.aspectj.lang.JoinPoint;
30 import org.aspectj.lang.Signature;
31 import org.hibernate.validator.internal.util.annotation.AnnotationDescriptor;
32 import org.hibernate.validator.internal.util.annotation.AnnotationDescriptor.Builder;
33 import org.hibernate.validator.internal.util.annotation.AnnotationFactory;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.api.extension.ExtendWith;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.mockito.junit.jupiter.MockitoExtension;
40 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
41 import org.openecomp.sdc.be.components.impl.aaf.AafPermission.PermNames;
42 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
43 import org.openecomp.sdc.be.components.impl.aaf.RoleAuthorizationHandler;
44 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
45 import org.openecomp.sdc.be.config.ConfigurationManager;
46 import org.openecomp.sdc.be.dao.api.ActionStatus;
47 import org.openecomp.sdc.be.servlets.BeGenericServlet;
48 import org.openecomp.sdc.common.api.FilterDecisionEnum;
49 import org.openecomp.sdc.common.impl.ExternalConfiguration;
50 import org.openecomp.sdc.common.impl.FSConfigurationSource;
51 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
52
53 @ExtendWith(MockitoExtension.class)
54 class RoleAuthorizationHandlerTest {
55
56     private RoleAuthorizationHandler roleAuthorizationHandler;
57     @Mock
58     private JoinPoint joinPoint;
59     @Mock
60     private Signature signature;
61     @Mock
62     private BeGenericServlet beGenericServlet;
63     @Mock
64     private HttpServletRequest httpServletRequest;
65
66     @BeforeEach
67     public void setUp() {
68         MockitoAnnotations.openMocks(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     void testAuthorizeRoleOnePermittedRole() {
80         final String[] permsAllowed = {PermNames.WRITE_VALUE};
81         final AnnotationDescriptor<PermissionAllowed> permissionDescriptor = createTestSubject(permsAllowed);
82         final PermissionAllowed rolesAllowed = AnnotationFactory.create(permissionDescriptor);
83         when(httpServletRequest.isUserInRole(AafPermission.getEnumByString(permsAllowed[0]).getFullPermission())).thenReturn(true);
84         roleAuthorizationHandler.authorizeRole(joinPoint, rolesAllowed);
85     }
86
87     @Test
88     void testAuthorizeRoleTwoPermittedRole() {
89         final String[] permsAllowed = {PermNames.WRITE_VALUE, PermNames.READ_VALUE};
90         final AnnotationDescriptor<PermissionAllowed> permissionDescriptor = createTestSubject(permsAllowed);
91         final PermissionAllowed rolesAllowed = AnnotationFactory.create(permissionDescriptor);
92         when(httpServletRequest.isUserInRole(AafPermission.getEnumByString(permsAllowed[0]).getFullPermission())).thenReturn(true);
93         roleAuthorizationHandler.authorizeRole(joinPoint, rolesAllowed);
94     }
95
96     @Test
97     void testAuthorizeRoleNonPermittedRole() {
98         final String[] permsAllowed = {PermNames.WRITE_VALUE, PermNames.READ_VALUE};
99         final AnnotationDescriptor<PermissionAllowed> permissionDescriptor = createTestSubject(permsAllowed);
100         final PermissionAllowed rolesAllowed = AnnotationFactory.create(permissionDescriptor);
101         when(httpServletRequest.isUserInRole(AafPermission.getEnumByString(permsAllowed[0]).getFullPermission())).thenReturn(false);
102
103         final ComponentException thrown = (ComponentException) catchThrowable(() -> roleAuthorizationHandler.authorizeRole(joinPoint, rolesAllowed));
104         assertThat(thrown.getActionStatus()).isEqualTo(ActionStatus.AUTH_FAILED);
105     }
106
107     @Test
108     void testAuthorizeRoleEmptyRole() {
109         final String[] permsAllowed = {};
110         final AnnotationDescriptor<PermissionAllowed> permissionDescriptor = createTestSubject(permsAllowed);
111         final PermissionAllowed rolesAllowed = AnnotationFactory.create(permissionDescriptor);
112
113         final ComponentException thrown = (ComponentException) catchThrowable(() -> roleAuthorizationHandler.authorizeRole(joinPoint, rolesAllowed));
114         assertThat(thrown.getActionStatus()).isEqualTo(ActionStatus.AUTH_FAILED);
115     }
116
117     private AnnotationDescriptor<PermissionAllowed> createTestSubject(final String[] permsAllowed) {
118         return new Builder<>(PermissionAllowed.class, Collections.singletonMap("value", permsAllowed)).build();
119     }
120
121 }