Skip to content

Commit

Permalink
feat: added call to post a further steps message
Browse files Browse the repository at this point in the history
  • Loading branch information
mobo4b committed Feb 26, 2021
1 parent fdf8ee6 commit 2a87bb9
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public class MessageController implements MessagesApi {

/**
* Returns a list of {@link MessageStreamDTO}s from the specified Rocket.Chat group.
*
* @param rcToken (required) Rocket.Chat token of the user
* @param rcUserId (required) Rocket.Chat user ID
* @param rcGroupId (required) Rocket.Chat group ID
* @return {@link ResponseEntity} containing {@link MessageStreamDTO}
*/
@Override
public ResponseEntity<MessageStreamDTO> getMessageStream(@RequestHeader String rcToken,
Expand All @@ -56,7 +61,10 @@ public ResponseEntity<MessageStreamDTO> getMessageStream(@RequestHeader String r
}

/**
* Upates the Master-Key Fragment for the en-/decryption of messages.
* Updates the Master-Key Fragment for the en-/decryption of messages.
*
* @param masterKey the master key
* @return {@link ResponseEntity} with the {@link HttpStatus}
*/
@Override
public ResponseEntity<Void> updateKey(@Valid @RequestBody MasterKeyDTO masterKey) {
Expand All @@ -72,6 +80,12 @@ public ResponseEntity<Void> updateKey(@Valid @RequestBody MasterKeyDTO masterKey

/**
* Posts a message in the specified Rocket.Chat group.
*
* @param rcToken (required) Rocket.Chat token of the user
* @param rcUserId (required) Rocket.Chat user ID
* @param rcGroupId (required) Rocket.Chat group ID
* @param message (required) the message
* @return {@link ResponseEntity} with the {@link HttpStatus}
*/
@Override
public ResponseEntity<Void> createMessage(@RequestHeader String rcToken,
Expand All @@ -86,6 +100,12 @@ public ResponseEntity<Void> createMessage(@RequestHeader String rcToken,
/**
* Forwards/posts a message in the specified Rocket.Chat group and sets the values from the body
* object in the alias object of the Rocket.Chat message.
*
* @param rcToken (required) Rocket.Chat token of the user
* @param rcUserId (required) Rocket.Chat user ID
* @param rcGroupId (required) Rocket.Chat group ID
* @param forwardMessageDTO (required) {@link ForwardMessageDTO}
* @return {@link ResponseEntity} with the {@link HttpStatus}
*/
@Override
public ResponseEntity<Void> forwardMessage(@RequestHeader String rcToken,
Expand All @@ -108,6 +128,12 @@ public ResponseEntity<Void> forwardMessage(@RequestHeader String rcToken,

/**
* Posts a message in the specified Feedback Rocket.Chat group.
*
* @param rcToken (required) Rocket.Chat token of the user
* @param rcUserId (required) Rocket.Chat user ID
* @param rcFeedbackGroupId (required) Rocket.Chat group ID
* @param message (required) the message
* @return {@link ResponseEntity} with the {@link HttpStatus}
*/
@Override
public ResponseEntity<Void> createFeedbackMessage(@RequestHeader String rcToken,
Expand Down Expand Up @@ -138,6 +164,10 @@ public ResponseEntity<Void> createVideoHintMessage(@RequestHeader String rcGroup

/**
* Saves a draft message identified by current authenticated user and Rocket.Chat group.
*
* @param rcGroupId (required) Rocket.Chat group ID
* @param message the message
* @return {@link ResponseEntity} with the {@link HttpStatus}
*/
@Override
public ResponseEntity<Void> saveDraftMessage(@RequestHeader String rcGroupId,
Expand All @@ -147,12 +177,28 @@ public ResponseEntity<Void> saveDraftMessage(@RequestHeader String rcGroupId,
}

/**
* Returnes a saved draft message if present.
* Returns a saved draft message if present.
*
* @param rcGroupId (required) Rocket.Chat group ID
* @return {@link ResponseEntity} with the {@link HttpStatus}
*/
@Override
public ResponseEntity<String> findDraftMessage(@RequestHeader String rcGroupId) {
String draftMessage = this.draftMessageService.findAndDecryptDraftMessage(rcGroupId);
return nonNull(draftMessage) ? ResponseEntity.ok(draftMessage) :
new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

/**
* Posts a further steps message in the specified Rocket.Chat group.
*
* @param rcGroupId (required) Rocket.Chat group ID
* @return {@link ResponseEntity} with the {@link HttpStatus}
*/
@Override
public ResponseEntity<Void> saveFurtherStepsMessage(@RequestHeader String rcGroupId) {
postGroupMessageFacade.postFurtherStepsMessage(rcGroupId);

return new ResponseEntity<>(HttpStatus.CREATED);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import de.caritas.cob.messageservice.api.exception.InternalServerErrorException;
import de.caritas.cob.messageservice.api.exception.RocketChatPostMarkGroupAsReadException;
import de.caritas.cob.messageservice.api.exception.RocketChatPostMessageException;
import de.caritas.cob.messageservice.api.helper.JSONHelper;
import de.caritas.cob.messageservice.api.model.AliasMessageDTO;
import de.caritas.cob.messageservice.api.model.MessageDTO;
import de.caritas.cob.messageservice.api.model.MessageType;
import de.caritas.cob.messageservice.api.model.VideoCallMessageDTO;
import de.caritas.cob.messageservice.api.model.rocket.chat.group.GetGroupInfoDto;
import de.caritas.cob.messageservice.api.model.rocket.chat.message.PostMessageResponseDTO;
Expand Down Expand Up @@ -129,16 +129,18 @@ private void validateFeedbackChatId(String rcToken, String rcUserId, String rcFe
* @param videoCallMessageDTO the {@link VideoCallMessageDTO}
*/
public void createVideoHintMessage(String rcGroupId, VideoCallMessageDTO videoCallMessageDTO) {

AliasMessageDTO aliasMessageDTO = new AliasMessageDTO()
.videoCallMessageDTO(videoCallMessageDTO);
this.rocketChatService.postAliasOnlyMessageAsSystemUser(rcGroupId, aliasMessageDTO);
}

String alias = JSONHelper.convertAliasMessageDTOToString(aliasMessageDTO).orElse(null);

try {
this.rocketChatService.postGroupVideoHintMessageBySystemUser(rcGroupId, alias);
} catch (CustomCryptoException e) {
throw new InternalServerErrorException(e, LogService::logInternalServerError);
}
/**
* Posts a further steps metadata message in the specified Rocket.Chat group.
*
* @param rcGroupId (required) Rocket.Chat group ID
*/
public void postFurtherStepsMessage(String rcGroupId) {
AliasMessageDTO aliasMessageDTO = new AliasMessageDTO().messageType(MessageType.FURTHER_STEPS);
this.rocketChatService.postAliasOnlyMessageAsSystemUser(rcGroupId, aliasMessageDTO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import de.caritas.cob.messageservice.api.exception.RocketChatBadRequestException;
import de.caritas.cob.messageservice.api.exception.RocketChatUserNotInitializedException;
import de.caritas.cob.messageservice.api.helper.Helper;
import de.caritas.cob.messageservice.api.helper.JSONHelper;
import de.caritas.cob.messageservice.api.model.AliasMessageDTO;
import de.caritas.cob.messageservice.api.model.MessageStreamDTO;
import de.caritas.cob.messageservice.api.model.MessageType;
import de.caritas.cob.messageservice.api.model.rocket.chat.RocketChatCredentials;
Expand Down Expand Up @@ -195,17 +197,22 @@ public PostMessageResponseDTO postGroupMessage(
}

/**
* Posts metadata of a video call as hint in Rocket.Chat group with an empty message containing
* meta data in the alias object.
* Posts metadata contained in an {@link AliasMessageDTO} in the given Rocket.Chat group with an
* empty message.
*
* @param rcGroupId the Rocket.Chat group id
* @param alias the alias Json object string
* @param rcGroupId the Rocket.Chat group id
* @param aliasMessageDTO {@link AliasMessageDTO}
*/
public void postGroupVideoHintMessageBySystemUser(String rcGroupId,
String alias) throws CustomCryptoException {
public void postAliasOnlyMessageAsSystemUser(String rcGroupId, AliasMessageDTO aliasMessageDTO) {
RocketChatCredentials systemUser = retrieveSystemUser();
postGroupMessage(systemUser.getRocketChatToken(), systemUser.getRocketChatUserId(), rcGroupId,
EMPTY, alias);
String alias = JSONHelper.convertAliasMessageDTOToString(aliasMessageDTO).orElse(null);

try {
this.postGroupMessage(systemUser.getRocketChatToken(), systemUser.getRocketChatUserId(),
rcGroupId, EMPTY, alias);
} catch (CustomCryptoException e) {
throw new InternalServerErrorException(e, LogService::logInternalServerError);
}
}

private RocketChatCredentials retrieveSystemUser() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,16 @@ protected void configure(HttpSecurity http) throws Exception {
CsrfFilter.class)
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.sessionAuthenticationStrategy(sessionAuthenticationStrategy()).and().authorizeRequests()
.antMatchers(SpringFoxConfig.WHITE_LIST).permitAll().antMatchers("/messages/key")
.hasAuthority(Authority.TECHNICAL_DEFAULT).antMatchers("/messages")
.antMatchers(SpringFoxConfig.WHITE_LIST).permitAll()
.antMatchers("/messages/key", "/messages/furthersteps/new")
.hasAuthority(Authority.TECHNICAL_DEFAULT)
.antMatchers("/messages", "/messages/draft", "/messages/videohint/new")
.hasAnyAuthority(Authority.USER_DEFAULT, Authority.CONSULTANT_DEFAULT)
.antMatchers("/messages/new")
.hasAnyAuthority(Authority.USER_DEFAULT, Authority.CONSULTANT_DEFAULT,
Authority.TECHNICAL_DEFAULT)
.antMatchers("/messages/forward").hasAnyAuthority(Authority.USE_FEEDBACK)
.antMatchers("/messages/feedback/new").hasAnyAuthority(Authority.USE_FEEDBACK)
.antMatchers("/messages/draft")
.hasAnyAuthority(Authority.USER_DEFAULT, Authority.CONSULTANT_DEFAULT)
.antMatchers("/messages/videohint/new").hasAnyAuthority(Authority.USER_DEFAULT,
Authority.CONSULTANT_DEFAULT)
.antMatchers("/messages/forward", "/messages/feedback/new")
.hasAnyAuthority(Authority.USE_FEEDBACK)
.anyRequest()
.denyAll();
}
Expand Down Expand Up @@ -108,7 +106,7 @@ public void configureGlobal(final AuthenticationManagerBuilder auth,
}

/**
* From the Keycloag documentation: "Spring Boot attempts to eagerly register filter beans with
* From the Keycloak documentation: "Spring Boot attempts to eagerly register filter beans with
* the web application context. Therefore, when running the Keycloak Spring Security adapter in a
* Spring Boot environment, it may be necessary to add FilterRegistrationBeans to your security
* configuration to prevent the Keycloak filters from being registered twice."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class MessageControllerAuthorizationTestIT {
protected final static String PATH_POST_CREATE_MESSAGE = "/messages/new";
protected final static String PATH_POST_CREATE_FEEDBACK_MESSAGE = "/messages/feedback/new";
protected final static String PATH_POST_CREATE_VIDEO_HINT_MESSAGE = "/messages/videohint/new";
protected final static String PATH_POST_CREATE_FURTHER_STEPS_MESSAGE = "/messages/furthersteps/new";
protected final static String PATH_POST_UPDATE_KEY = "/messages/key";
protected final static String PATH_POST_FORWARD_MESSAGE = "/messages/forward";
private final static String CSRF_COOKIE = "CSRF-TOKEN";
Expand Down Expand Up @@ -356,4 +357,64 @@ public void createVideoHintMessage_Should_ReturnCreatedAndCallPostGroupMessageFa
verify(postGroupMessageFacade, times(1)).createVideoHintMessage(any(), any());
}

@Test
public void saveFurtherStepsMessage_Should_ReturnUnauthorizedAndCallNoMethods_When_NoKeycloakAuthorization()
throws Exception {
mvc.perform(
post(PATH_POST_CREATE_FURTHER_STEPS_MESSAGE)
.cookie(csrfCookie)
.header(CSRF_HEADER, CSRF_VALUE)
.header("rcGroupId", RC_GROUP_ID)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isUnauthorized());

verifyNoMoreInteractions(postGroupMessageFacade);
}

@Test
@WithMockUser
public void saveFurtherStepsMessage_Should_ReturnForbiddenAndCallNoMethods_When_NoTechnicalUserAuthority()
throws Exception {
mvc.perform(
post(PATH_POST_CREATE_FURTHER_STEPS_MESSAGE)
.cookie(csrfCookie)
.header(CSRF_HEADER, CSRF_VALUE)
.header("rcGroupId", RC_GROUP_ID)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isForbidden());

verifyNoMoreInteractions(postGroupMessageFacade);
}

@Test
@WithMockUser(authorities = {Authority.TECHNICAL_DEFAULT})
public void saveFurtherStepsMessage_Should_ReturnForbiddenAndCallNoMethods_When_NoCsrfTokens()
throws Exception {
mvc.perform(
post(PATH_POST_CREATE_FURTHER_STEPS_MESSAGE)
.header("rcGroupId", RC_GROUP_ID)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isForbidden());

verifyNoMoreInteractions(postGroupMessageFacade);
}

@Test
@WithMockUser(authorities = {Authority.TECHNICAL_DEFAULT})
public void saveFurtherStepsMessage_Should_ReturnCreatedAndCallPostGroupMessageFacade_When_TechnicalUserAuthority()
throws Exception {
mvc.perform(
post(PATH_POST_CREATE_FURTHER_STEPS_MESSAGE)
.cookie(csrfCookie)
.header(CSRF_HEADER, CSRF_VALUE)
.header("rcGroupId", RC_GROUP_ID)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated());

verify(postGroupMessageFacade, times(1)).postFurtherStepsMessage(any());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static de.caritas.cob.messageservice.api.controller.MessageControllerAuthorizationTestIT.PATH_GET_MESSAGE_STREAM;
import static de.caritas.cob.messageservice.api.controller.MessageControllerAuthorizationTestIT.PATH_POST_CREATE_FEEDBACK_MESSAGE;
import static de.caritas.cob.messageservice.api.controller.MessageControllerAuthorizationTestIT.PATH_POST_CREATE_FURTHER_STEPS_MESSAGE;
import static de.caritas.cob.messageservice.api.controller.MessageControllerAuthorizationTestIT.PATH_POST_CREATE_MESSAGE;
import static de.caritas.cob.messageservice.api.controller.MessageControllerAuthorizationTestIT.PATH_POST_CREATE_VIDEO_HINT_MESSAGE;
import static de.caritas.cob.messageservice.api.controller.MessageControllerAuthorizationTestIT.PATH_POST_FORWARD_MESSAGE;
Expand Down Expand Up @@ -548,4 +549,28 @@ public void createVideoHintMessage_Should_ReturnCreated_When_paramsAreValid()
verify(this.postGroupMessageFacade, times(1)).createVideoHintMessage(any(), any());
}

@Test
public void saveFurtherStepsMessage_Should_ReturnBadRequest_When_rcGroupIdIsMissing()
throws Exception {
mvc.perform(
post(PATH_POST_CREATE_FURTHER_STEPS_MESSAGE)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest());

verifyNoInteractions(this.postGroupMessageFacade);
}

@Test
public void saveFurtherStepsMessage_Should_ReturnCreated_When_paramsAreValid()
throws Exception {
mvc.perform(
post(PATH_POST_CREATE_FURTHER_STEPS_MESSAGE)
.header("rcGroupId", RC_GROUP_ID)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated());

verify(this.postGroupMessageFacade, times(1)).postFurtherStepsMessage(RC_GROUP_ID);
}
}
Loading

0 comments on commit 2a87bb9

Please sign in to comment.