Example usage for org.springframework.mock.web MockHttpSession getAttribute

List of usage examples for org.springframework.mock.web MockHttpSession getAttribute

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpSession getAttribute.

Prototype

@Override
    public Object getAttribute(String name) 

Source Link

Usage

From source file:org.jasig.cas.support.oauth.web.OAuth20AuthorizeCallbackControllerTests.java

@Test
public void verifyNoPromptWithExistingToken() throws Exception {
    final Principal principal = mock(Principal.class);
    when(principal.getId()).thenReturn(PRINCIPAL_ID);

    final Authentication authentication = mock(Authentication.class);
    when(authentication.getPrincipal()).thenReturn(principal);

    final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class);
    when(ticketGrantingTicket.isExpired()).thenReturn(false);
    when(ticketGrantingTicket.getAuthentication()).thenReturn(authentication);

    final TicketRegistry ticketRegistry = mock(TicketRegistry.class);
    when(ticketRegistry.getTicket(TICKET_GRANTING_TICKET_ID)).thenReturn(ticketGrantingTicket);

    final Map<String, Scope> scopeMap = new HashMap<>();
    scopeMap.put("scope1", new Scope("scope1", "description2"));
    scopeMap.put("scope2", new Scope("scope2", "description2"));

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getScopes(anySetOf(String.class))).thenReturn(scopeMap);
    when(centralOAuthService.isAccessToken(TokenType.ONLINE, CLIENT_ID, PRINCIPAL_ID, scopeMap.keySet()))
            .thenReturn(true);//ww w. ja va2s.c o m
    when(centralOAuthService.isRefreshToken(CLIENT_ID, PRINCIPAL_ID, scopeMap.keySet())).thenReturn(false);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_URL);
    final MockHttpSession mockSession = new MockHttpSession();
    mockSession.putValue(OAuthConstants.OAUTH20_LOGIN_TICKET_ID, TICKET_GRANTING_TICKET_ID);
    mockSession.putValue(OAuthConstants.OAUTH20_SCOPE, SCOPE);
    mockSession.putValue(OAuthConstants.OAUTH20_SERVICE_NAME, SERVICE_NAME);
    mockSession.putValue(OAuthConstants.OAUTH20_CLIENT_ID, CLIENT_ID);
    mockSession.putValue(OAuthConstants.OAUTH20_TOKEN_TYPE, TokenType.ONLINE);
    mockRequest.setSession(mockSession);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.setTicketRegistry(ticketRegistry);
    oauth20WrapperController.setCentralOAuthService(centralOAuthService);
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertTrue(modelAndView.getView() instanceof RedirectView);
    final RedirectView redirectView = (RedirectView) modelAndView.getView();
    assertTrue(redirectView.getUrl()
            .endsWith(CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_ACTION_URL + "?action=allow"));

    assertEquals(scopeMap.keySet(), mockSession.getAttribute(OAuthConstants.OAUTH20_SCOPE_SET));
}

From source file:org.jasig.cas.support.oauth.web.OAuth20AuthorizeCallbackControllerTests.java

@Test
public void verifyOK() throws Exception {
    final Principal principal = mock(Principal.class);
    when(principal.getId()).thenReturn(PRINCIPAL_ID);

    final Authentication authentication = mock(Authentication.class);
    when(authentication.getPrincipal()).thenReturn(principal);

    final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class);
    when(ticketGrantingTicket.isExpired()).thenReturn(false);
    when(ticketGrantingTicket.getAuthentication()).thenReturn(authentication);

    final TicketRegistry ticketRegistry = mock(TicketRegistry.class);
    when(ticketRegistry.getTicket(TICKET_GRANTING_TICKET_ID)).thenReturn(ticketGrantingTicket);

    final Map<String, Scope> scopeMap = new HashMap<>();
    scopeMap.put("scope1", new Scope("scope1", "description2"));
    scopeMap.put("scope2", new Scope("scope2", "description2"));

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getScopes(anySetOf(String.class))).thenReturn(scopeMap);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_URL);
    final MockHttpSession mockSession = new MockHttpSession();
    mockSession.putValue(OAuthConstants.OAUTH20_LOGIN_TICKET_ID, TICKET_GRANTING_TICKET_ID);
    mockSession.putValue(OAuthConstants.OAUTH20_SCOPE, SCOPE);
    mockSession.putValue(OAuthConstants.OAUTH20_SERVICE_NAME, SERVICE_NAME);
    mockSession.putValue(OAuthConstants.OAUTH20_TOKEN_TYPE, TokenType.OFFLINE);
    mockSession.putValue(OAuthConstants.OAUTH20_APPROVAL_PROMPT, OAuthConstants.APPROVAL_PROMPT_FORCE);
    mockRequest.setSession(mockSession);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.setTicketRegistry(ticketRegistry);
    oauth20WrapperController.setCentralOAuthService(centralOAuthService);
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertEquals(OAuthConstants.CONFIRM_VIEW, modelAndView.getViewName());

    final Map<String, Object> map = modelAndView.getModel();
    assertEquals(SERVICE_NAME, map.get("serviceName"));
    assertEquals(scopeMap.hashCode(), map.get("scopeMap").hashCode());

    assertEquals(scopeMap.keySet(), mockSession.getAttribute(OAuthConstants.OAUTH20_SCOPE_SET));
}

