1+ package hudson .security .csrf ;
2+
3+ import static org .hamcrest .MatcherAssert .assertThat ;
4+ import static org .hamcrest .Matchers .containsString ;
5+ import static org .hamcrest .Matchers .not ;
6+
7+ import org .htmlunit .html .HtmlPage ;
8+ import org .junit .jupiter .api .BeforeEach ;
9+ import org .junit .jupiter .api .Test ;
10+ import org .jvnet .hudson .test .JenkinsRule ;
11+ import org .jvnet .hudson .test .junit .jupiter .WithJenkins ;
12+
13+ @ WithJenkins
14+ class GlobalCrumbIssuerConfigurationTest {
15+
16+ private JenkinsRule j ;
17+
18+ @ BeforeEach
19+ void setUp (JenkinsRule rule ) {
20+ j = rule ;
21+ }
22+
23+ @ Test
24+ void csrfSectionShownWhenNonDefaultIssuerConfigured () throws Exception {
25+ // DefaultCrumbIssuer is default, but other CrumbIssuer descriptors exist in test environment
26+ // so the CSRF section should be visible
27+ j .jenkins .setCrumbIssuer (new DefaultCrumbIssuer (false ));
28+
29+ JenkinsRule .WebClient wc = j .createWebClient ();
30+ HtmlPage page = wc .goTo ("configureSecurity" );
31+ String pageContent = page .asNormalizedText ();
32+
33+ // With multiple CrumbIssuer descriptors available (from test extensions),
34+ // the CSRF Protection section should always be shown
35+ assertThat ("CSRF Protection section should be shown when multiple issuers are available" ,
36+ pageContent , containsString ("CSRF Protection" ));
37+ }
38+
39+ @ Test
40+ void csrfSectionShownWhenCsrfProtectionDisabled () throws Exception {
41+ boolean original = GlobalCrumbIssuerConfiguration .DISABLE_CSRF_PROTECTION ;
42+ try {
43+ GlobalCrumbIssuerConfiguration .DISABLE_CSRF_PROTECTION = true ;
44+
45+ JenkinsRule .WebClient wc = j .createWebClient ();
46+ HtmlPage page = wc .goTo ("configureSecurity" );
47+ String pageContent = page .asNormalizedText ();
48+
49+ assertThat ("CSRF section should be shown when CSRF protection is disabled" ,
50+ pageContent , containsString ("CSRF Protection" ));
51+ } finally {
52+ GlobalCrumbIssuerConfiguration .DISABLE_CSRF_PROTECTION = original ;
53+ }
54+ }
55+ }
0 commit comments