3406042c159c76dbcd1d3346e35b5ae9824597c2
[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.assertEquals;
29 import static org.junit.Assert.assertFalse;
30 import static org.junit.Assert.assertTrue;
31
32 import java.util.Arrays;
33 import java.util.Collection;
34 import org.apache.camel.Exchange;
35 import org.junit.BeforeClass;
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.authorization.SecureServicePermission;
41 import org.onap.clamp.clds.exception.NotAuthorizedException;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.boot.test.context.SpringBootTest;
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.context.SecurityContextHolder;
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(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
57 public class AuthorizationControllerItCase {
58
59     @Autowired
60     private AuthorizationController auth;
61
62     private static SecurityContext sc = SecurityContextHolder.getContext();
63
64     /**
65      * Setup the variable before the tests execution.
66      */
67     @BeforeClass
68     public static void setupBefore() {
69
70         sc.setAuthentication(new Authentication() {
71             @Override
72             public Collection<? extends GrantedAuthority> getAuthorities() {
73                 return Arrays.asList(new SimpleGrantedAuthority(
74                                 new SecureServicePermission("permission-type-cl", "dev", "read").getKey()),
75                         new SimpleGrantedAuthority(new SecureServicePermission("permission-type-cl-manage", "dev",
76                                 "DEPLOY").getKey()),
77                         new SimpleGrantedAuthority(new SecureServicePermission("permission-type-filter-vf", "dev",
78                                 "12345-55555-55555-5555").getKey()));
79             }
80
81             @Override
82             public Object getCredentials() {
83                 return null;
84             }
85
86             @Override
87             public Object getDetails() {
88                 return null;
89             }
90
91             @Override
92             public Object getPrincipal() {
93                 return "admin";
94             }
95
96             @Override
97             public boolean isAuthenticated() {
98                 return true;
99             }
100
101             @Override
102             public void setAuthenticated(boolean authenticatedFlag) throws IllegalArgumentException {
103
104             }
105
106             @Override
107             public String getName() {
108                 return "admin";
109             }
110         });
111
112     }
113
114     @Test
115     public void testIsUserPermitted() {
116         assertEquals(AuthorizationController.getPrincipalName(sc),"admin");
117         assertTrue(auth.isUserPermitted(new SecureServicePermission("permission-type-cl", "dev", "read")));
118         assertTrue(auth.isUserPermitted(new SecureServicePermission("permission-type-cl-manage", "dev", "DEPLOY")));
119         assertTrue(auth.isUserPermitted(
120                 new SecureServicePermission("permission-type-filter-vf", "dev", "12345-55555-55555-5555")));
121         assertFalse(auth.isUserPermitted(new SecureServicePermission("permission-type-cl", "test", "read")));
122     }
123
124     @Test(expected = NotAuthorizedException.class)
125     public void testIfAuthorizeThrowException() {
126         Exchange ex = Mockito.mock(Exchange.class);
127         auth.authorize(ex, "cl", "test", "read");
128     }
129 }