Modify the Ui
[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  * Modifications Copyright (c) 2019 Samsung
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END============================================
22  * ===================================================================
23  *
24  */
25
26 package org.onap.clamp.clds.it;
27
28 import static org.junit.Assert.assertFalse;
29 import static org.junit.Assert.assertTrue;
30 import static org.junit.Assert.fail;
31
32 import java.util.List;
33
34 import org.apache.camel.Exchange;
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.exception.NotAuthorizedException;
41 import org.onap.clamp.clds.service.SecureServicePermission;
42 import org.onap.clamp.util.PrincipalUtils;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.boot.test.context.SpringBootTest;
45 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
46 import org.springframework.security.core.GrantedAuthority;
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     private PermissionTestDefaultHelper permissionTestHelper = new PermissionTestDefaultHelper();
60
61     // @Spy
62     // MockEnvironment env;
63
64     @Autowired
65     private AuthorizationController auth;
66
67     /**
68      * Setup the variable before the tests execution.
69      */
70     @Before
71     public void setupBefore() {
72         // permissionTestHelper.setupMockEnv(env);
73         List<GrantedAuthority> authList = permissionTestHelper.getAuthList();
74
75         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
76         Mockito.when(securityContext.getAuthentication())
77                 .thenReturn(new UsernamePasswordAuthenticationToken(new User("admin", "", authList), "", authList));
78         PrincipalUtils.setSecurityContext(securityContext);
79     }
80
81     @Test
82     public void testIsUserPermitted() {
83         assertTrue(auth.isUserPermitted(new SecureServicePermission("permission-type-cl", "dev", "read")));
84         assertTrue(auth.isUserPermitted(new SecureServicePermission("permission-type-cl-manage", "dev", "DEPLOY")));
85         assertTrue(auth.isUserPermitted(
86                 new SecureServicePermission("permission-type-filter-vf", "dev", "12345-55555-55555-5555")));
87         assertFalse(auth.isUserPermitted(new SecureServicePermission("permission-type-cl", "test", "read")));
88     }
89
90     @Test
91     public void testIfUserAuthorize() {
92         Exchange ex = Mockito.mock(Exchange.class);
93         try {
94             permissionTestHelper
95                     .doActionOnAllPermissions(((type, instance, action) -> auth.authorize(ex, type, instance, action)));
96         } catch (NotAuthorizedException e) {
97             fail(e.getMessage());
98         }
99     }
100
101     @Test(expected = NotAuthorizedException.class)
102     public void testIfAuthorizeThrowException() {
103         Exchange ex = Mockito.mock(Exchange.class);
104         auth.authorize(ex, "cl", "test", "read");
105     }
106 }