HostValidatorService implements ApplicationListener<ContextRefreshedEvent> { public boolean isRequestFromAdmin(final ServletRequest request) { String host = ipAddressResolver.resolveToHostName(request.getRemoteAddr()); return !wilmaAdminHostsDTO.isSecurityEnabled() || allowedHosts.contains(host); }  boolean isRequestFromAdmin(final ServletRequest request); @Override void onApplicationEvent(final ContextRefreshedEvent event);  }
@Test public void testIsRequestFromAdminShouldAlwaysReturnTrueIfSecurityIsDisabled() { given(httpServletRequest.getRemoteAddr()).willReturn("ip"); given(ipAddressResolver.resolveToHostName("ip")).willReturn("host"); given(wilmaAdminHostsDTO.isSecurityEnabled()).willReturn(false); boolean result = underTest.isRequestFromAdmin(httpServletRequest); assertTrue(result); }
@Test public void testIsRequestFromAdminShouldReturnTrueWhenHostIsAdminAndSecurityIsEnabled() { given(httpServletRequest.getRemoteAddr()).willReturn("ip"); given(ipAddressResolver.resolveToHostName("ip")).willReturn("host"); given(wilmaAdminHostsDTO.isSecurityEnabled()).willReturn(true); given(allowedHosts.contains("host")).willReturn(true); boolean result = underTest.isRequestFromAdmin(httpServletRequest); assertTrue(result); }
@Test public void testIsRequestFromAdminShouldReturnFalseWhenHostIsNotAdminAndSecurityIsEnabled() { given(httpServletRequest.getRemoteAddr()).willReturn("ip"); given(ipAddressResolver.resolveToHostName("ip")).willReturn("host"); given(wilmaAdminHostsDTO.isSecurityEnabled()).willReturn(true); given(allowedHosts.contains("host")).willReturn(false); boolean result = underTest.isRequestFromAdmin(httpServletRequest); assertFalse(result); }