Permalink
Browse files

Merge pull request #2 from luiz/master

Changing authorization in Calopsita in order to use VRaptor's Result instead of plain HttpServletRequest/Response.
  • Loading branch information...
2 parents 1b70f37 + 2333b79 commit aedf8a8025aab2e8fe4f5c55efe93b3dbe123e65 @ceci ceci committed Apr 23, 2012
@@ -1,11 +1,7 @@
package br.com.caelum.calopsita.infra.interceptor;
-import java.io.IOException;
import java.util.Arrays;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
import br.com.caelum.calopsita.controller.HomeController;
import br.com.caelum.calopsita.controller.UsersController;
import br.com.caelum.calopsita.infra.vraptor.SessionUser;
@@ -19,14 +15,10 @@
@Intercepts
public class AuthenticationInterceptor implements Interceptor {
private final SessionUser sessionUser;
- private final HttpServletRequest request;
- private final HttpServletResponse response;
private final Result result;
- public AuthenticationInterceptor(SessionUser sessionUser, HttpServletRequest request, HttpServletResponse response, Result result) {
+ public AuthenticationInterceptor(SessionUser sessionUser, Result result) {
this.sessionUser = sessionUser;
- this.request = request;
- this.response = response;
this.result = result;
}
@@ -38,11 +30,7 @@ public boolean accepts(ResourceMethod method) {
public void intercept(InterceptorStack stack, ResourceMethod method,
Object resourceInstance) throws InterceptionException {
if (this.sessionUser.getUser() == null) {
- try {
- response.sendRedirect(request.getContextPath() + "/home/login/");
- } catch (IOException e) {
- throw new InterceptionException(e);
- }
+ result.redirectTo(HomeController.class).login();
} else {
result.include("currentUser", sessionUser.getUser());
stack.next(method, resourceInstance);
@@ -1,18 +1,15 @@
package br.com.caelum.calopsita.infra.interceptor;
-import java.io.IOException;
import java.util.Arrays;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
import br.com.caelum.calopsita.controller.HomeController;
import br.com.caelum.calopsita.controller.UsersController;
import br.com.caelum.calopsita.infra.vraptor.SessionUser;
import br.com.caelum.calopsita.repository.ProjectRepository;
import br.com.caelum.calopsita.repository.UserRepository;
import br.com.caelum.vraptor.InterceptionException;
import br.com.caelum.vraptor.Intercepts;
+import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.core.InterceptorStack;
import br.com.caelum.vraptor.core.MethodInfo;
import br.com.caelum.vraptor.interceptor.Interceptor;
@@ -22,17 +19,15 @@
private final ProjectRepository repository;
private final SessionUser user;
- private final HttpServletResponse response;
- private final HttpServletRequest request;
private final MethodInfo parameters;
private final UserRepository userRepository;
+ private final Result result;
- public AuthorizationInterceptor(SessionUser user, UserRepository userRepository, ProjectRepository repository, HttpServletRequest request, HttpServletResponse response, MethodInfo parameters) {
+ public AuthorizationInterceptor(SessionUser user, UserRepository userRepository, ProjectRepository repository, Result result, MethodInfo parameters) {
this.user = user;
this.userRepository = userRepository;
this.repository = repository;
- this.request = request;
- this.response = response;
+ this.result = result;
this.parameters = parameters;
}
@@ -46,11 +41,7 @@ public void intercept(InterceptorStack stack, ResourceMethod method,
Object resourceInstance) throws InterceptionException {
if (user.getUser() != null && repository.hasInconsistentValues(parameters.getParameters(), user.getUser())) {
- try {
- response.sendRedirect(request.getContextPath() + "/home/notAllowed/");
- } catch (IOException e) {
- throw new InterceptionException(e);
- }
+ result.redirectTo(HomeController.class).notAllowed();
return;
}
if (user.getUser() != null) {
@@ -1,13 +1,9 @@
package br.com.caelum.calopsita.infra;
-import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertFalse;
import java.io.IOException;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.After;
@@ -20,32 +16,29 @@
import br.com.caelum.calopsita.infra.vraptor.SessionUser;
import br.com.caelum.calopsita.mocks.MockHttpSession;
import br.com.caelum.calopsita.model.User;
-import br.com.caelum.vraptor.InterceptionException;
+import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.core.InterceptorStack;
import br.com.caelum.vraptor.resource.DefaultResourceClass;
import br.com.caelum.vraptor.resource.DefaultResourceMethod;
import br.com.caelum.vraptor.resource.ResourceMethod;
-import br.com.caelum.vraptor.util.test.MockResult;
public class AuthenticationInterceptorTest {
private Mockery mockery;
private AuthenticationInterceptor interceptor;
- private HttpServletRequest request;
- private HttpServletResponse response;
private InterceptorStack stack;
private SessionUser sessionUser;
+ private Result result;
@Before
public void setUp() throws Exception {
mockery = new Mockery();
- request = mockery.mock(HttpServletRequest.class);
- response = mockery.mock(HttpServletResponse.class);
stack = mockery.mock(InterceptorStack.class);
sessionUser = new SessionUser(new MockHttpSession());
- interceptor = new AuthenticationInterceptor(sessionUser, request, response, new MockResult());
+ result = mockery.mock(Result.class);
+ interceptor = new AuthenticationInterceptor(sessionUser, result);
}
@After
@@ -58,6 +51,7 @@ public void authorizeIfThereIsAUserInTheSession() throws Exception {
givenThereIsAUserInTheSession();
shouldExecuteFlow();
+ shouldAddLoggedUserIntoRequest();
whenInterceptOccurs();
}
@@ -76,34 +70,15 @@ public void shouldBypassUsersAndHomeController() throws Exception {
assertFalse(interceptor.accepts(anyResourceMethodOf(UsersController.class)));
}
- @Test(expected=InterceptionException.class)
- public void shouldRethrowExceptions() throws Exception {
- givenThereIsNotAUserInTheSession();
- givenResponseThrowsException();
-
- whenInterceptOccurs();
- }
- private void givenResponseThrowsException() throws IOException {
-
- mockery.checking(new Expectations() {
- {
- one(response).sendRedirect(with(any(String.class)));
- will(throwException(new IOException()));
-
- ignoring(anything());
- }
- });
- }
-
private ResourceMethod anyResourceMethodOf(Class<?> clazz) {
return new DefaultResourceMethod(new DefaultResourceClass(clazz), clazz.getDeclaredMethods()[0]);
}
private void shouldRedirectToLoginPage() throws IOException {
mockery.checking(new Expectations() {
{
- one(response).sendRedirect(with(containsString("/")));
- allowing(request);
+ one(result).redirectTo(HomeController.class);
+ will(returnValue(new HomeController()));
}
});
}
@@ -133,6 +108,14 @@ private void shouldNotExecuteFlow() {
}
+ private void shouldAddLoggedUserIntoRequest() {
+ mockery.checking(new Expectations() {
+ {
+ one(result).include("currentUser", sessionUser.getUser());
+ }
+ });
+ }
+
private void whenInterceptOccurs() {
interceptor.intercept(stack, null, null);
}
@@ -1,13 +1,9 @@
package br.com.caelum.calopsita.infra;
-import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertFalse;
import java.io.IOException;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.Before;
@@ -21,7 +17,7 @@
import br.com.caelum.calopsita.model.User;
import br.com.caelum.calopsita.repository.ProjectRepository;
import br.com.caelum.calopsita.repository.UserRepository;
-import br.com.caelum.vraptor.InterceptionException;
+import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.core.InterceptorStack;
import br.com.caelum.vraptor.core.MethodInfo;
import br.com.caelum.vraptor.resource.DefaultResourceClass;
@@ -35,11 +31,10 @@
private ProjectRepository repository;
private User user;
private AuthorizationInterceptor interceptor;
- private HttpServletRequest request;
- private HttpServletResponse response;
private InterceptorStack stack;
private MethodInfo info;
private SessionUser sessionUser;
+ private Result result;
@Before
public void setUp() throws Exception {
@@ -49,11 +44,10 @@ public void setUp() throws Exception {
sessionUser = new SessionUser(new MockHttpSession());
sessionUser.setUser(user);
- request = mockery.mock(HttpServletRequest.class);
- response = mockery.mock(HttpServletResponse.class);
stack = mockery.mock(InterceptorStack.class);
+ result = mockery.mock(Result.class);
info = mockery.mock(MethodInfo.class);
- interceptor = new AuthorizationInterceptor(sessionUser, mockery.mock(UserRepository.class), repository, request, response, info);
+ interceptor = new AuthorizationInterceptor(sessionUser, mockery.mock(UserRepository.class), repository, result, info);
mockery.checking(new Expectations() {
{
allowing(info).getParameters();
@@ -90,25 +84,6 @@ public void shouldBypassUsersAndHomeController() throws Exception {
assertFalse(interceptor.accepts(anyResourceMethodOf(UsersController.class)));
}
- @Test(expected=InterceptionException.class)
- public void shouldRethrowExceptions() throws Exception {
- givenThereIsSomeInconsistency();
- givenResponseThrowsException();
-
- whenInterceptOccurs();
- }
- private void givenResponseThrowsException() throws IOException {
-
- mockery.checking(new Expectations() {
- {
- one(response).sendRedirect(with(any(String.class)));
- will(throwException(new IOException()));
-
- ignoring(anything());
- }
- });
- }
-
private ResourceMethod anyResourceMethodOf(Class<?> clazz) {
return new DefaultResourceMethod(new DefaultResourceClass(clazz), clazz.getDeclaredMethods()[0]);
}
@@ -136,8 +111,8 @@ private void shouldRedirectToNotAllowedPage() throws IOException {
mockery.checking(new Expectations() {
{
- one(response).sendRedirect(with(containsString("/home/notAllowed/")));
- allowing(request);
+ one(result).redirectTo(HomeController.class);
+ will(returnValue(new HomeController()));
}
});
}

0 comments on commit aedf8a8

Please sign in to comment.