@@ -109,3 +109,40 @@ test('should handle invalid credentials', async () => {
109
109
expect ( error ) . not . toBeNull ( )
110
110
expect ( data . user ) . toBeNull ( )
111
111
} )
112
+
113
+ test ( 'should upload and list file in bucket' , async ( ) => {
114
+ const bucket = 'test-bucket'
115
+ const filePath = 'test-file.txt'
116
+ const fileContent = new Blob ( [ 'Hello, Supabase Storage!' ] , { type : 'text/plain' } )
117
+
118
+ // use service_role key for bypass RLS
119
+ const SERVICE_ROLE_KEY =
120
+ process . env . SUPABASE_SERVICE_ROLE_KEY ||
121
+ 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImV4cCI6MTk4MzgxMjk5Nn0.EGIM96RAZx35lJzdJsyH-qQwv8Hdp7fsn3W0YpN81IU'
122
+ const supabaseWithServiceRole = createClient ( SUPABASE_URL , SERVICE_ROLE_KEY , {
123
+ realtime : { heartbeatIntervalMs : 500 } ,
124
+ } )
125
+
126
+ // upload
127
+ const { data : uploadData , error : uploadError } = await supabaseWithServiceRole . storage
128
+ . from ( bucket )
129
+ . upload ( filePath , fileContent , { upsert : true } )
130
+ expect ( uploadError ) . toBeNull ( )
131
+ expect ( uploadData ) . toBeDefined ( )
132
+
133
+ // list
134
+ const { data : listData , error : listError } = await supabaseWithServiceRole . storage
135
+ . from ( bucket )
136
+ . list ( )
137
+ expect ( listError ) . toBeNull ( )
138
+ expect ( Array . isArray ( listData ) ) . toBe ( true )
139
+ if ( ! listData ) throw new Error ( 'listData is null' )
140
+ const fileNames = listData . map ( ( f : any ) => f . name )
141
+ expect ( fileNames ) . toContain ( 'test-file.txt' )
142
+
143
+ // delete file
144
+ const { error : deleteError } = await supabaseWithServiceRole . storage
145
+ . from ( bucket )
146
+ . remove ( [ filePath ] )
147
+ expect ( deleteError ) . toBeNull ( )
148
+ } )
0 commit comments