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

Merge branch 'dev-hotfix' into 'dev'

Hotfix

See merge request !194
parents b06e72a3 3a1d72c3
Branches
Tags
4 merge requests!231CI: Remove build for log-service,!228Better error message handling in the frontend,!223Release of version 1.4.0,!194Hotfix
Showing
with 280 additions and 70 deletions
......@@ -12,6 +12,7 @@ METADATA_PASSWORD=dbrepo
AUTH_DB=keycloak
AUTH_USERNAME=root
AUTH_PASSWORD=dbrepo
BROKER_ENDPOINT=http://broker-service:15672/admin/broker
BROKER_USERNAME=fda
BROKER_PASSWORD=fda
KEYCLOAK_ADMIN=fda
......
......@@ -124,20 +124,26 @@ test-frontend:
test-default-deployment:
stage: test-deployment
timeout: 5m
needs:
- test-frontend
script:
- "make teardown"
- "rm -f .env"
- "docker compose up -d || docker compose down"
- "docker compose up -d && make teardown"
- "make teardown"
coverage: '/TOTAL.*?([0-9]{1,3})%/'
test-env-deployment:
stage: test-deployment
timeout: 5m
needs:
- test-frontend
script:
- "make teardown"
- "cp .env.unix.example .env"
- "docker compose up -d || docker compose down"
- "docker compose up -d && make teardown"
- "make teardown"
scan-analyse-service:
stage: scan-docker
......
......@@ -102,7 +102,7 @@ test-backend: test-metadata-service test-analyse-service test-search-sync-agent
test-search-sync-agent: build-search-sync-agent
mvn -f ./dbrepo-search-sync-agent/pom.xml clean test verify
test-metadata-service: build-metadata-service
test-metadata-service: build-metadata-service teardown
mvn -f ./dbrepo-metadata-service/pom.xml clean test verify
test-analyse-service: build-analyse-service
......
......@@ -13,5 +13,3 @@ RUN rabbitmq-plugins enable --offline rabbitmq_prometheus rabbitmq_mqtt rabbitmq
EXPOSE 5672
EXPOSE 15672
HEALTHCHECK --interval=10s --timeout=5s --retries=12 CMD wget --spider http://localhost:15672/broker/
......@@ -10,8 +10,8 @@ default_permissions.write = .*
# enable http outside localhost
listeners.tcp.1 = 0.0.0.0:5672
# management ui (https://www.rabbitmq.com/management.html#path-prefix)
management.path_prefix = /broker
# management prefix (https://www.rabbitmq.com/management.html#path-prefix)
management.path_prefix = /admin/broker
# logging
log.console = true
......
......@@ -34,8 +34,7 @@ server {
listen 80 default_server;
server_name _;
location /api/broker {
rewrite /api/broker/(.*) /api/$1 break;
location /admin/broker {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
......@@ -44,22 +43,22 @@ server {
proxy_read_timeout 90;
}
location /api/analyse {
location /api/broker {
rewrite /api/broker/(.*) /admin/broker/api/$1 break;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://analyse;
proxy_pass http://broker;
proxy_read_timeout 90;
}
location /pid {
rewrite /pid/(.*) /api/pid/$1 break;
location /api/analyse {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://metadata;
proxy_pass http://analyse;
proxy_read_timeout 90;
}
......@@ -91,17 +90,17 @@ server {
proxy_read_timeout 90;
}
location /retrieve {
rewrite /retrieve/(.*) /$1 break;
location /api {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://search;
proxy_pass http://metadata;
proxy_read_timeout 90;
}
location /api {
location /pid {
rewrite /pid/(.*) /api/pid/$1 break;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
......@@ -110,6 +109,16 @@ server {
proxy_read_timeout 90;
}
location /retrieve {
rewrite /retrieve/(.*) /$1 break;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://search;
proxy_read_timeout 90;
}
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
......
......@@ -36,7 +36,7 @@ ENV ADMIN_MAIL="noreply@localhost"
ENV BASE_URL="http://localhost"
ENV GRANT_PRIVILEGES="SELECT, CREATE, CREATE VIEW, CREATE ROUTINE, CREATE TEMPORARY TABLES, LOCK TABLES, INDEX, TRIGGER, INSERT, UPDATE, DELETE"
ENV BROKER_CONSUMERS=2
ENV BROKER_ENDPOINT="http://broker-service:15672"
ENV BROKER_ENDPOINT="http://broker-service:15672/admin/broker"
ENV BROKER_USERNAME=fda
ENV BROKER_PASSWORD=fda
ENV DELETED_RECORD=persistent
......
package at.tuwien.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(code = HttpStatus.SERVICE_UNAVAILABLE)
public class BrokerRemoteException extends Exception {
public BrokerRemoteException(String msg) {
super(msg);
}
public BrokerRemoteException(String msg, Throwable thr) {
super(msg, thr);
}
public BrokerRemoteException(Throwable thr) {
super(thr);
}
}
......@@ -4,17 +4,17 @@ import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(code = HttpStatus.NOT_ACCEPTABLE)
public class BrokerVirtualHostCreationException extends Exception {
public class BrokerVirtualHostModificationException extends Exception {
public BrokerVirtualHostCreationException(String msg) {
public BrokerVirtualHostModificationException(String msg) {
super(msg);
}
public BrokerVirtualHostCreationException(String msg, Throwable thr) {
public BrokerVirtualHostModificationException(String msg, Throwable thr) {
super(msg, thr);
}
public BrokerVirtualHostCreationException(Throwable thr) {
public BrokerVirtualHostModificationException(Throwable thr) {
super(thr);
}
}
......@@ -3,7 +3,7 @@ package at.tuwien.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(code = HttpStatus.LOCKED)
@ResponseStatus(code = HttpStatus.SERVICE_UNAVAILABLE)
public class KeycloakRemoteException extends Exception {
public KeycloakRemoteException(String msg) {
......
......@@ -163,9 +163,9 @@ public interface DatabaseMapper {
}
default PreparedStatement rawGrantCreatorAccessQuery(Connection connection, String databaseName, String username,
String priviliges) throws QueryMalformedException {
String privileges) throws QueryMalformedException {
final StringBuilder statement = new StringBuilder("GRANT ")
.append(priviliges)
.append(privileges)
.append(" ON ")
.append(databaseName)
.append(".* TO `")
......
......@@ -2,7 +2,6 @@ package at.tuwien.endpoints;
import at.tuwien.api.database.*;
import at.tuwien.api.error.ApiErrorDto;
import at.tuwien.api.user.UserDto;
import at.tuwien.entities.database.Database;
import at.tuwien.entities.database.DatabaseAccess;
import at.tuwien.entities.user.User;
......@@ -84,7 +83,7 @@ public class DatabaseEndpoint {
}
@PostMapping
@Transactional
@Transactional(rollbackFor = Exception.class)
@PreAuthorize("hasAuthority('create-database')")
@Timed(value = "database.create", description = "Time needed to create a database")
@Operation(summary = "Create database", security = @SecurityRequirement(name = "bearerAuth"))
......@@ -140,8 +139,8 @@ public class DatabaseEndpoint {
throws ImageNotSupportedException, ContainerNotFoundException, DatabaseMalformedException,
AmqpException, ContainerConnectionException, UserNotFoundException,
DatabaseNotFoundException, DatabaseNameExistsException, DatabaseConnectionException,
QueryMalformedException, NotAllowedException, BrokerVirtualHostCreationException, QueryStoreException,
BrokerVirtualHostGrantException, KeycloakRemoteException, AccessDeniedException {
QueryMalformedException, NotAllowedException, BrokerVirtualHostModificationException, QueryStoreException,
BrokerVirtualHostGrantException, KeycloakRemoteException, AccessDeniedException, BrokerRemoteException {
log.debug("endpoint create database, createDto={}, principal={}", createDto,
principal);
final User user = userService.findByUsername(principal.getName());
......@@ -272,7 +271,7 @@ public class DatabaseEndpoint {
}
@DeleteMapping("/{id}")
@Transactional
@Transactional(rollbackFor = Exception.class)
@PreAuthorize("hasAuthority('delete-database')")
@Timed(value = "database.delete", description = "Time needed to delete a database")
@Operation(summary = "Delete some database", security = @SecurityRequirement(name = "bearerAuth"))
......@@ -321,7 +320,7 @@ public class DatabaseEndpoint {
public ResponseEntity<?> delete(@NotNull @PathVariable Long id, Principal principal)
throws DatabaseNotFoundException, ImageNotSupportedException, DatabaseMalformedException, AmqpException,
QueryMalformedException, UserNotFoundException, BrokerVirtualHostGrantException,
DatabaseConnectionException, KeycloakRemoteException, AccessDeniedException {
DatabaseConnectionException, KeycloakRemoteException, AccessDeniedException, BrokerRemoteException {
log.debug("endpoint delete database, id={}, principal={}", id,
principal);
final Database database = databaseService.findById(id);
......
......@@ -6,6 +6,7 @@ import at.tuwien.api.user.*;
import at.tuwien.entities.user.User;
import at.tuwien.exception.*;
import at.tuwien.mapper.UserMapper;
import at.tuwien.service.AuthenticationService;
import at.tuwien.service.DatabaseService;
import at.tuwien.service.MessageQueueService;
import at.tuwien.service.UserService;
......@@ -42,15 +43,16 @@ public class UserEndpoint {
private final UserService userService;
private final DatabaseService databaseService;
private final MessageQueueService messageQueueService;
private final AuthenticationService authenticationService;
@Autowired
public UserEndpoint(UserMapper userMapper, UserService userService, DatabaseService databaseService,
MessageQueueService messageQueueService) {
MessageQueueService messageQueueService, AuthenticationService authenticationService) {
this.userMapper = userMapper;
this.userService = userService;
this.databaseService = databaseService;
this.messageQueueService = messageQueueService;
this.authenticationService = authenticationService;
}
@GetMapping
......@@ -75,7 +77,7 @@ public class UserEndpoint {
}
@PostMapping
@Transactional
@Transactional(rollbackFor = Exception.class)
@PreAuthorize("!isAuthenticated()")
@Timed(value = "user.create", description = "Time needed to create a user in the metadata database")
@Operation(summary = "Create user")
......@@ -85,8 +87,11 @@ public class UserEndpoint {
content = {@Content(
mediaType = "application/json",
schema = @Schema(implementation = UserBriefDto.class))}),
@ApiResponse(responseCode = "400",
description = "Parameters are not well-formed (likely email)",
content = {@Content(mediaType = "application/json")}),
@ApiResponse(responseCode = "404",
description = "Realm or default role not found",
description = "default role not found",
content = {@Content(
mediaType = "application/json",
schema = @Schema(implementation = ApiErrorDto.class))}),
......@@ -102,15 +107,35 @@ public class UserEndpoint {
schema = @Schema(implementation = ApiErrorDto.class))}),
})
public ResponseEntity<UserBriefDto> create(@NotNull @Valid @RequestBody SignupRequestDto data)
throws RealmNotFoundException, UserAlreadyExistsException, UserEmailAlreadyExistsException,
UserNotFoundException, KeycloakRemoteException, AccessDeniedException, BrokerVirtualHostCreationException {
throws UserAlreadyExistsException, UserEmailAlreadyExistsException, UserNotFoundException,
KeycloakRemoteException, AccessDeniedException, BrokerRemoteException,
BrokerVirtualHostModificationException {
log.debug("endpoint create a user, data={}", data);
/* check */
userService.validateUsernameNotExists(data.getUsername());
userService.validateEmailNotExists(data.getEmail());
/* create */
final UserBriefDto dto = userMapper.userToUserBriefDto(userService.create(data));
messageQueueService.createUser(dto.getUsername());
authenticationService.create(data);
final at.tuwien.api.keycloak.UserDto keycloakUserDto = authenticationService.findByUsername(data.getUsername());
try {
messageQueueService.createUser(data.getUsername());
} catch (BrokerRemoteException e) {
try {
authenticationService.delete(keycloakUserDto.getId());
} catch (UserNotFoundException e2) {
/* ignore */
}
throw new BrokerRemoteException(e);
} catch (BrokerVirtualHostModificationException e) {
try {
authenticationService.delete(keycloakUserDto.getId());
} catch (UserNotFoundException e2) {
/* ignore */
}
throw new BrokerVirtualHostModificationException(e);
}
final User user = userService.create(data, keycloakUserDto.getId());
final UserBriefDto dto = userMapper.userToUserBriefDto(user);
log.trace("create user resulted in dto {}", dto);
return ResponseEntity.status(HttpStatus.CREATED)
.body(dto);
......@@ -275,6 +300,7 @@ public class UserEndpoint {
}
/* modify password */
userService.updatePassword(id, data);
authenticationService.updatePassword(id, data);
return ResponseEntity.accepted()
.build();
}
......
......@@ -40,17 +40,29 @@ public class ApiExceptionHandler extends ResponseEntityExceptionHandler {
}
@Hidden
@ResponseStatus(HttpStatus.LOCKED)
@ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE)
@ExceptionHandler(KeycloakRemoteException.class)
public ResponseEntity<ApiErrorDto> handle(KeycloakRemoteException e, WebRequest request) {
final ApiErrorDto response = ApiErrorDto.builder()
.status(HttpStatus.LOCKED)
.status(HttpStatus.SERVICE_UNAVAILABLE)
.message(e.getLocalizedMessage())
.code("error.metadata.keycloak")
.build();
return new ResponseEntity<>(response, new HttpHeaders(), response.getStatus());
}
@Hidden
@ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE)
@ExceptionHandler(BrokerRemoteException.class)
public ResponseEntity<ApiErrorDto> handle(BrokerRemoteException e, WebRequest request) {
final ApiErrorDto response = ApiErrorDto.builder()
.status(HttpStatus.SERVICE_UNAVAILABLE)
.message(e.getLocalizedMessage())
.code("error.metadata.broker")
.build();
return new ResponseEntity<>(response, new HttpHeaders(), response.getStatus());
}
@Hidden
@ResponseStatus(HttpStatus.CONFLICT)
@ExceptionHandler(ContainerAlreadyExistsException.class)
......@@ -233,8 +245,8 @@ public class ApiExceptionHandler extends ResponseEntityExceptionHandler {
@Hidden
@ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
@ExceptionHandler(BrokerVirtualHostCreationException.class)
public ResponseEntity<ApiErrorDto> handle(BrokerVirtualHostCreationException e, WebRequest request) {
@ExceptionHandler(BrokerVirtualHostModificationException.class)
public ResponseEntity<ApiErrorDto> handle(BrokerVirtualHostModificationException e, WebRequest request) {
final ApiErrorDto response = ApiErrorDto.builder()
.status(HttpStatus.NOT_ACCEPTABLE)
.message(e.getLocalizedMessage())
......
......@@ -57,6 +57,7 @@ public class MariaDbContainerConfig {
@Override
public synchronized void start() {
if (!started) {
super.stop();
super.start();
started = true;
}
......
......@@ -4,7 +4,6 @@ import at.tuwien.BaseUnitTest;
import at.tuwien.annotations.MockAmqp;
import at.tuwien.annotations.MockOpensearch;
import at.tuwien.api.database.*;
import at.tuwien.api.user.UserDto;
import at.tuwien.entities.container.Container;
import at.tuwien.entities.database.Database;
import at.tuwien.entities.database.DatabaseAccess;
......@@ -116,8 +115,8 @@ public class DatabaseEndpointUnitTest extends BaseUnitTest {
public void create_succeeds() throws UserNotFoundException, BrokerVirtualHostGrantException,
DatabaseNameExistsException, NotAllowedException, ContainerConnectionException, DatabaseMalformedException,
QueryStoreException, DatabaseConnectionException, QueryMalformedException, DatabaseNotFoundException,
ImageNotSupportedException, AmqpException, BrokerVirtualHostCreationException, ContainerNotFoundException,
KeycloakRemoteException, AccessDeniedException {
ImageNotSupportedException, AmqpException, BrokerVirtualHostModificationException, ContainerNotFoundException,
KeycloakRemoteException, AccessDeniedException, BrokerRemoteException {
final DatabaseCreateDto request = DatabaseCreateDto.builder()
.cid(CONTAINER_1_ID)
.name(DATABASE_1_NAME)
......@@ -399,7 +398,7 @@ public class DatabaseEndpointUnitTest extends BaseUnitTest {
@WithMockUser(username = USER_2_USERNAME, authorities = {"delete-database"})
public void delete_hasRole_succeeds() throws UserNotFoundException, BrokerVirtualHostGrantException,
DatabaseConnectionException, QueryMalformedException, DatabaseNotFoundException, ImageNotSupportedException,
AmqpException, DatabaseMalformedException, KeycloakRemoteException, AccessDeniedException {
AmqpException, DatabaseMalformedException, KeycloakRemoteException, AccessDeniedException, BrokerRemoteException {
/* test */
delete_generic(DATABASE_2_ID, DATABASE_2, USER_2_PRINCIPAL);
......@@ -429,8 +428,8 @@ public class DatabaseEndpointUnitTest extends BaseUnitTest {
Principal principal) throws UserNotFoundException, DatabaseNameExistsException,
NotAllowedException, ContainerConnectionException, DatabaseMalformedException, QueryStoreException,
DatabaseConnectionException, QueryMalformedException, DatabaseNotFoundException, ImageNotSupportedException,
AmqpException, BrokerVirtualHostCreationException, ContainerNotFoundException,
BrokerVirtualHostGrantException, KeycloakRemoteException, AccessDeniedException {
AmqpException, BrokerVirtualHostModificationException, ContainerNotFoundException,
BrokerVirtualHostGrantException, KeycloakRemoteException, AccessDeniedException, BrokerRemoteException {
/* mock */
doNothing()
......@@ -499,7 +498,7 @@ public class DatabaseEndpointUnitTest extends BaseUnitTest {
public void delete_generic(Long databaseId, Database database, Principal principal)
throws DatabaseNotFoundException, UserNotFoundException, DatabaseConnectionException,
QueryMalformedException, ImageNotSupportedException, AmqpException, DatabaseMalformedException,
BrokerVirtualHostGrantException, KeycloakRemoteException, AccessDeniedException {
BrokerVirtualHostGrantException, KeycloakRemoteException, AccessDeniedException, BrokerRemoteException {
/* mock */
if (database != null) {
......
......@@ -7,6 +7,7 @@ import at.tuwien.api.auth.SignupRequestDto;
import at.tuwien.api.user.*;
import at.tuwien.entities.user.User;
import at.tuwien.exception.*;
import at.tuwien.service.AuthenticationService;
import at.tuwien.service.MessageQueueService;
import at.tuwien.service.UserService;
import lombok.extern.log4j.Log4j2;
......@@ -44,6 +45,9 @@ public class UserEndpointUnitTest extends BaseUnitTest {
@MockBean
private MessageQueueService messageQueueService;
@MockBean
private AuthenticationService authenticationService;
@Autowired
private UserEndpoint userEndpoint;
......@@ -66,8 +70,8 @@ public class UserEndpointUnitTest extends BaseUnitTest {
@Test
@WithAnonymousUser
public void create_anonymous_succeeds() throws UserNotFoundException, UserEmailAlreadyExistsException,
RealmNotFoundException, UserAlreadyExistsException, KeycloakRemoteException,
at.tuwien.exception.AccessDeniedException, BrokerVirtualHostCreationException {
UserAlreadyExistsException, KeycloakRemoteException,
at.tuwien.exception.AccessDeniedException, BrokerRemoteException, BrokerVirtualHostModificationException {
final SignupRequestDto request = SignupRequestDto.builder()
.email(USER_1_EMAIL)
.username(USER_1_USERNAME)
......@@ -75,7 +79,7 @@ public class UserEndpointUnitTest extends BaseUnitTest {
.build();
/* test */
create_generic(request, USER_1);
create_generic(request, USER_1, USER_1_KEYCLOAK_DTO, USER_1_ID);
}
@Test
......@@ -89,7 +93,7 @@ public class UserEndpointUnitTest extends BaseUnitTest {
/* test */
assertThrows(org.springframework.security.access.AccessDeniedException.class, () -> {
create_generic(request, null);
create_generic(request, null, null, null);
});
}
......@@ -302,16 +306,22 @@ public class UserEndpointUnitTest extends BaseUnitTest {
assertEquals(2, body.size());
}
protected void create_generic(SignupRequestDto data, User user) throws UserEmailAlreadyExistsException,
RealmNotFoundException, UserAlreadyExistsException, UserNotFoundException, KeycloakRemoteException,
AccessDeniedException, BrokerVirtualHostCreationException {
protected void create_generic(SignupRequestDto data, User user, at.tuwien.api.keycloak.UserDto userDto, UUID id)
throws UserEmailAlreadyExistsException, UserAlreadyExistsException, UserNotFoundException,
KeycloakRemoteException, AccessDeniedException, BrokerRemoteException,
BrokerVirtualHostModificationException {
/* mock */
when(userService.create(data))
when(userService.create(data, id))
.thenReturn(user);
doNothing()
.when(messageQueueService)
.createUser(anyString());
when(authenticationService.findByUsername(data.getUsername()))
.thenReturn(userDto);
doNothing()
.when(authenticationService)
.create(any(SignupRequestDto.class));
/* test */
final ResponseEntity<UserBriefDto> response = userEndpoint.create(data);
......
......@@ -3,7 +3,8 @@ package at.tuwien.gateway;
import at.tuwien.BaseUnitTest;
import at.tuwien.annotations.MockAmqp;
import at.tuwien.annotations.MockOpensearch;
import at.tuwien.exception.BrokerVirtualHostCreationException;
import at.tuwien.exception.BrokerRemoteException;
import at.tuwien.exception.BrokerVirtualHostModificationException;
import at.tuwien.exception.BrokerVirtualHostGrantException;
import lombok.extern.log4j.Log4j2;
import org.junit.jupiter.api.Test;
......@@ -37,7 +38,7 @@ public class BrokerServiceGatewayTest extends BaseUnitTest {
private BrokerServiceGateway brokerServiceGateway;
@Test
public void createVirtualHost_succeeds() throws BrokerVirtualHostCreationException {
public void createVirtualHost_succeeds() throws BrokerVirtualHostModificationException, BrokerRemoteException {
final ResponseEntity<Void> mock = ResponseEntity.status(HttpStatus.CREATED)
.build();
......@@ -59,13 +60,13 @@ public class BrokerServiceGatewayTest extends BaseUnitTest {
.thenReturn(mock);
/* test */
assertThrows(BrokerVirtualHostCreationException.class, () -> {
assertThrows(BrokerVirtualHostModificationException.class, () -> {
brokerServiceGateway.createVirtualHost(VIRTUAL_HOST_CREATE_DTO);
});
}
@Test
public void grantPermission_exchangeNoRightsBefore_succeeds() throws BrokerVirtualHostGrantException {
public void grantPermission_exchangeNoRightsBefore_succeeds() throws BrokerVirtualHostGrantException, BrokerRemoteException {
final ResponseEntity<Void> mock = ResponseEntity.status(HttpStatus.CREATED)
.build();
......@@ -78,7 +79,7 @@ public class BrokerServiceGatewayTest extends BaseUnitTest {
}
@Test
public void grantPermission_exchangeRightsSame_succeeds() throws BrokerVirtualHostGrantException {
public void grantPermission_exchangeRightsSame_succeeds() throws BrokerVirtualHostGrantException, BrokerRemoteException {
final ResponseEntity<Void> mock = ResponseEntity.status(HttpStatus.NO_CONTENT)
.build();
......@@ -106,7 +107,7 @@ public class BrokerServiceGatewayTest extends BaseUnitTest {
}
@Test
public void grantPermission_virtualHostNoRightsBefore_succeeds() throws BrokerVirtualHostGrantException {
public void grantPermission_virtualHostNoRightsBefore_succeeds() throws BrokerRemoteException, BrokerVirtualHostGrantException {
final ResponseEntity<Void> mock = ResponseEntity.status(HttpStatus.CREATED)
.build();
......@@ -119,7 +120,7 @@ public class BrokerServiceGatewayTest extends BaseUnitTest {
}
@Test
public void grantPermission_virtualHostRightsSame_succeeds() throws BrokerVirtualHostGrantException {
public void grantPermission_virtualHostRightsSame_succeeds() throws BrokerRemoteException, BrokerVirtualHostGrantException {
final ResponseEntity<Void> mock = ResponseEntity.status(HttpStatus.NO_CONTENT)
.build();
......@@ -147,7 +148,7 @@ public class BrokerServiceGatewayTest extends BaseUnitTest {
}
@Test
public void createUser_succeeds() throws BrokerVirtualHostCreationException {
public void createUser_succeeds() throws BrokerRemoteException, BrokerVirtualHostModificationException {
final ResponseEntity<Void> mock = ResponseEntity.status(HttpStatus.NO_CONTENT)
.build();
......@@ -169,7 +170,7 @@ public class BrokerServiceGatewayTest extends BaseUnitTest {
.thenReturn(mock);
/* test */
assertThrows(BrokerVirtualHostCreationException.class, () -> {
assertThrows(BrokerVirtualHostModificationException.class, () -> {
brokerServiceGateway.createUser(USER_1_USERNAME);
});
}
......
package at.tuwien.endpoints;
package at.tuwien.mvc;
import at.tuwien.BaseUnitTest;
import at.tuwien.annotations.MockAmqp;
import at.tuwien.annotations.MockOpensearch;
import at.tuwien.repository.sdb.DatabaseIdxRepository;
import lombok.extern.log4j.Log4j2;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
......@@ -24,7 +22,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@SpringBootTest
@MockAmqp
@MockOpensearch
public class SwaggerComponentTest extends BaseUnitTest {
public class SwaggerEndpointMvcTest extends BaseUnitTest {
@Autowired
private MockMvc mockMvc;
......
package at.tuwien.mvc;
import at.tuwien.BaseUnitTest;
import at.tuwien.annotations.MockAmqp;
import at.tuwien.annotations.MockOpensearch;
import at.tuwien.api.auth.CreateUserDto;
import at.tuwien.api.auth.SignupRequestDto;
import at.tuwien.api.keycloak.UserCreateDto;
import at.tuwien.exception.BrokerRemoteException;
import at.tuwien.exception.KeycloakRemoteException;
import at.tuwien.gateway.BrokerServiceGateway;
import at.tuwien.gateway.KeycloakGateway;
import at.tuwien.gateway.impl.KeycloakGatewayImpl;
import lombok.extern.log4j.Log4j2;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import static at.tuwien.test.utils.ObjectUtil.asJsonString;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@Log4j2
@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
@SpringBootTest
@MockAmqp
@MockOpensearch
public class UserEndpointMvcTest extends BaseUnitTest {
@MockBean
private BrokerServiceGateway brokerServiceGateway;
@MockBean
private KeycloakGatewayImpl keycloakGateway;
@Autowired
private MockMvc mockMvc;
@Test
public void createUser_malformed_fails() throws Exception {
final SignupRequestDto request = SignupRequestDto.builder()
.username(USER_1_USERNAME)
.password(USER_1_PASSWORD)
.email("invalid_email")
.build();
/* mock */
doNothing()
.when(brokerServiceGateway)
.createUser(USER_1_USERNAME);
/* test */
this.mockMvc.perform(post("/api/user")
.content(asJsonString(request))
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().is(400));
}
@Test
public void createUser_keycloakOffline_503_fails() throws Exception {
/* mock */
doThrow(KeycloakRemoteException.class)
.when(keycloakGateway)
.createUser(any(UserCreateDto.class));
/* test */
this.mockMvc.perform(post("/api/user")
.content(asJsonString(USER_1_SIGNUP_REQUEST_DTO))
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().is(503));
}
@Test
public void createUser_brokerOffline_503_fails() throws Exception {
/* mock */
doNothing()
.when(keycloakGateway)
.createUser(any(UserCreateDto.class));
when(keycloakGateway.findByUsername(USER_1_USERNAME))
.thenReturn(USER_1_KEYCLOAK_DTO);
doThrow(BrokerRemoteException.class)
.when(brokerServiceGateway)
.createUser(USER_1_USERNAME);
/* test */
this.mockMvc.perform(post("/api/user")
.content(asJsonString(USER_1_SIGNUP_REQUEST_DTO))
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().is(503));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment