|
5 | 5 | import static org.hamcrest.CoreMatchers.is; |
6 | 6 | import static org.hamcrest.CoreMatchers.not; |
7 | 7 | import static org.hamcrest.MatcherAssert.assertThat; |
| 8 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
8 | 9 |
|
9 | 10 | import hudson.security.SecurityRealm; |
10 | 11 | import hudson.util.Secret; |
11 | 12 | import java.io.File; |
12 | 13 | import java.io.FileReader; |
13 | 14 | import java.io.IOException; |
14 | 15 | import java.util.Collections; |
15 | | -import java.util.concurrent.Callable; |
16 | 16 | import jenkins.model.Jenkins; |
17 | 17 | import org.apache.commons.io.IOUtils; |
18 | | -import org.junit.Assert; |
19 | | -import org.junit.Before; |
20 | | -import org.junit.Rule; |
21 | | -import org.junit.Test; |
| 18 | +import org.junit.jupiter.api.BeforeEach; |
| 19 | +import org.junit.jupiter.api.Test; |
22 | 20 | import org.jvnet.hudson.test.Issue; |
23 | 21 | import org.jvnet.hudson.test.JenkinsRule; |
| 22 | +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; |
24 | 23 | import org.jvnet.hudson.test.recipes.LocalData; |
25 | 24 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
26 | 25 | import org.springframework.security.core.Authentication; |
27 | 26 | import org.springframework.security.core.userdetails.UserDetails; |
28 | 27 |
|
29 | | -public class ReverseProxySecurityRealmTest { |
30 | | - @Rule |
31 | | - public final JenkinsRule jenkinsRule = new JenkinsRule(); |
| 28 | +@WithJenkins |
| 29 | +class ReverseProxySecurityRealmTest { |
32 | 30 |
|
33 | | - private Jenkins jenkins; |
| 31 | + private JenkinsRule j; |
34 | 32 |
|
35 | | - @Before |
36 | | - public void setUp() { |
37 | | - jenkins = jenkinsRule.jenkins; |
| 33 | + @BeforeEach |
| 34 | + void setUp(JenkinsRule rule) { |
| 35 | + j = rule; |
38 | 36 | } |
39 | 37 |
|
40 | 38 | @Test |
41 | | - public void basicGetUserDetails() { |
| 39 | + void basicGetUserDetails() { |
42 | 40 | final ReverseProxySecurityRealm realm = createBasicRealm(); |
43 | 41 | final UserDetails userDetails = realm. loadUserByUsername2( "[email protected]"); |
44 | | - Assert.assertEquals( "[email protected]", userDetails. getUsername()); |
| 42 | + assertEquals( "[email protected]", userDetails. getUsername()); |
45 | 43 | } |
46 | 44 |
|
47 | 45 | @Test |
48 | 46 | @Issue("JENKINS-49274") |
49 | | - public void basicAuthenticate() throws Exception { |
| 47 | + void basicAuthenticate() throws Exception { |
50 | 48 | final ReverseProxySecurityRealm realm = createBasicRealm(); |
51 | | - jenkins.setSecurityRealm(realm); |
| 49 | + j.jenkins.setSecurityRealm(realm); |
52 | 50 |
|
53 | | - final JenkinsRule.WebClient client = jenkinsRule.createWebClient(); |
54 | | - client. addRequestHeader( realm. getForwardedUser(), "[email protected]"); |
55 | | - final Authentication authentication = client.executeOnServer(new Callable<Authentication>() { |
56 | | - @Override |
57 | | - public Authentication call() { |
58 | | - return Jenkins.getAuthentication2(); |
59 | | - } |
60 | | - }); |
61 | | - Assert.assertEquals( |
62 | | - "Authentication should match", |
63 | | - new UsernamePasswordAuthenticationToken( |
64 | | - "[email protected]", "", Collections. singleton( SecurityRealm. AUTHENTICATED_AUTHORITY2)), |
65 | | - authentication); |
| 51 | + try (JenkinsRule.WebClient client = j.createWebClient()) { |
| 52 | + client. addRequestHeader( realm. getForwardedUser(), "[email protected]"); |
| 53 | + final Authentication authentication = client.executeOnServer(Jenkins::getAuthentication2); |
| 54 | + assertEquals( |
| 55 | + new UsernamePasswordAuthenticationToken( |
| 56 | + "[email protected]", "", Collections. singleton( SecurityRealm. AUTHENTICATED_AUTHORITY2)), |
| 57 | + authentication, |
| 58 | + "Authentication should match"); |
| 59 | + } |
66 | 60 | } |
67 | 61 |
|
68 | 62 | private ReverseProxySecurityRealm createBasicRealm() { |
@@ -94,8 +88,8 @@ private ReverseProxySecurityRealm createBasicRealm() { |
94 | 88 |
|
95 | 89 | @Test |
96 | 90 | @LocalData |
97 | | - public void testPasswordMigration() throws IOException { |
98 | | - final SecurityRealm securityRealm = jenkinsRule.jenkins.getSecurityRealm(); |
| 91 | + void testPasswordMigration() throws IOException { |
| 92 | + final SecurityRealm securityRealm = j.jenkins.getSecurityRealm(); |
99 | 93 | assertThat(securityRealm, instanceOf(ReverseProxySecurityRealm.class)); |
100 | 94 | ReverseProxySecurityRealm reverseProxySecurityRealm = (ReverseProxySecurityRealm) securityRealm; |
101 | 95 | assertThat(reverseProxySecurityRealm.getManagerPassword().getPlainText(), is("theManagerPassw0rd")); |
|
0 commit comments