You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by gp...@apache.org on 2012/03/29 13:02:59 UTC

[1/2] git commit: DELTASPIKE-138 tests for @SecurityBindingType

Updated Branches:
  refs/heads/master 07b8332cf -> f9a920042


DELTASPIKE-138 tests for @SecurityBindingType


Project: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/commit/f9a92004
Tree: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/tree/f9a92004
Diff: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/diff/f9a92004

Branch: refs/heads/master
Commit: f9a920042e60c8c28ec16036eefaf9a316b7cbfa
Parents: 750f7cd
Author: gpetracek <gp...@apache.org>
Authored: Thu Mar 29 12:53:32 2012 +0200
Committer: gpetracek <gp...@apache.org>
Committed: Thu Mar 29 12:54:36 2012 +0200

----------------------------------------------------------------------
 .../securitybinding/CustomAuthorizer.java          |   37 +++++
 .../securitybinding/CustomSecurityBinding.java     |   40 ++++++
 .../securitybinding/SecuredBean1.java              |   39 ++++++
 .../securitybinding/SecuredBean2.java              |   37 +++++
 .../securitybinding/SecurityBindingTest.java       |  103 +++++++++++++++
 5 files changed, 256 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/f9a92004/deltaspike/modules/security/impl/src/test/java/org/apache/deltaspike/test/security/impl/authorization/securitybinding/CustomAuthorizer.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/security/impl/src/test/java/org/apache/deltaspike/test/security/impl/authorization/securitybinding/CustomAuthorizer.java b/deltaspike/modules/security/impl/src/test/java/org/apache/deltaspike/test/security/impl/authorization/securitybinding/CustomAuthorizer.java
