List of usage examples for org.springframework.security.oauth2.provider OAuth2Authentication isAuthenticated
@Override public boolean isAuthenticated()
From source file:com.ge.predix.uaa.token.lib.ZacTokenServiceTest.java
@SuppressWarnings("unchecked") private OAuth2Authentication loadAuthentication(final String zoneName, final String zoneUserScope, final String requestUri, final List<String> nonZoneUriPatterns) { ZacTokenService zacTokenServices = new ZacTokenService(); zacTokenServices.setServiceZoneHeaders(PREDIX_ZONE_HEADER_NAME); Collection<GrantedAuthority> authorities = new HashSet<GrantedAuthority>(); authorities.add(new SimpleGrantedAuthority(zoneUserScope)); OAuth2Authentication oauth2Authentication = Mockito.mock(OAuth2Authentication.class); Mockito.when(oauth2Authentication.isAuthenticated()).thenReturn(true); FastTokenServices mockFTS = Mockito.mock(FastTokenServices.class); Mockito.doNothing().when(mockFTS).setUseHttps(true); Mockito.doNothing().when(mockFTS).setStoreClaims(true); Mockito.doNothing().when(mockFTS).setTrustedIssuers(Matchers.anyList()); Mockito.when(oauth2Authentication.getAuthorities()).thenReturn(authorities); Mockito.when(mockFTS.loadAuthentication(Matchers.anyString())).thenReturn(oauth2Authentication); FastTokenServicesCreator mockFTSC = Mockito.mock(FastTokenServicesCreator.class); when(mockFTSC.newInstance()).thenReturn(mockFTS); zacTokenServices.setFastRemoteTokenServicesCreator(mockFTSC); zacTokenServices.setServiceBaseDomain(BASE_DOMAIN); zacTokenServices.setServiceId(SERVICEID); DefaultZoneConfiguration zoneConfig = new DefaultZoneConfiguration(); List<String> trustedIssuers; // Non zone specific request, using default issuer if (StringUtils.isEmpty(zoneName)) { trustedIssuers = Arrays.asList(DEFAULT_TRUSTED_ISSUER); // Zone specific request, using the issuers returned by mockTrustedIssuersResponseEntity } else {//from w ww .j a va2 s . com trustedIssuers = ZONE_TRUSTED_ISSUERS; } zoneConfig.setTrustedIssuerIds(trustedIssuers); zacTokenServices.setDefaultZoneConfig(zoneConfig); zoneConfig.setAllowedUriPatterns(nonZoneUriPatterns); HttpServletRequest request = Mockito.mock(HttpServletRequest.class); when(request.getServerName()).thenReturn("localhost"); when(request.getHeader(PREDIX_ZONE_HEADER_NAME)).thenReturn(zoneName); when(request.getRequestURI()).thenReturn(requestUri); zacTokenServices.setRequest(request); RestTemplate restTemplateMock = Mockito.mock(RestTemplate.class); zacTokenServices.setOauth2RestTemplate(restTemplateMock); try { zacTokenServices.afterPropertiesSet(); } catch (Exception e) { Assert.fail("Unexpected exception after properties set on zacTokenServices " + e.getMessage()); } when(restTemplateMock.getForEntity("null/v1/registration/" + SERVICEID + "/" + ZONE, TrustedIssuers.class)) .thenReturn(mockTrustedIssuersResponseEntity()); when(restTemplateMock.getForEntity("null/v1/registration/" + SERVICEID + "/" + INVALID_ZONE, TrustedIssuers.class)).thenThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND)); String accessToken = this.tokenUtil.mockAccessToken(600, zoneUserScope); OAuth2Authentication loadAuthentication = zacTokenServices.loadAuthentication(accessToken); // Making sure we are passing the right set of issuers to the FastTokenServices Mockito.verify(mockFTS).setTrustedIssuers(trustedIssuers); return loadAuthentication; }