Commit f7a3303a authored by Alexander Basov's avatar Alexander Basov Committed by Matija Obreza
Browse files

TokenControllerTest completed

parent 630eaec7
...@@ -29,6 +29,9 @@ import org.springframework.context.annotation.PropertySource; ...@@ -29,6 +29,9 @@ import org.springframework.context.annotation.PropertySource;
import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
...@@ -232,12 +235,40 @@ public abstract class AbstractRestTest { ...@@ -232,12 +235,40 @@ public abstract class AbstractRestTest {
} }
@Bean @Bean
public HttpClientBuilder httpClientBuilder(){ public HttpClientBuilder httpClientBuilder() {
return HttpClientBuilder.create(); return HttpClientBuilder.create();
} }
@Bean
public TokenController tokenController() {
return new TokenController();
}
@Bean
public ConsumerTokenServices consumerTokenServices() {
DefaultTokenServices tokenServices = new DefaultTokenServices();
tokenServices.setTokenStore(tokenStore());
return tokenServices;
}
@Bean
public TokenStore tokenStore(){
return new OAuth2JPATokenStoreImpl();
}
} }
@Autowired
TokenController tokenController;
@Autowired
ConsumerTokenServices tokenServices;
@Autowired
TokenStore tokenStore;
@Autowired
ConsumerTokenServices consumerTokenServices;
@Autowired @Autowired
EasySMTA easySMTAConnector; EasySMTA easySMTAConnector;
......
package org.genesys2.tests.resttests;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.exception.UserException;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.model.impl.User;
import org.genesys2.server.security.AuthUserDetails;
import org.genesys2.server.servlet.controller.rest.TokenController;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.DefaultAuthorizationRequest;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import static org.hamcrest.Matchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
public class TokenControllerTest extends AbstractRestTest {
private static final Log LOG = LogFactory.getLog(TokenControllerTest.class);
@Autowired
WebApplicationContext webApplicationContext;
MockMvc mockMvc;
private User user;
private OAuth2AccessToken accessToken;
@Before
public void startUp() throws UserException {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
user = new User();
user.setEmail("salexandrbasov@gmail.com");
user.setPassword("Alexandr19011990");
user.setName("SYS_ADMIN");
userService.addUser(user);
HashMap<String, String> authorizationParameters = new HashMap<String, String>();
authorizationParameters.put("scope", "read");
authorizationParameters.put("username", user.getName());
authorizationParameters.put("client_id", user.getName());
authorizationParameters.put("grant", user.getPassword());
DefaultAuthorizationRequest authorizationRequest = new DefaultAuthorizationRequest(authorizationParameters);
authorizationRequest.setApproved(true);
List<GrantedAuthority> authorities = new ArrayList<>();
GrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(UserRole.ADMINISTRATOR.getName());
authorities.add(simpleGrantedAuthority);
authorizationRequest.setAuthorities(authorities);
HashSet<String> resourceIds = new HashSet<String>();
resourceIds.add(user.getName());
authorizationRequest.setResourceIds(resourceIds);
AuthUserDetails authUserDetails = new AuthUserDetails(user.getUuid(), user.getPassword(), authorities);
// set actual DB user
authUserDetails.setUser(user);
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(authUserDetails, authUserDetails, authorities);
OAuth2Authentication authenticationRequest = new OAuth2Authentication(authorizationRequest, authenticationToken);
authenticationRequest.setAuthenticated(true);
accessToken = ((DefaultTokenServices) tokenServices).createAccessToken(authenticationRequest);
SecurityContextHolder.getContext().setAuthentication(authenticationRequest);
}
@After
public void teerDown() {
userPersistence.deleteAll();
}
@Test
public void listTokensForUserTest() throws Exception {
LOG.info("Start test-method listTokensForUserTest");
mockMvc.perform(get("/api/v0/users/{username}/list/tokens", user.getUuid())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$", hasSize(1)))
.andExpect(jsonPath("$[0].access_token", not(isEmptyString())))
.andExpect(jsonPath("$[0].token_type", is(accessToken.getTokenType())))
.andExpect(jsonPath("$[0].expires_in", is(43199)))
.andExpect(jsonPath("$[0].scope", is(accessToken.getScope().toArray()[0])))
.andExpect(jsonPath("$[0].bearer", not(isEmptyOrNullString())))
.andExpect(jsonPath("$[0].client_id", is(user.getName())));
LOG.info("Test listTokensForUserTest passed");
}
@Test
public void revokeUserTokenTest() throws Exception {
LOG.info("Start test-method revokeUserTokenTest");
OAuth2AccessToken oAuth2AccessTokenForTest = (OAuth2AccessToken) tokenController.listTokensForUser(user.getUuid()).toArray()[0];
ObjectMapper objectMapper = new ObjectMapper();
TokenController.SimpleMessage simpleMessage = new TokenController.SimpleMessage("ok", "user token revoked");
mockMvc.perform(delete("/api/v0/users/{username}/tokens/revoke/{token}", user.getUuid(), oAuth2AccessTokenForTest)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().string(objectMapper.writeValueAsString(simpleMessage)));
LOG.info("Test revokeUserTokenTest passed");
}
@Test
public void listTokensForClientTest() throws Exception {
LOG.info("Start test-method listTokensForClientTest");
mockMvc.perform(get("/api/v0/clients/{client}/list/tokens", user.getUuid())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$", hasSize(1)))
.andExpect(jsonPath("$[0].access_token", not(isEmptyString())))
.andExpect(jsonPath("$[0].token_type", is(accessToken.getTokenType())))
.andExpect(jsonPath("$[0].expires_in", is(43199)))
.andExpect(jsonPath("$[0].scope", is(accessToken.getScope().toArray()[0])))
.andExpect(jsonPath("$[0].bearer", not(isEmptyOrNullString())))
.andExpect(jsonPath("$[0].client_id", is(user.getName())));
LOG.info("Test listTokensForClientTest passed");
}
@Test
public void revokeClientTokenTest() throws Exception {
LOG.info("Start test-method revokeClientTokenTest");
OAuth2AccessToken oAuth2AccessTokenForTest = (OAuth2AccessToken) tokenController.listTokensForUser(user.getUuid()).toArray()[0];
ObjectMapper objectMapper = new ObjectMapper();
TokenController.SimpleMessage simpleMessage = new TokenController.SimpleMessage("ok", "client token revoked");
mockMvc.perform(delete("/api/v0/clients/{client}/tokens/revoke/{token}", user.getUuid(), oAuth2AccessTokenForTest)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().string(objectMapper.writeValueAsString(simpleMessage)));
LOG.info("Test revokeClientTokenTest passed");
}
}
...@@ -172,7 +172,7 @@ public class UserControllerTest extends AbstractRestTest { ...@@ -172,7 +172,7 @@ public class UserControllerTest extends AbstractRestTest {
.content(objectMapper.writeValueAsString(teamJson))) .content(objectMapper.writeValueAsString(teamJson)))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("name").value("name")) .andExpect(jsonPath("$.name").value("name"))
.andExpect(content().string(objectMapper.writeValueAsString(teamRepository.findAll().get(0)))); .andExpect(content().string(objectMapper.writeValueAsString(teamRepository.findAll().get(0))));
LOG.info("Test createTeamTest passed"); LOG.info("Test createTeamTest passed");
......
...@@ -41,8 +41,9 @@ lucene.indexDir=./lucene/ ...@@ -41,8 +41,9 @@ lucene.indexDir=./lucene/
download.files.dir=./data/ download.files.dir=./data/
# ITPGRFA Easy-SMTA account (if you have one) # ITPGRFA Easy-SMTA account (if you have one)
itpgrfa.easysmta.username= itpgrfa.easysmta.url=https://mls.planttreaty.org/itt/index.php?r=extsys/userinfo
itpgrfa.easysmta.password= itpgrfa.easysmta.username=bar
itpgrfa.easysmta.password=foo
# GA Account # GA Account
google.analytics.account= google.analytics.account=
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment