mirror of
https://github.com/sismics/docs.git
synced 2025-12-13 01:36:18 +00:00
#243: call webhooks
This commit is contained in:
@@ -67,6 +67,7 @@ public class WebhookResource extends BaseResource {
|
||||
* Add a webhook.
|
||||
*
|
||||
* @api {put} /webhook Add a webhook
|
||||
* @apiDescription Each time the specified event is raised, the webhook URL will be POST-ed with the following JSON payload: {"event": "Event name", "id": "ID of the document or file"}
|
||||
* @apiName PutWebhook
|
||||
* @apiWebhook Webhook
|
||||
* @apiParam {String="DOCUMENT_CREATED","DOCUMENT_UPDATED","DOCUMENT_DELETED","FILE_CREATED","FILE_UPDATED","FILE_DELETED"} event Event
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.sismics.docs.rest;
|
||||
|
||||
import com.sismics.docs.rest.resource.ThirdPartyWebhookResource;
|
||||
import com.sismics.util.filter.TokenBasedSecurityFilter;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
@@ -8,6 +9,7 @@ import javax.json.JsonArray;
|
||||
import javax.json.JsonObject;
|
||||
import javax.ws.rs.client.Entity;
|
||||
import javax.ws.rs.core.Form;
|
||||
import java.util.Date;
|
||||
|
||||
|
||||
/**
|
||||
@@ -23,6 +25,10 @@ public class TestWebhookResource extends BaseJerseyTest {
|
||||
public void testWebhookResource() {
|
||||
// Login admin
|
||||
String adminToken = clientUtil.login("admin", "admin", false);
|
||||
|
||||
// Login webhook1
|
||||
clientUtil.createUser("webhook1");
|
||||
String webhook1Token = clientUtil.login("webhook1");
|
||||
|
||||
// Get all webhooks
|
||||
JsonObject json = target().path("/webhook")
|
||||
@@ -37,7 +43,21 @@ public class TestWebhookResource extends BaseJerseyTest {
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
|
||||
.put(Entity.form(new Form()
|
||||
.param("event", "DOCUMENT_CREATED")
|
||||
.param("url", "https://www.sismics.com")), JsonObject.class);
|
||||
.param("url", "http://localhost:" + getPort() + "/docs/thirdpartywebhook")), JsonObject.class);
|
||||
|
||||
// Create a document
|
||||
json = target().path("/document").request()
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, webhook1Token)
|
||||
.put(Entity.form(new Form()
|
||||
.param("title", "Webhook document 1")
|
||||
.param("language", "eng")
|
||||
.param("create_date", Long.toString(new Date().getTime()))), JsonObject.class);
|
||||
String document1Id = json.getString("id");
|
||||
|
||||
// Check the webhook payload
|
||||
JsonObject payload = ThirdPartyWebhookResource.getLastPayload();
|
||||
Assert.assertEquals("DOCUMENT_CREATED", payload.getString("event"));
|
||||
Assert.assertEquals(document1Id, payload.getString("id"));
|
||||
|
||||
// Get all webhooks
|
||||
json = target().path("/webhook")
|
||||
@@ -49,7 +69,7 @@ public class TestWebhookResource extends BaseJerseyTest {
|
||||
JsonObject webhook = webhooks.getJsonObject(0);
|
||||
String webhookId = webhook.getString("id");
|
||||
Assert.assertEquals("DOCUMENT_CREATED", webhook.getString("event"));
|
||||
Assert.assertEquals("https://www.sismics.com", webhook.getString("url"));
|
||||
Assert.assertEquals("http://localhost:" + getPort() + "/docs/thirdpartywebhook", webhook.getString("url"));
|
||||
Assert.assertNotNull(webhook.getJsonNumber("create_date"));
|
||||
|
||||
// Delete a webhook
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.sismics.docs.rest.resource;
|
||||
|
||||
import javax.json.JsonObject;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
/**
|
||||
* Webhook REST resources.
|
||||
*
|
||||
* @author bgamard
|
||||
*/
|
||||
@Path("/thirdpartywebhook")
|
||||
public class ThirdPartyWebhookResource extends BaseResource {
|
||||
/**
|
||||
* Last payload received.
|
||||
*/
|
||||
private static JsonObject lastPayload;
|
||||
|
||||
/**
|
||||
* Add a webhook.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
@POST
|
||||
public Response webhook(JsonObject request) {
|
||||
lastPayload = request;
|
||||
return Response.ok().build();
|
||||
}
|
||||
|
||||
public static JsonObject getLastPayload() {
|
||||
return lastPayload;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user