Skip to content

Commit

Permalink
[JENKINS-28298] Added tests for disabling strategies.
Browse files Browse the repository at this point in the history
  • Loading branch information
ikedam committed Jul 13, 2015
1 parent 5a5d514 commit 396f3ea
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
Expand Up @@ -25,11 +25,18 @@
package org.jenkinsci.plugins.authorizeproject;

import static org.junit.Assert.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import jenkins.model.Jenkins;
import jenkins.security.QueueItemAuthenticatorConfiguration;
import hudson.matrix.AxisList;
import hudson.matrix.MatrixProject;
import hudson.matrix.TextAxis;
import hudson.model.AbstractProject;
import hudson.model.Describable;
import hudson.model.Descriptor;
import hudson.model.FreeStyleProject;
import hudson.model.Job;
Expand All @@ -47,6 +54,7 @@
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsRule.WebClient;
import org.jvnet.hudson.test.TestExtension;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

import com.gargoylesoftware.htmlunit.html.HtmlForm;
Expand Down Expand Up @@ -79,6 +87,10 @@ public class ProjectQueueItemAuthenticatorTest {
public JenkinsRule j = new AuthorizeProjectJenkinsRule();

public static class NullAuthorizeProjectStrategy extends AuthorizeProjectStrategy {
@DataBoundConstructor
public NullAuthorizeProjectStrategy() {
}

@Override
public Authentication authenticate(Job<?, ?> project, Queue.Item item) {
return null;
Expand Down Expand Up @@ -196,6 +208,58 @@ public void testWorkForMatrixProject() throws Exception {
}
}

@Test
public void testDisabledInProjectConfiguration() throws Exception {
FreeStyleProject p = j.createFreeStyleProject();
p.addProperty(new AuthorizeProjectProperty(new AnonymousAuthorizationStrategy()));

assertTrue(ProjectQueueItemAuthenticator.getConfigured().isStrategyEnabled(j.jenkins.getDescriptor(AnonymousAuthorizationStrategy.class)));

j.configRoundtrip(p);

// can be reconfigured if it is enabled.
assertEquals(AnonymousAuthorizationStrategy.class, p.getProperty(AuthorizeProjectProperty.class).getStrategy().getClass());

Map<String, Boolean> strategyEnabledMap = new HashMap<String, Boolean>();
strategyEnabledMap.put(j.jenkins.getDescriptor(AnonymousAuthorizationStrategy.class).getId(), false);

QueueItemAuthenticatorConfiguration.get().getAuthenticators().clear();
QueueItemAuthenticatorConfiguration.get().getAuthenticators().add(new ProjectQueueItemAuthenticator(strategyEnabledMap));

assertFalse(ProjectQueueItemAuthenticator.getConfigured().isStrategyEnabled(j.jenkins.getDescriptor(AnonymousAuthorizationStrategy.class)));

j.configRoundtrip(p);

// cannot be reconfigured if it is disabled.
assertNotEquals(AnonymousAuthorizationStrategy.class, p.getProperty(AuthorizeProjectProperty.class).getStrategy().getClass());
}

@Test
public void testDisabledAtRuntime() throws Exception {
FreeStyleProject p = j.createFreeStyleProject();
AuthorizationCheckBuilder checker = new AuthorizationCheckBuilder();
p.getBuildersList().add(checker);
p.addProperty(new AuthorizeProjectProperty(new AnonymousAuthorizationStrategy()));

assertTrue(ProjectQueueItemAuthenticator.getConfigured().isStrategyEnabled(j.jenkins.getDescriptor(AnonymousAuthorizationStrategy.class)));

// strategy works if it is enabled
j.assertBuildStatusSuccess(p.scheduleBuild2(0));
assertEquals(Jenkins.ANONYMOUS, checker.authentication);

Map<String, Boolean> strategyEnabledMap = new HashMap<String, Boolean>();
strategyEnabledMap.put(j.jenkins.getDescriptor(AnonymousAuthorizationStrategy.class).getId(), false);

QueueItemAuthenticatorConfiguration.get().getAuthenticators().clear();
QueueItemAuthenticatorConfiguration.get().getAuthenticators().add(new ProjectQueueItemAuthenticator(strategyEnabledMap));

assertFalse(ProjectQueueItemAuthenticator.getConfigured().isStrategyEnabled(j.jenkins.getDescriptor(AnonymousAuthorizationStrategy.class)));

// strategy doesn't work if it is disabled even when it is configured
j.assertBuildStatusSuccess(p.scheduleBuild2(0));
assertEquals(ACL.SYSTEM, checker.authentication);
}

/**
* Test no exception even if AuthorizeProjectStrategyDescriptor is not used.
*/
Expand Down Expand Up @@ -371,6 +435,43 @@ public void testGlobalSecurityConfiguration() throws Exception {
assertEquals(alternateValue1, alternateField.getValueAttribute());
}

// enabled / disabled preservation
// all are enabled
Map<String, Boolean> strategyEnabledMap = new HashMap<String, Boolean>();
strategyEnabledMap.put(j.jenkins.getDescriptor(AuthorizeProjectStrategyExtendingBaseDescrptor.class).getId(), true);
strategyEnabledMap.put(j.jenkins.getDescriptor(AuthorizeProjectStrategyWithoutGlobalSecurityConfiguration.class).getId(), true);
strategyEnabledMap.put(j.jenkins.getDescriptor(AuthorizeProjectStrategyWithGlobalSecurityConfiguration.class).getId(), true);
strategyEnabledMap.put(j.jenkins.getDescriptor(AuthorizeProjectStrategyWithAlternateGlobalSecurityConfiguration.class).getId(), true);
assertStrategyEnablingConfigurationPreserved(strategyEnabledMap);

// all are disabled
strategyEnabledMap.put(j.jenkins.getDescriptor(AuthorizeProjectStrategyExtendingBaseDescrptor.class).getId(), false);
strategyEnabledMap.put(j.jenkins.getDescriptor(AuthorizeProjectStrategyWithoutGlobalSecurityConfiguration.class).getId(), false);
strategyEnabledMap.put(j.jenkins.getDescriptor(AuthorizeProjectStrategyWithGlobalSecurityConfiguration.class).getId(), false);
strategyEnabledMap.put(j.jenkins.getDescriptor(AuthorizeProjectStrategyWithAlternateGlobalSecurityConfiguration.class).getId(), false);
assertStrategyEnablingConfigurationPreserved(strategyEnabledMap);

// mixed
strategyEnabledMap.put(j.jenkins.getDescriptor(AuthorizeProjectStrategyExtendingBaseDescrptor.class).getId(), true);
strategyEnabledMap.put(j.jenkins.getDescriptor(AuthorizeProjectStrategyWithoutGlobalSecurityConfiguration.class).getId(), false);
strategyEnabledMap.put(j.jenkins.getDescriptor(AuthorizeProjectStrategyWithGlobalSecurityConfiguration.class).getId(), true);
strategyEnabledMap.put(j.jenkins.getDescriptor(AuthorizeProjectStrategyWithAlternateGlobalSecurityConfiguration.class).getId(), false);
assertStrategyEnablingConfigurationPreserved(strategyEnabledMap);
}

public void assertStrategyEnablingConfigurationPreserved(Map<String, Boolean> strategyEnabledMap) throws Exception {
QueueItemAuthenticatorConfiguration.get().getAuthenticators().clear();
QueueItemAuthenticatorConfiguration.get().getAuthenticators().add(new ProjectQueueItemAuthenticator(strategyEnabledMap));
j.submit(j.createWebClient().goTo("configureSecurity").getFormByName("config"));
for (Entry<String, Boolean> entry: strategyEnabledMap.entrySet()) {
assertEquals(
entry.getKey(),
entry.getValue(),
ProjectQueueItemAuthenticator.getConfigured().isStrategyEnabled(
j.jenkins.getDescriptor(entry.getKey())
)
);
}
}

/**
Expand Down
@@ -0,0 +1,27 @@
<!--
The MIT License
Copyright (c) 2015 IKEDA Yasuyuki
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<!-- dropdownDescriptorSelector causes exception without config.jelly -->
</j:jelly>

0 comments on commit 396f3ea

Please sign in to comment.