|
| 1 | +import { after, before, describe, it, mock } from 'node:test'; |
| 2 | +import fsp from 'fs/promises'; |
| 3 | +import * as path from 'path'; |
| 4 | +import yargs, { CommandModule } from 'yargs'; |
| 5 | +import { TestCommandRunner } from '../../../test-utils/command_runner.js'; |
| 6 | +import { printer } from '@aws-amplify/cli-core'; |
| 7 | +import { CommandMiddleware } from '../../../command_middleware.js'; |
| 8 | +import { SandboxSeedGeneratePolicyCommand } from './sandbox_seed_policy_command.js'; |
| 9 | +import assert from 'node:assert'; |
| 10 | +import { BackendIdentifier } from '@aws-amplify/plugin-types'; |
| 11 | +import { SandboxBackendIdResolver } from '../sandbox_id_resolver.js'; |
| 12 | +import { Effect, PolicyDocument, PolicyStatement } from 'aws-cdk-lib/aws-iam'; |
| 13 | +import { generateSeedPolicyTemplate } from '../../../seed-policy-generation/generate_seed_policy_template.js'; |
| 14 | + |
| 15 | +const seedFileContents = 'console.log(`seed has been run`);'; |
| 16 | + |
| 17 | +const testBackendNameSpace = 'testSandboxId'; |
| 18 | +const testSandboxName = 'testSandboxName'; |
| 19 | + |
| 20 | +const testBackendId: BackendIdentifier = { |
| 21 | + namespace: testBackendNameSpace, |
| 22 | + name: testSandboxName, |
| 23 | + type: 'sandbox', |
| 24 | +}; |
| 25 | + |
| 26 | +void describe('sandbox policy seed command', () => { |
| 27 | + let commandRunner: TestCommandRunner; |
| 28 | + |
| 29 | + const commandMiddleware = new CommandMiddleware(printer); |
| 30 | + const mockHandleProfile = mock.method( |
| 31 | + commandMiddleware, |
| 32 | + 'ensureAwsCredentialAndRegion', |
| 33 | + () => null, |
| 34 | + ); |
| 35 | + |
| 36 | + let amplifySeedDir: string; |
| 37 | + let fullPath: string; |
| 38 | + |
| 39 | + const sandboxIdResolver: SandboxBackendIdResolver = { |
| 40 | + resolve: () => Promise.resolve(testBackendId), |
| 41 | + } as SandboxBackendIdResolver; |
| 42 | + |
| 43 | + const seedPolicyMock = mock.fn( |
| 44 | + generateSeedPolicyTemplate, |
| 45 | + (): Promise<PolicyDocument> => |
| 46 | + Promise.resolve( |
| 47 | + new PolicyDocument({ |
| 48 | + statements: [ |
| 49 | + new PolicyStatement({ |
| 50 | + effect: Effect.ALLOW, |
| 51 | + actions: [ |
| 52 | + 'cognito-idp:AdminCreateUser', |
| 53 | + 'cognito-idp:AdminAddUserToGroup', |
| 54 | + ], |
| 55 | + resources: ['test-arn'], |
| 56 | + }), |
| 57 | + ], |
| 58 | + }), |
| 59 | + ), |
| 60 | + ); |
| 61 | + |
| 62 | + before(async () => { |
| 63 | + const sandboxSeedGeneratePolicyCommand = |
| 64 | + new SandboxSeedGeneratePolicyCommand(sandboxIdResolver, seedPolicyMock); |
| 65 | + |
| 66 | + const parser = yargs().command( |
| 67 | + sandboxSeedGeneratePolicyCommand as unknown as CommandModule, |
| 68 | + ); |
| 69 | + commandRunner = new TestCommandRunner(parser); |
| 70 | + mockHandleProfile.mock.resetCalls(); |
| 71 | + |
| 72 | + await fsp.mkdir(path.join(process.cwd(), 'amplify', 'seed'), { |
| 73 | + recursive: true, |
| 74 | + }); |
| 75 | + |
| 76 | + amplifySeedDir = path.join(process.cwd(), 'amplify'); |
| 77 | + fullPath = path.join(process.cwd(), 'amplify', 'seed', 'seed.ts'); |
| 78 | + await fsp.writeFile(fullPath, seedFileContents, 'utf8'); |
| 79 | + }); |
| 80 | + |
| 81 | + after(async () => { |
| 82 | + await fsp.rm(amplifySeedDir, { recursive: true, force: true }); |
| 83 | + if (process.env.AMPLIFY_BACKEND_IDENTIFIER) { |
| 84 | + delete process.env.AMPLIFY_BACKEND_IDENTIFIER; |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + void it('runs seed policy command without identifier', async () => { |
| 89 | + const output = await commandRunner.runCommand( |
| 90 | + 'sandbox seed generate-policy', |
| 91 | + ); |
| 92 | + |
| 93 | + assert.ok(output !== undefined); |
| 94 | + }); |
| 95 | + |
| 96 | + void it('runs seed policy command with identifier', async () => { |
| 97 | + const output = await commandRunner.runCommand( |
| 98 | + 'sandbox seed generate-policy --identifier app-name', |
| 99 | + ); |
| 100 | + |
| 101 | + assert.ok(output !== undefined); |
| 102 | + }); |
| 103 | +}); |
0 commit comments