1+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+ // SPDX-License-Identifier: MIT-0
3+ import {
4+ sleep ,
5+ findOutputValue ,
6+ clearDatabase ,
7+ initializeDatabase ,
8+ } from './helper' ;
9+ import {
10+ EventBridgeClient ,
11+ PutEventsCommand ,
12+ } from '@aws-sdk/client-eventbridge' ;
13+ import {
14+ SFNClient ,
15+ ListExecutionsCommand ,
16+ GetExecutionHistoryCommand ,
17+ } from '@aws-sdk/client-sfn' ;
18+
19+ import ContractStatusChangedDraftEvent from '../events/eventbridge/contract_status_changed_event_contract_1_draft.json' ;
20+ import PublicationApprovalNonExistingContractEvent from '../events/eventbridge/publication_approval_requested_event_non_existing_contract.json' ;
21+ import PublicationApprovalInappropriateDescriptionEvent from '../events/eventbridge/publication_approval_requested_event_inappropriate_description.json' ;
22+ import PublicationApprovalInappropriateImagesEvent from '../events/eventbridge/publication_approval_requested_event_inappropriate_images.json' ;
23+
24+ describe ( 'Tests that failed workflow' , ( ) => {
25+ beforeAll ( async ( ) => {
26+ await clearDatabase ( ) ;
27+ await initializeDatabase ( ) ;
28+
29+ // Create a draft contract
30+ const evb = new EventBridgeClient ( {
31+ region : process . env . AWS_DEFAULT_REGION ,
32+ } ) ;
33+ await evb . send (
34+ new PutEventsCommand ( { Entries : ContractStatusChangedDraftEvent } )
35+ ) ;
36+ await sleep ( 5000 ) ;
37+ } , 30000 ) ;
38+
39+ afterAll ( async ( ) => {
40+ await clearDatabase ( ) ;
41+ } , 30000 ) ;
42+
43+ it ( 'Fails if the contract does not exist' , async ( ) => {
44+ // Arrange
45+ const evb = new EventBridgeClient ( {
46+ region : process . env . AWS_DEFAULT_REGION ,
47+ } ) ;
48+ const sfn = new SFNClient ( { region : process . env . AWS_DEFAULT_REGION } ) ;
49+
50+ // Act
51+ await evb . send (
52+ new PutEventsCommand ( {
53+ Entries : PublicationApprovalNonExistingContractEvent ,
54+ } )
55+ ) ;
56+ await sleep ( 5000 ) ;
57+ // Assert
58+ const listExecutionsCommand = new ListExecutionsCommand ( {
59+ stateMachineArn : await findOutputValue (
60+ 'uni-prop-local-properties-approval' ,
61+ 'ApprovalStateMachineArn'
62+ ) ,
63+ statusFilter : 'FAILED' ,
64+ } ) ;
65+ const sfnResp = await sfn . send ( listExecutionsCommand ) ;
66+
67+ if ( sfnResp . executions ) {
68+ const getExecutionHistory = new GetExecutionHistoryCommand ( {
69+ executionArn : sfnResp . executions [ 0 ] . executionArn ,
70+ } ) ;
71+ const sfnHistory = await sfn . send ( getExecutionHistory ) ;
72+ // filter through sfnHistory looking for an event with "type": "FailStateEntered" and stateEnteredEventDetails.name set to "NotFound"
73+ const failStateEvent = sfnHistory . events ?. find (
74+ ( event : any ) =>
75+ event . type === 'FailStateEntered' &&
76+ event . stateEnteredEventDetails ?. name === 'NotFound'
77+ ) ;
78+
79+ expect ( failStateEvent ) . toBeTruthy ( ) ;
80+ expect ( failStateEvent ?. type ) . toBe ( 'FailStateEntered' ) ;
81+ expect ( failStateEvent ?. stateEnteredEventDetails ?. name ) . toBe ( 'NotFound' ) ;
82+ } else {
83+ throw new Error ( 'No executions found' ) ;
84+ }
85+ } , 20000 ) ;
86+
87+ it ( 'Flags content moderation for inappropriate description' , async ( ) => {
88+ // Arrange
89+ const evb = new EventBridgeClient ( {
90+ region : process . env . AWS_DEFAULT_REGION ,
91+ } ) ;
92+ const sfn = new SFNClient ( { region : process . env . AWS_DEFAULT_REGION } ) ;
93+
94+ // Act
95+ await evb . send (
96+ new PutEventsCommand ( {
97+ Entries : PublicationApprovalInappropriateDescriptionEvent ,
98+ } )
99+ ) ;
100+ await sleep ( 5000 ) ;
101+ // Assert
102+ const listExecutionsCommand = new ListExecutionsCommand ( {
103+ stateMachineArn : await findOutputValue (
104+ 'uni-prop-local-properties-approval' ,
105+ 'ApprovalStateMachineArn'
106+ ) ,
107+ statusFilter : 'SUCCEEDED' ,
108+ } ) ;
109+ const sfnResp = await sfn . send ( listExecutionsCommand ) ;
110+
111+ if ( sfnResp . executions ) {
112+ const getExecutionHistory = new GetExecutionHistoryCommand ( {
113+ executionArn : sfnResp . executions [ 0 ] . executionArn ,
114+ } ) ;
115+ const sfnHistory = await sfn . send ( getExecutionHistory ) ;
116+ const failStateEvent = sfnHistory . events ?. find (
117+ ( event : any ) =>
118+ event . type === 'SucceedStateEntered' &&
119+ event . stateEnteredEventDetails ?. name === 'Declined'
120+ ) ;
121+
122+ expect ( failStateEvent ) . toBeTruthy ( ) ;
123+ expect ( failStateEvent ?. type ) . toBe ( 'SucceedStateEntered' ) ;
124+ expect ( failStateEvent ?. stateEnteredEventDetails ?. name ) . toBe ( 'Declined' ) ;
125+ } else {
126+ throw new Error ( 'No executions found' ) ;
127+ }
128+ } , 20000 ) ;
129+
130+ it ( 'Flags content moderation for inappropriate images' , async ( ) => {
131+ // Arrange
132+ const evb = new EventBridgeClient ( {
133+ region : process . env . AWS_DEFAULT_REGION ,
134+ } ) ;
135+ const sfn = new SFNClient ( { region : process . env . AWS_DEFAULT_REGION } ) ;
136+
137+ // Act
138+ await evb . send (
139+ new PutEventsCommand ( {
140+ Entries : PublicationApprovalInappropriateImagesEvent ,
141+ } )
142+ ) ;
143+ await sleep ( 5000 ) ;
144+ // Assert
145+ const listExecutionsCommand = new ListExecutionsCommand ( {
146+ stateMachineArn : await findOutputValue (
147+ 'uni-prop-local-properties-approval' ,
148+ 'ApprovalStateMachineArn'
149+ ) ,
150+ statusFilter : 'SUCCEEDED' ,
151+ } ) ;
152+ const sfnResp = await sfn . send ( listExecutionsCommand ) ;
153+
154+ if ( sfnResp . executions ) {
155+ const getExecutionHistory = new GetExecutionHistoryCommand ( {
156+ executionArn : sfnResp . executions [ 0 ] . executionArn ,
157+ } ) ;
158+ const sfnHistory = await sfn . send ( getExecutionHistory ) ;
159+ const failStateEvent = sfnHistory . events ?. find (
160+ ( event : any ) =>
161+ event . type === 'SucceedStateEntered' &&
162+ event . stateEnteredEventDetails ?. name === 'Declined'
163+ ) ;
164+
165+ expect ( failStateEvent ) . toBeTruthy ( ) ;
166+ expect ( failStateEvent ?. type ) . toBe ( 'SucceedStateEntered' ) ;
167+ expect ( failStateEvent ?. stateEnteredEventDetails ?. name ) . toBe ( 'Declined' ) ;
168+ } else {
169+ throw new Error ( 'No executions found' ) ;
170+ }
171+ } , 20000 ) ;
172+ } ) ;
0 commit comments