new file mode 100644
index 0000000..778c535
--- /dev/null
+++ b/deltaspike/modules/security/impl/src/test/java/org/apache/deltaspike/test/security/impl/authorization/securitybinding/CustomAuthorizer.java
@@ -0,0 +1,37 @@
+/*
+ * 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.deltaspike.test.security.impl.authorization.securitybinding;
+
+import org.apache.deltaspike.security.api.authorization.annotation.Secures;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.interceptor.InvocationContext;
+
+@ApplicationScoped
+@SuppressWarnings("UnusedDeclaration")
+public class CustomAuthorizer
+{
+    @Secures
+    @CustomSecurityBinding
+    @SuppressWarnings("UnusedDeclaration")
+    public boolean doSecuredCheck(InvocationContext invocationContext) throws Exception
+    {
+        return !invocationContext.getMethod().getName().contains("Blocked");
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/f9a92004/deltaspike/modules/security/impl/src/test/java/org/apache/deltaspike/test/security/impl/authorization/securitybinding/CustomSecurityBinding.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/security/impl/src/test/java/org/apache/deltaspike/test/security/impl/authorization/securitybinding/CustomSecurityBinding.java b/deltaspike/modules/security/impl/src/test/java/org/apache/deltaspike/test/security/impl/authorization/securitybinding/CustomSecurityBinding.java
new file mode 100644
index 0000000..96bbf28
--- /dev/null
+++ b/deltaspike/modules/security/impl/src/test/java/org/apache/deltaspike/test/security/impl/authorization/securitybinding/CustomSecurityBinding.java
@@ -0,0 +1,40 @@
+/*
+ * 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.deltaspike.test.security.impl.authorization.securitybinding;
+
+import org.apache.deltaspike.security.api.authorization.annotation.SecurityBindingType;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+@Retention(value = RUNTIME)
+@Target({TYPE, METHOD})
+
+@Documented
+
+//cdi annotations
+@SecurityBindingType
+public @interface CustomSecurityBinding
+{
+}

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/f9a92004/deltaspike/modules/security/impl/src/test/java/org/apache/deltaspike/test/security/impl/authorization/securitybinding/SecuredBean1.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/security/impl/src/test/java/org/apache/deltaspike/test/security/impl/authorization/securitybinding/SecuredBean1.java b/deltaspike/modules/security/impl/src/test/java/org/apache/deltaspike/test/security/impl/authorization/securitybinding/SecuredBean1.java
new file mode 100644
index 0000000..854ed78
--- /dev/null
+++ b/deltaspike/modules/security/impl/src/test/java/org/apache/deltaspike/test/security/impl/authorization/securitybinding/SecuredBean1.java
@@ -0,0 +1,39 @@
+/*
+ * 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.deltaspike.test.security.impl.authorization.securitybinding;
+
+import org.apache.deltaspike.security.api.authorization.annotation.Secured;
+import org.apache.deltaspike.test.security.impl.authorization.secured.TestAccessDecisionVoter;
+
+import javax.enterprise.context.ApplicationScoped;
+
+@CustomSecurityBinding
+@ApplicationScoped
+public class SecuredBean1
+{
+    public String getBlockedResult()
+    {
+        return "blocked result";
+    }
+
+    public String getResult()
+    {
+        return "result";
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/f9a92004/deltaspike/modules/security/impl/src/test/java/org/apache/deltaspike/test/security/impl/authorization/securitybinding/SecuredBean2.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/security/impl/src/test/java/org/apache/deltaspike/test/security/impl/authorization/securitybinding/SecuredBean2.java b/deltaspike/modules/security/impl/src/test/java/org/apache/deltaspike/test/security/impl/authorization/securitybinding/SecuredBean2.java
new file mode 100644
index 0000000..c714dce
--- /dev/null
+++ b/deltaspike/modules/security/impl/src/test/java/org/apache/deltaspike/test/security/impl/authorization/securitybinding/SecuredBean2.java
@@ -0,0 +1,37 @@
+/*
+ * 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.deltaspike.test.security.impl.authorization.securitybinding;
+
+import javax.enterprise.context.ApplicationScoped;
+
+@ApplicationScoped
+public class SecuredBean2
+{
+    @CustomSecurityBinding
+    public String getBlockedResult()
+    {
+        return "blocked result";
+    }
+
+    @CustomSecurityBinding
+    public String getResult()
+    {
+        return "result";
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/f9a92004/deltaspike/modules/security/impl/src/test/java/org/apache/deltaspike/test/security/impl/authorization/securitybinding/SecurityBindingTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/security/impl/src/test/java/org/apache/deltaspike/test/security/impl/authorization/securitybinding/SecurityBindingTest.java b/deltaspike/modules/security/impl/src/test/java/org/apache/deltaspike/test/security/impl/authorization/securitybinding/SecurityBindingTest.java
new file mode 100644
index 0000000..c25ffd5
--- /dev/null
+++ b/deltaspike/modules/security/impl/src/test/java/org/apache/deltaspike/test/security/impl/authorization/securitybinding/SecurityBindingTest.java
@@ -0,0 +1,103 @@
+/*
+ * 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.deltaspike.test.security.impl.authorization.securitybinding;
+
+import org.apache.deltaspike.core.api.provider.BeanManagerProvider;
+import org.apache.deltaspike.core.api.provider.BeanProvider;
+import org.apache.deltaspike.core.impl.exclude.ExcludeExtension;
+import org.apache.deltaspike.security.api.authorization.AccessDeniedException;
+import org.apache.deltaspike.test.util.ArchiveUtils;
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.EmptyAsset;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.enterprise.inject.spi.Extension;
+
+
+/**
+ * Test for {@link org.apache.deltaspike.security.api.authorization.annotation.Secured}
+ */
+@RunWith(Arquillian.class)
+public class SecurityBindingTest
+{
+    @Deployment
+    public static WebArchive deploy()
+    {
+        new BeanManagerProvider()
+        {
+            @Override
+            public void setTestMode()
+            {
+                super.setTestMode();
+            }
+        }.setTestMode();
+
+        JavaArchive testJar = ShrinkWrap.create(JavaArchive.class, SecurityBindingTest.class.getSimpleName() + ".jar")
+                .addPackage(SecurityBindingTest.class.getPackage().getName())
+                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
+
+        return ShrinkWrap.create(WebArchive.class)
+                .addAsLibraries(ArchiveUtils.getDeltaSpikeCoreAndSecurityArchive())
+                .addAsLibraries(testJar)
+                .addAsServiceProvider(Extension.class, ExcludeExtension.class)
+                .addAsWebInfResource(ArchiveUtils.getBeansXml(), "beans.xml");
+    }
+
+    @Test
+    public void simpleInterceptorTest()
+    {
+        SecuredBean1 testBean = BeanProvider.getContextualReference(SecuredBean1.class, false);
+
+        Assert.assertEquals("result", testBean.getResult());
+
+        try
+        {
+            testBean.getBlockedResult();
+            Assert.fail();
+        }
+        catch (AccessDeniedException e)
+        {
+            //expected exception
+        }
+    }
+
+    @Test
+    public void simpleInterceptorTestOnMethods()
+    {
+        SecuredBean2 testBean = BeanProvider.getContextualReference(SecuredBean2.class, false);
+
+        Assert.assertEquals("result", testBean.getResult());
+
+        try
+        {
+            testBean.getBlockedResult();
+            Assert.fail();
+        }
+        catch (AccessDeniedException e)
+        {
+            //expected exception
+        }
+    }
+}