@Test public void ofApi() { Date d = new Date(); assertThat(Context.of("test", 12, "value", true, 123, 456L, true, false, d, "today")) .isInstanceOf(Context5.class) .hasToString("Context5{test=12, value=true, 123=456, true=false, " + d + "=today}"); }
@Test public void ofApi() { assertThat(Context.of("test", 12, "value", true)) .isInstanceOf(Context2.class) .hasToString("Context2{test=12, value=true}"); }
@Test public void ofApi() { assertThat(Context.of("test", 12, "value", true, 123, 456L, true, false)) .isInstanceOf(Context4.class) .hasToString("Context4{test=12, value=true, 123=456, true=false}"); }
@Test public void newScope_appliesWhenCurrentRequestContext() { try (SafeCloseable requestContextScope = mockRequestContext.push()) { try (Scope traceContextScope = currentTraceContext.newScope(traceContext)) { assertThat(traceContextScope).hasToString("InitialRequestScope"); assertThat(currentTraceContext.get()).isEqualTo(traceContext); } } }
@Test public void emptyApi() { assertThat(Context.empty()) .isInstanceOf(Context0.class) .hasToString("Context0{}"); }
@Test public void ofApi() { assertThat(Context.of("test", 12)) .isInstanceOf(Context1.class) .hasToString("Context1{test=12}"); }
@Test public void ofApi() { assertThat(Context.of("test", 12, "value", true, 123, 456L)) .isInstanceOf(Context3.class) .hasToString("Context3{test=12, value=true, 123=456}"); }
@Test public void newScope_doesNothingWhenNoCurrentRequestContext() { try (Scope traceContextScope = currentTraceContext.newScope(traceContext)) { assertThat(traceContextScope).hasToString("IncompleteConfigurationScope"); assertThat(currentTraceContext.get()).isNull(); } }
@Test public void putAllOf() { Context m = Context.of("A", 1, "B", 2, "C", 3); Context put = c.putAll(m); assertThat(put).isInstanceOf(Context3.class) .hasToString("Context3{A=1, B=2, C=3}"); }
@Test public void putAllOf() { Context m = Context.of("A", 1, "B", 2, "C", 3); Context put = c.putAll(m); assertThat(put).isInstanceOf(Context5.class) .hasToString("Context5{1=A, 2=B, A=1, B=2, C=3}"); }
@Test public void putAllOf() { Context m = Context.of("A", 1, "B", 2, "C", 3); Context put = c.putAll(m); assertThat(put).isInstanceOf(Context4.class) .hasToString("Context4{1=A, A=1, B=2, C=3}"); }
@Test public void newScope_closeDoesntClearFirstScope() { final TraceContext traceContext2 = TraceContext.newBuilder().traceId(1).spanId(2).build(); try (SafeCloseable requestContextScope = mockRequestContext.push()) { try (Scope traceContextScope = currentTraceContext.newScope(traceContext)) { assertThat(traceContextScope).hasToString("InitialRequestScope"); assertThat(currentTraceContext.get()).isEqualTo(traceContext); try (Scope traceContextScope2 = currentTraceContext.newScope(traceContext2)) { assertThat(traceContextScope2).hasToString("RequestContextTraceContextScope"); assertThat(currentTraceContext.get()).isEqualTo(traceContext2); } assertThat(currentTraceContext.get()).isEqualTo(traceContext); } // the first scope is attached to the request context and cleared when that's destroyed assertThat(currentTraceContext.get()).isEqualTo(traceContext); } }
@Test public void oauth2LoginCustomWithConfigurer() throws Exception { // setup application context loadConfig(OAuth2LoginConfigCustomWithConfigurer.class); // setup authorization request OAuth2AuthorizationRequest authorizationRequest = createOAuth2AuthorizationRequest(); this.authorizationRequestRepository.saveAuthorizationRequest( authorizationRequest, this.request, this.response); // setup authentication parameters this.request.setParameter("code", "code123"); this.request.setParameter("state", authorizationRequest.getState()); // perform test this.springSecurityFilterChain.doFilter(this.request, this.response, this.filterChain); // assertions Authentication authentication = this.securityContextRepository .loadContext(new HttpRequestResponseHolder(this.request, this.response)) .getAuthentication(); assertThat(authentication.getAuthorities()).hasSize(2); assertThat(authentication.getAuthorities()).first().hasToString("ROLE_USER"); assertThat(authentication.getAuthorities()).last().hasToString("ROLE_OAUTH2_USER"); }
@Test public void oauth2LoginCustomWithBeanRegistration() throws Exception { // setup application context loadConfig(OAuth2LoginConfigCustomWithBeanRegistration.class); // setup authorization request OAuth2AuthorizationRequest authorizationRequest = createOAuth2AuthorizationRequest(); this.authorizationRequestRepository.saveAuthorizationRequest( authorizationRequest, this.request, this.response); // setup authentication parameters this.request.setParameter("code", "code123"); this.request.setParameter("state", authorizationRequest.getState()); // perform test this.springSecurityFilterChain.doFilter(this.request, this.response, this.filterChain); // assertions Authentication authentication = this.securityContextRepository .loadContext(new HttpRequestResponseHolder(this.request, this.response)) .getAuthentication(); assertThat(authentication.getAuthorities()).hasSize(2); assertThat(authentication.getAuthorities()).first().hasToString("ROLE_USER"); assertThat(authentication.getAuthorities()).last().hasToString("ROLE_OAUTH2_USER"); }
@Test public void oidcLoginCustomWithConfigurer() throws Exception { // setup application context loadConfig(OAuth2LoginConfigCustomWithConfigurer.class, JwtDecoderFactoryConfig.class); // setup authorization request OAuth2AuthorizationRequest authorizationRequest = createOAuth2AuthorizationRequest("openid"); this.authorizationRequestRepository.saveAuthorizationRequest( authorizationRequest, this.request, this.response); // setup authentication parameters this.request.setParameter("code", "code123"); this.request.setParameter("state", authorizationRequest.getState()); // perform test this.springSecurityFilterChain.doFilter(this.request, this.response, this.filterChain); // assertions Authentication authentication = this.securityContextRepository .loadContext(new HttpRequestResponseHolder(this.request, this.response)) .getAuthentication(); assertThat(authentication.getAuthorities()).hasSize(2); assertThat(authentication.getAuthorities()).first().hasToString("ROLE_USER"); assertThat(authentication.getAuthorities()).last().hasToString("ROLE_OIDC_USER"); }
@Test public void oidcLoginCustomWithBeanRegistration() throws Exception { // setup application context loadConfig(OAuth2LoginConfigCustomWithBeanRegistration.class, JwtDecoderFactoryConfig.class); // setup authorization request OAuth2AuthorizationRequest authorizationRequest = createOAuth2AuthorizationRequest("openid"); this.authorizationRequestRepository.saveAuthorizationRequest( authorizationRequest, this.request, this.response); // setup authentication parameters this.request.setParameter("code", "code123"); this.request.setParameter("state", authorizationRequest.getState()); // perform test this.springSecurityFilterChain.doFilter(this.request, this.response, this.filterChain); // assertions Authentication authentication = this.securityContextRepository .loadContext(new HttpRequestResponseHolder(this.request, this.response)) .getAuthentication(); assertThat(authentication.getAuthorities()).hasSize(2); assertThat(authentication.getAuthorities()).first().hasToString("ROLE_USER"); assertThat(authentication.getAuthorities()).last().hasToString("ROLE_OIDC_USER"); }
@Test public void newScope_respondsToPing() { final PingPongExtra extra = new PingPongExtra(); final TraceContext extraContext = TraceContext.newBuilder().traceId(1).spanId(1) .extra(Collections.singletonList(extra)).build(); try (Scope traceContextScope = currentTraceContext.newScope(extraContext)) { assertThat(traceContextScope).hasToString("NoopScope"); assertThat(extra.isPong()).isTrue(); } }
@Test public void oidcLogin() throws Exception { // setup application context loadConfig(OAuth2LoginConfig.class, JwtDecoderFactoryConfig.class); // setup authorization request OAuth2AuthorizationRequest authorizationRequest = createOAuth2AuthorizationRequest("openid"); this.authorizationRequestRepository.saveAuthorizationRequest( authorizationRequest, this.request, this.response); // setup authentication parameters this.request.setParameter("code", "code123"); this.request.setParameter("state", authorizationRequest.getState()); // perform test this.springSecurityFilterChain.doFilter(this.request, this.response, this.filterChain); // assertions Authentication authentication = this.securityContextRepository .loadContext(new HttpRequestResponseHolder(this.request, this.response)) .getAuthentication(); assertThat(authentication.getAuthorities()).hasSize(1); assertThat(authentication.getAuthorities()).first() .isInstanceOf(OidcUserAuthority.class).hasToString("ROLE_USER"); }
@Test public void oauth2Login() throws Exception { // setup application context loadConfig(OAuth2LoginConfig.class); // setup authorization request OAuth2AuthorizationRequest authorizationRequest = createOAuth2AuthorizationRequest(); this.authorizationRequestRepository.saveAuthorizationRequest( authorizationRequest, this.request, this.response); // setup authentication parameters this.request.setParameter("code", "code123"); this.request.setParameter("state", authorizationRequest.getState()); // perform test this.springSecurityFilterChain.doFilter(this.request, this.response, this.filterChain); // assertions Authentication authentication = this.securityContextRepository .loadContext(new HttpRequestResponseHolder(this.request, this.response)) .getAuthentication(); assertThat(authentication.getAuthorities()).hasSize(1); assertThat(authentication.getAuthorities()).first() .isInstanceOf(OAuth2UserAuthority.class).hasToString("ROLE_USER"); }
@Test public void oauth2LoginConfigLoginProcessingUrl() throws Exception { // setup application context loadConfig(OAuth2LoginConfigLoginProcessingUrl.class); // setup authorization request OAuth2AuthorizationRequest authorizationRequest = createOAuth2AuthorizationRequest(); this.request.setServletPath("/login/oauth2/google"); this.authorizationRequestRepository.saveAuthorizationRequest( authorizationRequest, this.request, this.response); // setup authentication parameters this.request.setParameter("code", "code123"); this.request.setParameter("state", authorizationRequest.getState()); // perform test this.springSecurityFilterChain.doFilter(this.request, this.response, this.filterChain); // assertions Authentication authentication = this.securityContextRepository .loadContext(new HttpRequestResponseHolder(this.request, this.response)) .getAuthentication(); assertThat(authentication.getAuthorities()).hasSize(1); assertThat(authentication.getAuthorities()).first() .isInstanceOf(OAuth2UserAuthority.class).hasToString("ROLE_USER"); }