Skip to content

Commit

Permalink
feat: generalised further steps call to accept any message type
Browse files Browse the repository at this point in the history
  • Loading branch information
mobo4b committed Apr 1, 2021
1 parent db7135f commit 0c587fa
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 31 deletions.
21 changes: 18 additions & 3 deletions api/messageservice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -309,18 +309,25 @@ paths:
security:
- Bearer: []

/messages/furthersteps/new:
/messages/aliasonly/new:
post:
tags:
- message-controller
summary: 'Saves a new further steps system message [Authorization: Role: user, consultant]'
operationId: saveFurtherStepsMessage
summary: 'Saves a new empty system message containing the provided message type in the alias.
[Authorization: Role: user, consultant]'
operationId: saveAliasOnlyMessage
parameters:
- name: rcGroupId
in: header
required: true
schema:
type: string
requestBody:
required: true
content:
'application/json':
schema:
$ref: '#/components/schemas/AliasOnlyMessageDTO'
responses:
201:
description: CREATED - message was successfully created
Expand Down Expand Up @@ -460,6 +467,14 @@ components:
type: string
example: "ag89h3tjkerg94t"

AliasOnlyMessageDTO:
type: object
required:
- messageType
properties:
messageType:
$ref: '#/components/schemas/MessageType'

MessageType:
type: string
enum:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import de.caritas.cob.messageservice.api.facade.PostGroupMessageFacade;
import de.caritas.cob.messageservice.api.helper.JSONHelper;
import de.caritas.cob.messageservice.api.model.AliasMessageDTO;
import de.caritas.cob.messageservice.api.model.AliasOnlyMessageDTO;
import de.caritas.cob.messageservice.api.model.ForwardMessageDTO;
import de.caritas.cob.messageservice.api.model.MasterKeyDTO;
import de.caritas.cob.messageservice.api.model.MessageDTO;
import de.caritas.cob.messageservice.api.model.MessageStreamDTO;
import de.caritas.cob.messageservice.api.model.MessageType;
import de.caritas.cob.messageservice.api.model.VideoCallMessageDTO;
import de.caritas.cob.messageservice.api.model.draftmessage.SavedDraftType;
import de.caritas.cob.messageservice.api.service.DraftMessageService;
Expand Down Expand Up @@ -190,14 +192,16 @@ public ResponseEntity<String> findDraftMessage(@RequestHeader String rcGroupId)
}

