1e469eca6d669c556c8c24c1a30991efdffc83eb
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / aaf / v2_0 / test / JU_AAFTrustChecker.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
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  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.cadi.aaf.v2_0.test;
23
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.junit.Assert.assertThat;
26 import static org.mockito.Mockito.when;
27
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32
33 import java.io.ByteArrayOutputStream;
34 import java.io.PrintStream;
35
36 import javax.servlet.http.HttpServletRequest;
37
38 import org.onap.aaf.cadi.Lur;
39 import org.onap.aaf.cadi.PropAccess;
40 import org.onap.aaf.cadi.aaf.v2_0.AAFTrustChecker;
41 import org.onap.aaf.cadi.config.Config;
42 import org.onap.aaf.cadi.principal.TaggedPrincipal;
43 import org.onap.aaf.cadi.taf.TafResp;
44 import org.onap.aaf.cadi.taf.TrustNotTafResp;
45 import org.onap.aaf.cadi.taf.TrustTafResp;
46 import org.onap.aaf.misc.env.Env;
47
48 public class JU_AAFTrustChecker {
49
50         private final static String type = "type";
51         private final static String instance = "instance";
52         private final static String action = "action";
53         private final static String key = type + '|' + instance + '|' + action;
54         private final static String name = "name";
55         private final static String otherName = "otherName";
56
57         private PropAccess access;
58
59         @Mock private Env envMock;
60         @Mock private TafResp trespMock;
61         @Mock private HttpServletRequest reqMock;
62         @Mock private TaggedPrincipal tpMock;
63         @Mock private Lur lurMock;
64         @Mock private TaggedPrincipal princMock;
65
66         @Before
67         public void setup() {
68                 MockitoAnnotations.initMocks(this);
69                 access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
70         }
71
72         @Test
73         public void test() {
74                 AAFTrustChecker trustChecker;
75
76                 // coverage calls
77                 trustChecker = new AAFTrustChecker(access);
78                 trustChecker = new AAFTrustChecker(envMock);
79
80                 access.setProperty(Config.CADI_TRUST_PERM, "example");
81                 when(envMock.getProperty(Config.CADI_TRUST_PERM)).thenReturn("example");
82                 trustChecker = new AAFTrustChecker(access);
83                 trustChecker = new AAFTrustChecker(envMock);
84
85                 access.setProperty(Config.CADI_TRUST_PERM, key);
86                 when(envMock.getProperty(Config.CADI_TRUST_PERM)).thenReturn(key);
87                 trustChecker = new AAFTrustChecker(access);
88                 trustChecker = new AAFTrustChecker(envMock);
89
90                 trustChecker.setLur(lurMock);
91
92                 assertThat(trustChecker.mayTrust(trespMock, reqMock), is(trespMock));
93
94                 when(reqMock.getHeader(null)).thenReturn("comma,comma,comma");
95                 assertThat(trustChecker.mayTrust(trespMock, reqMock), is(trespMock));
96
97                 when(reqMock.getHeader(null)).thenReturn("colon:colon:colon:colon,comma,comma");
98                 assertThat(trustChecker.mayTrust(trespMock, reqMock), is(trespMock));
99
100                 when(reqMock.getHeader(null)).thenReturn("colon:colon:colon:AS,comma,comma");
101                 when(trespMock.getPrincipal()).thenReturn(tpMock);
102                 when(tpMock.getName()).thenReturn(name);
103                 when(lurMock.fish(princMock, null)).thenReturn(true);
104                 TafResp tntResp = trustChecker.mayTrust(trespMock, reqMock);
105
106                 assertThat(tntResp instanceof TrustNotTafResp, is(true));
107                 assertThat(tntResp.toString(), is("name requested trust as colon, but does not have Authorization"));
108
109                 when(reqMock.getHeader(null)).thenReturn(name + ":colon:colon:AS,comma,comma");
110                 assertThat(trustChecker.mayTrust(trespMock, reqMock), is(trespMock));
111
112                 when(envMock.getProperty(Config.CADI_ALIAS, null)).thenReturn(name);
113                 when(envMock.getProperty(Config.CADI_TRUST_PERM)).thenReturn(null);
114                 trustChecker = new AAFTrustChecker(envMock);
115                 trustChecker.setLur(lurMock);
116
117                 when(trespMock.getPrincipal()).thenReturn(princMock);
118                 when(princMock.getName()).thenReturn(otherName);
119                 when(lurMock.fish(princMock, null)).thenReturn(true);
120                 TafResp ttResp = trustChecker.mayTrust(trespMock, reqMock);
121                 assertThat(ttResp instanceof TrustTafResp, is(true));
122                 assertThat(ttResp.toString(), is(name + " by trust of   " + name + " validated using colon by colon, null"));
123
124                 when(princMock.getName()).thenReturn(name);
125                 ttResp = trustChecker.mayTrust(trespMock, reqMock);
126                 assertThat(ttResp instanceof TrustTafResp, is(true));
127                 assertThat(ttResp.toString(), is(name + " by trust of   " + name + " validated using colon by colon, null"));
128         }
129
130 }