@@ -4,53 +4,192 @@ import { v4 as uuidv4 } from 'uuid';
44import { figmaWebhookAuthMiddleware } from './figma-webhook-auth-middleware' ;
55
66import { flushMacrotaskQueue } from '../../../common/testing/utils' ;
7- import { generateFigmaTeam } from '../../../domain/entities/testing' ;
8- import { figmaTeamRepository } from '../../../infrastructure/repositories' ;
7+ import {
8+ generateFigmaFileWebhook ,
9+ generateFigmaTeam ,
10+ } from '../../../domain/entities/testing' ;
11+ import {
12+ figmaFileWebhookRepository ,
13+ figmaTeamRepository ,
14+ } from '../../../infrastructure/repositories' ;
915import { BadRequestResponseStatusError } from '../../errors' ;
10- import { generatePingWebhookEventRequestBody } from '../../routes/figma/testing' ;
16+ import {
17+ generateDevModeStatusUpdateWebhookEventRequestBody ,
18+ generateFileUpdateWebhookEventRequestBody ,
19+ generatePingWebhookEventRequestBody ,
20+ } from '../../routes/figma/testing' ;
1121
1222describe ( 'figmaWebhookAuthMiddleware' , ( ) => {
13- it ( 'should authenticate request and set figmaTeam in locals if request is authentic' , async ( ) => {
14- const webhookId = uuidv4 ( ) ;
15- const webhookPasscode = uuidv4 ( ) ;
16- const figmaTeam = generateFigmaTeam ( { webhookId, webhookPasscode } ) ;
17- jest
18- . spyOn ( figmaTeamRepository , 'findByWebhookId' )
19- . mockResolvedValue ( figmaTeam ) ;
20-
21- const request = {
22- body : generatePingWebhookEventRequestBody ( {
23- webhook_id : webhookId ,
24- passcode : webhookPasscode ,
25- } ) ,
26- } as Request ;
27- const response = {
28- locals : { } ,
29- } as Response ;
30- const next = jest . fn ( ) ;
31-
32- figmaWebhookAuthMiddleware ( request , response , next ) ;
33- await flushMacrotaskQueue ( ) ;
34-
35- expect ( next ) . toHaveBeenCalledWith ( ) ;
36- expect ( response . locals . figmaTeam ) . toBe ( figmaTeam ) ;
37- expect ( figmaTeamRepository . findByWebhookId ) . toHaveBeenCalledWith ( webhookId ) ;
23+ describe ( 'file webhook' , ( ) => {
24+ beforeEach ( ( ) => {
25+ jest
26+ . spyOn ( figmaTeamRepository , 'findByWebhookId' )
27+ . mockResolvedValue ( null ) ;
28+ } ) ;
29+
30+ it ( 'should authenticate request and set figmaFileWebhook in locals if request is authentic' , async ( ) => {
31+ const webhookId = uuidv4 ( ) ;
32+ const webhookPasscode = uuidv4 ( ) ;
33+ const figmaFileWebhook = generateFigmaFileWebhook ( {
34+ webhookId,
35+ webhookPasscode,
36+ } ) ;
37+ jest
38+ . spyOn ( figmaFileWebhookRepository , 'findByWebhookId' )
39+ . mockResolvedValue ( figmaFileWebhook ) ;
40+
41+ const request = {
42+ body : generateFileUpdateWebhookEventRequestBody ( {
43+ webhook_id : webhookId ,
44+ passcode : webhookPasscode ,
45+ } ) ,
46+ } as Request ;
47+ const response = {
48+ locals : { } ,
49+ } as Response ;
50+ const next = jest . fn ( ) ;
51+
52+ figmaWebhookAuthMiddleware ( request , response , next ) ;
53+ await flushMacrotaskQueue ( ) ;
54+
55+ expect ( next ) . toHaveBeenCalledWith ( ) ;
56+ expect ( response . locals . webhookInfo ) . toEqual ( {
57+ figmaFileWebhook,
58+ webhookType : 'file' ,
59+ } ) ;
60+ expect ( figmaTeamRepository . findByWebhookId ) . toHaveBeenCalledWith (
61+ webhookId ,
62+ ) ;
63+ } ) ;
64+
65+ it ( 'can handle dev mode status update webhook events' , async ( ) => {
66+ const webhookId = uuidv4 ( ) ;
67+ const webhookPasscode = uuidv4 ( ) ;
68+ const figmaFileWebhook = generateFigmaFileWebhook ( {
69+ webhookId,
70+ webhookPasscode,
71+ } ) ;
72+ jest
73+ . spyOn ( figmaFileWebhookRepository , 'findByWebhookId' )
74+ . mockResolvedValue ( figmaFileWebhook ) ;
75+
76+ const request = {
77+ body : generateDevModeStatusUpdateWebhookEventRequestBody ( {
78+ webhook_id : webhookId ,
79+ passcode : webhookPasscode ,
80+ } ) ,
81+ } as Request ;
82+
83+ const response = {
84+ locals : { } ,
85+ } as Response ;
86+ const next = jest . fn ( ) ;
87+
88+ figmaWebhookAuthMiddleware ( request , response , next ) ;
89+ await flushMacrotaskQueue ( ) ;
90+
91+ expect ( next ) . toHaveBeenCalledWith ( ) ;
92+ expect ( response . locals . webhookInfo ) . toEqual ( {
93+ figmaFileWebhook,
94+ webhookType : 'file' ,
95+ } ) ;
96+ expect ( figmaFileWebhookRepository . findByWebhookId ) . toHaveBeenCalledWith (
97+ webhookId ,
98+ ) ;
99+ } ) ;
100+
101+ it ( 'should not authenticate request if request contains invalid passcode' , async ( ) => {
102+ const webhookId = uuidv4 ( ) ;
103+ const figmaFileWebhook = generateFigmaFileWebhook ( { webhookId } ) ;
104+ jest
105+ . spyOn ( figmaFileWebhookRepository , 'findByWebhookId' )
106+ . mockResolvedValue ( figmaFileWebhook ) ;
107+
108+ const request = {
109+ body : generatePingWebhookEventRequestBody ( {
110+ webhook_id : webhookId ,
111+ passcode : 'invalid' ,
112+ } ) ,
113+ } as Request ;
114+ const next = jest . fn ( ) ;
115+
116+ figmaWebhookAuthMiddleware ( request , { } as Response , next ) ;
117+ await flushMacrotaskQueue ( ) ;
118+
119+ expect ( next ) . toHaveBeenCalledWith (
120+ expect . any ( BadRequestResponseStatusError ) ,
121+ ) ;
122+ } ) ;
38123 } ) ;
39124
40- it ( 'should not authenticate request if request does not contain webhook credentials' , async ( ) => {
41- const request = { } as Request ;
42- const next = jest . fn ( ) ;
43-
44- figmaWebhookAuthMiddleware ( request , { } as Response , next ) ;
45- await flushMacrotaskQueue ( ) ;
46-
47- expect ( next ) . toHaveBeenCalledWith (
48- expect . any ( BadRequestResponseStatusError ) ,
49- ) ;
125+ describe ( 'team webhook' , ( ) => {
126+ beforeEach ( ( ) => {
127+ jest
128+ . spyOn ( figmaFileWebhookRepository , 'findByWebhookId' )
129+ . mockResolvedValue ( null ) ;
130+ } ) ;
131+
132+ it ( 'should authenticate request and set figmaTeam in locals if request is authentic' , async ( ) => {
133+ const webhookId = uuidv4 ( ) ;
134+ const webhookPasscode = uuidv4 ( ) ;
135+ const figmaTeam = generateFigmaTeam ( { webhookId, webhookPasscode } ) ;
136+ jest
137+ . spyOn ( figmaTeamRepository , 'findByWebhookId' )
138+ . mockResolvedValue ( figmaTeam ) ;
139+
140+ const request = {
141+ body : generatePingWebhookEventRequestBody ( {
142+ webhook_id : webhookId ,
143+ passcode : webhookPasscode ,
144+ } ) ,
145+ } as Request ;
146+ const response = {
147+ locals : { } ,
148+ } as Response ;
149+ const next = jest . fn ( ) ;
150+
151+ figmaWebhookAuthMiddleware ( request , response , next ) ;
152+ await flushMacrotaskQueue ( ) ;
153+
154+ expect ( next ) . toHaveBeenCalledWith ( ) ;
155+ expect ( response . locals . webhookInfo ) . toEqual ( {
156+ figmaTeam,
157+ webhookType : 'team' ,
158+ } ) ;
159+ expect ( figmaTeamRepository . findByWebhookId ) . toHaveBeenCalledWith (
160+ webhookId ,
161+ ) ;
162+ } ) ;
163+
164+ it ( 'should not authenticate request if request contains invalid passcode' , async ( ) => {
165+ const webhookId = uuidv4 ( ) ;
166+ const figmaTeam = generateFigmaTeam ( { webhookId } ) ;
167+ jest
168+ . spyOn ( figmaTeamRepository , 'findByWebhookId' )
169+ . mockResolvedValue ( figmaTeam ) ;
170+
171+ const request = {
172+ body : generatePingWebhookEventRequestBody ( {
173+ webhook_id : figmaTeam . webhookId ,
174+ passcode : 'invalid' ,
175+ } ) ,
176+ } as Request ;
177+ const next = jest . fn ( ) ;
178+
179+ figmaWebhookAuthMiddleware ( request , { } as Response , next ) ;
180+ await flushMacrotaskQueue ( ) ;
181+
182+ expect ( next ) . toHaveBeenCalledWith (
183+ expect . any ( BadRequestResponseStatusError ) ,
184+ ) ;
185+ } ) ;
50186 } ) ;
51187
52188 it ( 'should not authenticate request if webhook is unknown' , async ( ) => {
53189 jest . spyOn ( figmaTeamRepository , 'findByWebhookId' ) . mockResolvedValue ( null ) ;
190+ jest
191+ . spyOn ( figmaFileWebhookRepository , 'findByWebhookId' )
192+ . mockResolvedValue ( null ) ;
54193
55194 const request = {
56195 body : generatePingWebhookEventRequestBody ( {
@@ -68,19 +207,8 @@ describe('figmaWebhookAuthMiddleware', () => {
68207 ) ;
69208 } ) ;
70209
71- it ( 'should not authenticate request if request contains invalid passcode' , async ( ) => {
72- const webhookId = uuidv4 ( ) ;
73- const figmaTeam = generateFigmaTeam ( { webhookId } ) ;
74- jest
75- . spyOn ( figmaTeamRepository , 'findByWebhookId' )
76- . mockResolvedValue ( figmaTeam ) ;
77-
78- const request = {
79- body : generatePingWebhookEventRequestBody ( {
80- webhook_id : figmaTeam . webhookId ,
81- passcode : 'invalid' ,
82- } ) ,
83- } as Request ;
210+ it ( 'should not authenticate request if request does not contain webhook credentials' , async ( ) => {
211+ const request = { } as Request ;
84212 const next = jest . fn ( ) ;
85213
86214 figmaWebhookAuthMiddleware ( request , { } as Response , next ) ;
0 commit comments