/**
* Posts a further steps message in the specified Rocket.Chat group.
* Posts an empty message which only contains an alias with the provided {@link MessageType} in
* the specified Rocket.Chat group.
*
* @param rcGroupId (required) Rocket.Chat group ID
* @param rcGroupId (required) Rocket.Chat group ID
* @param aliasOnlyMessageDTO {@link AliasOnlyMessageDTO}
* @return {@link ResponseEntity} with the {@link HttpStatus}
*/
@Override
public ResponseEntity<Void> saveFurtherStepsMessage(@RequestHeader String rcGroupId) {
postGroupMessageFacade.postFurtherStepsMessage(rcGroupId);
@Override public ResponseEntity<Void> saveAliasOnlyMessage(@RequestHeader String rcGroupId,
@Valid AliasOnlyMessageDTO aliasOnlyMessageDTO) {
postGroupMessageFacade.postAliasOnlyMessage(rcGroupId, aliasOnlyMessageDTO.getMessageType());

return new ResponseEntity<>(HttpStatus.CREATED);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,14 @@ public void createVideoHintMessage(String rcGroupId, VideoCallMessageDTO videoCa
}

/**
* Posts a further steps metadata message in the specified Rocket.Chat group.
* Posts an empty message which only contains an alias with the provided {@link MessageType} in
* the specified Rocket.Chat group.
*
* @param rcGroupId (required) Rocket.Chat group ID
* @param rcGroupId Rocket.Chat group ID
* @param messageType {@link MessageType}
*/
public void postFurtherStepsMessage(String rcGroupId) {
AliasMessageDTO aliasMessageDTO = new AliasMessageDTO().messageType(MessageType.FURTHER_STEPS);
public void postAliasOnlyMessage(String rcGroupId, MessageType messageType) {
AliasMessageDTO aliasMessageDTO = new AliasMessageDTO().messageType(messageType);
this.rocketChatService.postAliasOnlyMessageAsSystemUser(rcGroupId, aliasMessageDTO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected void configure(HttpSecurity http) throws Exception {
Authority.TECHNICAL_DEFAULT)
.antMatchers("/messages/forward", "/messages/feedback/new")
.hasAnyAuthority(Authority.USE_FEEDBACK)
.antMatchers("/messages/furthersteps/new")
.antMatchers("/messages/aliasonly/new")
.hasAuthority(Authority.USER_DEFAULT)
.anyRequest()
.denyAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import de.caritas.cob.messageservice.api.authorization.Authority;
import de.caritas.cob.messageservice.api.facade.PostGroupMessageFacade;
import de.caritas.cob.messageservice.api.model.AliasOnlyMessageDTO;
import de.caritas.cob.messageservice.api.model.VideoCallMessageDTO;
import de.caritas.cob.messageservice.api.service.EncryptionService;
import de.caritas.cob.messageservice.api.service.RocketChatService;
Expand Down Expand Up @@ -40,7 +41,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_CREATE_ALIAS_ONLY_MESSAGE = "/messages/aliasonly/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 @@ -358,14 +359,18 @@ public void createVideoHintMessage_Should_ReturnCreatedAndCallPostGroupMessageFa
}

@Test
public void saveFurtherStepsMessage_Should_ReturnUnauthorizedAndCallNoMethods_When_NoKeycloakAuthorization()
public void saveAliasOnlyMessage_Should_ReturnUnauthorizedAndCallNoMethods_When_NoKeycloakAuthorization()
throws Exception {
AliasOnlyMessageDTO aliasOnlyMessageDTO =
new EasyRandom().nextObject(AliasOnlyMessageDTO.class);

mvc.perform(
post(PATH_POST_CREATE_FURTHER_STEPS_MESSAGE)
post(PATH_POST_CREATE_ALIAS_ONLY_MESSAGE)
.cookie(csrfCookie)
.header(CSRF_HEADER, CSRF_VALUE)
.header("rcGroupId", RC_GROUP_ID)
.contentType(MediaType.APPLICATION_JSON)
.content(new ObjectMapper().writeValueAsString(aliasOnlyMessageDTO))
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isUnauthorized());

Expand All @@ -374,14 +379,18 @@ public void saveFurtherStepsMessage_Should_ReturnUnauthorizedAndCallNoMethods_Wh

@Test
@WithMockUser
public void saveFurtherStepsMessage_Should_ReturnForbiddenAndCallNoMethods_When_NoUserDefaultAuthority()
public void saveAliasOnlyMessage_Should_ReturnForbiddenAndCallNoMethods_When_NoUserDefaultAuthority()
throws Exception {
AliasOnlyMessageDTO aliasOnlyMessageDTO =
new EasyRandom().nextObject(AliasOnlyMessageDTO.class);

mvc.perform(
post(PATH_POST_CREATE_FURTHER_STEPS_MESSAGE)
post(PATH_POST_CREATE_ALIAS_ONLY_MESSAGE)
.cookie(csrfCookie)
.header(CSRF_HEADER, CSRF_VALUE)
.header("rcGroupId", RC_GROUP_ID)
.contentType(MediaType.APPLICATION_JSON)
.content(new ObjectMapper().writeValueAsString(aliasOnlyMessageDTO))
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isForbidden());

Expand All @@ -390,12 +399,16 @@ public void saveFurtherStepsMessage_Should_ReturnForbiddenAndCallNoMethods_When_

@Test
@WithMockUser(authorities = {Authority.USER_DEFAULT})
public void saveFurtherStepsMessage_Should_ReturnForbiddenAndCallNoMethods_When_NoCsrfTokens()
public void saveAliasOnlyMessage_Should_ReturnForbiddenAndCallNoMethods_When_NoCsrfTokens()
throws Exception {
AliasOnlyMessageDTO aliasOnlyMessageDTO =
new EasyRandom().nextObject(AliasOnlyMessageDTO.class);

mvc.perform(
post(PATH_POST_CREATE_FURTHER_STEPS_MESSAGE)
post(PATH_POST_CREATE_ALIAS_ONLY_MESSAGE)
.header("rcGroupId", RC_GROUP_ID)
.contentType(MediaType.APPLICATION_JSON)
.content(new ObjectMapper().writeValueAsString(aliasOnlyMessageDTO))
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isForbidden());

Expand All @@ -404,17 +417,21 @@ public void saveFurtherStepsMessage_Should_ReturnForbiddenAndCallNoMethods_When_

@Test
@WithMockUser(authorities = {Authority.USER_DEFAULT})
public void saveFurtherStepsMessage_Should_ReturnCreatedAndCallPostGroupMessageFacade_When_UserDefaultAuthority()
public void saveAliasOnlyMessage_Should_ReturnCreatedAndCallPostGroupMessageFacade_When_UserDefaultAuthority()
throws Exception {
AliasOnlyMessageDTO aliasOnlyMessageDTO =
new EasyRandom().nextObject(AliasOnlyMessageDTO.class);

mvc.perform(
post(PATH_POST_CREATE_FURTHER_STEPS_MESSAGE)
post(PATH_POST_CREATE_ALIAS_ONLY_MESSAGE)
.cookie(csrfCookie)
.header(CSRF_HEADER, CSRF_VALUE)
.header("rcGroupId", RC_GROUP_ID)
.contentType(MediaType.APPLICATION_JSON)
.content(new ObjectMapper().writeValueAsString(aliasOnlyMessageDTO))
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated());

verify(postGroupMessageFacade, times(1)).postFurtherStepsMessage(any());
verify(postGroupMessageFacade, times(1)).postAliasOnlyMessage(any(), any());
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package de.caritas.cob.messageservice.api.controller;

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_ALIAS_ONLY_MESSAGE;
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 @@ -50,9 +50,11 @@
import de.caritas.cob.messageservice.api.authorization.RoleAuthorizationAuthorityMapper;
import de.caritas.cob.messageservice.api.exception.InternalServerErrorException;
import de.caritas.cob.messageservice.api.facade.PostGroupMessageFacade;
import de.caritas.cob.messageservice.api.model.AliasOnlyMessageDTO;
import de.caritas.cob.messageservice.api.model.AttachmentDTO;
import de.caritas.cob.messageservice.api.model.FileDTO;
import de.caritas.cob.messageservice.api.model.MessageStreamDTO;
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.message.MessagesDTO;
import de.caritas.cob.messageservice.api.model.rocket.chat.message.UserDTO;
Expand Down Expand Up @@ -550,27 +552,50 @@ public void createVideoHintMessage_Should_ReturnCreated_When_paramsAreValid()
}

@Test
public void saveFurtherStepsMessage_Should_ReturnBadRequest_When_rcGroupIdIsMissing()
public void saveAliasOnlyMessage_Should_ReturnBadRequest_When_rcGroupIdIsMissing()
throws Exception {
AliasOnlyMessageDTO aliasOnlyMessageDTO =
new EasyRandom().nextObject(AliasOnlyMessageDTO.class);

mvc.perform(
post(PATH_POST_CREATE_FURTHER_STEPS_MESSAGE)
post(PATH_POST_CREATE_ALIAS_ONLY_MESSAGE)
.contentType(MediaType.APPLICATION_JSON)
.content(new ObjectMapper().writeValueAsString(aliasOnlyMessageDTO))
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest());

verifyNoInteractions(this.postGroupMessageFacade);
}

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

verifyNoInteractions(this.postGroupMessageFacade);
}

@Test
public void saveAliasOnlyMessage_Should_ReturnCreated_When_paramsAreValid()
throws Exception {
AliasOnlyMessageDTO aliasOnlyMessageDTO =
new EasyRandom().nextObject(AliasOnlyMessageDTO.class);
aliasOnlyMessageDTO.setMessageType(MessageType.FORWARD);

mvc.perform(
post(PATH_POST_CREATE_ALIAS_ONLY_MESSAGE)
.header("rcGroupId", RC_GROUP_ID)
.contentType(MediaType.APPLICATION_JSON)
.content(new ObjectMapper().writeValueAsString(aliasOnlyMessageDTO))
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated());

verify(this.postGroupMessageFacade, times(1)).postFurtherStepsMessage(RC_GROUP_ID);
verify(this.postGroupMessageFacade, times(1))
.postAliasOnlyMessage(RC_GROUP_ID, MessageType.FORWARD);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,8 @@ public void createVideoHintMessage_Should_triggerRocketChatPost_When_paramsAreGi
}

@Test
public void postFurtherStepsMessage_Should_triggerRocketChatPostWithCorrectMessageType_When_RcGroupIdIsGiven() {
this.postGroupMessageFacade.postFurtherStepsMessage(RC_GROUP_ID);
public void postAliasOnlyMessage_Should_triggerRocketChatPostWithCorrectMessageType_When_RcGroupIdIsGiven() {
this.postGroupMessageFacade.postAliasOnlyMessage(RC_GROUP_ID, MessageType.FURTHER_STEPS);

ArgumentCaptor<AliasMessageDTO> captor = ArgumentCaptor.forClass(AliasMessageDTO.class);
verify(rocketChatService).postAliasOnlyMessageAsSystemUser(anyString(), captor.capture());
Expand Down

0 comments on commit 0c587fa

Please sign in to comment.