PageRenderTime 39ms CodeModel.GetById 22ms app.highlight 16ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/security/loginmodules/IdentityLoginModuleTestCase.java

#
Java | 199 lines | 124 code | 33 blank | 42 comment | 0 complexity | 10c64fdfd73455c1df52cecff7390605 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1/*
  2 * JBoss, Home of Professional Open Source.
  3 * Copyright (c) 2011, Red Hat, Inc., and individual contributors
  4 * as indicated by the @author tags. See the copyright.txt file in the
  5 * distribution for a full listing of individual contributors.
  6 *
  7 * This is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU Lesser General Public License as
  9 * published by the Free Software Foundation; either version 2.1 of
 10 * the License, or (at your option) any later version.
 11 *
 12 * This software is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15 * Lesser General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU Lesser General Public
 18 * License along with this software; if not, write to the Free
 19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21 */
 22
 23package org.jboss.as.test.integration.security.loginmodules;
 24
 25import java.io.IOException;
 26import java.net.URL;
 27import java.util.HashMap;
 28import java.util.Map;
 29
 30import org.apache.http.HttpResponse;
 31import org.apache.http.client.methods.HttpGet;
 32import org.apache.http.impl.client.DefaultHttpClient;
 33import org.jboss.arquillian.container.test.api.Deployment;
 34import org.jboss.arquillian.container.test.api.OperateOnDeployment;
 35import org.jboss.arquillian.container.test.api.RunAsClient;
 36import org.jboss.arquillian.junit.Arquillian;
 37import org.jboss.arquillian.test.api.ArquillianResource;
 38import org.jboss.as.arquillian.api.ServerSetup;
 39import org.jboss.as.arquillian.container.ManagementClient;
 40import org.jboss.as.test.integration.security.common.AbstractSecurityDomainSetup;
 41import org.jboss.as.test.integration.security.common.Utils;
 42import org.jboss.as.test.integration.security.loginmodules.common.servlets.PrincipalPrintingServlet;
 43import org.jboss.logging.Logger;
 44import org.jboss.security.auth.spi.IdentityLoginModule;
 45import org.jboss.shrinkwrap.api.ShrinkWrap;
 46import org.jboss.shrinkwrap.api.spec.WebArchive;
 47import org.junit.Test;
 48import org.junit.runner.RunWith;
 49
 50import static org.junit.Assert.assertTrue;
 51
 52/**
 53 * Tests of login via IdentityLoginModule
 54 *
 55 * @author <a href="mailto:jlanik@redhat.com">Jan Lanik</a>.
 56 */
 57@RunWith(Arquillian.class)
 58@RunAsClient
 59@ServerSetup({IdentityLoginModuleTestCase.SecurityDomain1Setup.class, IdentityLoginModuleTestCase.SecurityDomain2Setup.class})
 60public class IdentityLoginModuleTestCase {
 61
 62   private static Logger log = Logger.getLogger(IdentityLoginModuleTestCase.class);
 63
 64   private static final String DEP1 = "IdentityLoginModule-defaultPrincipal";
 65
 66    static class SecurityDomain1Setup extends AbstractSecurityDomainSetup {
 67
 68        @Override
 69        protected String getSecurityDomainName() {
 70            return "TestIdentityLoginDomain";
 71        }
 72
 73        @Override
 74        public void setup(final ManagementClient managementClient, final String containerId) throws Exception {
 75            log.debug("adding module options");
 76            Map<String,String> moduleOptionsMap = new HashMap<String,String>();
 77            moduleOptionsMap.put("roles", "role1,role2");
 78
 79            log.info("creating security domain: TestIdentityLoginDomain");
 80            createSecurityDomain(IdentityLoginModule.class, moduleOptionsMap, managementClient.getControllerClient());
 81            log.info("security domain created");
 82        }
 83    }
 84    static class SecurityDomain2Setup extends AbstractSecurityDomainSetup {
 85
 86        @Override
 87        protected String getSecurityDomainName() {
 88            return "TestIdentityLoginDomain2";
 89        }
 90
 91        @Override
 92        public void setup(final ManagementClient managementClient, final String containerId) throws Exception {
 93
 94            log.debug("adding module options");
 95            Map<String,String> moduleOptionsMap = new HashMap<String,String>();
 96            moduleOptionsMap.put("roles", "role1,role2");
 97            moduleOptionsMap.put("principal", "SomeName");
 98
 99            log.info("creating security domain: TestIdentityLoginDomain");
100            createSecurityDomain(IdentityLoginModule.class, moduleOptionsMap, managementClient.getControllerClient());
101            log.info("security domain created");
102
103        }
104    }
105
106
107   /**
108    * Test deployment with
109    *  <module-option name="roles" value="role1,role2"/>
110    */
111   @Deployment(name = DEP1, order = 1)
112   public static WebArchive appDeployment1() {
113      log.info("start" + DEP1 + "deployment");
114
115      WebArchive war = ShrinkWrap.create(WebArchive.class, DEP1 + ".war");
116      war.addClass(PrincipalPrintingServlet.class);
117      war.setWebXML(Utils.getResource("loginmodules/deployments/IdentityLoginModule/web.xml"));
118      war.addAsWebInfResource(Utils.getResource("loginmodules/deployments/IdentityLoginModule/dep1/jboss-web.xml"),"jboss-web.xml");
119      log.debug(war.toString(true));
120      return war;
121   }
122
123   private static final String DEP2 = "IdentityLoginModule-customPrincipal";
124
125   /**
126    * Test deployment with
127    *  <module-option name="prinipal" value="SomeName"/>
128    *  <module-option name="roles" value="role1,role2"/>
129    */
130   @Deployment(name = DEP2, order = 2)
131   public static WebArchive appDeployment2() {
132      log.info("start" + DEP2 + "deployment");
133
134      WebArchive war = ShrinkWrap.create(WebArchive.class, DEP2 + ".war");
135      war.addClass(PrincipalPrintingServlet.class);
136      war.setWebXML(Utils.getResource("loginmodules/deployments/IdentityLoginModule/web.xml"));
137      war.addAsWebInfResource(Utils.getResource("loginmodules/deployments/IdentityLoginModule/dep2/jboss-web.xml"), "jboss-web.xml");
138      log.debug(war.toString(true));
139
140      return war;
141   }
142
143   @OperateOnDeployment(DEP1)
144   @ArquillianResource
145   URL URL1;
146
147   /**
148    * Tests assignment of default principal name to the caller
149    */
150   @OperateOnDeployment(DEP1)
151   @Test
152   public void testDefaultPrincipal(){
153
154      DefaultHttpClient httpclient = new DefaultHttpClient();
155      HttpResponse response;
156      HttpGet httpget = new HttpGet(URL1.toString());
157      httpget.addHeader("Authorization", "Basic Yzpj");  //I'm not sure why this have to be here, however it does not work without it
158      String text;
159
160      try {
161         response = httpclient.execute(httpget);
162         text = Utils.getContent(response);
163      } catch (IOException e) {
164         throw new RuntimeException("Servlet response IO exception", e);
165      }
166
167      assertTrue("default principal ('guest') not assigned to the request by IdentityLoinModule: returned text = " +
168         text, text.contains("guest"));
169   }
170
171   @OperateOnDeployment(DEP2)
172   @ArquillianResource
173   URL URL2;
174
175   /**
176    * Tests assignment of custom principal name to the caller
177    */
178   @OperateOnDeployment(DEP2)
179   @Test
180   public void testCustomPrincipal(){
181
182      DefaultHttpClient httpclient = new DefaultHttpClient();
183      HttpResponse response;
184      //HttpGet httpget = new HttpGet("http://localhost:8080/" + DEP2 + "/");
185      HttpGet httpget = new HttpGet(URL2.toString());
186      httpget.addHeader("Authorization", "Basic Yzpj");//I'm not sure why this have to be here, however it does not work without it
187      String text;
188
189      try {
190         response = httpclient.execute(httpget);
191         text = Utils.getContent(response);
192      } catch (IOException e) {
193         throw new RuntimeException("Servlet response IO exception", e);
194      }
195
196      assertTrue("default principal ('guest') not assigned to the request by IdentityLoinModule: returned text = " +
197         text, text.contains("SomeName"));
198   }
199}