@Test public void removeConnection() throws Exception { ConnectionFactoryRegistry connectionFactoryLocator = new ConnectionFactoryRegistry(); ConnectionFactory<TestApi2> connectionFactory = new StubOAuth2ConnectionFactory("clientId", "clientSecret", THROW_EXCEPTION); connectionFactoryLocator.addConnectionFactory(connectionFactory); StubConnectionRepository connectionRepository = new StubConnectionRepository(); connectionRepository.addConnection( connectionFactory.createConnection( new ConnectionData( "oauth2Provider", "provider1User1", null, null, null, null, null, null, null))); connectionRepository.addConnection( connectionFactory.createConnection( new ConnectionData( "oauth2Provider", "provider1User2", null, null, null, null, null, null, null))); assertEquals(2, connectionRepository.findConnections("oauth2Provider").size()); ConnectController connectController = new ConnectController(connectionFactoryLocator, connectionRepository); List<DisconnectInterceptor<?>> interceptors = getDisconnectInterceptor(); connectController.setDisconnectInterceptors(interceptors); MockMvc mockMvc = standaloneSetup(connectController).build(); mockMvc .perform(delete("/connect/oauth2Provider/provider1User1")) .andExpect(redirectedUrl("/connect/oauth2Provider")); assertEquals(1, connectionRepository.findConnections("oauth2Provider").size()); assertFalse(((TestConnectInterceptor<?>) (interceptors.get(0))).preDisconnectInvoked); assertFalse(((TestConnectInterceptor<?>) (interceptors.get(0))).postDisconnectInvoked); assertNull(((TestConnectInterceptor<?>) (interceptors.get(0))).connectionFactory); assertTrue(((TestConnectInterceptor<?>) (interceptors.get(1))).preDisconnectInvoked); assertTrue(((TestConnectInterceptor<?>) (interceptors.get(1))).postDisconnectInvoked); assertSame( connectionFactory, ((TestConnectInterceptor<?>) (interceptors.get(1))).connectionFactory); }
@Test public void oauth2Callback() throws Exception { ConnectionFactoryRegistry connectionFactoryLocator = new ConnectionFactoryRegistry(); ConnectionFactory<TestApi2> connectionFactory = new StubOAuth2ConnectionFactory("clientId", "clientSecret"); connectionFactoryLocator.addConnectionFactory(connectionFactory); StubConnectionRepository connectionRepository = new StubConnectionRepository(); ConnectController connectController = new ConnectController(connectionFactoryLocator, connectionRepository); List<ConnectInterceptor<?>> interceptors = getConnectInterceptor(); connectController.setConnectInterceptors(interceptors); connectController.afterPropertiesSet(); MockMvc mockMvc = standaloneSetup(connectController).build(); assertEquals(0, connectionRepository.findConnections("oauth2Provider").size()); mockMvc .perform(get("/connect/oauth2Provider").param("code", "oauth2Code")) .andExpect(redirectedUrl("/connect/oauth2Provider")); List<Connection<?>> connections = connectionRepository.findConnections("oauth2Provider"); assertEquals(1, connections.size()); assertEquals("oauth2Provider", connections.get(0).getKey().getProviderId()); // Check for postConnect() only. The preConnect() is only invoked during the initial portion of // the flow assertFalse(((TestConnectInterceptor<?>) (interceptors.get(0))).postConnectInvoked); TestConnectInterceptor<?> testInterceptor2 = (TestConnectInterceptor<?>) (interceptors.get(1)); assertTrue(testInterceptor2.postConnectInvoked); }
@Test public void connect_OAuth1Provider() throws Exception { ConnectionFactoryRegistry connectionFactoryLocator = new ConnectionFactoryRegistry(); ConnectionFactory<TestApi1> connectionFactory = new StubOAuth1ConnectionFactory("clientId", "clientSecret"); connectionFactoryLocator.addConnectionFactory(connectionFactory); ConnectController connectController = new ConnectController(connectionFactoryLocator, null); List<ConnectInterceptor<?>> interceptors = getConnectInterceptor(); connectController.setConnectInterceptors(interceptors); connectController.afterPropertiesSet(); MockMvc mockMvc = standaloneSetup(connectController).build(); mockMvc .perform(post("/connect/oauth1Provider")) .andExpect( redirectedUrl("https://someprovider.com/oauth/authorize?oauth_token=requestToken")) .andExpect( request() .sessionAttribute( "oauthToken", samePropertyValuesAs(new OAuthToken("requestToken", "requestTokenSecret")))); // Check for preConnect() only. The postConnect() won't be invoked until after callback TestConnectInterceptor<?> textInterceptor1 = (TestConnectInterceptor<?>) (interceptors.get(0)); assertTrue(textInterceptor1.preConnectInvoked); assertEquals("oauth1Provider", textInterceptor1.connectionFactory.getProviderId()); assertFalse(((TestConnectInterceptor<?>) (interceptors.get(1))).preConnectInvoked); }
@Test public void testGetText() throws Exception { String inputString = "User related info!"; FormData data = new FormData(); data.setComment(inputString); mockMvc = MockMvcBuilders.standaloneSetup(new ActionController()).build(); MvcResult result = this.mockMvc .perform(get("/users").contentType(MediaType.APPLICATION_JSON).content(data.toString())) .andExpect(status().isOk()) .andReturn(); String returnString = result.getResponse().getContentAsString(); ObjectMapper objectMapper = new ObjectMapper(); logger.info("Call to /users returned: " + returnString); // convert json string to object FormActionResult returnData = objectMapper.readValue(returnString.getBytes(), FormActionResult.class); String expectedString = returnData.getFormData().getComment(); logger.debug("USERS ACTION RETURNED: " + expectedString); assertTrue( "Expected data: \"" + inputString + "\" actual data=\"" + expectedString + "\" ", inputString.equals(expectedString)); }
@Test public void connect_OAuth2Provider() throws Exception { ConnectionFactoryRegistry connectionFactoryLocator = new ConnectionFactoryRegistry(); ConnectionFactory<TestApi2> connectionFactory = new StubOAuth2ConnectionFactory("clientId", "clientSecret"); connectionFactoryLocator.addConnectionFactory(connectionFactory); ConnectController connectController = new ConnectController(connectionFactoryLocator, null); List<ConnectInterceptor<?>> interceptors = getConnectInterceptor(); connectController.setConnectInterceptors(interceptors); MockMvc mockMvc = standaloneSetup(connectController).build(); mockMvc.perform(post("/connect/oauth2Provider")).andExpect(redirectedUrl(OAUTH2_AUTHORIZE_URL)); // Check for preConnect() only. The postConnect() won't be invoked until after callback assertFalse(((TestConnectInterceptor<?>) (interceptors.get(0))).preConnectInvoked); TestConnectInterceptor<?> testInterceptor2 = (TestConnectInterceptor<?>) (interceptors.get(1)); assertTrue(testInterceptor2.preConnectInvoked); assertEquals("oauth2Provider", testInterceptor2.connectionFactory.getProviderId()); }
@Test public void testSearch() throws Exception { // reindex before searching UserManager userManager = (UserManager) applicationContext.getBean("userManager"); userManager.reindex(); Map<String, Object> model = mockMvc .perform((get("/admin/users")).param("q", "admin")) .andExpect(status().isOk()) .andExpect(model().attributeExists(Constants.USER_LIST)) .andExpect(view().name("admin/userList")) .andReturn() .getModelAndView() .getModel(); List results = (List) model.get(Constants.USER_LIST); assertNotNull(results); assertTrue(results.size() >= 1); }