Skip to content
Permalink
Browse files
Merge pull request #1340 from daniel-beck/JENKINS-23988
[FIXED JENKINS-23988] Consider CLIAuthenticator for CLIMethods
  • Loading branch information
daniel-beck committed Aug 14, 2014
2 parents 1cf7354 + d8489fc commit c869cfdc0e9ee0d6f77a9335b1055220e57bc7d5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
@@ -149,8 +149,6 @@ private CmdLineParser bindMethod(List<MethodBinder> binders) {
while (!chains.isEmpty())
binders.add(new MethodBinder(chains.pop(),this,parser));

new ClassParser().parse(Jenkins.getInstance().getSecurityRealm().createCliAuthenticator(this), parser);

return parser;
}

@@ -169,6 +167,7 @@ public int main(List<String> args, Locale locale, InputStream stdin, PrintStream
try {
// authentication
CliAuthenticator authenticator = Jenkins.getInstance().getSecurityRealm().createCliAuthenticator(this);
new ClassParser().parse(authenticator,parser);

// fill up all the binders
parser.parseArgument(args);
@@ -0,0 +1,36 @@
package hudson.cli;

import static hudson.cli.CLICommandInvoker.Matcher.failedWith;
import static hudson.cli.CLICommandInvoker.Matcher.succeededSilently;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.containsString;
import jenkins.model.Jenkins;
import hudson.cli.CLICommandInvoker;
import hudson.cli.CLICommandInvoker.Result;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

public class CLIRegistererTest {

@Rule
public JenkinsRule j = new JenkinsRule();

@Test
public void testAuthWithSecurityRealmCLIAuthenticator() {
j.getInstance().setSecurityRealm(j.createDummySecurityRealm());

CLICommandInvoker command = new CLICommandInvoker(j, "quiet-down");

Result invocation = command.invokeWithArgs("--username", "foo", "--password", "invalid");
assertThat(invocation, failedWith(1));
assertThat(invocation.stderr(), containsString("BadCredentialsException: foo"));
Assert.assertFalse("Unauthorized command was executed", Jenkins.getInstance().isQuietingDown());

invocation = command.invokeWithArgs("--username", "foo", "--password", "foo");
assertThat(invocation, succeededSilently());
Assert.assertTrue("Authorized command was not executed", Jenkins.getInstance().isQuietingDown());
}
}

0 comments on commit c869cfd

Please sign in to comment.