2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.impl.aaf;
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;
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;
53 @ExtendWith(MockitoExtension.class)
54 class RoleAuthorizationHandlerTest {
56 private RoleAuthorizationHandler roleAuthorizationHandler;
58 private JoinPoint joinPoint;
60 private Signature signature;
62 private BeGenericServlet beGenericServlet;
64 private HttpServletRequest httpServletRequest;
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();
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);
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);
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);
103 final ComponentException thrown = (ComponentException) catchThrowable(() -> roleAuthorizationHandler.authorizeRole(joinPoint, rolesAllowed));
104 assertThat(thrown.getActionStatus()).isEqualTo(ActionStatus.AUTH_FAILED);
108 void testAuthorizeRoleEmptyRole() {
109 final String[] permsAllowed = {};
110 final AnnotationDescriptor<PermissionAllowed> permissionDescriptor = createTestSubject(permsAllowed);
111 final PermissionAllowed rolesAllowed = AnnotationFactory.create(permissionDescriptor);
113 final ComponentException thrown = (ComponentException) catchThrowable(() -> roleAuthorizationHandler.authorizeRole(joinPoint, rolesAllowed));
114 assertThat(thrown.getActionStatus()).isEqualTo(ActionStatus.AUTH_FAILED);
117 private AnnotationDescriptor<PermissionAllowed> createTestSubject(final String[] permsAllowed) {
118 return new Builder<>(PermissionAllowed.class, Collections.singletonMap("value", permsAllowed)).build();