31876fef12fd6fa2e6c0c9d7a2eaf830bc7a29a0
[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         @Mock\r
45         AAFAuthn authn;\r
46 \r
47         @Mock(answer = Answers.RETURNS_DEEP_STUBS)\r
48         AuthzTrans trans;\r
49 \r
50         @Mock\r
51         HttpServletRequest req;\r
52 \r
53         @Mock\r
54         HttpServletResponse resp;\r
55 \r
56         @Mock\r
57         LogTarget error;\r
58 \r
59         @Mock\r
60         LocateFacade facade;\r
61 \r
62         @Mock\r
63         BasicPrincipal basicPrincipal;\r
64         @Mock\r
65         X509Principal x509Principal;\r
66 \r
67         @Before\r
68         public void setUp() throws Exception {\r
69                 initMocks(this);\r
70         }\r
71 \r
72         @Test\r
73         public void testWithNullUserPrincipal() throws Exception {\r
74                 BasicAuthCode basicAuthCode = new BasicAuthCode(authn, facade);\r
75                 LocateCode locateCode = basicAuthCode.clone(facade, false);\r
76 \r
77                 assertEquals(locateCode.desc(), basicAuthCode.desc());\r
78 \r
79                 when(trans.getUserPrincipal()).thenReturn(null);\r
80                 when(trans.error()).thenReturn(error);\r
81 \r
82                 basicAuthCode.handle(trans, req, resp);\r
83         }\r
84 \r
85         @Test\r
86         public void testWithBasicUserPrincipal() throws Exception {\r
87                 BasicAuthCode basicAuthCode = new BasicAuthCode(authn, facade);\r
88                 LocateCode locateCode = basicAuthCode.clone(facade, false);\r
89 \r
90                 assertEquals(locateCode.desc(), basicAuthCode.desc());\r
91 \r
92                 when(trans.getUserPrincipal()).thenReturn(basicPrincipal);\r
93 \r
94                 basicAuthCode.handle(trans, req, resp);\r
95 \r
96                 verify(resp).setStatus(HttpStatus.OK_200);\r
97         }\r
98 \r
99         @Test\r
100         public void testWithX509UserPrincipal() throws Exception {\r
101                 BasicAuthCode basicAuthCode = new BasicAuthCode(authn, facade);\r
102                 LocateCode locateCode = basicAuthCode.clone(facade, false);\r
103 \r
104                 assertEquals(locateCode.desc(), basicAuthCode.desc());\r
105 \r
106                 when(trans.getUserPrincipal()).thenReturn(x509Principal);\r
107                 when(req.getHeader("Authorization")).thenReturn("Basic 76//76");\r
108 \r
109                 basicAuthCode.handle(trans, req, resp);\r
110 \r
111                 verify(resp).setStatus(HttpStatus.FORBIDDEN_403);\r
112         }\r
113 \r
114 }\r