@Test public void testAttemptAuthentication() throws IOException, ServletException { VistaBasicAuthenticationFilter f = new VistaBasicAuthenticationFilter(); f.setAuthenticationManager(mockAuthenticationManager); f.setAuthenticationEntryPoint(authenticationEntryPoint); f.afterPropertiesSet(); String vistaId = "9F2B"; String division = "500"; String accessCode = "10VEHU"; String verifyCode = "VEHU10"; byte[] token = new String(vistaId + ";" + division + ":" + accessCode + ";" + verifyCode) .getBytes("UTF-8"); request.addHeader("Authorization", "Basic " + new String(Base64.encodeBase64(token), "UTF-8")); request.setRemoteAddr("10.0.1.34"); request.setRemoteHost("www.example.org"); request.setRequestURI("/foo.xml"); request.setMethod("GET"); VistaAuthenticationToken authRequest = new VistaAuthenticationToken( "9F2B", "500", "10VEHU", "VEHU10", "10.0.1.34", "www.example.org"); when(mockAuthenticationManager.authenticate(AuthenticationTokenMatchers.eq(authRequest))) .thenReturn( new VistaAuthenticationToken( new VistaUser( new RpcHost("localhost"), "9F2B", "500", "500", "12345", "500:10VEHU;VEHU10", "Vehu,Ten", true, true, true, true, new ArrayList<GrantedAuthority>()), "500:10VEHU;VEHU10", "10.0.1.34", null, new ArrayList<GrantedAuthority>())); f.doFilter(request, response, filterChain); assertSame(request, filterChain.getRequest()); assertSame(response, filterChain.getResponse()); verify(mockAuthenticationManager).authenticate(AuthenticationTokenMatchers.eq(authRequest)); }
private MockHttpServletRequest newMockRequest( String method, String requestUri, String host, int port) { MockHttpServletRequest req = new MockHttpServletRequest(method, requestUri); req.setRemoteHost(host); req.setContentType("application/x-www-form-urlencoded"); req.setRemotePort(port); if (port == 443) { req.setScheme("https"); req.setSecure(true); } else { req.setScheme("http"); req.setSecure(false); } return req; }
/** * Test method for {@link * se.vgregion.portal.auditlog.AuditLogInfoContainerFactoryImpl#getAuditLogInfoContainer(java.lang.String, * java.lang.String, javax.portlet.PortletRequest)} . */ @Test public final void testGetAuditLogInfoContainer() { MockPortletRequest portletRequest = new MockPortletRequest(); Map<String, String> uInfoMap = new HashMap<String, String>(); uInfoMap.put(PortletRequest.P3PUserInfos.USER_LOGIN_ID.toString(), "remoteUid"); portletRequest.setAttribute(PortletRequest.USER_INFO, uInfoMap); MockHttpServletRequest httpRequest = new MockHttpServletRequest(); httpRequest.setRemoteAddr("127.0.0.1"); httpRequest.setRemoteHost("remoteHost"); httpRequest.setRemotePort(123); given(converter.getHttpServletRequest(portletRequest)).willReturn(httpRequest); AuditLogInfoContainer container = factory.getAuditLogInfoContainer("patientId", portletRequest); assertEquals("patientId", container.getPatientId()); assertEquals("[LiferayUser:]remoteUid", container.getSearcherId()); assertEquals("remoteUid", container.getRemoteUser()); assertEquals("127.0.0.1 [Default]", container.getRemoteIpAddress()); assertEquals("remoteHost", container.getRemoteHost()); assertEquals(123, container.getRemotePort()); }