eea60eb0a3fb93b29fe6ae7c73c2f467d7667a49
[aaf/authz.git] / auth / auth-locate / src / test / java / org / onap / aaf / auth / locate / JU_BasicAuthCodeTest.java
1 /**\r
2  * ============LICENSE_START====================================================\r
3  * org.onap.aaf\r
4  * ===========================================================================\r
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.\r
6  * ===========================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END====================================================\r
19  *\r
20  */\r
21 package org.onap.aaf.auth.locate;\r
22 \r
23 import static org.junit.Assert.assertEquals;\r
24 import static org.mockito.Mockito.verify;\r
25 import static org.mockito.Mockito.when;\r
26 import static org.mockito.MockitoAnnotations.initMocks;\r
27 \r
28 import javax.servlet.http.HttpServletRequest;\r
29 import javax.servlet.http.HttpServletResponse;\r
30 \r
31 import org.eclipse.jetty.http.HttpStatus;\r
32 import org.junit.Before;\r
33 import org.junit.Test;\r
34 import org.mockito.Answers;\r
35 import org.mockito.Mock;\r
36 import org.onap.aaf.auth.env.AuthzTrans;\r
37 import org.onap.aaf.auth.locate.facade.LocateFacade;\r
38 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;\r
39 import org.onap.aaf.cadi.principal.BasicPrincipal;\r
40 import org.onap.aaf.cadi.principal.X509Principal;\r
41 import org.onap.aaf.misc.env.LogTarget;\r
42 \r
43 public class JU_BasicAuthCodeTest {\r
44 \r
45         @Mock\r
46         AAFAuthn authn;\r
47 \r
48         @Mock(answer = Answers.RETURNS_DEEP_STUBS)\r
49         AuthzTrans trans;\r
50 \r
51         @Mock\r
52         HttpServletRequest req;\r
53 \r
54         @Mock\r
55         HttpServletResponse resp;\r
56 \r
57         @Mock\r
58         LogTarget error;\r
59 \r
60         @Mock\r
61         LocateFacade facade;\r
62 \r
63         @Mock\r
64         BasicPrincipal basicPrincipal;\r
65         @Mock\r
66         X509Principal x509Principal;\r
67 \r
68         @Before\r
69         public void setUp() throws Exception {\r
70                 initMocks(this);\r
71         }\r
72 \r
73         @Test\r
74         public void testWithNullUserPrincipal() throws Exception {\r
75                 BasicAuthCode basicAuthCode = new BasicAuthCode(authn, facade);\r
76                 LocateCode locateCode = basicAuthCode.clone(facade, false);\r
77 \r
78                 assertEquals(locateCode.desc(), basicAuthCode.desc());\r
79 \r
80                 when(trans.getUserPrincipal()).thenReturn(null);\r
81                 when(trans.error()).thenReturn(error);\r
82 \r
83                 basicAuthCode.handle(trans, req, resp);\r
84         }\r
85 \r
86         @Test\r
87         public void testWithBasicUserPrincipal() throws Exception {\r
88                 BasicAuthCode basicAuthCode = new BasicAuthCode(authn, facade);\r
89                 LocateCode locateCode = basicAuthCode.clone(facade, false);\r
90 \r
91                 assertEquals(locateCode.desc(), basicAuthCode.desc());\r
92 \r
93                 when(trans.getUserPrincipal()).thenReturn(basicPrincipal);\r
94 \r
95                 basicAuthCode.handle(trans, req, resp);\r
96 \r
97                 verify(resp).setStatus(HttpStatus.OK_200);\r
98         }\r
99 \r
100         @Test\r
101         public void testWithX509UserPrincipal() throws Exception {\r
102                 BasicAuthCode basicAuthCode = new BasicAuthCode(authn, facade);\r
103                 LocateCode locateCode = basicAuthCode.clone(facade, false);\r
104 \r
105                 assertEquals(locateCode.desc(), basicAuthCode.desc());\r
106 \r
107                 when(trans.getUserPrincipal()).thenReturn(x509Principal);\r
108                 when(req.getHeader("Authorization")).thenReturn("Basic 76//76");\r
109 \r
110                 basicAuthCode.handle(trans, req, resp);\r
111 \r
112                 verify(resp).setStatus(HttpStatus.FORBIDDEN_403);\r
113         }\r
114 \r
115 }\r