File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\test\src\test\java\hudson\security\CliAuthenticationTest.java.html

1 package hudson.security;
2
3 import hudson.cli.CLI;
4 import hudson.cli.CLICommand;
5 import hudson.cli.ClientAuthenticationCache;
6 import hudson.cli.LoginCommand;
7 import hudson.cli.LogoutCommand;
8 import jenkins.model.Jenkins;
9 import org.acegisecurity.Authentication;
10 import org.jvnet.hudson.test.For;
11 import org.jvnet.hudson.test.HudsonTestCase;
12 import org.jvnet.hudson.test.TestExtension;
13 import junit.framework.Assert;
14
15 /**
16 * @author Kohsuke Kawaguchi
17 */
18 public class CliAuthenticationTest extends HudsonTestCase {
19 public void test1() throws Exception {
20 // dummy security realm that authenticates when username==password
21 jenkins.setSecurityRealm(createDummySecurityRealm());
22
23 successfulCommand("test","--username","abc","--password","abc");
24 }
25
26 private void successfulCommand(String... args) throws Exception {
27 assertEquals(0, command(args));
28 }
29
30 private int command(String... args) throws Exception {
31 CLI cli = new CLI(getURL());
32 try {
33 return cli.execute(args);
34 } finally {
35 cli.close();
36 }
37 }
38
39 @TestExtension
40 public static class TestCommand extends CLICommand {
41 @Override
42 public String getShortDescription() {
43 return "test command";
44 }
45
46 @Override
47 protected int run() throws Exception {
48 Authentication auth = Jenkins.getAuthentication();
49 Assert.assertNotSame(Jenkins.ANONYMOUS,auth);
50 Assert.assertEquals("abc", auth.getName());
51 return 0;
52 }
53 }
54
55 @TestExtension
56 public static class AnonymousCommand extends CLICommand {
57 @Override
58 public String getShortDescription() {
59 return "makes sure that the command is running as anonymous user";
60 }
61
62 @Override
63 protected int run() throws Exception {
64 Authentication auth = Jenkins.getAuthentication();
65 Assert.assertSame(Jenkins.ANONYMOUS,auth);
66 return 0;
67 }
68 }
69
70 @For({LoginCommand.class, LogoutCommand.class, ClientAuthenticationCache.class})
71 public void testLogin() throws Exception {
72 jenkins.setSecurityRealm(createDummySecurityRealm());
73
74 successfulCommand("login","--username","abc","--password","abc");
75 successfulCommand("test"); // now we can run without an explicit credential
76 successfulCommand("logout");
77 successfulCommand("anonymous"); // now we should run as anonymous
78 }
79 }
80