|
| 1 | +package net.helpscout.api; |
| 2 | + |
| 3 | +import lombok.SneakyThrows; |
| 4 | +import net.helpscout.api.model.Customer; |
| 5 | +import org.junit.Test; |
| 6 | + |
| 7 | +import javax.servlet.http.HttpServletRequest; |
| 8 | +import java.io.BufferedReader; |
| 9 | +import java.io.StringReader; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.Path; |
| 12 | +import java.nio.file.Paths; |
| 13 | + |
| 14 | +import static org.hamcrest.Matchers.equalTo; |
| 15 | +import static org.junit.Assert.assertThat; |
| 16 | +import static org.junit.Assert.assertTrue; |
| 17 | +import static org.mockito.Mockito.*; |
| 18 | + |
| 19 | +/** |
| 20 | + * @Author: ivan |
| 21 | + * Date: 22.03.16 |
| 22 | + * Time: 0:33 |
| 23 | + */ |
| 24 | +public class WebhookApiTest { |
| 25 | + |
| 26 | + |
| 27 | + /** |
| 28 | + * Real webhook request contains such kind of headers: |
| 29 | + * |
| 30 | + * host: youcallback.domain.com |
| 31 | + * user-agent: Help Scout/Webhooks |
| 32 | + * accept-encoding: UTF-8 |
| 33 | + * content-type: application/json |
| 34 | + * x-helpscout-event: customer.created |
| 35 | + * x-helpscout-signature: qALfoJFZ/WVbevIxtFYKHJ86D8o= |
| 36 | + * x-forwarded-proto: http |
| 37 | + * x-forwarded-port: 80 |
| 38 | + * x-request-start: t=1458595138602884 |
| 39 | + * x-client-ip: 000.000.000.000 |
| 40 | + * content-length: 406 |
| 41 | + * x-forwarded-host: youcallback.domain.com |
| 42 | + * x-forwarded-server: youcallback.domain.com |
| 43 | + * connection: Keep-Alive |
| 44 | + */ |
| 45 | + @Test |
| 46 | + public void basicTestForCustomEventHeader() { |
| 47 | + |
| 48 | + HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); |
| 49 | + |
| 50 | + when(httpServletRequest.getHeader("X-HELPSCOUT-EVENT")).thenReturn("customer.created"); |
| 51 | + |
| 52 | + Webhook webhook = new Webhook("SecretKey", httpServletRequest); |
| 53 | + |
| 54 | + assertThat(webhook.getEventType(), equalTo("customer.created")); |
| 55 | + assertTrue(webhook.isCustomerEvent()); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Calculating of signature performed based on JSON data and compared to |
| 60 | + * data in header 'x-helpscout-signature'. |
| 61 | + * At file 'webhook_customer' located example structure of json send to webhook. |
| 62 | + */ |
| 63 | + @Test |
| 64 | + @SneakyThrows |
| 65 | + public void basicTestForCheckIfRequestIsValid() { |
| 66 | + |
| 67 | + HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); |
| 68 | + BufferedReader bufferedReader = new BufferedReader(new StringReader(readJsonDataToString("webhook_customer"))); |
| 69 | + |
| 70 | + when(httpServletRequest.getReader()).thenReturn(bufferedReader); |
| 71 | + when(httpServletRequest.getHeader("x-helpscout-signature".toUpperCase())).thenReturn("gV91IzHpvzCSLYW+/QGAxfm7KOM="); |
| 72 | + |
| 73 | + Webhook webhook = new Webhook("SecretKey", httpServletRequest); |
| 74 | + |
| 75 | + assertTrue(webhook.isValid()); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Simple test for getting customer object from Webhook class via JSON data |
| 80 | + */ |
| 81 | + @Test |
| 82 | + @SneakyThrows |
| 83 | + public void basicTestForReadingJsonData() { |
| 84 | + |
| 85 | + HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); |
| 86 | + BufferedReader bufferedReader = new BufferedReader(new StringReader(readJsonDataToString("webhook_customer"))); |
| 87 | + |
| 88 | + when(httpServletRequest.getReader()).thenReturn(bufferedReader); |
| 89 | + |
| 90 | + Webhook webhook = new Webhook("SecretKey", httpServletRequest); |
| 91 | + Customer customer = webhook.getCustomer(); |
| 92 | + |
| 93 | + assertThat(customer.getFirstName(), equalTo("First_Name")); |
| 94 | + assertThat(customer.getLastName(), equalTo("Last_Name")); |
| 95 | + assertThat( customer. getEmails(). get( 0). getValue(), equalTo( "[email protected]")); |
| 96 | + } |
| 97 | + |
| 98 | + @SneakyThrows |
| 99 | + private static String readJsonDataToString(String jsonFileName) { |
| 100 | + Path pathToJsonData = Paths.get(ClassLoader.getSystemResource("responses/" + jsonFileName + ".json").toURI()); |
| 101 | + return new String(Files.readAllBytes(pathToJsonData), "UTF-8"); |
| 102 | + } |
| 103 | +} |
0 commit comments