1- import { PassThrough } from 'stream' ;
21import { createSendTeamsMessageViaWebhookAction } from './send-ms-teams-message' ;
3- import * as winston from 'winston' ;
4- import { Config } from '@backstage/config' ;
2+ import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-utils' ;
53import axios from 'axios' ;
4+ import { Config } from '@backstage/config' ;
65
76jest . mock ( 'axios' ) ;
8-
97const mockedAxios = axios as jest . Mocked < typeof axios > ;
108
11- const defaultHandlerOptions = {
12- workspacePath : '/tmp' ,
13- logStream : new PassThrough ( ) ,
14- output : jest . fn ( ) ,
15- createTemporaryDirectory ( ) {
16- throw new Error ( 'Not implemented' ) ;
17- } ,
18- checkpoint : jest . fn ( ) ,
19- getInitiatorCredentials : jest . fn ( ) ,
20- } ;
21-
229describe ( 'ms-teams:sendMessage' , ( ) => {
23- beforeEach ( ( ) => { } ) ;
10+ afterEach ( ( ) => {
11+ jest . resetAllMocks ( ) ;
12+ } ) ;
2413
2514 it ( 'should throw error if webhookUrl is not defined' , async ( ) => {
2615 const action = createSendTeamsMessageViaWebhookAction ( {
2716 config : {
28- getOptionalString : ( _key : string ) : string | undefined => undefined ,
17+ getOptionalString : ( _key : string ) => undefined ,
2918 } as Config ,
3019 } ) ;
3120
32- const logger = { } as winston . Logger ;
33-
34- try {
35- await action . handler ( {
36- ...defaultHandlerOptions ,
37- input : {
38- message : 'Hello, Teams!' ,
39- } ,
40- logger,
41- } ) ;
42- throw new Error ( 'This should not succeed' ) ;
43- } catch ( err : any ) {
44- // eslint-disable-next-line jest/no-conditional-expect
45- expect ( err . message ) . toContain (
46- 'Webhook URL is not specified in either the app-config or the action input. This must be specified in at least one place in order to send a message' ,
47- ) ;
48- }
21+ await expect (
22+ action . handler (
23+ createMockActionContext ( {
24+ input : {
25+ message : 'Hello, Teams!' ,
26+ } ,
27+ } ) ,
28+ ) ,
29+ ) . rejects . toThrow (
30+ 'Webhook URL is not specified in either the app-config or the action input. This must be specified in at least one place in order to send a message' ,
31+ ) ;
4932 } ) ;
5033
5134 it ( 'should send to config webhook URL if provided' , async ( ) => {
5235 const action = createSendTeamsMessageViaWebhookAction ( {
5336 config : {
54- getOptionalString : ( _key : string ) : string | undefined =>
55- 'https://example-teams.com' ,
37+ getOptionalString : ( _key : string ) => 'https://example-teams.com' ,
5638 } as Config ,
5739 } ) ;
5840
5941 mockedAxios . post . mockResolvedValue ( { status : 200 } ) ;
6042
61- const logger = { } as winston . Logger ;
62-
63- await action . handler ( {
64- ...defaultHandlerOptions ,
65- input : {
66- message : 'Hello, Teams!' ,
67- } ,
68- logger,
69- } ) ;
43+ await action . handler (
44+ createMockActionContext ( {
45+ input : {
46+ message : 'Hello, Teams!' ,
47+ } ,
48+ } ) ,
49+ ) ;
7050
71- expect ( axios . post ) . toHaveBeenCalledWith (
51+ expect ( mockedAxios . post ) . toHaveBeenCalledWith (
7252 'https://example-teams.com' ,
7353 expect . anything ( ) ,
7454 ) ;
7555 } ) ;
7656
77- it ( 'should prefer webhook url from config if provided in input' , async ( ) => {
57+ it ( 'should prefer webhook url from config even if provided in input' , async ( ) => {
7858 const action = createSendTeamsMessageViaWebhookAction ( {
7959 config : {
80- getOptionalString : ( _key : string ) : string | undefined =>
81- 'https://example-teams.com' ,
60+ getOptionalString : ( _key : string ) => 'https://example-teams.com' ,
8261 } as Config ,
8362 } ) ;
8463
8564 mockedAxios . post . mockResolvedValue ( { status : 200 } ) ;
8665
87- const logger = { } as winston . Logger ;
88-
89- await action . handler ( {
90- ...defaultHandlerOptions ,
91- input : {
92- message : 'Hello, Teams!' ,
93- webhookUrl : 'https://dontusethis-teams.com' ,
94- } ,
95- logger,
96- } ) ;
66+ await action . handler (
67+ createMockActionContext ( {
68+ input : {
69+ message : 'Hello, Teams!' ,
70+ webhookUrl : 'https://input-teams.com' ,
71+ } ,
72+ } ) ,
73+ ) ;
9774
98- expect ( axios . post ) . toHaveBeenCalledWith (
75+ expect ( mockedAxios . post ) . toHaveBeenCalledWith (
9976 'https://example-teams.com' ,
10077 expect . anything ( ) ,
10178 ) ;
10279 } ) ;
10380
104- it ( 'should use the webhook url on input if config value is not present' , async ( ) => {
81+ it ( 'should use webhookUrl from input if config value is not present' , async ( ) => {
10582 const action = createSendTeamsMessageViaWebhookAction ( {
10683 config : {
107- getOptionalString : ( _key : string ) : string | undefined => undefined ,
84+ getOptionalString : ( _key : string ) => undefined ,
10885 } as Config ,
10986 } ) ;
11087
11188 mockedAxios . post . mockResolvedValue ( { status : 200 } ) ;
11289
113- const logger = { } as winston . Logger ;
114-
115- await action . handler ( {
116- ...defaultHandlerOptions ,
117- input : {
118- message : 'Hello, Teams!' ,
119- webhookUrl : 'https://nevergonnagiveyouup-teams.com' ,
120- } ,
121- logger,
122- } ) ;
90+ await action . handler (
91+ createMockActionContext ( {
92+ input : {
93+ message : 'Hello, Teams!' ,
94+ webhookUrl : 'https://nevergonnagiveyouup-teams.com' ,
95+ } ,
96+ } ) ,
97+ ) ;
12398
124- expect ( axios . post ) . toHaveBeenCalledWith (
99+ expect ( mockedAxios . post ) . toHaveBeenCalledWith (
125100 'https://nevergonnagiveyouup-teams.com' ,
126101 expect . anything ( ) ,
127102 ) ;
@@ -130,25 +105,22 @@ describe('ms-teams:sendMessage', () => {
130105 it ( 'should send message in proper format to webhook URL' , async ( ) => {
131106 const action = createSendTeamsMessageViaWebhookAction ( {
132107 config : {
133- getOptionalString : ( _key : string ) : string | undefined =>
134- 'https://example-teams.com' ,
108+ getOptionalString : ( _key : string ) => 'https://example-teams.com' ,
135109 } as Config ,
136110 } ) ;
137111
138112 mockedAxios . post . mockResolvedValue ( { status : 200 } ) ;
139113
140- const logger = { } as winston . Logger ;
141-
142- await action . handler ( {
143- ...defaultHandlerOptions ,
144- input : {
145- message : 'Hello, Teams!' ,
146- webhookUrl : 'https://dontusethis-teams.com' ,
147- } ,
148- logger,
149- } ) ;
114+ await action . handler (
115+ createMockActionContext ( {
116+ input : {
117+ message : 'Hello, Teams!' ,
118+ webhookUrl : 'https://should-not-be-used.com' ,
119+ } ,
120+ } ) ,
121+ ) ;
150122
151- expect ( axios . post ) . toHaveBeenCalledWith (
123+ expect ( mockedAxios . post ) . toHaveBeenCalledWith (
152124 'https://example-teams.com' ,
153125 expect . objectContaining ( { text : 'Hello, Teams!' } ) ,
154126 ) ;
@@ -157,31 +129,23 @@ describe('ms-teams:sendMessage', () => {
157129 it ( 'should throw an error if result.status is not 200' , async ( ) => {
158130 const action = createSendTeamsMessageViaWebhookAction ( {
159131 config : {
160- getOptionalString : ( _key : string ) : string | undefined =>
161- 'https://example-teams.com' ,
132+ getOptionalString : ( _key : string ) => 'https://example-teams.com' ,
162133 } as Config ,
163134 } ) ;
164135
165136 mockedAxios . post . mockResolvedValue ( { status : 400 } ) ;
166- const logger = jest . fn ( ) as unknown as winston . Logger ;
167- logger . error = jest . fn ( ) ;
168- logger . debug = jest . fn ( ) ;
169137
170- try {
171- await action . handler ( {
172- ...defaultHandlerOptions ,
173- input : {
174- message : 'Hello, Teams!' ,
175- webhookUrl : 'https://dontusethis-teams.com' ,
176- } ,
177- logger,
178- } ) ;
179- expect ( true ) . toBeFalsy ( ) ;
180- } catch ( err : any ) {
181- // eslint-disable-next-line jest/no-conditional-expect
182- expect ( err . message ) . toContain (
183- `Something went wrong while trying to send a request to the Teams webhook URL - StatusCode 400` ,
184- ) ;
185- }
138+ await expect (
139+ action . handler (
140+ createMockActionContext ( {
141+ input : {
142+ message : 'Hello, Teams!' ,
143+ webhookUrl : 'https://ignored.com' ,
144+ } ,
145+ } ) ,
146+ ) ,
147+ ) . rejects . toThrow (
148+ 'Something went wrong while trying to send a request to the Teams webhook URL - StatusCode 400' ,
149+ ) ;
186150 } ) ;
187151} ) ;
0 commit comments