Merge "Added unit tests for GuardPolicyDelegate"
[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.InjectMocks;
39 import org.mockito.Mockito;
40 import org.mockito.Spy;
41 import org.onap.clamp.authorization.AuthorizationController;
42 import org.onap.clamp.clds.config.ClampProperties;
43 import org.onap.clamp.clds.exception.NotAuthorizedException;
44 import org.onap.clamp.clds.service.SecureServicePermission;
45 import org.onap.clamp.util.PrincipalUtils;
46 import org.springframework.boot.test.context.SpringBootTest;
47 import org.springframework.mock.env.MockEnvironment;
48 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
49 import org.springframework.security.core.GrantedAuthority;
50 import org.springframework.security.core.context.SecurityContext;
51 import org.springframework.security.core.userdetails.User;
52 import org.springframework.test.context.junit4.SpringRunner;
53
54 /**
55  * Test CldsDAO calls through CldsModel and CldsEvent. This really test the DB
56  * and stored procedures.
57  */
58 @RunWith(SpringRunner.class)
59 @SpringBootTest
60 public class AuthorizationControllerItCase {
61
62     private PermissionTestDefaultHelper permissionTestHelper = new PermissionTestDefaultHelper();
63
64     @Spy
65     MockEnvironment env;
66
67     @Spy
68     @InjectMocks
69     private ClampProperties clampProp = new ClampProperties();
70
71     @InjectMocks
72     private AuthorizationController auth;
73
74     /**
75      * Setup the variable before the tests execution.
76      */
77     @Before
78     public void setupBefore() {
79         permissionTestHelper.setupMockEnv(env);
80         List<GrantedAuthority> authList = permissionTestHelper.getAuthList();
81
82         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
83         Mockito.when(securityContext.getAuthentication()).thenReturn(
84                 new UsernamePasswordAuthenticationToken(new User("admin", "", authList),
85                         "", authList)
86         );
87         PrincipalUtils.setSecurityContext(securityContext);
88     }
89
90     @Test
91     public void testIsUserPermitted() {
92         assertTrue(auth.isUserPermitted(new SecureServicePermission("permission-type-cl","dev","read")));
93         assertTrue(auth.isUserPermitted(new SecureServicePermission("permission-type-cl-manage","dev","DEPLOY")));
94         assertTrue(auth.isUserPermitted(new SecureServicePermission("permission-type-filter-vf","dev",
95                 "12345-55555-55555-5555")));
96         assertFalse(auth.isUserPermitted(new SecureServicePermission("permission-type-cl","test","read")));
97     }
98
99     @Test
100     public void testIfUserAuthorize() {
101         Exchange ex = Mockito.mock(Exchange.class);
102         try {
103             permissionTestHelper.doActionOnAllPermissions(((type, instance, action) ->
104                         auth.authorize(ex, type, instance, action)
105                     )
106             );
107         } catch (NotAuthorizedException e) {
108             fail(e.getMessage());
109         }
110     }
111
112     @Test(expected = NotAuthorizedException.class)
113     public void testIfAuthorizeThrowException() {
114         Exchange ex = Mockito.mock(Exchange.class);
115         auth.authorize(ex,"permission-type-cl","test","read");
116     }
117 }