Rework authorization controller
[clamp.git] / src / test / java / org / onap / clamp / clds / it / AuthorizationControllerItCase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.it;
25
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30
31 import java.io.IOException;
32 import java.util.LinkedList;
33 import java.util.List;
34
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mockito;
39 import org.onap.clamp.authorization.AuthorizationController;
40 import org.onap.clamp.clds.service.SecureServicePermission;
41 import org.onap.clamp.util.PrincipalUtils;
42 import org.springframework.boot.test.context.SpringBootTest;
43 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
44 import org.springframework.security.core.Authentication;
45 import org.springframework.security.core.GrantedAuthority;
46 import org.springframework.security.core.authority.SimpleGrantedAuthority;
47 import org.springframework.security.core.context.SecurityContext;
48 import org.springframework.security.core.userdetails.User;
49 import org.springframework.test.context.junit4.SpringRunner;
50
51 /**
52  * Test CldsDAO calls through CldsModel and CldsEvent. This really test the DB
53  * and stored procedures.
54  */
55 @RunWith(SpringRunner.class)
56 @SpringBootTest
57 public class AuthorizationControllerItCase {
58
59     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(AuthorizationControllerItCase.class);
60     private Authentication authentication;
61     private List<GrantedAuthority> authList = new LinkedList<GrantedAuthority>();
62
63     /**
64      * Setup the variable before the tests execution.
65      *
66      * @throws IOException
67      *         In case of issues when opening the files
68      */
69     @Before
70     public void setupBefore() throws IOException {
71         authList.add(new SimpleGrantedAuthority("permission-type-cl-manage|dev|*"));
72         authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|read"));
73         authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|update"));
74         authList.add(new SimpleGrantedAuthority("permission-type-template|dev|read"));
75         authList.add(new SimpleGrantedAuthority("permission-type-template|dev|update"));
76         authList.add(new SimpleGrantedAuthority("permission-type-filter-vf|dev|*"));
77         authList.add(new SimpleGrantedAuthority("permission-type-cl-event|dev|*"));
78
79         authentication = new UsernamePasswordAuthenticationToken(new User("admin", "", authList), "", authList);
80     }
81
82     @Test
83     public void testIsUserPermittedNoException() {
84         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
85         Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
86         PrincipalUtils.setSecurityContext(securityContext);
87
88         AuthorizationController auth = new AuthorizationController ();
89         assertTrue(auth.isUserPermittedNoException(new SecureServicePermission("permission-type-cl","dev","read")));
90         assertTrue(auth.isUserPermittedNoException(new SecureServicePermission("permission-type-cl-manage","dev","DEPLOY")));
91         assertTrue(auth.isUserPermittedNoException(new SecureServicePermission("permission-type-filter-vf","dev","12345-55555-55555-5555")));
92         assertFalse(auth.isUserPermittedNoException(new SecureServicePermission("permission-type-cl","test","read")));
93     }
94 }