Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixing broken tests

  • Loading branch information...
commit 0ad563a8de90eef07f211575e326f6884ca04578 1 parent 1c0f099
@abstractj authored
View
7 aerogear-controller-demo/src/main/java/org/jboss/aerogear/controller/demo/Login.java
@@ -1,5 +1,6 @@
package org.jboss.aerogear.controller.demo;
+import org.jboss.aerogear.controller.demo.idm.authentication.AuthenticatorManager;
import org.jboss.aerogear.controller.demo.model.UserLogin;
import javax.ejb.Stateless;
@@ -9,8 +10,7 @@
public class Login {
@Inject
- //TODO replace with DS
- //private AuthenticatorManager authenticatorManager;
+ private AuthenticatorManager authenticatorManager;
public void index() {
System.out.println("hello from security");
@@ -18,8 +18,7 @@ public void index() {
public UserLogin welcome(UserLogin userLogin) {
- //authenticatorManager.login(userLogin);
- //TODO authentication exception should redirect to some error page
+ authenticatorManager.login(userLogin.getUsername(), userLogin.getPassword());
return userLogin;
}
View
2  aerogear-controller-demo/src/main/java/org/jboss/aerogear/controller/demo/Shop.java
@@ -13,7 +13,7 @@
private ShopCartService shopCartService;
//TODO replace with DS
- //@Protected(role = "customer")
+ //@CustomSecurityBinding(role = "customer")
public void index() {
System.out.println("hello from shop");
}
View
3  ...ear/controller/demo/idm/annotation/Protected.java → ...er/demo/idm/annotation/CustomSecurityBinding.java
@@ -11,5 +11,6 @@
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@SecurityBindingType
-public @interface Protected {
+public @interface CustomSecurityBinding {
+
}
View
25 ...oller-demo/src/main/java/org/jboss/aerogear/controller/demo/idm/authentication/AuthenticatorManager.java
@@ -5,7 +5,6 @@
import org.apache.deltaspike.security.api.credential.Credential;
import org.apache.deltaspike.security.api.credential.LoginCredential;
import org.apache.deltaspike.security.spi.authentication.BaseAuthenticator;
-import org.jboss.aerogear.controller.demo.idm.fixture.InMemoryUserStorage;
import org.jboss.aerogear.controller.demo.idm.persistence.UserRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -51,18 +50,20 @@ public User getUser() {
public void login(String userName, final String password) {
+ Object user = userRegistry.findBy(userName);
- this.loginCredential.setUserId(userName);
- //TODO discuss #setSecurityToken
- this.loginCredential.setCredential(new Credential<String>() {
- @Override
- public String getValue() {
- return password;
- }
- });
-
- this.identity.login();
-
+ if (user != null) {
+ this.loginCredential.setUserId(userName);
+ //TODO discuss #setSecurityToken
+ this.loginCredential.setCredential(new Credential<String>() {
+ @Override
+ public String getValue() {
+ return password;
+ }
+ });
+
+ this.identity.login();
+ }
}
public void logout() {
View
17 ...controller-demo/src/main/java/org/jboss/aerogear/controller/demo/idm/authorization/CustomAuthorizer.java
@@ -18,12 +18,14 @@
*/
package org.jboss.aerogear.controller.demo.idm.authorization;
+import org.apache.deltaspike.security.api.Identity;
import org.apache.deltaspike.security.api.authorization.annotation.Secures;
-import org.jboss.aerogear.controller.demo.idm.annotation.Protected;
+import org.jboss.aerogear.controller.demo.idm.annotation.CustomSecurityBinding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Inject;
import javax.interceptor.InvocationContext;
@ApplicationScoped
@@ -32,11 +34,18 @@
private static final Logger log = LoggerFactory.getLogger(CustomAuthorizer.class);
+ @Inject
+ private Identity identity;
+
@Secures
- @Protected
- @SuppressWarnings("UnusedDeclaration")
+ @CustomSecurityBinding
public boolean doSecuredCheck(InvocationContext invocationContext) throws Exception {
log.info("============================== CustomAuthorizer ==============================");
- return !invocationContext.getMethod().getName().contains("Blocked");
+ log.info("============================== CustomAuthorizer " + invocationContext.getMethod().getName() + " ==============================");
+ if (this.identity.isLoggedIn()) {
+ return true;
+ } else {
+ throw new Exception("Authorization check failed");
+ }
}
}
View
41 ...ar-controller-demo/src/main/java/org/jboss/aerogear/controller/demo/idm/fixture/InMemoryUserStorage.java
@@ -1,41 +0,0 @@
-/*
- * 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.jboss.aerogear.controller.demo.idm.fixture;
-
-import org.apache.deltaspike.core.api.exclude.annotation.Exclude;
-
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-
-@Exclude
-public class InMemoryUserStorage {
- private static Map<String, String> simpleUserPasswordMapping = new ConcurrentHashMap<String, String>();
-
- static {
- simpleUserPasswordMapping.put("test", "test");
- }
-
- public static void setPassword(String userName, String password) {
- simpleUserPasswordMapping.put(userName, password);
- }
-
- public static String getPassword(String userName) {
- return simpleUserPasswordMapping.get(userName);
- }
-}
View
2  aerogear-controller-demo/src/main/java/org/jboss/aerogear/controller/demo/model/UserLogin.java
@@ -17,7 +17,7 @@ public UserLogin(String username, String password, String role) {
this.role = role;
}
- public Object getPassword() {
+ public String getPassword() {
return password;
}
View
8 aerogear-controller-demo/src/main/java/org/jboss/aerogear/controller/demo/service/ShopCartService.java
@@ -1,7 +1,6 @@
package org.jboss.aerogear.controller.demo.service;
-import org.apache.deltaspike.security.api.authorization.annotation.Secures;
-import org.jboss.aerogear.controller.demo.idm.annotation.Protected;
+import org.jboss.aerogear.controller.demo.idm.annotation.CustomSecurityBinding;
import org.jboss.aerogear.controller.demo.model.Car;
import javax.enterprise.context.RequestScoped;
@@ -9,10 +8,7 @@
@RequestScoped
public class ShopCartService {
- //TODO replace with DS
- //@Protected(role = "customer")
- //@Protected
- @Protected
+ @CustomSecurityBinding
public Car add(Car car) {
System.out.println("car: " + car.getBrand());
return car;
View
19 aerogear-controller-demo/src/test/java/org/jboss/aerogear/controller/demo/service/ShopCartServiceIT.java
@@ -1,9 +1,9 @@
package org.jboss.aerogear.controller.demo.service;
import org.apache.deltaspike.security.impl.authorization.SecurityInterceptor;
-import org.jboss.aerogear.controller.demo.idm.annotation.Protected;
+import org.jboss.aerogear.controller.demo.idm.annotation.CustomSecurityBinding;
+import org.jboss.aerogear.controller.demo.idm.authentication.AuthenticatorManager;
import org.jboss.aerogear.controller.demo.idm.authorization.CustomAuthorizer;
-import org.jboss.aerogear.controller.demo.idm.fixture.InMemoryUserStorage;
import org.jboss.aerogear.controller.demo.idm.persistence.Role;
import org.jboss.aerogear.controller.demo.idm.persistence.RoleRegistry;
import org.jboss.aerogear.controller.demo.idm.persistence.User;
@@ -11,7 +11,6 @@
import org.jboss.aerogear.controller.demo.model.Car;
import org.jboss.aerogear.controller.demo.util.ArchiveUtils;
import org.jboss.aerogear.controller.demo.util.Resources;
-import org.jboss.aerogear.controller.demo.idm.authentication.AuthenticatorManager;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
@@ -60,8 +59,8 @@ public static WebArchive createDeployment() {
.addAsLibraries(ArchiveUtils.getDeltaSpikeCoreAndSecurityArchive())
.addClasses(ShopCartService.class, Resources.class, CustomAuthorizer.class,
SecurityInterceptor.class, AuthenticatorManager.class,
- Role.class, User.class, Car.class, InMemoryUserStorage.class,
- Protected.class, CustomAuthorizer.class,
+ Role.class, User.class, Car.class,
+ CustomSecurityBinding.class, CustomAuthorizer.class,
RoleRegistry.class, UserRegistry.class)
.addAsWebInfResource(ArchiveUtils.getBeansXml(), "beans.xml")
.addAsResource("persistence.xml", "META-INF/persistence.xml");
@@ -75,9 +74,9 @@ public void shouldAccessProtectedResourceWithValidLogin() throws Exception {
user.setRoles(buildRole("admin"));
userRegistry.newUser(user);
authenticatorManager.login("test", "test");
- shopCartService.add(new Car("red", "hat"));
+ shopCartService.add(new Car("red", "camaro"));
} catch (Exception e) {
- e.printStackTrace();
+ fail("Request failed");
}
}
@@ -88,8 +87,8 @@ public void shouldThrowExceptionWhenRoleInvalid() throws Exception {
user.setRoles(buildRole("manager"));
userRegistry.newUser(user);
//TODO must be replaced
- authenticatorManager.login("test", "test");
- shopCartService.add(new Car("red", "hat"));
+ authenticatorManager.login("john", "doe");
+ shopCartService.add(new Car("chevelle", "ss396"));
fail("Should throw authorization exception");
} catch (Exception e) {
assertTrue(true);
@@ -99,7 +98,7 @@ public void shouldThrowExceptionWhenRoleInvalid() throws Exception {
@Test
public void shouldThrowExceptionWithoutValidLogin() throws Exception {
try {
- shopCartService.add(new Car("red", "hat"));
+ shopCartService.add(new Car("red", "camaro"));
fail("Should throw authorization exception");
} catch (Exception e) {
assertTrue(true);
Please sign in to comment.
Something went wrong with that request. Please try again.