From source file:org.openmrs.module.radiology.web.controller.RadiologyOrderFormControllerTest.java

/**
 * @see RadiologyOrderFormController#postDiscontinueRadiologyOrder(HttpServletRequest,
 *      HttpServletResponse, Order, String, Date)
 *//*from   ww  w . ja  v a  2s  . c  o m*/
@Test
@Verifies(value = "should not redirect if discontinuation failed through date in the future", method = "postDiscontinueRadiologyOrder(HttpServletRequest, HttpServletResponse, Order, String, Date)")
public void postDiscontinueRadiologyOrder_shouldNotRedirectIfDiscontinuationFailedThroughDateInTheFuture()
        throws Exception {
    //given
    RadiologyOrder mockRadiologyOrderToDiscontinue = RadiologyTestData.getMockRadiologyOrder1();
    mockRadiologyOrderToDiscontinue.getStudy().setMwlStatus(MwlStatus.DISCONTINUE_OK);
    String discontinueReason = "Wrong Procedure";
    Date discontinueDate = new Date();
    APIException apiException = new APIException("Discontinue date cannot be in the future");

    Order mockDiscontinuationOrder = new Order();
    mockDiscontinuationOrder.setOrderId(2);
    mockDiscontinuationOrder.setAction(Order.Action.DISCONTINUE);
    mockDiscontinuationOrder.setOrderer(mockRadiologyOrderToDiscontinue.getOrderer());
    mockDiscontinuationOrder.setOrderReasonNonCoded(discontinueReason);
    mockDiscontinuationOrder.setDateActivated(discontinueDate);
    mockDiscontinuationOrder.setPreviousOrder(mockRadiologyOrderToDiscontinue);

    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.addParameter("discontinueOrder", "discontinueOrder");
    MockHttpSession mockSession = new MockHttpSession();
    mockRequest.setSession(mockSession);

    when(radiologyService.getRadiologyOrderByOrderId(mockRadiologyOrderToDiscontinue.getOrderId()))
            .thenReturn(mockRadiologyOrderToDiscontinue);
    when(radiologyService.discontinueRadiologyOrder(mockRadiologyOrderToDiscontinue,
            mockDiscontinuationOrder.getOrderer(), mockDiscontinuationOrder.getDateActivated(),
            mockDiscontinuationOrder.getOrderReasonNonCoded())).thenThrow(apiException);

    assertThat(mockRadiologyOrderToDiscontinue.getAction(), is(Order.Action.NEW));
    ModelAndView modelAndView = radiologyOrderFormController.postDiscontinueRadiologyOrder(mockRequest, null,
            mockRadiologyOrderToDiscontinue, mockDiscontinuationOrder);

    assertNotNull(modelAndView);
    assertThat(modelAndView.getViewName(), is("module/radiology/radiologyOrderForm"));

    assertThat(modelAndView.getModelMap(), hasKey("order"));
    Order order = (Order) modelAndView.getModelMap().get("order");
    assertThat(order, is((Order) mockRadiologyOrderToDiscontinue));

    assertThat(modelAndView.getModelMap(), hasKey("radiologyOrder"));
    RadiologyOrder radiologyOrder = (RadiologyOrder) modelAndView.getModelMap().get("radiologyOrder");
    assertThat(radiologyOrder, is(mockRadiologyOrderToDiscontinue));

    assertNotNull(mockSession.getAttribute(WebConstants.OPENMRS_ERROR_ATTR));
    assertThat((String) mockSession.getAttribute(WebConstants.OPENMRS_ERROR_ATTR),
            is("Discontinue date cannot be in the future"));
}

