Skip to content
Snippets Groups Projects
Verified Commit 71ffae8f authored by Martin Weise's avatar Martin Weise
Browse files

Updated tests

parent 01bb47e8
Branches
Tags
2 merge requests!163Relase 1.3.0,!155Added readme to authentication service and added eureka service
...@@ -27,42 +27,6 @@ public class ApiExceptionHandler extends ResponseEntityExceptionHandler { ...@@ -27,42 +27,6 @@ public class ApiExceptionHandler extends ResponseEntityExceptionHandler {
return new ResponseEntity<>(response, new HttpHeaders(), response.getStatus()); return new ResponseEntity<>(response, new HttpHeaders(), response.getStatus());
} }
@Hidden
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(ContainerNotFoundException.class)
public ResponseEntity<ApiErrorDto> handle(ContainerNotFoundException e, WebRequest request) {
final ApiErrorDto response = ApiErrorDto.builder()
.status(HttpStatus.NOT_FOUND)
.message(e.getLocalizedMessage())
.code("error.container.notfound")
.build();
return new ResponseEntity<>(response, new HttpHeaders(), response.getStatus());
}
@Hidden
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler(NotAllowedException.class)
public ResponseEntity<ApiErrorDto> handle(NotAllowedException e, WebRequest request) {
final ApiErrorDto response = ApiErrorDto.builder()
.status(HttpStatus.METHOD_NOT_ALLOWED)
.message(e.getLocalizedMessage())
.code("error.container.notallowed")
.build();
return new ResponseEntity<>(response, new HttpHeaders(), response.getStatus());
}
@Hidden
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ImageInvalidException.class)
public ResponseEntity<ApiErrorDto> handle(ImageInvalidException e, WebRequest request) {
final ApiErrorDto response = ApiErrorDto.builder()
.status(HttpStatus.BAD_REQUEST)
.message(e.getLocalizedMessage())
.code("error.image.invalid")
.build();
return new ResponseEntity<>(response, new HttpHeaders(), response.getStatus());
}
@Hidden @Hidden
@ResponseStatus(HttpStatus.GONE) @ResponseStatus(HttpStatus.GONE)
@ExceptionHandler(ContainerAlreadyRemovedException.class) @ExceptionHandler(ContainerAlreadyRemovedException.class)
...@@ -99,6 +63,18 @@ public class ApiExceptionHandler extends ResponseEntityExceptionHandler { ...@@ -99,6 +63,18 @@ public class ApiExceptionHandler extends ResponseEntityExceptionHandler {
return new ResponseEntity<>(response, new HttpHeaders(), response.getStatus()); return new ResponseEntity<>(response, new HttpHeaders(), response.getStatus());
} }
@Hidden
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(ContainerNotFoundException.class)
public ResponseEntity<ApiErrorDto> handle(ContainerNotFoundException e, WebRequest request) {
final ApiErrorDto response = ApiErrorDto.builder()
.status(HttpStatus.NOT_FOUND)
.message(e.getLocalizedMessage())
.code("error.container.notfound")
.build();
return new ResponseEntity<>(response, new HttpHeaders(), response.getStatus());
}
@Hidden @Hidden
@ResponseStatus(HttpStatus.BAD_GATEWAY) @ResponseStatus(HttpStatus.BAD_GATEWAY)
@ExceptionHandler(ContainerNotRunningException.class) @ExceptionHandler(ContainerNotRunningException.class)
...@@ -118,7 +94,7 @@ public class ApiExceptionHandler extends ResponseEntityExceptionHandler { ...@@ -118,7 +94,7 @@ public class ApiExceptionHandler extends ResponseEntityExceptionHandler {
final ApiErrorDto response = ApiErrorDto.builder() final ApiErrorDto response = ApiErrorDto.builder()
.status(HttpStatus.CONFLICT) .status(HttpStatus.CONFLICT)
.message(e.getLocalizedMessage()) .message(e.getLocalizedMessage())
.code("error.container.running") .code("error.container.stillrunning")
.build(); .build();
return new ResponseEntity<>(response, new HttpHeaders(), response.getStatus()); return new ResponseEntity<>(response, new HttpHeaders(), response.getStatus());
} }
...@@ -147,6 +123,18 @@ public class ApiExceptionHandler extends ResponseEntityExceptionHandler { ...@@ -147,6 +123,18 @@ public class ApiExceptionHandler extends ResponseEntityExceptionHandler {
return new ResponseEntity<>(response, new HttpHeaders(), response.getStatus()); return new ResponseEntity<>(response, new HttpHeaders(), response.getStatus());
} }
@Hidden
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ImageInvalidException.class)
public ResponseEntity<ApiErrorDto> handle(ImageInvalidException e, WebRequest request) {
final ApiErrorDto response = ApiErrorDto.builder()
.status(HttpStatus.BAD_REQUEST)
.message(e.getLocalizedMessage())
.code("error.image.invalid")
.build();
return new ResponseEntity<>(response, new HttpHeaders(), response.getStatus());
}
@Hidden @Hidden
@ResponseStatus(HttpStatus.NOT_FOUND) @ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(ImageNotFoundException.class) @ExceptionHandler(ImageNotFoundException.class)
...@@ -159,6 +147,18 @@ public class ApiExceptionHandler extends ResponseEntityExceptionHandler { ...@@ -159,6 +147,18 @@ public class ApiExceptionHandler extends ResponseEntityExceptionHandler {
return new ResponseEntity<>(response, new HttpHeaders(), response.getStatus()); return new ResponseEntity<>(response, new HttpHeaders(), response.getStatus());
} }
@Hidden
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler(NotAllowedException.class)
public ResponseEntity<ApiErrorDto> handle(NotAllowedException e, WebRequest request) {
final ApiErrorDto response = ApiErrorDto.builder()
.status(HttpStatus.METHOD_NOT_ALLOWED)
.message(e.getLocalizedMessage())
.code("error.container.notallowed")
.build();
return new ResponseEntity<>(response, new HttpHeaders(), response.getStatus());
}
@Hidden @Hidden
@ResponseStatus(HttpStatus.FORBIDDEN) @ResponseStatus(HttpStatus.FORBIDDEN)
@ExceptionHandler(PersistenceException.class) @ExceptionHandler(PersistenceException.class)
......
...@@ -5,7 +5,6 @@ import at.tuwien.api.container.ContainerCreateRequestDto; ...@@ -5,7 +5,6 @@ import at.tuwien.api.container.ContainerCreateRequestDto;
import at.tuwien.config.DockerConfig; import at.tuwien.config.DockerConfig;
import at.tuwien.config.ReadyConfig; import at.tuwien.config.ReadyConfig;
import at.tuwien.entities.container.Container; import at.tuwien.entities.container.Container;
import at.tuwien.entities.container.image.ContainerImage;
import at.tuwien.exception.*; import at.tuwien.exception.*;
import at.tuwien.repository.jpa.ContainerRepository; import at.tuwien.repository.jpa.ContainerRepository;
import at.tuwien.repository.jpa.ImageRepository; import at.tuwien.repository.jpa.ImageRepository;
...@@ -55,9 +54,10 @@ public class ContainerServiceIntegrationTest extends BaseUnitTest { ...@@ -55,9 +54,10 @@ public class ContainerServiceIntegrationTest extends BaseUnitTest {
/* create networks */ /* create networks */
DockerConfig.createAllNetworks(); DockerConfig.createAllNetworks();
/* mock data */ /* mock data */
realmRepository.save(REALM_DBREPO);
userRepository.save(USER_1_SIMPLE); userRepository.save(USER_1_SIMPLE);
userRepository.save(USER_2_SIMPLE);
imageRepository.save(IMAGE_1_SIMPLE); imageRepository.save(IMAGE_1_SIMPLE);
realmRepository.save(REALM_DBREPO);
} }
@AfterEach @AfterEach
...@@ -81,7 +81,6 @@ public class ContainerServiceIntegrationTest extends BaseUnitTest { ...@@ -81,7 +81,6 @@ public class ContainerServiceIntegrationTest extends BaseUnitTest {
assertEquals(CONTAINER_1_NAME, container.getName()); assertEquals(CONTAINER_1_NAME, container.getName());
assertEquals(USER_1_USERNAME, container.getCreator().getUsername()); assertEquals(USER_1_USERNAME, container.getCreator().getUsername());
assertEquals(USER_1_USERNAME, container.getOwner().getUsername()); assertEquals(USER_1_USERNAME, container.getOwner().getUsername());
assertEquals(1, userRepository.findAll().size());
} }
@Test @Test
......
...@@ -121,6 +121,8 @@ public class ContainerServiceImpl implements ContainerService { ...@@ -121,6 +121,8 @@ public class ContainerServiceImpl implements ContainerService {
} }
container.setHash(response1.getId()); container.setHash(response1.getId());
container = containerRepository.save(container); container = containerRepository.save(container);
container.setCreator(user);
container.setOwner(user);
log.info("Created container {}", container.getId()); log.info("Created container {}", container.getId());
return container; return container;
} }
......
...@@ -15,6 +15,18 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExcep ...@@ -15,6 +15,18 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExcep
@ControllerAdvice @ControllerAdvice
public class ApiExceptionHandler extends ResponseEntityExceptionHandler { public class ApiExceptionHandler extends ResponseEntityExceptionHandler {
@Hidden
@ResponseStatus(HttpStatus.FORBIDDEN)
@ExceptionHandler(AccessDeniedException.class)
public ResponseEntity<ApiErrorDto> handle(AccessDeniedException e, WebRequest request) {
final ApiErrorDto response = ApiErrorDto.builder()
.status(HttpStatus.FORBIDDEN)
.message(e.getLocalizedMessage())
.code("error.identifier.accessdenied")
.build();
return new ResponseEntity<>(response, new HttpHeaders(), response.getStatus());
}
@Hidden @Hidden
@ResponseStatus(HttpStatus.NOT_FOUND) @ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(DatabaseNotFoundException.class) @ExceptionHandler(DatabaseNotFoundException.class)
...@@ -87,6 +99,18 @@ public class ApiExceptionHandler extends ResponseEntityExceptionHandler { ...@@ -87,6 +99,18 @@ public class ApiExceptionHandler extends ResponseEntityExceptionHandler {
return new ResponseEntity<>(response, new HttpHeaders(), response.getStatus()); return new ResponseEntity<>(response, new HttpHeaders(), response.getStatus());
} }
@Hidden
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler(NotAllowedException.class)
public ResponseEntity<ApiErrorDto> handle(NotAllowedException e, WebRequest request) {
final ApiErrorDto response = ApiErrorDto.builder()
.status(HttpStatus.METHOD_NOT_ALLOWED)
.message(e.getLocalizedMessage())
.code("error.identifier.notallowed")
.build();
return new ResponseEntity<>(response, new HttpHeaders(), response.getStatus());
}
@Hidden @Hidden
@ResponseStatus(HttpStatus.NOT_FOUND) @ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(QueryNotFoundException.class) @ExceptionHandler(QueryNotFoundException.class)
......
...@@ -6,11 +6,9 @@ import at.tuwien.api.identifier.IdentifierTypeDto; ...@@ -6,11 +6,9 @@ import at.tuwien.api.identifier.IdentifierTypeDto;
import at.tuwien.config.IndexInitializer; import at.tuwien.config.IndexInitializer;
import at.tuwien.config.ReadyConfig; import at.tuwien.config.ReadyConfig;
import at.tuwien.endpoints.IdentifierEndpoint; import at.tuwien.endpoints.IdentifierEndpoint;
import at.tuwien.exception.*;
import at.tuwien.repository.jpa.*; import at.tuwien.repository.jpa.*;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -58,9 +56,6 @@ public class IdentifierEndpointIntegrationTest extends BaseUnitTest { ...@@ -58,9 +56,6 @@ public class IdentifierEndpointIntegrationTest extends BaseUnitTest {
@Autowired @Autowired
private RealmRepository realmRepository; private RealmRepository realmRepository;
@Autowired
private AccessRepository accessRepository;
@Autowired @Autowired
private IdentifierEndpoint identifierEndpoint; private IdentifierEndpoint identifierEndpoint;
...@@ -70,6 +65,8 @@ public class IdentifierEndpointIntegrationTest extends BaseUnitTest { ...@@ -70,6 +65,8 @@ public class IdentifierEndpointIntegrationTest extends BaseUnitTest {
realmRepository.save(REALM_DBREPO); realmRepository.save(REALM_DBREPO);
userRepository.save(USER_1); userRepository.save(USER_1);
userRepository.save(USER_2); userRepository.save(USER_2);
userRepository.save(USER_3);
userRepository.save(USER_4);
containerRepository.save(CONTAINER_1_SIMPLE); containerRepository.save(CONTAINER_1_SIMPLE);
containerRepository.save(CONTAINER_2_SIMPLE); containerRepository.save(CONTAINER_2_SIMPLE);
databaseRepository.save(DATABASE_1_SIMPLE); databaseRepository.save(DATABASE_1_SIMPLE);
......
...@@ -13,10 +13,9 @@ import at.tuwien.gateway.BrokerServiceGateway; ...@@ -13,10 +13,9 @@ import at.tuwien.gateway.BrokerServiceGateway;
import at.tuwien.listener.impl.RabbitMqListenerImpl; import at.tuwien.listener.impl.RabbitMqListenerImpl;
import at.tuwien.querystore.Query; import at.tuwien.querystore.Query;
import at.tuwien.repository.jpa.DatabaseAccessRepository; import at.tuwien.repository.jpa.DatabaseAccessRepository;
import at.tuwien.repository.jpa.IdentifierRepository; import at.tuwien.repository.jpa.UserRepository;
import at.tuwien.service.AccessService; import at.tuwien.service.AccessService;
import at.tuwien.service.DatabaseService; import at.tuwien.service.DatabaseService;
import at.tuwien.service.QueryService;
import at.tuwien.service.impl.StoreServiceImpl; import at.tuwien.service.impl.StoreServiceImpl;
import com.rabbitmq.client.Channel; import com.rabbitmq.client.Channel;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
...@@ -54,11 +53,9 @@ public class StoreEndpointUnitTest extends BaseUnitTest { ...@@ -54,11 +53,9 @@ public class StoreEndpointUnitTest extends BaseUnitTest {
@MockBean @MockBean
private IndexConfig indexInitializer; private IndexConfig indexInitializer;
/* keep */
@MockBean @MockBean
private RabbitMqListenerImpl rabbitMqListener; private RabbitMqListenerImpl rabbitMqListener;
/* keep */
@MockBean @MockBean
private BrokerServiceGateway brokerServiceGateway; private BrokerServiceGateway brokerServiceGateway;
...@@ -77,6 +74,9 @@ public class StoreEndpointUnitTest extends BaseUnitTest { ...@@ -77,6 +74,9 @@ public class StoreEndpointUnitTest extends BaseUnitTest {
@MockBean @MockBean
private AccessService accessService; private AccessService accessService;
@MockBean
private UserRepository userRepository;
@Test @Test
@WithAnonymousUser @WithAnonymousUser
public void findAll_anonymous_succeeds() throws QueryStoreException, DatabaseNotFoundException, ImageNotSupportedException, public void findAll_anonymous_succeeds() throws QueryStoreException, DatabaseNotFoundException, ImageNotSupportedException,
...@@ -108,10 +108,6 @@ public class StoreEndpointUnitTest extends BaseUnitTest { ...@@ -108,10 +108,6 @@ public class StoreEndpointUnitTest extends BaseUnitTest {
@WithMockUser(username = USER_1_USERNAME, authorities = {"list-queries"}) @WithMockUser(username = USER_1_USERNAME, authorities = {"list-queries"})
public void findAll_noAccess_fails() { public void findAll_noAccess_fails() {
/* mock */
when(accessRepository.findByDatabaseIdAndUsername(DATABASE_2_ID, USER_1_USERNAME))
.thenReturn(Optional.of(DATABASE_1_RESEARCHER_READ_ACCESS));
/* test */ /* test */
assertThrows(NotAllowedException.class, () -> { assertThrows(NotAllowedException.class, () -> {
findAll_generic(CONTAINER_2_ID, DATABASE_2_ID, DATABASE_2, USER_1_PRINCIPAL); findAll_generic(CONTAINER_2_ID, DATABASE_2_ID, DATABASE_2, USER_1_PRINCIPAL);
...@@ -150,134 +146,111 @@ public class StoreEndpointUnitTest extends BaseUnitTest { ...@@ -150,134 +146,111 @@ public class StoreEndpointUnitTest extends BaseUnitTest {
findAll_generic(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, USER_3_PRINCIPAL); findAll_generic(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, USER_3_PRINCIPAL);
} }
@Test
public void find_anonymous_succeeds() throws QueryStoreException, QueryNotFoundException, DatabaseNotFoundException,
ImageNotSupportedException, UserNotFoundException, NotAllowedException, DatabaseConnectionException {
/* test */
final QueryDto response = find_generic(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, QUERY_1_ID, QUERY_1, USER_1_ID, null, null);
assertEquals(QUERY_1_ID, response.getId());
assertEquals(QUERY_1_STATEMENT, response.getQuery());
}
@Test @Test
@WithAnonymousUser @WithAnonymousUser
public void find_anonymous2_succeeds() throws QueryStoreException, QueryNotFoundException, DatabaseNotFoundException, public void find_anonymous_succeeds() throws QueryStoreException, QueryNotFoundException, DatabaseNotFoundException,
ImageNotSupportedException, UserNotFoundException, NotAllowedException, DatabaseConnectionException { ImageNotSupportedException, UserNotFoundException, NotAllowedException, DatabaseConnectionException {
/* test */ /* mock */
final QueryDto response = find_generic(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, QUERY_1_ID, QUERY_1, USER_1_ID, null, null); when(userRepository.findByUsername(USER_1_USERNAME))
assertEquals(QUERY_1_ID, response.getId()); .thenReturn(Optional.of(USER_1));
assertEquals(QUERY_1_STATEMENT, response.getQuery());
}
@Test
@WithMockUser(username = USER_1_USERNAME, roles = "RESEARCHER")
public void find_researcher_succeeds() throws QueryStoreException, QueryNotFoundException, DatabaseNotFoundException,
ImageNotSupportedException, UserNotFoundException, NotAllowedException, DatabaseConnectionException {
/* test */ /* test */
final QueryDto response = find_generic(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, QUERY_1_ID, QUERY_1, USER_1_ID, USER_1, USER_1_PRINCIPAL); final QueryDto response = find_generic(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, QUERY_1_ID, QUERY_1, USER_1_USERNAME, null, null);
assertEquals(QUERY_1_ID, response.getId()); assertEquals(QUERY_1_ID, response.getId());
assertEquals(QUERY_1_STATEMENT, response.getQuery()); assertEquals(QUERY_1_STATEMENT, response.getQuery());
} }
@Test @Test
@WithMockUser(username = USER_2_USERNAME, roles = "DATA_STEWARD") @WithMockUser(username = USER_1_USERNAME, authorities = "find-query")
public void find_dataSteward_succeeds() throws QueryStoreException, QueryNotFoundException, DatabaseNotFoundException, public void find_hasRole_succeeds() throws QueryStoreException, QueryNotFoundException, DatabaseNotFoundException,
ImageNotSupportedException, UserNotFoundException, NotAllowedException, DatabaseConnectionException { ImageNotSupportedException, UserNotFoundException, NotAllowedException, DatabaseConnectionException {
/* test */ /* test */
final QueryDto response = find_generic(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, QUERY_1_ID, QUERY_1, USER_2_ID, USER_2, USER_2_PRINCIPAL); final QueryDto response = find_generic(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, QUERY_1_ID, QUERY_1, USER_1_USERNAME, USER_1, USER_1_PRINCIPAL);
assertEquals(QUERY_1_ID, response.getId()); assertEquals(QUERY_1_ID, response.getId());
assertEquals(QUERY_1_STATEMENT, response.getQuery()); assertEquals(QUERY_1_STATEMENT, response.getQuery());
} }
@Test @Test
@WithMockUser(username = USER_3_USERNAME, roles = "DEVELOPER") @WithMockUser(username = USER_1_USERNAME)
public void find_developer_succeeds() throws QueryStoreException, QueryNotFoundException, DatabaseNotFoundException, public void find_noRole_succeeds() throws QueryStoreException, QueryNotFoundException, DatabaseNotFoundException,
ImageNotSupportedException, UserNotFoundException, NotAllowedException, DatabaseConnectionException { ImageNotSupportedException, UserNotFoundException, NotAllowedException, DatabaseConnectionException {
/* test */ /* test */
final QueryDto response = find_generic(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, QUERY_1_ID, QUERY_1, USER_3_ID, USER_3, USER_3_PRINCIPAL); final QueryDto response = find_generic(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, QUERY_1_ID, QUERY_1, USER_1_USERNAME, USER_1, USER_1_PRINCIPAL);
assertEquals(QUERY_1_ID, response.getId()); assertEquals(QUERY_1_ID, response.getId());
assertEquals(QUERY_1_STATEMENT, response.getQuery()); assertEquals(QUERY_1_STATEMENT, response.getQuery());
} }
@Test @Test
@WithMockUser(username = USER_1_USERNAME, roles = "RESEARCHER") @WithMockUser(username = USER_1_USERNAME, authorities = "find-query")
public void find_notFound_fails() { public void find_notFound_fails() {
/* test */ /* test */
assertThrows(QueryNotFoundException.class, () -> { assertThrows(QueryNotFoundException.class, () -> {
find_generic(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, QUERY_1_ID, null, USER_1_ID, USER_1, USER_1_PRINCIPAL); find_generic(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, QUERY_1_ID, null, USER_1_USERNAME, USER_1, USER_1_PRINCIPAL);
}); });
} }
@Test @Test
@WithMockUser(username = USER_1_USERNAME, roles = "RESEARCHER") @WithMockUser(username = USER_1_USERNAME, authorities = "find-query")
public void find_databaseNotFound_fails() { public void find_databaseNotFound_fails() {
/* test */ /* test */
assertThrows(NotAllowedException.class, () -> { assertThrows(NotAllowedException.class, () -> {
find_generic(CONTAINER_1_ID, DATABASE_1_ID, null, QUERY_1_ID, QUERY_1, USER_1_ID, USER_1, USER_1_PRINCIPAL); find_generic(CONTAINER_1_ID, DATABASE_1_ID, null, QUERY_1_ID, QUERY_1, USER_1_USERNAME, USER_1, USER_1_PRINCIPAL);
});
}
@Test
public void persist_publicAnonymous_fails() {
/* test */
assertThrows(NotAllowedException.class, () -> {
persist_generic(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, QUERY_1_ID, QUERY_1, null, null, null);
});
}
@Test
public void persist_publicRead_fails() {
/* test */
assertThrows(NotAllowedException.class, () -> {
persist_generic(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, QUERY_1_ID, QUERY_1, USER_2_USERNAME, USER_2_PRINCIPAL, DATABASE_1_RESEARCHER_READ_ACCESS);
}); });
} }
@Test @Test
public void persist_publicWriteOwn_fails() { @WithMockUser(username = USER_1_USERNAME, authorities = "persist-query")
public void persist_ownRead_succeeds() throws UserNotFoundException, QueryStoreException,
NotAllowedException, DatabaseConnectionException, QueryAlreadyPersistedException, QueryNotFoundException,
DatabaseNotFoundException, ImageNotSupportedException {
/* test */ /* test */
assertThrows(NotAllowedException.class, () -> { final QueryDto response = persist_generic(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, QUERY_1_ID, QUERY_1, USER_1_USERNAME, USER_1, USER_1_PRINCIPAL, DATABASE_1_RESEARCHER_READ_ACCESS);
persist_generic(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, QUERY_1_ID, QUERY_1, USER_2_USERNAME, USER_2_PRINCIPAL, DATABASE_1_RESEARCHER_WRITE_OWN_ACCESS); assertEquals(QUERY_1_ID, response.getId());
}); assertEquals(QUERY_1_STATEMENT, response.getQuery());
} }
@Test @Test
public void persist_publicWriteAll_succeeds() throws UserNotFoundException, QueryStoreException, @WithMockUser(username = USER_1_USERNAME, authorities = "persist-query")
public void persist_ownWriteOwn_succeeds() throws UserNotFoundException, QueryStoreException,
NotAllowedException, DatabaseConnectionException, QueryAlreadyPersistedException, QueryNotFoundException, NotAllowedException, DatabaseConnectionException, QueryAlreadyPersistedException, QueryNotFoundException,
DatabaseNotFoundException, ImageNotSupportedException { DatabaseNotFoundException, ImageNotSupportedException {
/* test */ /* test */
final QueryDto response = persist_generic(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, QUERY_1_ID, QUERY_1, USER_2_USERNAME, USER_2_PRINCIPAL, DATABASE_1_RESEARCHER_WRITE_ALL_ACCESS); final QueryDto response = persist_generic(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, QUERY_1_ID, QUERY_1, USER_1_USERNAME, USER_1, USER_1_PRINCIPAL, DATABASE_1_RESEARCHER_WRITE_OWN_ACCESS);
assertEquals(QUERY_1_ID, response.getId()); assertEquals(QUERY_1_ID, response.getId());
assertEquals(QUERY_1_STATEMENT, response.getQuery()); assertEquals(QUERY_1_STATEMENT, response.getQuery());
} }
@Test @Test
public void persist_publicWriteAllAlreadyPersisted_succeeds() { @WithMockUser(username = USER_1_USERNAME, authorities = "persist-query")
public void persist_ownWriteAll_succeeds() throws UserNotFoundException, QueryStoreException,
NotAllowedException, DatabaseConnectionException, QueryAlreadyPersistedException, QueryNotFoundException,
DatabaseNotFoundException, ImageNotSupportedException {
/* test */ /* test */
assertThrows(QueryAlreadyPersistedException.class, () -> { final QueryDto response = persist_generic(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, QUERY_1_ID, QUERY_1, USER_1_USERNAME, USER_1, USER_1_PRINCIPAL, DATABASE_1_RESEARCHER_WRITE_ALL_ACCESS);
persist_generic(CONTAINER_2_ID, DATABASE_2_ID, DATABASE_2, QUERY_2_ID, QUERY_2, USER_2_USERNAME, USER_2_PRINCIPAL, DATABASE_2_RESEARCHER_WRITE_ALL_ACCESS); assertEquals(QUERY_1_ID, response.getId());
}); assertEquals(QUERY_1_STATEMENT, response.getQuery());
} }
@Test @Test
public void persist_publicOwner_succeeds() throws UserNotFoundException, QueryStoreException, @WithMockUser(username = USER_2_USERNAME, authorities = "persist-query")
public void persist_foreignWriteAll_succeeds() throws UserNotFoundException, QueryStoreException,
NotAllowedException, DatabaseConnectionException, QueryAlreadyPersistedException, QueryNotFoundException, NotAllowedException, DatabaseConnectionException, QueryAlreadyPersistedException, QueryNotFoundException,
DatabaseNotFoundException, ImageNotSupportedException { DatabaseNotFoundException, ImageNotSupportedException {
/* mock */
when(userRepository.findByUsername(USER_1_USERNAME))
.thenReturn(Optional.of(USER_1));
/* test */ /* test */
final QueryDto response = persist_generic(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, QUERY_1_ID, QUERY_1, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_RESEARCHER_WRITE_ALL_ACCESS); final QueryDto response = persist_generic(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, QUERY_1_ID, QUERY_1, USER_2_USERNAME, USER_2, USER_2_PRINCIPAL, DATABASE_1_DEVELOPER_WRITE_ALL_ACCESS);
assertEquals(QUERY_1_ID, response.getId()); assertEquals(QUERY_1_ID, response.getId());
assertEquals(QUERY_1_STATEMENT, response.getQuery()); assertEquals(QUERY_1_STATEMENT, response.getQuery());
} }
...@@ -287,7 +260,7 @@ public class StoreEndpointUnitTest extends BaseUnitTest { ...@@ -287,7 +260,7 @@ public class StoreEndpointUnitTest extends BaseUnitTest {
/* ################################################################################################### */ /* ################################################################################################### */
protected QueryDto persist_generic(Long containerId, Long databaseId, Database database, Long queryId, Query query, protected QueryDto persist_generic(Long containerId, Long databaseId, Database database, Long queryId, Query query,
String username, Principal principal, DatabaseAccess access) String username, User user, Principal principal, DatabaseAccess access)
throws DatabaseNotFoundException, UserNotFoundException, QueryStoreException, QueryNotFoundException, throws DatabaseNotFoundException, UserNotFoundException, QueryStoreException, QueryNotFoundException,
ImageNotSupportedException, NotAllowedException, DatabaseConnectionException, ImageNotSupportedException, NotAllowedException, DatabaseConnectionException,
QueryAlreadyPersistedException { QueryAlreadyPersistedException {
...@@ -308,6 +281,8 @@ public class StoreEndpointUnitTest extends BaseUnitTest { ...@@ -308,6 +281,8 @@ public class StoreEndpointUnitTest extends BaseUnitTest {
when(accessService.find(databaseId, username)) when(accessService.find(databaseId, username))
.thenThrow(NotAllowedException.class); .thenThrow(NotAllowedException.class);
} }
when(userRepository.findByUsername(username))
.thenReturn(Optional.of(user));
/* test */ /* test */
final ResponseEntity<QueryDto> response = storeEndpoint.persist(containerId, databaseId, queryId, principal); final ResponseEntity<QueryDto> response = storeEndpoint.persist(containerId, databaseId, queryId, principal);
...@@ -337,7 +312,7 @@ public class StoreEndpointUnitTest extends BaseUnitTest { ...@@ -337,7 +312,7 @@ public class StoreEndpointUnitTest extends BaseUnitTest {
} }
protected QueryDto find_generic(Long containerId, Long databaseId, Database database, Long queryId, Query query, protected QueryDto find_generic(Long containerId, Long databaseId, Database database, Long queryId, Query query,
UUID userId, User user, Principal principal) throws QueryStoreException, String username, User user, Principal principal) throws QueryStoreException,
QueryNotFoundException, DatabaseNotFoundException, ImageNotSupportedException, UserNotFoundException, QueryNotFoundException, DatabaseNotFoundException, ImageNotSupportedException, UserNotFoundException,
NotAllowedException, DatabaseConnectionException { NotAllowedException, DatabaseConnectionException {
...@@ -356,6 +331,13 @@ public class StoreEndpointUnitTest extends BaseUnitTest { ...@@ -356,6 +331,13 @@ public class StoreEndpointUnitTest extends BaseUnitTest {
when(databaseService.find(containerId, databaseId)) when(databaseService.find(containerId, databaseId))
.thenThrow(DatabaseNotFoundException.class); .thenThrow(DatabaseNotFoundException.class);
} }
if (user != null) {
when(userRepository.findByUsername(username))
.thenReturn(Optional.of(user));
} else {
when(userRepository.findByUsername(username))
.thenReturn(Optional.empty());
}
/* test */ /* test */
final ResponseEntity<QueryDto> response = storeEndpoint.find(CONTAINER_1_ID, DATABASE_1_ID, QUERY_1_ID, principal); final ResponseEntity<QueryDto> response = storeEndpoint.find(CONTAINER_1_ID, DATABASE_1_ID, QUERY_1_ID, principal);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment