Search in sources :

Example 66 with DynamicObject

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"));
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) WebhooksService(com.formkiq.stacks.dynamodb.WebhooksService) Test(org.junit.jupiter.api.Test)

Example 67 with DynamicObject

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());
        }
    }
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Test(org.junit.jupiter.api.Test)

Example 68 with DynamicObject

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);
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) BadException(com.formkiq.lambda.apigateway.exception.BadException)

Example 69 with DynamicObject

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));
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) HashMap(java.util.HashMap) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse)

Example 70 with DynamicObject

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);
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject)

Aggregations

DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)93 Test (org.junit.jupiter.api.Test)55 Map (java.util.Map)46 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)39 Date (java.util.Date)26 HashMap (java.util.HashMap)17 DocumentItemDynamoDb (com.formkiq.stacks.dynamodb.DocumentItemDynamoDb)12 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)10 ZonedDateTime (java.time.ZonedDateTime)10 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)8 List (java.util.List)8 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)7 S3Client (software.amazon.awssdk.services.s3.S3Client)7 ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)6 BadException (com.formkiq.lambda.apigateway.exception.BadException)6 Collectors (java.util.stream.Collectors)6 NotFoundException (com.formkiq.lambda.apigateway.exception.NotFoundException)5 FormKiqClientV1 (com.formkiq.stacks.client.FormKiqClientV1)5 LocalDate (java.time.LocalDate)5 Test (org.junit.Test)5