From source file:org.jasig.cas.support.oauth.web.OAuth20AuthorizeCallbackControllerTests.java

@Test
public void verifyAutoPromptWithoutExistingToken() throws Exception {
    final Principal principal = mock(Principal.class);
    when(principal.getId()).thenReturn(PRINCIPAL_ID);

    final Authentication authentication = mock(Authentication.class);
    when(authentication.getPrincipal()).thenReturn(principal);

    final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class);
    when(ticketGrantingTicket.isExpired()).thenReturn(false);
    when(ticketGrantingTicket.getAuthentication()).thenReturn(authentication);

    final TicketRegistry ticketRegistry = mock(TicketRegistry.class);
    when(ticketRegistry.getTicket(TICKET_GRANTING_TICKET_ID)).thenReturn(ticketGrantingTicket);

    final Map<String, Scope> scopeMap = new HashMap<>();
    scopeMap.put("scope1", new Scope("scope1", "description2"));
    scopeMap.put("scope2", new Scope("scope2", "description2"));

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getScopes(anySetOf(String.class))).thenReturn(scopeMap);
    when(centralOAuthService.isAccessToken(TokenType.ONLINE, CLIENT_ID, PRINCIPAL_ID, scopeMap.keySet()))
            .thenReturn(true);/*from  www.jav  a2 s.com*/
    when(centralOAuthService.isRefreshToken(CLIENT_ID, PRINCIPAL_ID, scopeMap.keySet())).thenReturn(false);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_URL);
    final MockHttpSession mockSession = new MockHttpSession();
    mockSession.putValue(OAuthConstants.OAUTH20_LOGIN_TICKET_ID, TICKET_GRANTING_TICKET_ID);
    mockSession.putValue(OAuthConstants.OAUTH20_SCOPE, SCOPE);
    mockSession.putValue(OAuthConstants.OAUTH20_SERVICE_NAME, SERVICE_NAME);
    mockSession.putValue(OAuthConstants.OAUTH20_CLIENT_ID, CLIENT_ID);
    mockSession.putValue(OAuthConstants.OAUTH20_APPROVAL_PROMPT, "auto");
    mockSession.putValue(OAuthConstants.OAUTH20_TOKEN_TYPE, TokenType.OFFLINE);
    mockRequest.setSession(mockSession);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.setTicketRegistry(ticketRegistry);
    oauth20WrapperController.setCentralOAuthService(centralOAuthService);
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertEquals(OAuthConstants.CONFIRM_VIEW, modelAndView.getViewName());

    final Map<String, Object> map = modelAndView.getModel();
    assertEquals(SERVICE_NAME, map.get("serviceName"));
    assertEquals(scopeMap.hashCode(), map.get("scopeMap").hashCode());

    assertEquals(scopeMap.keySet(), mockSession.getAttribute(OAuthConstants.OAUTH20_SCOPE_SET));
}

From source file:org.jasig.cas.support.oauth.web.OAuth20AuthorizeCallbackControllerTests.java

