|
25 | 25 | package hudson.jobs;
|
26 | 26 |
|
27 | 27 | import static org.hamcrest.MatcherAssert.assertThat;
|
| 28 | +import static org.hamcrest.Matchers.containsInAnyOrder; |
28 | 29 | import static org.hamcrest.Matchers.nullValue;
|
29 | 30 | import static org.junit.Assert.assertEquals;
|
| 31 | +import static org.junit.Assert.assertFalse; |
30 | 32 | import static org.junit.Assert.assertNotNull;
|
| 33 | +import static org.junit.Assert.assertTrue; |
31 | 34 |
|
32 | 35 | import hudson.model.Failure;
|
33 | 36 | import hudson.model.FreeStyleProject;
|
34 | 37 | import hudson.model.Item;
|
35 | 38 | import hudson.model.ItemGroup;
|
| 39 | +import hudson.model.ListView; |
| 40 | +import hudson.model.User; |
36 | 41 | import hudson.model.listeners.ItemListener;
|
37 | 42 | import java.net.HttpURLConnection;
|
38 | 43 | import java.net.URI;
|
39 | 44 | import java.net.URL;
|
40 | 45 | import java.text.MessageFormat;
|
| 46 | +import jenkins.model.Jenkins; |
41 | 47 | import org.htmlunit.HttpMethod;
|
42 | 48 | import org.htmlunit.Page;
|
43 | 49 | import org.htmlunit.WebRequest;
|
|
46 | 52 | import org.junit.Test;
|
47 | 53 | import org.jvnet.hudson.test.Issue;
|
48 | 54 | import org.jvnet.hudson.test.JenkinsRule;
|
| 55 | +import org.jvnet.hudson.test.MockAuthorizationStrategy; |
49 | 56 | import org.jvnet.hudson.test.MockFolder;
|
50 | 57 | import org.jvnet.hudson.test.TestExtension;
|
51 | 58 |
|
@@ -142,4 +149,50 @@ public void onCheckCopy(Item src, ItemGroup parent) throws Failure {
|
142 | 149 | }
|
143 | 150 | }
|
144 | 151 |
|
| 152 | + @Issue("JENKINS-74795") |
| 153 | + @Test |
| 154 | + public void testCreateItemDoesNotPopulateDefaultView() throws Exception { |
| 155 | + // Create a view that only displays jobs that start with 'a-' |
| 156 | + FreeStyleProject aJob = rule.createFreeStyleProject("a-freestyle-job"); |
| 157 | + ListView aView = new ListView("a-view"); |
| 158 | + aView.setIncludeRegex("a-.*"); |
| 159 | + rule.jenkins.addView(aView); |
| 160 | + assertThat(aView.getItems(), containsInAnyOrder(aJob)); |
| 161 | + assertFalse(aView.isDefault()); // Not yet the default view |
| 162 | + |
| 163 | + // Create a view that only displays jobs that start with 'b-' |
| 164 | + FreeStyleProject bJob = rule.createFreeStyleProject("b-freestyle-job"); |
| 165 | + ListView bView = new ListView("b-view"); |
| 166 | + bView.setIncludeRegex("b-.*"); |
| 167 | + rule.jenkins.addView(bView); |
| 168 | + assertThat(bView.getItems(), containsInAnyOrder(bJob)); |
| 169 | + assertFalse(bView.isDefault()); // Not the default view |
| 170 | + |
| 171 | + // Make the a-view the default |
| 172 | + rule.jenkins.setPrimaryView(aView); |
| 173 | + assertTrue(aView.isDefault()); // Now a-view is the default view |
| 174 | + |
| 175 | + // Use createItem API to create a new job |
| 176 | + User user = User.getById("user", true); |
| 177 | + rule.jenkins.setSecurityRealm(rule.createDummySecurityRealm()); |
| 178 | + rule.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy() |
| 179 | + .grant(Jenkins.READ, Item.CREATE) |
| 180 | + .everywhere() |
| 181 | + .to(user.getId())); |
| 182 | + String b2JobName = "b-freestyle-job-2"; |
| 183 | + try (JenkinsRule.WebClient wc = rule.createWebClient()) { |
| 184 | + wc.login(user.getId()); |
| 185 | + WebRequest request = new WebRequest(wc.createCrumbedUrl("createItem?name=" + b2JobName), HttpMethod.POST); |
| 186 | + request.setAdditionalHeader("Content-Type", "application/xml"); |
| 187 | + request.setRequestBody("<project/>"); |
| 188 | + wc.getPage(request); |
| 189 | + } |
| 190 | + FreeStyleProject b2Job = rule.jenkins.getItemByFullName(b2JobName, FreeStyleProject.class); |
| 191 | + assertThat(bView.getItems(), containsInAnyOrder(bJob, b2Job)); |
| 192 | + assertFalse(bView.isDefault()); |
| 193 | + |
| 194 | + // Confirm new job is not visible in default view |
| 195 | + assertTrue(aView.isDefault()); // a-view is still the default view |
| 196 | + assertThat(aView.getItems(), containsInAnyOrder(aJob)); |
| 197 | + } |
145 | 198 | }
|
0 commit comments