66 inputValidationMockFns ,
77} from '@sim/testing/mocks/input-validation.mock'
88import { schemaMock } from '@sim/testing/mocks/schema.mock'
9- import { describe , expect , it , vi } from 'vitest'
9+ import { beforeEach , describe , expect , it , vi } from 'vitest'
1010import { OutboundRoutingError } from '@/lib/core/network/routing'
1111import { assertSlackFileHost } from '@/lib/internal/slack/file-download'
1212import {
@@ -18,6 +18,8 @@ import {
1818
1919vi . mock ( '@/lib/oauth/credential-service' , ( ) => authOAuthUtilsMock )
2020vi . mock ( '@/lib/core/security/input-validation.server' , ( ) => inputValidationMock )
21+ const { mockOutboundFetch } = vi . hoisted ( ( ) => ( { mockOutboundFetch : vi . fn ( ) } ) )
22+ vi . mock ( '@/lib/core/network/outbound-fetch.server' , ( ) => ( { outboundFetch : mockOutboundFetch } ) )
2123
2224const ctx = ( body : unknown ) => ( {
2325 webhook : { } ,
@@ -204,6 +206,17 @@ describe('slackHandler formatInput - native app bot token', () => {
204206} )
205207
206208describe ( 'slackHandler formatInput - file downloads' , ( ) => {
209+ beforeEach ( ( ) => {
210+ inputValidationMockFns . mockValidateUrlWithDNS . mockResolvedValue ( {
211+ isValid : true ,
212+ resolvedIP : '93.184.216.34' ,
213+ originalHostname : 'files.slack.com' ,
214+ } )
215+ inputValidationMockFns . mockSecureFetchWithPinnedIP . mockResolvedValue (
216+ new Response ( Buffer . from ( '%PDF' ) , { status : 200 } )
217+ )
218+ } )
219+
207220 async function formatWithFiles ( files : Record < string , unknown > [ ] ) {
208221 const formatInput = slackHandler . formatInput
209222 if ( ! formatInput ) throw new Error ( 'Slack handler has no formatInput' )
@@ -216,15 +229,6 @@ describe('slackHandler formatInput - file downloads', () => {
216229 }
217230
218231 it ( 'downloads from the Slack download URL and keeps redirects on the file host' , async ( ) => {
219- inputValidationMockFns . mockValidateUrlWithDNS . mockResolvedValue ( {
220- isValid : true ,
221- resolvedIP : '93.184.216.34' ,
222- originalHostname : 'files.slack.com' ,
223- } )
224- inputValidationMockFns . mockSecureFetchWithPinnedIP . mockResolvedValue (
225- new Response ( Buffer . from ( '%PDF' ) , { status : 200 } )
226- )
227-
228232 const { input } = await formatWithFiles ( [
229233 {
230234 id : 'F1' ,
@@ -242,16 +246,45 @@ describe('slackHandler formatInput - file downloads', () => {
242246 expect ( eventOf ( input ) . files ) . toHaveLength ( 1 )
243247 } )
244248
249+ it ( "looks up the download URL when the event carries only the file's private URL" , async ( ) => {
250+ mockOutboundFetch . mockResolvedValueOnce (
251+ Response . json ( {
252+ ok : true ,
253+ file : {
254+ id : 'F3' ,
255+ url_private : 'https://files.slack.com/files-pri/T1-F3/report.pdf' ,
256+ url_private_download : 'https://files.slack.com/files-pri/T1-F3/download/report.pdf' ,
257+ } ,
258+ } )
259+ )
260+
261+ const { input } = await formatWithFiles ( [
262+ {
263+ id : 'F3' ,
264+ name : 'report.pdf' ,
265+ mimetype : 'application/pdf' ,
266+ size : 4 ,
267+ url_private : 'https://files.slack.com/files-pri/T1-F3/report.pdf' ,
268+ } ,
269+ ] )
270+
271+ expect ( inputValidationMockFns . mockSecureFetchWithPinnedIP . mock . lastCall ?. [ 0 ] ) . toBe (
272+ 'https://files.slack.com/files-pri/T1-F3/download/report.pdf'
273+ )
274+ expect ( eventOf ( input ) . files ) . toHaveLength ( 1 )
275+ } )
276+
245277 it ( 'never sends the bot token to a file hosted outside Slack' , async ( ) => {
246278 const { input } = await formatWithFiles ( [
247279 {
248280 id : 'F2' ,
249281 name : 'plan.pdf' ,
250282 is_external : true ,
251- url_private : 'https://files.example .com/plan.pdf' ,
283+ url_private : 'https://files.slack .com/files-pri/T1-F2 /plan.pdf' ,
252284 } ,
253285 ] )
254286
287+ expect ( mockOutboundFetch ) . not . toHaveBeenCalled ( )
255288 expect ( inputValidationMockFns . mockSecureFetchWithPinnedIP ) . not . toHaveBeenCalled ( )
256289 expect ( eventOf ( input ) . files ) . toEqual ( [ ] )
257290 } )
0 commit comments