@Test
public void verifyAutoPromptWithExistingToken() throws Exception {
    final Principal principal = mock(Principal.class);
    when(principal.getId()).thenReturn(PRINCIPAL_ID);

    final Authentication authentication = mock(Authentication.class);
    when(authentication.getPrincipal()).thenReturn(principal);

    final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class);
    when(ticketGrantingTicket.isExpired()).thenReturn(false);
    when(ticketGrantingTicket.getAuthentication()).thenReturn(authentication);

    final TicketRegistry ticketRegistry = mock(TicketRegistry.class);
    when(ticketRegistry.getTicket(TICKET_GRANTING_TICKET_ID)).thenReturn(ticketGrantingTicket);

    final Map<String, Scope> scopeMap = new HashMap<>();
    scopeMap.put("scope1", new Scope("scope1", "description2"));
    scopeMap.put("scope2", new Scope("scope2", "description2"));

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getScopes(anySetOf(String.class))).thenReturn(scopeMap);
    when(centralOAuthService.isAccessToken(TokenType.OFFLINE, CLIENT_ID, PRINCIPAL_ID, scopeMap.keySet()))
            .thenReturn(false);//from   w ww  .ja va 2s .  c  o m
    when(centralOAuthService.isRefreshToken(CLIENT_ID, PRINCIPAL_ID, scopeMap.keySet())).thenReturn(true);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_URL);
    final MockHttpSession mockSession = new MockHttpSession();
    mockSession.putValue(OAuthConstants.OAUTH20_LOGIN_TICKET_ID, TICKET_GRANTING_TICKET_ID);
    mockSession.putValue(OAuthConstants.OAUTH20_SCOPE, SCOPE);
    mockSession.putValue(OAuthConstants.OAUTH20_SERVICE_NAME, SERVICE_NAME);
    mockSession.putValue(OAuthConstants.OAUTH20_CLIENT_ID, CLIENT_ID);
    mockSession.putValue(OAuthConstants.OAUTH20_APPROVAL_PROMPT, "auto");
    mockSession.putValue(OAuthConstants.OAUTH20_TOKEN_TYPE, TokenType.OFFLINE);
    mockRequest.setSession(mockSession);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.setTicketRegistry(ticketRegistry);
    oauth20WrapperController.setCentralOAuthService(centralOAuthService);
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertTrue(modelAndView.getView() instanceof RedirectView);
    final RedirectView redirectView = (RedirectView) modelAndView.getView();
    assertTrue(redirectView.getUrl()
            .endsWith(CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_ACTION_URL + "?action=allow"));

    assertEquals(scopeMap.keySet(), mockSession.getAttribute(OAuthConstants.OAUTH20_SCOPE_SET));
}

From source file:org.jasig.cas.support.oauth.web.OAuth20AuthorizeCallbackActionControllerTests.java

@Test
public void verifyResponseIsTokenWithoutState() throws Exception {
    final AuthorizationCode authorizationCode = mock(AuthorizationCode.class);

    final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class);
    when(ticketGrantingTicket.getCreationTime()).thenReturn(new Date().getTime());

    final AccessToken accessToken = mock(AccessToken.class);
    when(accessToken.getId()).thenReturn(AT_ID);
    when(accessToken.getTicket()).thenReturn(ticketGrantingTicket);

    final Set<String> scopes = new HashSet<>();
    scopes.add(NAME1);//from   w w w .jav  a  2  s.  co  m
    scopes.add(NAME2);

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.grantAuthorizationCode(TokenType.ONLINE, CLIENT_ID, TICKET_GRANTING_TICKET_ID,
            REDIRECT_URI, scopes)).thenReturn(authorizationCode);
    when(centralOAuthService.grantOnlineAccessToken(authorizationCode)).thenReturn(accessToken);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_ACTION_URL);
    final MockHttpSession mockSession = new MockHttpSession();
    mockSession.putValue(OAuthConstants.OAUTH20_RESPONSE_TYPE, RESPONSE_TYPE);
    mockSession.putValue(OAuthConstants.OAUTH20_CLIENT_ID, CLIENT_ID);
    mockSession.putValue(OAuthConstants.OAUTH20_REDIRECT_URI, REDIRECT_URI);
    mockSession.putValue(OAuthConstants.OAUTH20_LOGIN_TICKET_ID, TICKET_GRANTING_TICKET_ID);
    mockSession.putValue(OAuthConstants.OAUTH20_SCOPE_SET, scopes);
    mockRequest.setSession(mockSession);
    mockRequest.setParameter(OAuthConstants.OAUTH20_APPROVAL_PROMPT_ACTION,
            OAuthConstants.OAUTH20_APPROVAL_PROMPT_ACTION_ALLOW);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.setCentralOAuthService(centralOAuthService);
    oauth20WrapperController.setTimeout(TIMEOUT);
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertTrue(modelAndView.getView() instanceof RedirectView);
    final RedirectView redirectView = (RedirectView) modelAndView.getView();
    assertEquals(redirectView.getUrl(),
            REDIRECT_URI + "#" + OAuthConstants.ACCESS_TOKEN + "=" + accessToken.getId() + "&"
                    + OAuthConstants.EXPIRES_IN + '=' + TIMEOUT + "&" + OAuthConstants.TOKEN_TYPE + '='
                    + OAuthConstants.BEARER_TOKEN);

    assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_RESPONSE_TYPE));
    assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_CLIENT_ID));
    assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_STATE));
    assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_REDIRECT_URI));
    assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_TOKEN_TYPE));
    assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_LOGIN_TICKET_ID));
    assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_SCOPE_SET));
}

