|
| 1 | +import * as path from 'path'; |
| 2 | +import { Template } from '@aws-cdk/assertions'; |
| 3 | +import * as lambda from '@aws-cdk/aws-lambda'; |
| 4 | +import * as cdk from '@aws-cdk/core'; |
| 5 | +import { Duration } from '@aws-cdk/core'; |
| 6 | +import * as appsync from '../lib'; |
| 7 | + |
| 8 | +let stack: cdk.Stack; |
| 9 | +let api: appsync.GraphqlApi; |
| 10 | + |
| 11 | +beforeEach(() => { |
| 12 | + // GIVEN |
| 13 | + stack = new cdk.Stack(); |
| 14 | + api = new appsync.GraphqlApi(stack, 'api', { |
| 15 | + name: 'api', |
| 16 | + schema: appsync.Schema.fromAsset(path.join(__dirname, 'appsync.lambda.graphql')), |
| 17 | + }); |
| 18 | +}); |
| 19 | + |
| 20 | +describe('Lambda caching config', () => { |
| 21 | + // GIVEN |
| 22 | + let func: lambda.Function; |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + func = new lambda.Function(stack, 'func', { |
| 26 | + code: lambda.Code.fromAsset(path.join(__dirname, 'verify/lambda-tutorial')), |
| 27 | + handler: 'lambda-tutorial.handler', |
| 28 | + runtime: lambda.Runtime.NODEJS_12_X, |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + test('Lambda resolver contains caching config with caching key and TTL', () => { |
| 33 | + // WHEN |
| 34 | + const lambdaDS = api.addLambdaDataSource('LambdaDS', func); |
| 35 | + |
| 36 | + lambdaDS.createResolver({ |
| 37 | + typeName: 'Query', |
| 38 | + fieldName: 'allPosts', |
| 39 | + cachingConfig: { |
| 40 | + cachingKeys: ['$context.arguments', '$context.source', '$context.identity'], |
| 41 | + ttl: Duration.seconds(300), |
| 42 | + }, |
| 43 | + }); |
| 44 | + |
| 45 | + // THEN |
| 46 | + Template.fromStack(stack).hasResourceProperties('AWS::AppSync::Resolver', { |
| 47 | + TypeName: 'Query', |
| 48 | + FieldName: 'allPosts', |
| 49 | + CachingConfig: { |
| 50 | + CachingKeys: ['$context.arguments', '$context.source', '$context.identity'], |
| 51 | + Ttl: 300, |
| 52 | + }, |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + test('Lambda resolver throws error when caching config with TTL is less than 1 second', () => { |
| 57 | + // WHEN |
| 58 | + const ttlInSconds = 0; |
| 59 | + const lambdaDS = api.addLambdaDataSource('LambdaDS', func); |
| 60 | + |
| 61 | + // THEN |
| 62 | + expect(() => { |
| 63 | + lambdaDS.createResolver({ |
| 64 | + typeName: 'Query', |
| 65 | + fieldName: 'allPosts', |
| 66 | + cachingConfig: { |
| 67 | + cachingKeys: ['$context.identity'], |
| 68 | + ttl: Duration.seconds(0), |
| 69 | + }, |
| 70 | + }); |
| 71 | + }).toThrowError(`Caching config TTL must be between 1 and 3600 seconds. Received: ${ttlInSconds}`); |
| 72 | + }); |
| 73 | + |
| 74 | + test('Lambda resolver throws error when caching config with TTL is greater than 3600 seconds', () => { |
| 75 | + // WHEN |
| 76 | + const ttlInSconds = 4200; |
| 77 | + const lambdaDS = api.addLambdaDataSource('LambdaDS', func); |
| 78 | + |
| 79 | + // THEN |
| 80 | + expect(() => { |
| 81 | + lambdaDS.createResolver({ |
| 82 | + typeName: 'Query', |
| 83 | + fieldName: 'allPosts', |
| 84 | + cachingConfig: { |
| 85 | + cachingKeys: ['$context.identity'], |
| 86 | + ttl: Duration.seconds(ttlInSconds), |
| 87 | + }, |
| 88 | + }); |
| 89 | + }).toThrowError(`Caching config TTL must be between 1 and 3600 seconds. Received: ${ttlInSconds}`); |
| 90 | + }); |
| 91 | + |
| 92 | + test('Lambda resolver throws error when caching config has invalid caching keys', () => { |
| 93 | + // WHEN |
| 94 | + const invalidCachingKeys = ['$context.metadata']; |
| 95 | + const lambdaDS = api.addLambdaDataSource('LambdaDS', func); |
| 96 | + |
| 97 | + // THEN |
| 98 | + expect(() => { |
| 99 | + lambdaDS.createResolver({ |
| 100 | + typeName: 'Query', |
| 101 | + fieldName: 'allPosts', |
| 102 | + cachingConfig: { |
| 103 | + cachingKeys: invalidCachingKeys, |
| 104 | + ttl: Duration.seconds(300), |
| 105 | + }, |
| 106 | + }); |
| 107 | + }).toThrowError(`Caching config keys must begin with $context.arguments, $context.source or $context.identity. Received: ${invalidCachingKeys}`); |
| 108 | + }); |
| 109 | +}); |
0 commit comments