Skip to content

Commit

Permalink
Merge pull request #105 from mndzielski/POM-436-Recaptcha-in-dto-object
Browse files Browse the repository at this point in the history
POM-436 - Recaptcha in DTO object
  • Loading branch information
mndzielski authored Mar 29, 2022
2 parents 9b4cf62 + d9dcb28 commit 60d00eb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ public class MessageResource {
private final CaptchaValidator captchaValidator;

@PostMapping
public ResponseEntity<Void> sendMessage(@Valid @RequestBody SendMessageDTO messageDefinition,
@RequestParam(value = "recaptcha-response", required = false) final String recaptchaResponse) {
public ResponseEntity<Void> sendMessage(@Valid @RequestBody SendMessageDTO messageDefinition) {

if (!captchaValidator.validate(recaptchaResponse)) {
if (!captchaValidator.validate(messageDefinition.recaptchaResponse)) {
throw new CaptchaException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,26 @@ public final class SendMessageDTO {

@NotNull
public final Long offerId;

public final String text;

@NotEmpty
@Email(regexp = EMAIL_REGEX)
public final String replyEmail;

@NotNull
@AssertTrue(message = "{terms-and-conditions.validation}")
public final boolean tosApproved;

public final String recaptchaResponse;

public SendMessageDTO(Long offerId, String text, String replyEmail,
boolean tosApproved) {
boolean tosApproved, String recaptchaResponse) {
this.offerId = offerId;
this.text = text;
this.replyEmail = replyEmail;
this.tosApproved = tosApproved;
this.recaptchaResponse = recaptchaResponse;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ void shouldSendEmailToOfferCreator() throws Exception {
offer.id,
"message body",
"[email protected]",
true
true,
""
), Void.class);

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
Expand Down Expand Up @@ -136,7 +137,8 @@ void shouldFailWhenEmailPrefixIsIncorrect() {
offer.id,
"message body",
".email@[email protected]",
true
true,
""
), Void.class);

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
Expand All @@ -152,7 +154,8 @@ void shouldFailWhenEmailPrefixIsIncorrect_andWhenHeaderLanguageIsPL_expectPolish
offer.id,
"message body",
".email@[email protected]",
true
true,
""
), headers);

ResponseEntity<ErrorResponse> response = restTemplate.postForEntity("/api/message", entity, ErrorResponse.class);
Expand All @@ -175,7 +178,8 @@ void shouldFailWhenEmailPrefixIsIncorrect_andWhenHeaderLanguageIsEn_expectEnglis
offer.id,
"message body",
".email@[email protected]",
true
true,
""
), headers);

ResponseEntity<ErrorResponse> response = restTemplate.postForEntity("/api/message", entity, ErrorResponse.class);
Expand All @@ -196,7 +200,8 @@ void shouldFailWhenEmailSuffixIsIncorrect() {
offer.id,
"message body",
"email@invalid",
true
true,
""
), Void.class);

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
Expand All @@ -210,7 +215,8 @@ void shouldFailWithoutAcceptingToS() {
offer.id,
"abc",
"[email protected]",
false
false,
""
), Void.class);

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
Expand All @@ -221,7 +227,8 @@ void shouldFailWithoutOfferId() {
null,
"abc",
"[email protected]",
true
true,
""
), Void.class);

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
Expand All @@ -235,7 +242,8 @@ void shouldFailWithoutEmail() {
offer.id,
"text",
"",
true
true,
""
), Void.class);

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
Expand All @@ -248,7 +256,8 @@ void shouldFailWhenOfferAuthorDoesNotExist() {
offer.id,
"text",
"[email protected]",
true
true,
""
), Void.class);

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
Expand All @@ -261,7 +270,8 @@ void shouldFailForNotExistingOffer() {
1L,
"text",
"[email protected]",
true
true,
""
), Void.class);

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
Expand Down

0 comments on commit 60d00eb

Please sign in to comment.