-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathWebhookTest.java
More file actions
209 lines (174 loc) · 6.57 KB
/
WebhookTest.java
File metadata and controls
209 lines (174 loc) · 6.57 KB
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
package com.easypost;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import com.easypost.exception.EasyPostException;
import com.easypost.model.Event;
import com.easypost.model.Webhook;
import com.easypost.model.WebhookCustomHeader;
import com.easypost.utils.Utilities;
import com.google.common.collect.ImmutableMap;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public final class WebhookTest {
private static String testWebhookId = null;
private static TestUtils.VCR vcr;
/**
* Set up the testing environment for this file.
*
* @throws EasyPostException when the request fails.
*/
@BeforeAll
public static void setup() throws EasyPostException {
vcr = new TestUtils.VCR("webhook", TestUtils.ApiKey.TEST);
}
/**
* Clean up test attributes after each unit test.
*/
@AfterEach
public void cleanup() {
if (testWebhookId != null) {
try {
vcr.client.webhook.delete(testWebhookId);
testWebhookId = null;
} catch (Exception e) {
// in case we try to delete something that's already been deleted
}
}
}
/**
* Create a webhook.
*
* @return Webhook object
*/
private static Webhook createBasicWebhook() throws EasyPostException {
Map<String, Object> params = new HashMap<>();
params.put("url", Fixtures.webhookUrl());
params.put("webhook_secret", Fixtures.webhookSecret());
params.put("custom_headers", Fixtures.webhookCustomHeaders());
Webhook webhook = vcr.client.webhook.create(params);
testWebhookId = webhook.getId(); // trigger deletion after test
return webhook;
}
/**
* Test creating a webhook.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testCreate() throws EasyPostException {
vcr.setUpTest("create");
Webhook webhook = createBasicWebhook();
assertInstanceOf(Webhook.class, webhook);
assertTrue(webhook.getId().startsWith("hook_"));
assertEquals(Fixtures.webhookUrl(), webhook.getUrl());
WebhookCustomHeader customHeader = webhook.getCustomHeaders().get(0);
assertEquals("test", customHeader.getName());
assertEquals("header", customHeader.getValue());
}
/**
* Test retrieving a webhook.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testRetrieve() throws EasyPostException {
vcr.setUpTest("retrieve");
Webhook webhook = createBasicWebhook();
Webhook retrievedWebhook = vcr.client.webhook.retrieve(webhook.getId());
assertInstanceOf(Webhook.class, retrievedWebhook);
assertTrue(webhook.equals(retrievedWebhook));
}
/**
* Test retrieving all webhooks.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testAll() throws EasyPostException {
vcr.setUpTest("all");
createBasicWebhook();
List<Webhook> webhooks = vcr.client.webhook.all();
assertTrue(webhooks.size() > 0);
assertTrue(webhooks.stream().allMatch(webhook -> webhook != null));
// Test that deserialization worked by accessing a field with an underscore
for (Webhook webhook : webhooks) {
assertTrue(webhook.getCreatedAt() != null);
}
}
/**
* Test updating a webhook.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testUpdate() throws EasyPostException {
vcr.setUpTest("update");
Webhook webhook = createBasicWebhook();
Map<String, Object> params = new HashMap<>();
params.put("webhook_secret", Fixtures.webhookSecret());
params.put("custom_headers", Fixtures.webhookCustomHeaders());
Webhook updatedWebhook = vcr.client.webhook.update(webhook.getId(), params);
assertInstanceOf(Webhook.class, updatedWebhook);
WebhookCustomHeader customHeader = updatedWebhook.getCustomHeaders().get(0);
assertEquals("test", customHeader.getName());
assertEquals("header", customHeader.getValue());
}
/**
* Test deleting a webhook.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testDelete() throws EasyPostException {
vcr.setUpTest("delete");
Webhook webhook = createBasicWebhook();
Webhook retrievedWebhook = vcr.client.webhook.retrieve(webhook.getId());
assertDoesNotThrow(() -> vcr.client.webhook.delete(retrievedWebhook.getId()));
testWebhookId = null; // need to disable post-test deletion for test to work
}
/**
* Test validating a webhook.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testValidateWebhook() throws EasyPostException {
Map<String, Object> headers = ImmutableMap.of("X-Hmac-Signature",
Fixtures.webhookHmacSignature());
Event event = Utilities.validateWebhook(Fixtures.eventBytes(), headers, Fixtures.webhookSecret());
assertEquals("tracker.updated", event.getDescription());
assertEquals(614.4, event.getResult().get("weight")); // Ensure we convert floats properly
}
/**
* Test validating a webhook.
*/
@Test
public void testValidateWebhookInvalidSecret() {
String webhookSecret = "invalid_secret";
Map<String, Object> headers = ImmutableMap.of("X-Hmac-Signature", "some-signature");
assertThrows(EasyPostException.class, () -> {
Utilities.validateWebhook(Fixtures.eventBytes(), headers, webhookSecret);
});
}
/**
* Test validating a webhook.
*/
@Test
public void testValidateWebhookMissingSecret() {
String webhookSecret = "123";
Map<String, Object> headers = ImmutableMap.of("some-header", "some-value");
assertThrows(EasyPostException.class, () -> {
Utilities.validateWebhook(Fixtures.eventBytes(), headers, webhookSecret);
});
}
}