Author: ldywicki Date: Mon Sep 5 11:07:55 2011 New Revision: 1165243 URL: http://svn.apache.org/viewvc?rev=1165243&view=rev Log: Login operation test. Works only from maven
Added: karaf/sandbox/webconsole/trunk/core/src/test/ karaf/sandbox/webconsole/trunk/core/src/test/java/ karaf/sandbox/webconsole/trunk/core/src/test/java/org/ karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/ karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/ karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/ karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/ karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/LoginTest.java karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/TestInjector.java karaf/sandbox/webconsole/trunk/core/src/test/resources/ karaf/sandbox/webconsole/trunk/core/src/test/resources/jaas.conf karaf/sandbox/webconsole/trunk/core/src/test/resources/users.properties Modified: karaf/sandbox/webconsole/trunk/core/pom.xml Modified: karaf/sandbox/webconsole/trunk/core/pom.xml URL: http://svn.apache.org/viewvc/karaf/sandbox/webconsole/trunk/core/pom.xml?rev=1165243&r1=1165242&r2=1165243&view=diff ============================================================================== --- karaf/sandbox/webconsole/trunk/core/pom.xml (original) +++ karaf/sandbox/webconsole/trunk/core/pom.xml Mon Sep 5 11:07:55 2011 @@ -61,6 +61,26 @@ <artifactId>org.apache.karaf.jaas.modules</artifactId> <version>${karaf.version}</version> </dependency> + + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>servlet-api</artifactId> + <version>2.5</version> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.8.2</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + <version>${slf4j.version}</version> + <scope>test</scope> + </dependency> </dependencies> <build> @@ -105,6 +125,19 @@ </instructions> </configuration> </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <version>2.9</version> + <configuration> + <systemProperties> + <property> + <name>java.security.auth.login.config</name> + <value>${basedir}/src/test/resources/jaas.conf</value> + </property> + </systemProperties> + </configuration> + </plugin> </plugins> </build> </project> Added: karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/LoginTest.java URL: http://svn.apache.org/viewvc/karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/LoginTest.java?rev=1165243&view=auto ============================================================================== --- karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/LoginTest.java (added) +++ karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/LoginTest.java Mon Sep 5 11:07:55 2011 @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.karaf.webconsole.core; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.karaf.webconsole.core.brand.DefaultBrandProvider; +import org.apache.karaf.webconsole.core.dashboard.DashboardPage; +import org.apache.karaf.webconsole.core.internal.WebConsoleApplication; +import org.apache.karaf.webconsole.core.page.LoginPage; +import org.apache.wicket.authorization.UnauthorizedInstantiationException; +import org.apache.wicket.protocol.http.WebApplication; +import org.apache.wicket.util.tester.FormTester; +import org.apache.wicket.util.tester.WicketTester; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.BlockJUnit4ClassRunner; + +/** + * Test login operation and role verification. + */ +@RunWith(BlockJUnit4ClassRunner.class) +public class LoginTest { + + /** + * Application instance. + */ + private WebApplication application; + + @Before + public void setUp() { + application = new WebConsoleApplication(); + + final Map<String, Object> values = new HashMap<String, Object>(); + values.put("brandProvider", new DefaultBrandProvider()); + + application.addComponentInstantiationListener(new TestInjector(values)); + } + + @Test + public void testSuccessLogin() throws Exception { + WicketTester tester = new WicketTester(application); + tester.startPage(LoginPage.class); + //tester.debugComponentTrees(); + FormTester form = tester.newFormTester("signIn:signInForm"); + + form.setValue("username", "rootadmin"); + form.setValue("password", "admin"); + form.submit(); + + tester.assertNoErrorMessage(); + tester.assertRenderedPage(DashboardPage.class); + } + + @Test(expected = UnauthorizedInstantiationException.class) + public void testWrongRoleLogin() throws Exception { + WicketTester tester = new WicketTester(application); + tester.startPage(LoginPage.class); + + FormTester form = tester.newFormTester("signIn:signInForm"); + + form.setValue("username", "someuser"); + form.setValue("password", "user"); + form.submit(); + } + +} Added: karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/TestInjector.java URL: http://svn.apache.org/viewvc/karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/TestInjector.java?rev=1165243&view=auto ============================================================================== --- karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/TestInjector.java (added) +++ karaf/sandbox/webconsole/trunk/core/src/test/java/org/apache/karaf/webconsole/core/TestInjector.java Mon Sep 5 11:07:55 2011 @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.karaf.webconsole.core; + +import java.io.Serializable; +import java.lang.reflect.Field; +import java.util.Map; + +import net.sf.cglib.proxy.Enhancer; + +import org.apache.wicket.Component; +import org.apache.wicket.application.IComponentInstantiationListener; +import org.ops4j.pax.wicket.api.PaxWicketBean; +import org.ops4j.pax.wicket.internal.injection.AbstractPaxWicketInjector; +import org.ops4j.pax.wicket.internal.injection.ComponentProxy; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * {@link IComponentInstantiationListener} which injects given values to fields + * annotated with {@link PaxWicketBean}. + */ +public class TestInjector extends AbstractPaxWicketInjector implements IComponentInstantiationListener { + + /** + * Logger. + */ + private Logger logger = LoggerFactory.getLogger(TestInjector.class); + + /** + * Values to inject. + */ + private final Map<String, Object> values; + + public TestInjector(Map<String, Object> values) { + this.values = values; + } + + /** + * {@inheritDoc} + */ + public void onInstantiation(Component component) { + + for (Field field : getFields(component.getClass())) { + // iterate over fields and check if there is values to inject + String name = field.getName(); + Class<?> type = field.getType(); + + if (values.containsKey(name)) { + // we have value.. + Object value = values.get(name); + + // create instance proxy, just like pax-wicket does + if (type.isAssignableFrom(value.getClass())){ + Enhancer enhancer = new Enhancer(); + enhancer.setSuperclass(value.getClass()); + if (!(value instanceof Serializable)) { + logger.warn("Value for field {}.{} is not serializable. Adding Serializable interface for test, result in production may vary", + field.getDeclaringClass(), name); + enhancer.setInterfaces(new Class[] {Serializable.class}); + } + enhancer.setCallback(new ComponentProxy("test", null)); + setField(component, field, enhancer.create()); + } else { + logger.error("Type mismath for field {}.{}. Expected {} but {} given", new Object[] { + field.getDeclaringClass().getName(), name, field.getType().getName(), value.getClass().getName() + }); + System.err.println("Unknown type " + field.getType()); + } + } else { + logger.warn("No value provided for field {}.{}", field.getDeclaringClass().getName(), name); + } + } + } + + /** + * {@inheritDoc} + */ + public void inject(Object toInject, Class<?> toHandle) { + throw new UnsupportedOperationException("inject operation should never be executed"); + } +} \ No newline at end of file Added: karaf/sandbox/webconsole/trunk/core/src/test/resources/jaas.conf URL: http://svn.apache.org/viewvc/karaf/sandbox/webconsole/trunk/core/src/test/resources/jaas.conf?rev=1165243&view=auto ============================================================================== --- karaf/sandbox/webconsole/trunk/core/src/test/resources/jaas.conf (added) +++ karaf/sandbox/webconsole/trunk/core/src/test/resources/jaas.conf Mon Sep 5 11:07:55 2011 @@ -0,0 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +karaf { + org.apache.karaf.jaas.modules.properties.PropertiesLoginModule required + users="src/test/resources/users.properties"; +}; \ No newline at end of file Added: karaf/sandbox/webconsole/trunk/core/src/test/resources/users.properties URL: http://svn.apache.org/viewvc/karaf/sandbox/webconsole/trunk/core/src/test/resources/users.properties?rev=1165243&view=auto ============================================================================== --- karaf/sandbox/webconsole/trunk/core/src/test/resources/users.properties (added) +++ karaf/sandbox/webconsole/trunk/core/src/test/resources/users.properties Mon Sep 5 11:07:55 2011 @@ -0,0 +1,16 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +rootadmin=admin,admin +someuser=user,user \ No newline at end of file