AuthUtil tests added
[music.git] / src / test / java / org / onap / music / unittests / authentication / AuthUtilTest.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2019 AT&T Intellectual Property
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
10  * 
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  * 
19  * ============LICENSE_END=============================================
20  * ====================================================================
21  */
22
23 package org.onap.music.unittests.authentication;
24
25 import static org.junit.Assert.*;
26 import java.util.ArrayList;
27 import java.util.List;
28 import javax.servlet.ServletRequest;
29 import org.junit.Test;
30 import org.mockito.Mockito;
31 import org.onap.aaf.cadi.CadiWrap;
32 import org.onap.aaf.cadi.Permission;
33 import org.onap.aaf.cadi.aaf.AAFPermission;
34 import org.onap.music.authentication.AuthUtil;
35
36 public class AuthUtilTest {
37
38     @Test
39     public void testGetAAFPermissions() {
40         CadiWrap cw = Mockito.mock(CadiWrap.class);
41         List<Permission> permList = new ArrayList<Permission>();
42         Permission perm1 = Mockito.mock(AAFPermission.class);
43         permList.add(perm1);
44         Mockito.when(cw.getPermissions(Mockito.any())).thenReturn(permList);
45
46         List<AAFPermission> returnedPerm = AuthUtil.getAAFPermissions(cw);
47         
48         assertEquals(perm1, returnedPerm.get(0));
49     }
50
51     @Test
52     public void testDecodeFunctionCode() throws Exception {
53         String toDecode = "some%2dthing.something.%2a";
54         String decoded = AuthUtil.decodeFunctionCode(toDecode);
55         
56         assertEquals("some-thing.something.*", decoded);
57     }
58
59     @Test
60     public void testIsAccessAllowed() throws Exception {
61         System.out.println("Request perms");
62         assertTrue(AuthUtil.isAccessAllowed(createRequest("*", "*"), "testns"));
63     }
64     
65     @Test
66     public void testIsAccessNotAllowed() throws Exception {
67         System.out.println("Request to write when have read perms");
68         assertFalse(AuthUtil.isAccessAllowed(createRequest("POST", "GET"), "testns"));
69     }
70     
71     @Test
72     public void testIsAccessAllowedNullRequest() {
73         try {
74             assertFalse(AuthUtil.isAccessAllowed(null, "namespace"));
75             fail("Should throw exception");
76         } catch (Exception e) {
77         }
78     }
79     
80     @Test
81     public void testIsAccessAllowedNullNamespace() {
82         try {
83             assertFalse(AuthUtil.isAccessAllowed(createRequest(), null));
84             fail("Should throw exception");
85         } catch (Exception e) {
86         }
87     }
88     
89     @Test
90     public void testIsAccessAllowedEmptyNamespace() {
91         try {
92             assertFalse(AuthUtil.isAccessAllowed(createRequest(), ""));
93             fail("Should throw exception");
94         } catch (Exception e) {
95         }
96     }
97
98     /**
99      * 
100      * @param permRequested 'PUT', 'POST', 'GET', or 'DELETE'
101      * @param permGranted '*' or 'GET'
102      * @return
103      */
104     private ServletRequest createRequest(String permRequested, String permGranted) {
105         CadiWrap cw = Mockito.mock(CadiWrap.class);
106         List<Permission> permList = new ArrayList<Permission>();
107         AAFPermission perm1 = Mockito.mock(AAFPermission.class);
108         Mockito.when(perm1.getType()).thenReturn("testns");
109         Mockito.when(perm1.getKey()).thenReturn("org.onap.music.api.user.access|testns|" + permGranted);
110
111         permList.add(perm1);
112         Mockito.when(cw.getPermissions(Mockito.any())).thenReturn(permList);
113         Mockito.when(cw.getRequestURI()).thenReturn("/v2/locks/create/testns.MyTable.Field1");
114         Mockito.when(cw.getContextPath()).thenReturn("/v2/locks/create");
115         Mockito.when(cw.getMethod()).thenReturn(permRequested);
116         
117         return cw;
118     }
119     
120     private ServletRequest createRequest() {
121         return createRequest("POST","*");
122     }
123 }