Skip to content

Commit

Permalink
test: update unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ren-yamanashi committed Jan 12, 2025
1 parent c12092d commit 47e8800
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 61 deletions.
6 changes: 3 additions & 3 deletions packages/aws-cdk-lib/aws-cognito/lib/user-pool-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,14 +671,14 @@ export class UserPoolClient extends Resource implements IUserPoolClient {
analytics.applicationArn &&
(analytics.applicationId || analytics.externalId || analytics.roleArn)
) {
throw new Error('Either `applicationArn` or all of `applicationId`, `externalId`, and `roleArn` must be specified.');
throw new Error('Either `applicationArn` or all of `applicationId`, `externalId` and `roleArn` must be specified.');
}

if (
!analytics.applicationArn &&
!(analytics.applicationId && analytics.externalId && analytics.roleArn)
(!analytics.applicationId || !analytics.externalId || !analytics.roleArn)
) {
throw new Error('Either all of `applicationId`, `externalId`, and `roleArn` must be specified or `applicationArn` must be specified.');
throw new Error('Either all of `applicationId`, `externalId` and `roleArn` must be specified or `applicationArn` must be specified.');
}

if (analytics.applicationArn && !Token.isUnresolved(analytics.applicationArn) && !analytics.applicationArn.startsWith('arn:')) {
Expand Down
188 changes: 130 additions & 58 deletions packages/aws-cdk-lib/aws-cognito/test/user-pool-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { create } from 'lodash';
import { Match, Template } from '../../assertions';
import { Stack, Duration } from '../../core';
import { OAuthScope, ResourceServerScope, UserPool, UserPoolClient, UserPoolClientIdentityProvider, UserPoolIdentityProvider, ClientAttributes } from '../lib';
import { OAuthScope, ResourceServerScope, UserPool, UserPoolClient, UserPoolClientIdentityProvider, UserPoolIdentityProvider, ClientAttributes, AnalyticsConfiguration } from '../lib';

describe('User Pool Client', () => {
test('default setup', () => {
Expand Down Expand Up @@ -1342,74 +1343,145 @@ describe('User Pool Client', () => {
).toThrow(`defaultRedirectUri must match the \`^(?=.{1,1024}$)[\p{L}\p{M}\p{S}\p{N}\p{P}]+$\` pattern, got ${invalidUrl}`);
});

test('should add analytics configuration to userPoolClients ', () => {
// GIVEN
const stack = new Stack();
const pool = new UserPool(stack, 'Pool');
describe('analytics configuration', () => {
test('analytics configuration can be added to userPoolClients by specifying applicationArn', () => {
// GIVEN
const stack = new Stack();
const pool = new UserPool(stack, 'Pool');

// WHEN
new UserPoolClient(stack, 'Client', {
userPool: pool,
analytics: {
applicationArn: 'arn:aws:pinpoint:us-east-1:123456789012:application/abc123',
applicationId: '123456789012',
externalId: '123456789012',
roleArn: 'arn:aws:iam::123456789012:role/TestRole',
shareUserData: true,
},
// WHEN
new UserPoolClient(stack, 'Client', {
userPool: pool,
analytics: {
applicationArn: 'arn:aws:pinpoint:us-east-1:123456789012:application/abc123',
shareUserData: true,
},
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Cognito::UserPoolClient', {
AnalyticsConfiguration: {
ApplicationArn: 'arn:aws:pinpoint:us-east-1:123456789012:application/abc123',
UserDataShared: true,
},
});
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Cognito::UserPoolClient', {
AnalyticsConfiguration: {
ApplicationArn: 'arn:aws:pinpoint:us-east-1:123456789012:application/abc123',
ApplicationId: '123456789012',
ExternalId: '123456789012',
RoleArn: 'arn:aws:iam::123456789012:role/TestRole',
UserDataShared: true,
},
test('analytics configuration can be added to userPoolClients by specifying applicationId, externalId, and roleArn', () => {
// GIVEN
const stack = new Stack();
const pool = new UserPool(stack, 'Pool');

// WHEN
new UserPoolClient(stack, 'Client', {
userPool: pool,
analytics: {
applicationId: '12345678-1234-1234-1234-123456789012',
externalId: 'sample-external-id',
roleArn: 'arn:aws:iam::123456789012:role/TestRole',
shareUserData: true,
},
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Cognito::UserPoolClient', {
AnalyticsConfiguration: {
ApplicationId: '12345678-1234-1234-1234-123456789012',
ExternalId: 'sample-external-id',
RoleArn: 'arn:aws:iam::123456789012:role/TestRole',
UserDataShared: true,
},
});
});
});

test('throws an error when applicationArn is not an ARN', () => {
// GIVEN
const stack = new Stack();
const pool = new UserPool(stack, 'Pool');
test('throws an error when applicationArn is not an ARN', () => {
// GIVEN
const stack = new Stack();
const pool = new UserPool(stack, 'Pool');

// WHEN
const createUserPoolClient = () => new UserPoolClient(stack, 'Client', {
userPool: pool,
analytics: {
applicationArn: 'invalid-arn',
applicationId: '123456789012',
externalId: '123456789012',
roleArn: 'arn:aws:iam::123456789012:role/TestRole',
shareUserData: true,
},
// WHEN
const createUserPoolClient = () => new UserPoolClient(stack, 'Client', {
userPool: pool,
analytics: {
applicationArn: 'invalid-arn',
shareUserData: true,
},
});

// THEN
expect(() => createUserPoolClient()).toThrow('applicationArn must be start with "arn:"; received invalid-arn');
});

// THEN
expect(() => createUserPoolClient()).toThrow('applicationArn must be start with "arn:"; received invalid-arn');
});
test('throws an error when roleArn is not an ARN', () => {
// GIVEN
const stack = new Stack();
const pool = new UserPool(stack, 'Pool');

test('throws an error when roleArn is not an ARN', () => {
// GIVEN
const stack = new Stack();
const pool = new UserPool(stack, 'Pool');
// WHEN
const createUserPoolClient = () => new UserPoolClient(stack, 'Client', {
userPool: pool,
analytics: {
applicationId: '12345678-1234-1234-1234-123456789012',
externalId: 'sample-external-id',
roleArn: 'invalid-arn',
shareUserData: true,
},
});

// WHEN
const createUserPoolClient = () => new UserPoolClient(stack, 'Client', {
userPool: pool,
analytics: {
applicationArn: 'arn:aws:pinpoint:us-east-1:123456789012:application/abc123',
applicationId: '123456789012',
externalId: '123456789012',
roleArn: 'invalid-arn',
shareUserData: true,
},
// THEN
expect(() => createUserPoolClient()).toThrow('roleArn must be start with "arn:"; received invalid-arn');
});

// THEN
expect(() => createUserPoolClient()).toThrow('roleArn must be start with "arn:"; received invalid-arn');
test('throws an error when both applicationArn and applicationId are specified', () => {
// GIVEN
const stack = new Stack();
const pool = new UserPool(stack, 'Pool');

// WHEN
const createUserPoolClient = () => new UserPoolClient(stack, 'PoolClient', {
userPool: pool,
analytics: {
applicationArn: 'arn:aws:pinpoint:us-east-1:123456789012:application/abc123',
applicationId: '12345678-1234-1234-1234-123456789012',
},
});

// THEN
expect(() => createUserPoolClient()).toThrow('Either `applicationArn` or all of `applicationId`, `externalId` and `roleArn` must be specified.');
});

test('throws an error when either applicationId, externalId or roleArn is not specified', () => {
// GIVEN
const stack = new Stack();
const pool = new UserPool(stack, 'Pool');

// WHEN
const createUserPoolClient = (id: string, analytics: AnalyticsConfiguration) => new UserPoolClient(stack, id, {
userPool: pool,
analytics,
});

// THEN
expect(
() =>
createUserPoolClient('PoolClient1', {
applicationId: '12345678-1234-1234-1234-123456789012',
}),
).toThrow('Either all of `applicationId`, `externalId` and `roleArn` must be specified or `applicationArn` must be specified.');

expect(
() =>
createUserPoolClient('PoolClient2', {
externalId: 'sample-external-id',
}),
).toThrow('Either all of `applicationId`, `externalId` and `roleArn` must be specified or `applicationArn` must be specified.');

expect(
() =>
createUserPoolClient('PoolClient3', {
roleArn: 'arn:aws:iam::123456789012:role/TestRole',
}),
).toThrow('Either all of `applicationId`, `externalId` and `roleArn` must be specified or `applicationArn` must be specified.');
});
});
});

0 comments on commit 47e8800

Please sign in to comment.