Search in sources :

Example 91 with ApiGatewayRequestEvent

use of com.formkiq.lambda.apigateway.ApiGatewayRequestEvent in project formkiq-core by formkiq.

the class ApiPublicWebhooksRequestTest method testPostWebhooks03.

/**
 * Post /public/webhooks with NO BODY.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testPostWebhooks03() throws Exception {
    // givens
    ApiGatewayRequestEvent event = toRequestEvent("/request-post-public-webhooks03.json");
    event.getRequestContext().setAuthorizer(new HashMap<>());
    event.getRequestContext().setIdentity(new HashMap<>());
    // when
    String response = handleRequest(event);
    // then
    Map<String, String> m = fromJson(response, Map.class);
    verifyHeaders(m, "400.0");
}
Also used : ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Test(org.junit.jupiter.api.Test)

Example 92 with ApiGatewayRequestEvent

use of com.formkiq.lambda.apigateway.ApiGatewayRequestEvent in project formkiq-core by formkiq.

the class ApiPublicWebhooksRequestTest method testPostWebhooks11.

/**
 * Post /public/webhooks application/json, INVALID JSON body.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testPostWebhooks11() throws Exception {
    // given
    createApiRequestHandler(getMap());
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        String name = UUID.randomUUID().toString();
        String id = getAwsServices().webhookService().saveWebhook(siteId, name, "joe", null, "true");
        ApiGatewayRequestEvent event = toRequestEvent("/request-post-public-webhooks06.json", siteId, id);
        Map<String, String> headers = new HashMap<>(event.getHeaders());
        headers.put("Content-Type", "application/json");
        event.setHeaders(headers);
        // when
        String response = handleRequest(event);
        // then
        Map<String, String> m = fromJson(response, Map.class);
        verifyHeaders(m, "400.0");
        assertEquals("{\"message\":\"body isn't valid JSON\"}", m.get("body"));
    }
}
Also used : HashMap(java.util.HashMap) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Test(org.junit.jupiter.api.Test)

Example 93 with ApiGatewayRequestEvent

use of com.formkiq.lambda.apigateway.ApiGatewayRequestEvent in project formkiq-core by formkiq.

the class ApiPublicWebhooksRequestTest method testPostWebhooks10.

/**
 * Post /public/webhooks to disabled webhook.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testPostWebhooks10() throws Exception {
    // given
    createApiRequestHandler(getMap());
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        String name = UUID.randomUUID().toString();
        String id = getAwsServices().webhookService().saveWebhook(siteId, name, "joe", null, "false");
        ApiGatewayRequestEvent event = toRequestEvent("/request-post-public-webhooks01.json", siteId, id);
        // when
        String response = handleRequest(event);
        // then
        Map<String, String> m = fromJson(response, Map.class);
        verifyHeaders(m, "429.0");
    }
}
Also used : ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Test(org.junit.jupiter.api.Test)

Example 94 with ApiGatewayRequestEvent

use of com.formkiq.lambda.apigateway.ApiGatewayRequestEvent in project formkiq-core by formkiq.

the class ApiPublicWebhooksRequestTest method testPostWebhooks13.

/**
 * Post /public/webhooks with Idempotency-Key.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testPostWebhooks13() throws Exception {
    // given
    createApiRequestHandler(getMap());
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        String idempotencyKey = UUID.randomUUID().toString();
        String name = UUID.randomUUID().toString();
        String id = getAwsServices().webhookService().saveWebhook(siteId, name, "joe", null, "true");
        ApiGatewayRequestEvent event = toRequestEvent("/request-post-public-webhooks01.json", siteId, id);
        event.addHeader("Idempotency-Key", idempotencyKey);
        // when
        String response = handleRequest(event);
        // then
        Map<String, String> m = fromJson(response, Map.class);
        verifyHeaders(m, "200.0");
        String documentId = verifyDocumentId(m);
        verifyS3File(id, siteId, documentId, name, null, false);
        String key = SiteIdKeyGenerator.createDatabaseKey(siteId, "idkey#" + idempotencyKey);
        assertEquals(documentId, getAwsServices().documentCacheService().read(key));
        // given
        // when
        response = handleRequest(event);
        // then
        m = fromJson(response, Map.class);
        verifyHeaders(m, "200.0");
        try (S3Client s3 = getS3().buildClient()) {
            String s3key = createDatabaseKey(siteId, documentId + FORMKIQ_DOC_EXT);
            S3ObjectMetadata om = getS3().getObjectMetadata(s3, STAGE_BUCKET_NAME, s3key);
            assertFalse(om.isObjectExists());
        }
    }
}
Also used : ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) S3ObjectMetadata(com.formkiq.aws.s3.S3ObjectMetadata) S3Client(software.amazon.awssdk.services.s3.S3Client) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 95 with ApiGatewayRequestEvent

use of com.formkiq.lambda.apigateway.ApiGatewayRequestEvent in project formkiq-core by formkiq.

the class ApiRequestHandlerTest method testHandleGetRequest05.

/**
 * Get Document Request, Document found.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetRequest05() throws Exception {
    // given
    Date date = new Date();
    String documentId = "1a1d1938-451e-4e20-bf95-e0e7a749505a";
    String userId = "jsmith";
    DocumentItem item = new DocumentItemDynamoDb(documentId, date, userId);
    getDocumentService().saveDocument(null, item, new ArrayList<>());
    ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid02.json");
    // when
    String response = handleRequest(event);
    // then
    Map<String, String> m = fromJson(response, Map.class);
    final int mapsize = 3;
    assertEquals(mapsize, m.size());
    assertEquals("200.0", String.valueOf(m.get("statusCode")));
    assertEquals(getHeaders(), "\"headers\":" + GsonUtil.getInstance().toJson(m.get("headers")));
    DynamicObject resp = new DynamicObject(fromJson(m.get("body"), Map.class));
    assertEquals(documentId, resp.get("documentId"));
    assertEquals(userId, resp.get("userId"));
    assertNotNull(documentId, resp.get("insertedDate"));
    assertEquals(DEFAULT_SITE_ID, resp.get("siteId"));
    assertNull(resp.get("next"));
    assertNull(resp.get("previous"));
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Map(java.util.Map) Date(java.util.Date) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb) Test(org.junit.jupiter.api.Test)

Aggregations

ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)131 Test (org.junit.jupiter.api.Test)121 Date (java.util.Date)49 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)46 Map (java.util.Map)45 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)34 DocumentItemDynamoDb (com.formkiq.stacks.dynamodb.DocumentItemDynamoDb)28 HashMap (java.util.HashMap)13 List (java.util.List)11 S3Client (software.amazon.awssdk.services.s3.S3Client)10 ZonedDateTime (java.time.ZonedDateTime)9 ApiMessageResponse (com.formkiq.lambda.apigateway.ApiMessageResponse)8 LambdaLogger (com.amazonaws.services.lambda.runtime.LambdaLogger)7 ApiAuthorizer (com.formkiq.lambda.apigateway.ApiAuthorizer)7 ApiGatewayRequestEventUtil (com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil)7 ApiGatewayRequestHandler (com.formkiq.lambda.apigateway.ApiGatewayRequestHandler)7 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)7 SC_OK (com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK)7 AwsServiceCache (com.formkiq.lambda.apigateway.AwsServiceCache)7 LocalDate (java.time.LocalDate)6