|
| 1 | +import { TestProjectBase } from './test_project_base.js'; |
| 2 | +import fs from 'fs/promises'; |
| 3 | +import { createEmptyAmplifyProject } from './create_empty_amplify_project.js'; |
| 4 | +import { CloudFormationClient } from '@aws-sdk/client-cloudformation'; |
| 5 | +import { TestProjectCreator } from './test_project_creator.js'; |
| 6 | +import { AmplifyClient } from '@aws-sdk/client-amplify'; |
| 7 | +import { e2eToolingClientConfig } from '../e2e_tooling_client_config.js'; |
| 8 | +import { BackendIdentifier } from '@aws-amplify/plugin-types'; |
| 9 | +import { |
| 10 | + ListGeofenceCollectionsCommand, |
| 11 | + ListKeysCommand, |
| 12 | + Location, |
| 13 | + LocationClient, |
| 14 | +} from '@aws-sdk/client-location'; |
| 15 | +import { DeployedResourcesFinder } from '../find_deployed_resource.js'; |
| 16 | +import assert from 'node:assert'; |
| 17 | + |
| 18 | +/** |
| 19 | + * Creates test project for testing new Geo backend functionality with API key support. |
| 20 | + */ |
| 21 | +export class GeoAPIKeySupportTestProjectCreator implements TestProjectCreator { |
| 22 | + readonly name = 'geo-api-key-support'; |
| 23 | + |
| 24 | + /** |
| 25 | + * Initializes project creator object |
| 26 | + */ |
| 27 | + constructor( |
| 28 | + private readonly cfnClient: CloudFormationClient = new CloudFormationClient( |
| 29 | + e2eToolingClientConfig, |
| 30 | + ), |
| 31 | + private readonly amplifyClient: AmplifyClient = new AmplifyClient( |
| 32 | + e2eToolingClientConfig, |
| 33 | + ), |
| 34 | + private readonly locationClient: LocationClient = new Location( |
| 35 | + e2eToolingClientConfig, |
| 36 | + ), |
| 37 | + private readonly resourceFinder: DeployedResourcesFinder = new DeployedResourcesFinder(), |
| 38 | + ) {} |
| 39 | + |
| 40 | + createProject = async (e2eProjectDir: string): Promise<TestProjectBase> => { |
| 41 | + const { projectName, projectRoot, projectAmplifyDir } = |
| 42 | + await createEmptyAmplifyProject(this.name, e2eProjectDir); |
| 43 | + |
| 44 | + const project = new GeoAPIKeySupportTestProject( |
| 45 | + projectName, |
| 46 | + projectRoot, |
| 47 | + projectAmplifyDir, |
| 48 | + this.cfnClient, |
| 49 | + this.amplifyClient, |
| 50 | + this.locationClient, |
| 51 | + this.resourceFinder, |
| 52 | + ); |
| 53 | + |
| 54 | + await fs.cp( |
| 55 | + project.sourceProjectAmplifyDirURL, |
| 56 | + project.projectAmplifyDirPath, |
| 57 | + { recursive: true }, |
| 58 | + ); |
| 59 | + |
| 60 | + return project; |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Creates project with geo and auth resources initialized |
| 66 | + */ |
| 67 | +export class GeoAPIKeySupportTestProject extends TestProjectBase { |
| 68 | + readonly sourceProjectDirPath = '../../src/test-projects/geo-api-key-support'; |
| 69 | + readonly sourceProjectAmplifyDirURL = new URL( |
| 70 | + `${this.sourceProjectDirPath}/amplify`, |
| 71 | + import.meta.url, |
| 72 | + ); |
| 73 | + |
| 74 | + /** |
| 75 | + * Creates an instance of a test project with Geofence Collections and API keys provisioned |
| 76 | + */ |
| 77 | + constructor( |
| 78 | + name: string, |
| 79 | + projectDirPath: string, |
| 80 | + projectAmplifyDirPath: string, |
| 81 | + cfnClient: CloudFormationClient, |
| 82 | + amplifyClient: AmplifyClient, |
| 83 | + private readonly locationClient: LocationClient, |
| 84 | + private readonly resourceFinder: DeployedResourcesFinder, |
| 85 | + ) { |
| 86 | + super( |
| 87 | + name, |
| 88 | + projectDirPath, |
| 89 | + projectAmplifyDirPath, |
| 90 | + cfnClient, |
| 91 | + amplifyClient, |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Override implementation of post deployment assertions specific to Amplify Geo tests |
| 97 | + */ |
| 98 | + override async assertPostDeployment( |
| 99 | + backendId: BackendIdentifier, |
| 100 | + ): Promise<void> { |
| 101 | + await super.assertPostDeployment(backendId); // perform regular post-deployment tests |
| 102 | + |
| 103 | + const testCollection = await this.resourceFinder.findByBackendIdentifier( |
| 104 | + backendId, |
| 105 | + 'AWS::Location::GeofenceCollection', |
| 106 | + (name) => name.includes('amplifyTestCollection'), |
| 107 | + ); |
| 108 | + |
| 109 | + const testAPIKey = await this.resourceFinder.findByBackendIdentifier( |
| 110 | + backendId, |
| 111 | + 'AWS::Location::APIKey', |
| 112 | + (name) => name.includes('amplifyTestIndexKey'), |
| 113 | + ); |
| 114 | + |
| 115 | + assert.equal(testCollection.length, 1); |
| 116 | + assert.equal(testAPIKey.length, 1); |
| 117 | + |
| 118 | + const expectedCollectionResponse = { |
| 119 | + Entries: [ |
| 120 | + { |
| 121 | + CollectionName: 'amplifyTestCollection', |
| 122 | + Description: |
| 123 | + 'This is a geofence collection setup for integration testing purposes.', |
| 124 | + CreateTime: new Date(), |
| 125 | + UpdateTime: new Date(), |
| 126 | + }, |
| 127 | + ], |
| 128 | + }; |
| 129 | + |
| 130 | + const collectionResponse = await this.locationClient.send( |
| 131 | + new ListGeofenceCollectionsCommand(), |
| 132 | + ); |
| 133 | + |
| 134 | + const hasCollection = collectionResponse.Entries?.some( |
| 135 | + (entry) => |
| 136 | + entry.CollectionName === |
| 137 | + expectedCollectionResponse.Entries[0].CollectionName && |
| 138 | + entry.Description === expectedCollectionResponse.Entries[0].Description, |
| 139 | + ); |
| 140 | + |
| 141 | + assert.ok( |
| 142 | + hasCollection, |
| 143 | + 'Expected collection not found in client response.', |
| 144 | + ); |
| 145 | + |
| 146 | + const expectedKeysResponse = { |
| 147 | + Entries: { |
| 148 | + KeyName: 'amplifyTestIndexKey', |
| 149 | + CreateTime: new Date(), |
| 150 | + UpdateTime: new Date(), |
| 151 | + Restrictions: { |
| 152 | + AllowActions: ['geo-places:Autocomplete'], |
| 153 | + }, |
| 154 | + }, |
| 155 | + }; |
| 156 | + |
| 157 | + const keyResponse = await this.locationClient.send(new ListKeysCommand()); |
| 158 | + |
| 159 | + const hasKey = keyResponse.Entries?.some( |
| 160 | + (entry) => |
| 161 | + entry.KeyName === expectedKeysResponse.Entries.KeyName && |
| 162 | + JSON.stringify(entry.Restrictions?.AllowActions) === |
| 163 | + JSON.stringify( |
| 164 | + expectedKeysResponse.Entries.Restrictions.AllowActions, |
| 165 | + ), |
| 166 | + ); |
| 167 | + |
| 168 | + assert.ok(hasKey, 'Expected key not found in client response.'); |
| 169 | + } |
| 170 | + |
| 171 | + private verifyCollectionResponse = async (expectedResponse: unknown) => { |
| 172 | + const locationResponse = await this.locationClient.send( |
| 173 | + new ListGeofenceCollectionsCommand(), |
| 174 | + ); |
| 175 | + assert.equal(locationResponse, expectedResponse); |
| 176 | + }; |
| 177 | +} |
0 commit comments