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 org.aspectj.lang.JoinPoint;
24 import org.aspectj.lang.Signature;
25 import org.hibernate.validator.internal.util.annotationfactory.AnnotationDescriptor;
26 import org.hibernate.validator.internal.util.annotationfactory.AnnotationFactory;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.mockito.junit.MockitoJUnitRunner;
33 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
34 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
35 import org.openecomp.sdc.be.components.impl.aaf.RoleAuthorizationHandler;
36 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
37 import org.openecomp.sdc.be.config.ConfigurationManager;
38 import org.openecomp.sdc.be.dao.api.ActionStatus;
39 import org.openecomp.sdc.be.servlets.BeGenericServlet;
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;
45 import javax.servlet.http.HttpServletRequest;
47 import static org.assertj.core.api.Assertions.assertThat;
48 import static org.assertj.core.api.Assertions.catchThrowable;
49 import static org.mockito.Mockito.when;
51 @RunWith(MockitoJUnitRunner.Silent.class)
52 public class RoleAuthorizationHandlerTest {
54 private RoleAuthorizationHandler roleAuthorizationHandler;
60 BeGenericServlet beGenericServlet;
62 HttpServletRequest httpServletRequest;
66 MockitoAnnotations.initMocks(this);
67 when(joinPoint.getSignature()).thenReturn(signature);
68 when(signature.toShortString()).thenReturn("methodName");
69 when(joinPoint.getThis()).thenReturn(beGenericServlet);
70 when(beGenericServlet.getServletRequest()).thenReturn(httpServletRequest);
71 ThreadLocalsHolder.setApiType(FilterDecisionEnum.EXTERNAL);
72 new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be/auth"));
73 roleAuthorizationHandler = new RoleAuthorizationHandler();
77 public void testAuthorizeRoleOnePermittedRole() {
78 String[] permsAllowed = {AafPermission.PermNames.WRITE_VALUE};
79 AnnotationDescriptor<PermissionAllowed> permissionDescriptor = new AnnotationDescriptor<PermissionAllowed>(PermissionAllowed.class);
80 permissionDescriptor.setValue("value", permsAllowed);
81 PermissionAllowed rolesAllowed = (PermissionAllowed) AnnotationFactory.create(permissionDescriptor);
82 when(httpServletRequest.isUserInRole(AafPermission.getEnumByString(permsAllowed[0]).getFullPermission()))
84 roleAuthorizationHandler.authorizeRole(joinPoint, rolesAllowed);
88 public void testAuthorizeRoleTwoPermittedRole() {
89 String[] permsAllowed = {AafPermission.PermNames.WRITE_VALUE, AafPermission.PermNames.READ_VALUE};
90 AnnotationDescriptor<PermissionAllowed> permissionDescriptor = new AnnotationDescriptor<PermissionAllowed>(PermissionAllowed.class);
91 permissionDescriptor.setValue("value", permsAllowed);
92 PermissionAllowed rolesAllowed = (PermissionAllowed)AnnotationFactory.create(permissionDescriptor);
93 when(httpServletRequest.isUserInRole(AafPermission.getEnumByString(permsAllowed[0]).getFullPermission()))
95 roleAuthorizationHandler.authorizeRole(joinPoint, rolesAllowed);
99 public void testAuthorizeRoleNonPermittedRole() {
100 String[] permsAllowed = {AafPermission.PermNames.WRITE_VALUE, AafPermission.PermNames.READ_VALUE};
101 AnnotationDescriptor<PermissionAllowed> permissionDescriptor = new AnnotationDescriptor<PermissionAllowed>(PermissionAllowed.class);
102 permissionDescriptor.setValue("value", permsAllowed);
103 PermissionAllowed rolesAllowed = (PermissionAllowed)AnnotationFactory.create(permissionDescriptor);
104 when(httpServletRequest.isUserInRole(AafPermission.getEnumByString(permsAllowed[0]).getFullPermission()))
107 ComponentException thrown = (ComponentException) catchThrowable(()->roleAuthorizationHandler.authorizeRole(joinPoint, rolesAllowed));
108 assertThat(thrown.getActionStatus()).isEqualTo(ActionStatus.AUTH_FAILED);
112 public void testAuthorizeRoleEmptyRole() {
113 String[] permsAllowed = {};
114 AnnotationDescriptor<PermissionAllowed> permissionDescriptor = new AnnotationDescriptor<PermissionAllowed>(PermissionAllowed.class);
115 permissionDescriptor.setValue("value", permsAllowed);
116 PermissionAllowed rolesAllowed = (PermissionAllowed)AnnotationFactory.create(permissionDescriptor);
118 ComponentException thrown = (ComponentException) catchThrowable(()->roleAuthorizationHandler.authorizeRole(joinPoint, rolesAllowed));
119 assertThat(thrown.getActionStatus()).isEqualTo(ActionStatus.AUTH_FAILED);