import axios from 'axios' ; import { NotificationConfig , NotificationType , SlackParameter , WebhookParameter , } from '../notification-config/notificationConfig' ; import VM2SandboxExecutor from './condition-evaluation/vm2SandboxExecutor' ; import SlackCallback from './notificationCallbacks/slackCallback' ; import NotificationExecutor from './notificationExecutor' ; jest . mock ( 'axios' ) ; describe ( 'JSNotificationService' , ( ) => { describe ( 'notification system' , ( ) => { const post = axios . post as jest . Mock ; let notificationService ; let data ; beforeEach ( ( ) => { notificationService = new NotificationExecutor ( new VM2SandboxExecutor ( ) ) ; console . log = jest . fn ( ) ; data = { value1 : 1 , b : 2 } ; } ) ; afterEach ( ( ) => { jest . clearAllMocks ( ) ; } ) ; it ( 'should trigger notification when transformation failed and condition is "data == undefined' , async ( ) => { post . mockReturnValue ( Promise . resolve ( ) ) ; const webhookParameter = { url : 'callback' , } ; const notificationConfig = { id : 1 , pipelineId : 1 , condition : 'data === undefined' , type : NotificationType . WEBHOOK , parameter : webhookParameter , } ; const message = 'message' ; const dataLocation = 'location' ; await notificationService . execute ( notificationConfig , dataLocation , message , undefined ) ; expect ( post ) . toHaveBeenCalledTimes ( 1 ) ; expect ( post ) . toHaveBeenCalledWith ( webhookParameter . url , expect . objectContaining ( { location : dataLocation , message : message } ) ) ; } ) ; it ( 'should trigger notification when transformation failed and condition is "!data' , async ( ) => { post . mockReturnValue ( Promise . resolve ( ) ) ; const webhookParameter = { url : 'callback' , } ; const notificationConfig = { id : 1 , pipelineId : 1 , condition : '!data' , type : NotificationType . WEBHOOK , parameter : webhookParameter , } ; const message = 'message' ; const dataLocation = 'location' ; await notificationService . execute ( notificationConfig , dataLocation , message , undefined ) ; expect ( post ) . toHaveBeenCalledTimes ( 1 ) ; expect ( post ) . toHaveBeenCalledWith ( webhookParameter . url , expect . objectContaining ( { location : dataLocation , message : message } ) ) ; } ) ; it ( 'should trigger notification when condition is met' , async ( ) => { post . mockReturnValue ( Promise . resolve ( ) ) ; const webhookParameter = { url : 'callback' , } ; const notificationConfig = { id : 1 , pipelineId : 1 , condition : 'data.value1 > 0' , type : NotificationType . WEBHOOK , parameter : webhookParameter , } ; const message = 'message' ; const dataLocation = 'location' ; await notificationService . execute ( notificationConfig , dataLocation , message , data ) ; expect ( post ) . toHaveBeenCalledTimes ( 1 ) ; expect ( post ) . toHaveBeenCalledWith ( webhookParameter . url , expect . objectContaining ( { location : dataLocation , message : message } ) ) ; } ) ; test ( 'should not trigger when condition is malformed' , async ( ) => { const webhookParameter = { url : 'callback' , } ; const notificationConfig = { id : 1 , pipelineId : 3 , condition : 'asdfa;' , type : NotificationType . WEBHOOK , parameter : webhookParameter , } ; try { const message = 'message' ; const dataLocation = 'location' ; await notificationService . execute ( notificationConfig , dataLocation , message , data ) ; throw new Error ( 'Fail test' ) ; } catch ( err ) { expect ( err ) . toHaveProperty ( 'message' , 'Malformed expression received: asdfa;\n Error message: ' + 'ReferenceError: asdfa is not defined' ) ; } expect ( post ) . not . toHaveBeenCalled ( ) ; } ) ; test ( 'SLACK request' , async ( ) => { const slackParameter = { workspaceId : '012' , channelId : '123' , secret : '42' , } ; const request = { id : 1 , condition : 'data.value1 > 0' , pipelineId : 42 , type : NotificationType . SLACK , parameter : slackParameter , } ; const message = 'message' ; const dataLocation = 'location' ; await notificationService . execute ( request , dataLocation , message , data ) ; const expectedObject = { text : message , } ; expect ( post ) . toHaveBeenCalledTimes ( 1 ) ; const expectedUrl = `https://hooks.slack.com/services/${ slackParameter . workspaceId }/${ slackParameter . channelId }/${ slackParameter . secret }` ; expect ( post ) . toHaveBeenCalledWith ( expectedUrl , expectedObject ) ; } ) ; } ) ; } ) ;