Skip to content

Commit

Permalink
Fix DataAccessTokenController Test
Browse files Browse the repository at this point in the history
  • Loading branch information
haynescd committed Jan 17, 2024
1 parent 06425c4 commit 0852d36
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public void revokeAllDataAccessTokens(Authentication authentication) {
@RequestMapping(method = RequestMethod.DELETE, value = "/api/data-access-tokens/{token}")
@Operation(description = "Delete a data access token")
public void revokeDataAccessToken(@Parameter(required = true, description = "token") @PathVariable String token) {
throw new UnsupportedOperationException("this endpoint is does not apply to OAuth2 data access token method.");
tokenService.revokeDataAccessToken(token);
}

private String getAuthenticatedUser(Authentication authentication) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.cbioportal.model.DataAccessToken;
import org.cbioportal.service.DataAccessTokenService;
import org.cbioportal.service.exception.TokenNotFoundException;
import org.cbioportal.web.config.DataAccessTokenControllerConfig;
import org.cbioportal.web.config.DataAccessTokenControllerTestConfig;
import org.cbioportal.web.config.TestConfig;
import org.junit.Assert;
Expand All @@ -41,13 +42,17 @@
import org.springframework.mock.web.MockHttpSession;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@TestPropertySource(properties = {
"download_group=PLACEHOLDER_ROLE",
})
@RunWith(SpringJUnit4ClassRunner.class)
@WebMvcTest
@ContextConfiguration(classes = {DataAccessTokenController.class, TestConfig.class, DataAccessTokenControllerTestConfig.class })
Expand Down Expand Up @@ -170,7 +175,7 @@ public void revokeNonexistentTokenTest() throws Exception {
* Tests for 201 (CREATED) response code
*/
@Test
@WithMockUser
@WithMockUser(username = MOCK_USER, password = MOCK_PASSWORD, authorities = "PLACEHOLDER_ROLE")
public void createTokenValidUserTest() throws Exception {
when(tokenService.createDataAccessToken(ArgumentMatchers.anyString())).thenReturn(MOCK_TOKEN_INFO);
HttpSession session = getSession(MOCK_USER, MOCK_PASSWORD);
Expand All @@ -185,7 +190,6 @@ public void createTokenValidUserTest() throws Exception {
@Test
@WithMockUser(username = MOCK_USER, password = MOCK_PASSWORD, authorities = "PLACEHOLDER_ROLE")
public void createTokenValidUserTestWithUserRole() throws Exception {
ReflectionTestUtils.setField(DataAccessTokenController.class, "userRoleToAccessToken", "PLACEHOLDER_ROLE");
when(tokenService.createDataAccessToken(ArgumentMatchers.anyString())).thenReturn(MOCK_TOKEN_INFO);
HttpSession session = getSession(MOCK_USER, MOCK_PASSWORD);
MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post("/api/data-access-tokens").with(csrf())
Expand All @@ -199,7 +203,6 @@ public void createTokenValidUserTestWithUserRole() throws Exception {
@Test
@WithMockUser
public void createTokenUnauthorizedUserTestWithUserRole() throws Exception {
ReflectionTestUtils.setField(DataAccessTokenController.class, "userRoleToAccessToken", "TEST");
when(tokenService.createDataAccessToken(ArgumentMatchers.anyString())).thenReturn(MOCK_TOKEN_INFO);
HttpSession session = getSession(MOCK_USER, MOCK_PASSWORD);
MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post("/api/data-access-tokens")
Expand All @@ -216,7 +219,7 @@ public void createTokenUnauthorizedUserTestWithUserRole() throws Exception {
* Checks that correct username argument is passed to service class
*/
@Test
@WithMockUser(username = MOCK_USER, password = MOCK_PASSWORD)
@WithMockUser(username = MOCK_USER, password = MOCK_PASSWORD, authorities = "PLACEHOLDER_ROLE")
public void revokeAllTokensForUserTest() throws Exception {
resetReceivedArgument();
Answer<Void> tokenServiceRevokeAllTokensAnswer = new Answer<Void>() {
Expand All @@ -243,7 +246,7 @@ public Void answer(InvocationOnMock revokeAllTokensInvocation) {
* Checks that correct username argument is passed to service class
*/
@Test
@WithMockUser(username = MOCK_USER, password = MOCK_PASSWORD)
@WithMockUser(username = MOCK_USER, password = MOCK_PASSWORD, authorities = "PLACEHOLDER_ROLE")
public void getAllTokensForUserTest() throws Exception {
resetReceivedArgument();
Answer<Void> tokenServiceGetAllTokensAnswer = new Answer<Void>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,4 @@ public DataAccessTokenService tokenService() {
return Mockito.mock(DataAccessTokenService.class);
}

@Bean
public DataAccessTokenController dataAccessTokenController() {
return new DataAccessTokenController(tokenService());
}
}

0 comments on commit 0852d36

Please sign in to comment.