Search in sources :

Example 1 with UserContextService

use of com.mercedesbenz.sechub.sharedkernel.UserContextService in project sechub by mercedes-benz.

the class DefaultSecurityLogServiceTest method beforeEach.

@BeforeEach
void beforeEach() {
    logger = mock(Logger.class);
    userContextService = mock(UserContextService.class);
    // mock logsanitizer
    logsanitzer = mock(LogSanitizer.class);
    when(logsanitzer.sanitize(anyString(), anyInt())).thenAnswer(i -> SANITIZED + i.getArguments()[0]);
    when(logsanitzer.sanitize(anyString(), anyInt(), anyBoolean())).thenAnswer(i -> SANITIZED + i.getArguments()[0]);
    // mock obfuscation
    authorizedValueObfuscator = mock(AuthorizeValueObfuscator.class);
    when(authorizedValueObfuscator.obfuscate(anyString(), anyInt())).thenAnswer(i -> "obfuscated" + i.getArguments()[0]);
    // mock http data providers
    requestAttributesProvider = mock(RequestAttributesProvider.class);
    request = mock(HttpServletRequest.class);
    // final methods, so not by mockito
    ServletRequestAttributes attributes = new ServletRequestAttributes(request);
    httpSession = mock(HttpSession.class);
    when(request.getRemoteAddr()).thenReturn("fake-remote-addr");
    when(httpSession.getId()).thenReturn("fake-http-session-id");
    when(request.getRequestURI()).thenReturn("fake-request-uri");
    when(requestAttributesProvider.getRequestAttributes()).thenReturn(attributes);
    when(request.getSession()).thenReturn(httpSession);
    Map<String, String> map = new LinkedHashMap<>();
    map.put(KEY1, VALUE1);
    map.put(KEY2, VALUE2);
    map.put(KEY_AUTHORIZE, VALUE_AUTHORIZE);
    map.put(KEY_AUTHORIZE_mixcased, VALUE_AUTHORIZE);
    Iterator<String> it = map.keySet().iterator();
    when(request.getHeaderNames()).thenReturn(new Enumeration<String>() {

        @Override
        public String nextElement() {
            return it.next();
        }

        @Override
        public boolean hasMoreElements() {
            return it.hasNext();
        }
    });
    for (String key : map.keySet()) {
        when(request.getHeader(key)).thenReturn(map.get(key));
    }
    /* service to test uses a mocked logger */
    serviceToTest = new DefaultSecurityLogService() {

        Logger getLogger() {
            return logger;
        }
    };
    serviceToTest.logSanititzer = logsanitzer;
    serviceToTest.userContextService = userContextService;
    serviceToTest.requestAttributesProvider = requestAttributesProvider;
    serviceToTest.authorizedValueObfuscator = authorizedValueObfuscator;
}
Also used : HttpSession(javax.servlet.http.HttpSession) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) Logger(org.slf4j.Logger) LinkedHashMap(java.util.LinkedHashMap) HttpServletRequest(javax.servlet.http.HttpServletRequest) UserContextService(com.mercedesbenz.sechub.sharedkernel.UserContextService) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

UserContextService (com.mercedesbenz.sechub.sharedkernel.UserContextService)1 LinkedHashMap (java.util.LinkedHashMap)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpSession (javax.servlet.http.HttpSession)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Logger (org.slf4j.Logger)1 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)1