From source file:org.jasig.cas.support.oauth.web.OAuth20AuthorizeCallbackControllerTests.java

@Test
public void verifyOKWhenBypassApprovalFalse() throws Exception {
    final Principal principal = mock(Principal.class);
    when(principal.getId()).thenReturn(PRINCIPAL_ID);

    final Authentication authentication = mock(Authentication.class);
    when(authentication.getPrincipal()).thenReturn(principal);

    final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class);
    when(ticketGrantingTicket.isExpired()).thenReturn(false);
    when(ticketGrantingTicket.getAuthentication()).thenReturn(authentication);

    final TicketRegistry ticketRegistry = mock(TicketRegistry.class);
    when(ticketRegistry.getTicket(TICKET_GRANTING_TICKET_ID)).thenReturn(ticketGrantingTicket);

    final Map<String, Scope> scopeMap = new HashMap<>();
    scopeMap.put("scope1", new Scope("scope1", "description2"));
    scopeMap.put("scope2", new Scope("scope2", "description2"));

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getScopes(anySetOf(String.class))).thenReturn(scopeMap);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_URL);
    final MockHttpSession mockSession = new MockHttpSession();
    mockSession.putValue(OAuthConstants.OAUTH20_LOGIN_TICKET_ID, TICKET_GRANTING_TICKET_ID);
    mockSession.putValue(OAuthConstants.OAUTH20_SCOPE, SCOPE);
    mockSession.putValue(OAuthConstants.OAUTH20_SERVICE_NAME, SERVICE_NAME);
    mockSession.putValue(OAuthConstants.OAUTH20_TOKEN_TYPE, TokenType.OFFLINE);
    mockSession.putValue(OAuthConstants.OAUTH20_APPROVAL_PROMPT, OAuthConstants.APPROVAL_PROMPT_FORCE);
    mockSession.putValue(OAuthConstants.BYPASS_APPROVAL_PROMPT, false);
    mockRequest.setSession(mockSession);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.setTicketRegistry(ticketRegistry);
    oauth20WrapperController.setCentralOAuthService(centralOAuthService);
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertEquals(OAuthConstants.CONFIRM_VIEW, modelAndView.getViewName());

    final Map<String, Object> map = modelAndView.getModel();
    assertEquals(SERVICE_NAME, map.get("serviceName"));
    assertEquals(scopeMap.hashCode(), map.get("scopeMap").hashCode());

    assertEquals(scopeMap.keySet(), mockSession.getAttribute(OAuthConstants.OAUTH20_SCOPE_SET));
}

From source file:org.jasig.cas.support.oauth.web.OAuth20AuthorizeCallbackActionControllerTests.java

