-
Notifications
You must be signed in to change notification settings - Fork 3
/
ListingEventIntegrationTest.java
226 lines (190 loc) · 9.05 KB
/
ListingEventIntegrationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//------------------------------------------------------------------
// Copyright 2020 mobile.de GmbH.
// Author/Developer: Philipp Bartsch
//
// This code is licensed under MIT license (see LICENSE for details)
//------------------------------------------------------------------
package org.example.move.webhookreceiver.rest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.http.HttpStatus.UNAUTHORIZED;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import ecg.move.sellermodel.webhook.BadRequestModel;
import ecg.move.sellermodel.webhook.BadRequestModel.ErrorCodeEnum;
import ecg.move.sellermodel.webhook.SpecificError;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.example.move.IntegrationTestBase;
import org.example.move.webhookreceiver.movemodel.listing.ListingBeforeAfter;
import org.example.move.webhookreceiver.rest.hmac.HmacChecker;
import org.example.move.webhookreceiver.rest.listing.EnrichedListingEvent;
import org.example.move.webhookreceiver.rest.listing.EnrichedListingEventEnvelope;
import org.example.move.webhookreceiver.rest.listing.ListingReceiverController;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
@SuppressWarnings( {"ConstantConditions", "OptionalUsedAsFieldOrParameterType"})
@Slf4j
class ListingEventIntegrationTest extends IntegrationTestBase {
@Autowired
HmacChecker signatureChecker;
@Autowired
ObjectMapper objectMapper;
@Test
void enriched_listing_can_be_posted() throws JsonProcessingException {
// given
EnrichedListingEvent enrichedListingEvent = enrichedListingEvent().getPayload();
String listingId = enrichedListingEvent.getEntityId();
//when
ResponseEntity<?> response = postEnrichedListingCreate(enrichedListingEvent);
//then
assertThat(response.getStatusCode()).isEqualTo(OK);
assertThat(response.getHeaders().get("Link").get(0)).isEqualTo(createExpectedLinkHeader(listingId));
}
@Test
void unknown_seller_can_be_rejected() throws JsonProcessingException {
// given
EnrichedListingEvent enrichedListingEvent = enrichedListingEvent().getPayload();
enrichedListingEvent.getBeforeAfter().getNewState().getSeller().setForeignId(
ListingReceiverController.UNKNOWN_SELLER_ID);
//when
ResponseEntity<BadRequestModel> response = postEnrichedListingCreateExpecting400(enrichedListingEvent);
//then
assertThat(response.getStatusCode()).isEqualTo(BAD_REQUEST);
assertThat(response.getBody().getErrorCode()).isEqualTo(ErrorCodeEnum.OTHER_ERROR);
assertThat(response.getBody().getSpecificErrors())
.extracting(SpecificError::getErrorCode)
.containsOnly(SpecificError.ErrorCodeEnum.UNKNOWN_SELLER);
}
@Test
void inactive_seller_can_be_rejected() throws JsonProcessingException {
// given
EnrichedListingEvent enrichedListingEvent = enrichedListingEvent().getPayload();
enrichedListingEvent.getBeforeAfter().getNewState().getSeller().setForeignId(
ListingReceiverController.INACTIVE_SELLER_ID);
//when
ResponseEntity<BadRequestModel> response = postEnrichedListingCreateExpecting400(enrichedListingEvent);
//then
assertThat(response.getStatusCode()).isEqualTo(BAD_REQUEST);
assertThat(response.getBody().getErrorCode()).isEqualTo(ErrorCodeEnum.OTHER_ERROR);
assertThat(response.getBody().getSpecificErrors())
.extracting(SpecificError::getErrorCode)
.containsOnly(SpecificError.ErrorCodeEnum.INACTIVE_SELLER);
}
@Test
void hmac_is_enforced_when_provided() throws JsonProcessingException {
// given
EnrichedListingEvent enrichedListingEvent = enrichedListingEvent().getPayload();
// when
ResponseEntity<?> response = postEnrichedListingCreate(enrichedListingEvent, "broken_signature");
// then: a mismatching signature will be rejected as 400
assertThat(response.getStatusCode()).isEqualTo(UNAUTHORIZED);
}
private String createExpectedLinkHeader(String listingId) {
return String.format(
"<http://www.marketplace.com/id=%s>; rel=\"en\", "
+ "<%s>; rel=\"listing-id\"", listingId + "-received", listingId + "-received");
}
private ResponseEntity<?> postEnrichedListingCreate(EnrichedListingEvent enrichedListingEvent)
throws JsonProcessingException {
EnrichedListingEvent payload = EnrichedListingEvent.builder()
.beforeAfter(ListingBeforeAfter.builder()
.newState(enrichedListingEvent.getBeforeAfter().getNewState())
.build())
.dealer(enrichedListingEvent.getDealer())
.promotions(enrichedListingEvent.getPromotions())
.build();
return enrichedListingCall(payload, Optional.empty());
}
private ResponseEntity<BadRequestModel> postEnrichedListingCreateExpecting400(EnrichedListingEvent enrichedListingEvent)
throws JsonProcessingException {
EnrichedListingEvent payload = EnrichedListingEvent.builder()
.beforeAfter(ListingBeforeAfter.builder()
.newState(enrichedListingEvent.getBeforeAfter().getNewState())
.build())
.dealer(enrichedListingEvent.getDealer())
.promotions(enrichedListingEvent.getPromotions())
.build();
return enrichedListingCallFor400(payload);
}
private ResponseEntity<?> postEnrichedListingCreate(EnrichedListingEvent enrichedListingEvent, String hmac)
throws JsonProcessingException {
return enrichedListingCall(
EnrichedListingEvent.builder()
.beforeAfter(ListingBeforeAfter.builder()
.newState(enrichedListingEvent.getBeforeAfter().getNewState())
.build())
.dealer(enrichedListingEvent.getDealer())
.promotions(enrichedListingEvent.getPromotions())
.build(),
Optional.of(hmac)
);
}
private ResponseEntity<Void> enrichedListingCall(EnrichedListingEvent event, Optional<String> givenHmac)
throws JsonProcessingException {
EnrichedListingEventEnvelope payload = EnrichedListingEventEnvelope.builder()
.payload(event)
.eventType(WebhookEventType.ENRICHED_LISTING)
.timestamp(new Date())
.build();
String actualHmac = signatureChecker.createSignatureAsBase64String(objectMapper.writeValueAsString(payload));
return rest.exchange(
"/webhook/enriched-listing",
POST,
new HttpEntity<>(
payload,
CollectionUtils.toMultiValueMap(
Map.of("signature", List.of(givenHmac.orElse(actualHmac)))
)
),
Void.class);
}
private ResponseEntity<BadRequestModel> enrichedListingCallFor400(EnrichedListingEvent event)
throws JsonProcessingException {
EnrichedListingEventEnvelope payload = EnrichedListingEventEnvelope.builder()
.payload(event)
.eventType(WebhookEventType.ENRICHED_LISTING)
.timestamp(new Date())
.build();
String actualHmac = signatureChecker.createSignatureAsBase64String(objectMapper.writeValueAsString(payload));
return rest.exchange(
"/webhook/enriched-listing",
POST,
new HttpEntity<>(
payload,
CollectionUtils.toMultiValueMap(
Map.of("signature", List.of(actualHmac))
)
),
BadRequestModel.class);
}
private EnrichedListingEventEnvelope enrichedListingEvent() {
ObjectMapper om = new ObjectMapper();
try {
TypeReference<EnrichedListingEventEnvelope> eventWrapperTypeReference = new TypeReference<>() {
};
return om
.readValue(readResourceFile("webhook/real-enriched-listing-event.json"), eventWrapperTypeReference);
} catch (URISyntaxException | IOException e) {
throw new RuntimeException(e);
}
}
static String readResourceFile(String resource) throws URISyntaxException, IOException {
final Path resourcePath = Paths.get(ClassLoader.getSystemResource(resource).toURI());
return new String(Files.readAllBytes(resourcePath));
}
}