use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.
the class ApiWebhooksRequestTest method testPostWebhooks03.
/**
* POST /webhooks with NO SiteId Set.
*
* @throws Exception an error has occurred
*/
@SuppressWarnings("unchecked")
@Test
public void testPostWebhooks03() throws Exception {
// given
putSsmParameter("/formkiq/" + FORMKIQ_APP_ENVIRONMENT + "/api/DocumentsPublicHttpUrl", "http://localhost:8080");
String siteId = UUID.randomUUID().toString();
ApiGatewayRequestEvent event = toRequestEvent("/request-post-webhooks01.json");
setCognitoGroup(event, siteId);
event.setBody("{\"name\":\"joe smith\"}");
// when
String response = handleRequest(event);
// then
Map<String, String> m = GsonUtil.getInstance().fromJson(response, Map.class);
assertEquals("200.0", String.valueOf(m.get("statusCode")));
Map<String, Object> result = GsonUtil.getInstance().fromJson(m.get("body"), Map.class);
assertEquals(siteId, result.get("siteId"));
assertNotNull(result.get("id"));
final String id = result.get("id").toString();
assertNotNull(result.get("insertedDate"));
assertEquals("joe smith", result.get("name"));
assertEquals("test@formkiq.com", result.get("userId"));
verifyUrl(siteId, id, result);
WebhooksService webhookService = getAwsServices().webhookService();
DynamicObject obj = webhookService.findWebhook(siteId, id);
assertEquals("joe smith", obj.getString("path"));
assertEquals("test@formkiq.com", obj.getString("userId"));
}
use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.
the class ApiWebhooksRequestTest method testPostWebhooks04.
/**
* POST /webhooks too many webhooks.
*
* @throws Exception an error has occurred
*/
@SuppressWarnings("unchecked")
@Test
public void testPostWebhooks04() throws Exception {
// given
for (String maxWebHooks : Arrays.asList("2", "0")) {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
if (!"0".equals(maxWebHooks)) {
getAwsServices().configService().save(siteId, new DynamicObject(Map.of(MAX_WEBHOOKS, maxWebHooks)));
} else {
getAwsServices().configService().save(null, new DynamicObject(Map.of(MAX_WEBHOOKS, maxWebHooks)));
}
String response = null;
for (int i = 0; i <= 2; i++) {
ApiGatewayRequestEvent event = toRequestEvent("/request-post-webhooks01.json");
addParameter(event, "siteId", siteId);
event.setBody("{\"name\":\"john smith\"}");
// when
response = handleRequest(event);
}
// then
Map<String, String> m = GsonUtil.getInstance().fromJson(response, Map.class);
assertEquals("429.0", String.valueOf(m.get("statusCode")));
assertEquals("{\"message\":\"Reached max number of webhooks\"}", m.get("body").toString());
}
}
}
use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.
the class PublicWebhooksRequestHandler method post.
@Override
public ApiRequestHandlerResponse post(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
String siteId = getParameter(event, "siteId");
String webhookId = getPathParameter(event, "webhooks");
DynamicObject hook = awsservice.webhookService().findWebhook(siteId, webhookId);
checkIsWebhookValid(hook);
String body = ApiGatewayRequestEventUtil.getBodyAsString(event);
String contentType = getContentType(event);
if (isContentTypeJson(contentType) && !isJsonValid(body)) {
throw new BadException("body isn't valid JSON");
}
DynamicObject item = buildDynamicObject(awsservice, siteId, webhookId, hook, body, contentType);
if (!isIdempotencyCached(awsservice, event, siteId, item)) {
putObjectToStaging(logger, awsservice, item, siteId);
}
return buildResponse(event, item);
}
use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.
the class WebhooksIdRequestHandler method get.
@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsServices) throws Exception {
String siteId = authorizer.getSiteId();
String id = getPathParameter(event, "webhookId");
DynamicObject m = awsServices.webhookService().findWebhook(siteId, id);
if (m == null) {
throw new NotFoundException("Webhook 'id' not found");
}
String url = awsServices.ssmService().getParameterValue("/formkiq/" + awsServices.appEnvironment() + "/api/DocumentsPublicHttpUrl");
String path = "private".equals(m.getString("enabled")) ? "/private" : "/public";
String u = url + path + "/webhooks/" + m.getString("documentId");
if (siteId != null && !DEFAULT_SITE_ID.equals(siteId)) {
u += "?siteId=" + siteId;
}
Map<String, Object> map = new HashMap<>();
map.put("siteId", siteId != null ? siteId : DEFAULT_SITE_ID);
map.put("id", m.getString("documentId"));
map.put("name", m.getString("path"));
map.put("url", u);
map.put("insertedDate", m.getString("inserteddate"));
map.put("userId", m.getString("userId"));
map.put("enabled", m.getString("enabled"));
map.put("ttl", m.getString("ttl"));
return new ApiRequestHandlerResponse(SC_OK, new ApiMapResponse(map));
}
use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.
the class WebhooksRequestHandler method post.
@Override
public ApiRequestHandlerResponse post(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
String siteId = authorizer.getSiteId();
DynamicObject o = fromBodyToDynamicObject(logger, event);
validatePost(logger, awsservice, siteId, o);
String id = saveWebhook(event, awsservice, siteId, o);
return response(logger, event, authorizer, awsservice, id);
}
Aggregations