@Test
public void verifyResponseIsTokenWithState() throws Exception {
    final AuthorizationCode authorizationCode = mock(AuthorizationCode.class);

    final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class);
    when(ticketGrantingTicket.getCreationTime()).thenReturn(new Date().getTime());

    final AccessToken accessToken = mock(AccessToken.class);
    when(accessToken.getId()).thenReturn(AT_ID);
    when(accessToken.getTicket()).thenReturn(ticketGrantingTicket);

    final Set<String> scopes = new HashSet<>();
    scopes.add(NAME1);/*from w  ww .j  a  v  a  2  s .c o m*/
    scopes.add(NAME2);

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.grantAuthorizationCode(TokenType.ONLINE, CLIENT_ID, TICKET_GRANTING_TICKET_ID,
            REDIRECT_URI, scopes)).thenReturn(authorizationCode);
    when(centralOAuthService.grantOnlineAccessToken(authorizationCode)).thenReturn(accessToken);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_ACTION_URL);
    final MockHttpSession mockSession = new MockHttpSession();
    mockSession.putValue(OAuthConstants.OAUTH20_RESPONSE_TYPE, RESPONSE_TYPE);
    mockSession.putValue(OAuthConstants.OAUTH20_CLIENT_ID, CLIENT_ID);
    mockSession.putValue(OAuthConstants.OAUTH20_STATE, STATE);
    mockSession.putValue(OAuthConstants.OAUTH20_REDIRECT_URI, REDIRECT_URI);
    mockSession.putValue(OAuthConstants.OAUTH20_LOGIN_TICKET_ID, TICKET_GRANTING_TICKET_ID);
    mockSession.putValue(OAuthConstants.OAUTH20_SCOPE_SET, scopes);
    mockRequest.setSession(mockSession);
    mockRequest.setParameter(OAuthConstants.OAUTH20_APPROVAL_PROMPT_ACTION,
            OAuthConstants.OAUTH20_APPROVAL_PROMPT_ACTION_ALLOW);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.setCentralOAuthService(centralOAuthService);
    oauth20WrapperController.setTimeout(TIMEOUT);
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertTrue(modelAndView.getView() instanceof RedirectView);
    final RedirectView redirectView = (RedirectView) modelAndView.getView();
    assertEquals(redirectView.getUrl(),
            REDIRECT_URI + "#" + OAuthConstants.ACCESS_TOKEN + "=" + accessToken.getId() + "&"
                    + OAuthConstants.EXPIRES_IN + '=' + TIMEOUT + "&" + OAuthConstants.TOKEN_TYPE + '='
                    + OAuthConstants.BEARER_TOKEN + "&" + OAuthConstants.STATE + '=' + STATE);

    assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_RESPONSE_TYPE));
    assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_CLIENT_ID));
    assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_STATE));
    assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_REDIRECT_URI));
    assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_TOKEN_TYPE));
    assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_LOGIN_TICKET_ID));
    assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_SCOPE_SET));
}

From source file:org.openmrs.module.radiology.order.web.RadiologyOrderFormControllerTest.java

/**
 * @see RadiologyOrderFormController#discontinueRadiologyOrder(HttpServletRequest,RadiologyOrder,DiscontinuationOrderRequest,BindingResult)
 * @verifies not redirect and set session attribute with openmrs error if api exception is thrown by discontinue
 *           radiology order//from w  ww.j  a  va2 s.c  o  m
 */
@Test
public void discontinueRadiologyOrder_shouldNotRedirectAndSetSessionAttributeWithOpenmrsErrorIfApiExceptionIsThrownByDiscontinueRadiologyOrder()
        throws Exception {

    // given
    RadiologyOrder mockRadiologyOrderToDiscontinue = RadiologyTestData.getMockRadiologyOrder1();

    DiscontinuationOrderRequest discontinuationOrderRequest = new DiscontinuationOrderRequest();
    discontinuationOrderRequest.setOrderer(mockRadiologyOrderToDiscontinue.getOrderer());
    discontinuationOrderRequest.setReasonNonCoded("some");

    Order mockDiscontinuationOrder = new Order();
    mockDiscontinuationOrder.setOrderId(2);
    mockDiscontinuationOrder.setAction(Order.Action.DISCONTINUE);
    mockDiscontinuationOrder.setOrderer(discontinuationOrderRequest.getOrderer());
    mockDiscontinuationOrder.setOrderReasonNonCoded(discontinuationOrderRequest.getReasonNonCoded());
    mockDiscontinuationOrder.setPreviousOrder(mockRadiologyOrderToDiscontinue);

    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.addParameter("discontinueOrder", "discontinueOrder");
    MockHttpSession mockSession = new MockHttpSession();
    mockRequest.setSession(mockSession);

    when(radiologyOrderService.getRadiologyOrder(mockRadiologyOrderToDiscontinue.getOrderId()))
            .thenReturn(mockRadiologyOrderToDiscontinue);
    when(radiologyOrderService.discontinueRadiologyOrder(mockRadiologyOrderToDiscontinue,
            mockDiscontinuationOrder.getOrderer(), mockDiscontinuationOrder.getOrderReasonNonCoded()))
                    .thenThrow(new APIException(
                            "Cannot discontinue an order that is already stopped, expired or voided"));

    BindingResult resultDiscontinueOrderRequest = mock(BindingResult.class);

    assertThat(mockRadiologyOrderToDiscontinue.getAction(), is(Order.Action.NEW));
    ModelAndView modelAndView = radiologyOrderFormController.discontinueRadiologyOrder(mockRequest,
            mockRadiologyOrderToDiscontinue, discontinuationOrderRequest, resultDiscontinueOrderRequest);

    assertNotNull(modelAndView);
    assertThat(modelAndView.getViewName(), is(RadiologyOrderFormController.RADIOLOGY_ORDER_FORM_VIEW));

    assertThat(modelAndView.getModelMap(), hasKey("order"));
    Order order = (Order) modelAndView.getModelMap().get("order");
    assertThat(order, is(mockRadiologyOrderToDiscontinue));

    assertThat(modelAndView.getModelMap(), hasKey("radiologyOrder"));
    RadiologyOrder radiologyOrder = (RadiologyOrder) modelAndView.getModelMap().get("radiologyOrder");
    assertThat(radiologyOrder, is(mockRadiologyOrderToDiscontinue));

    assertThat((String) mockSession.getAttribute(WebConstants.OPENMRS_ERROR_ATTR),
            is("Cannot discontinue an order that is already stopped, expired or voided"));
}

