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.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;
46 import javax.servlet.http.HttpServletRequest;
47 import java.util.Collections;
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;
53 @RunWith(MockitoJUnitRunner.Silent.class)
54 public class RoleAuthorizationHandlerTest {
56 private RoleAuthorizationHandler roleAuthorizationHandler;
62 BeGenericServlet beGenericServlet;
64 HttpServletRequest httpServletRequest;
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();
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()))
85 roleAuthorizationHandler.authorizeRole(joinPoint, rolesAllowed);
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()))
95 roleAuthorizationHandler.authorizeRole(joinPoint, rolesAllowed);
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()))
106 ComponentException thrown = (ComponentException) catchThrowable(()->roleAuthorizationHandler.authorizeRole(joinPoint, rolesAllowed));
107 assertThat(thrown.getActionStatus()).isEqualTo(ActionStatus.AUTH_FAILED);
111 public void testAuthorizeRoleEmptyRole() {
112 String[] permsAllowed = {};
113 PermissionAllowed rolesAllowed =
114 (PermissionAllowed) AnnotationParser.annotationForMap(PermissionAllowed.class, Collections.singletonMap("value", permsAllowed));
116 ComponentException thrown = (ComponentException) catchThrowable(()->roleAuthorizationHandler.authorizeRole(joinPoint, rolesAllowed));
117 assertThat(thrown.getActionStatus()).isEqualTo(ActionStatus.AUTH_FAILED);