From source file:org.cloudfoundry.identity.uaa.mock.token.TokenMvcMockTests.java

@Test
public void test_authorization_code_grant_redirect_when_session_expires() throws Exception {
    String redirectUri = "https://example.com/dashboard/?appGuid=app-guid&ace_config=test";

    String clientId = "authclient-" + generator.generate();
    String scopes = "openid";
    setUpClients(clientId, scopes, scopes, GRANT_TYPES, true, redirectUri);
    String username = "authuser" + generator.generate();
    String userScopes = "openid";
    ScimUser user = setUpUser(username, userScopes, OriginKeys.UAA, IdentityZoneHolder.get().getId());
    String state = generator.generate();

    String url = UriComponentsBuilder.fromUriString(
            "/oauth/authorize?response_type=code&scope=openid&state={state}&client_id={clientId}&redirect_uri={redirectUri}")
            .buildAndExpand(state, clientId, redirectUri).encode().toUri().toString();

    String encodedRedirectUri = UriUtils.encodeQueryParam(redirectUri, "ISO-8859-1");

    MvcResult result = getMockMvc().perform(get(new URI(url))).andExpect(status().is3xxRedirection())
            .andReturn();//  w  w  w  .  j a  v a 2  s.c om
    String location = result.getResponse().getHeader("Location");
    assertThat(location, endsWith("/login"));

    MockHttpSession session = (MockHttpSession) result.getRequest().getSession(false);
    assertNotNull(session);
    SavedRequest savedRequest = (SavedRequest) session.getAttribute(SAVED_REQUEST_SESSION_ATTRIBUTE);
    assertNotNull(savedRequest);
    assertEquals("http://localhost" + url, savedRequest.getRedirectUrl());

    getMockMvc().perform(get("/login").session(session)).andDo(print()).andExpect(status().isOk())
            .andExpect(content().string(containsString(FORM_REDIRECT_PARAMETER)))
            .andExpect(content().string(containsString(encodedRedirectUri)));

    //a failed login should survive the flow
    //attempt to login without a session
    result = getMockMvc()
            .perform(post("/login.do").with(cookieCsrf()).param("form_redirect_uri", url)
                    .param("username", username).param("password", "invalid"))
            .andExpect(status().isFound()).andExpect(header().string("Location", containsString("/login")))
            .andReturn();

    session = (MockHttpSession) result.getRequest().getSession(false);
    assertNotNull(session);
    savedRequest = (SavedRequest) session.getAttribute(SAVED_REQUEST_SESSION_ATTRIBUTE);
    assertNotNull(savedRequest);

    getMockMvc().perform(get("/login").session(session)).andDo(print()).andExpect(status().isOk())
            .andExpect(content().string(containsString(FORM_REDIRECT_PARAMETER)))
            .andExpect(content().string(containsString(encodedRedirectUri)));

    //attempt to login without a session
    getMockMvc()
            .perform(post("/login.do").with(cookieCsrf()).param("form_redirect_uri", url)
                    .param("username", username).param("password", SECRET))
            .andExpect(status().isFound()).andExpect(header().string("Location", url));
}