@@ -257,4 +257,153 @@ defmodule Supabase.FunctionsTest do
257257 assert response == "chunk1chunk2"
258258 end
259259 end
260+
261+ describe "update_auth/2" do
262+ test "returns a new client with updated access token" , % { client: client } do
263+ new_token = "new_test_token"
264+ updated_client = Functions . update_auth ( client , new_token )
265+
266+ assert updated_client . access_token == new_token
267+ # Original client should remain unchanged
268+ assert client . access_token != new_token
269+ # All other fields should remain the same
270+ assert updated_client . base_url == client . base_url
271+ assert updated_client . api_key == client . api_key
272+ end
273+
274+ test "updated client works with invoke/3" , % { client: client } do
275+ new_token = "updated_token"
276+
277+ expect ( @ mock , :stream , fn request , _opts ->
278+ assert Request . get_header ( request , "authorization" ) == "Bearer #{ new_token } "
279+
280+ { :ok ,
281+ % Finch.Response {
282+ status: 200 ,
283+ headers: % { "content-type" => "application/json" } ,
284+ body: ~s( {"success": true})
285+ } }
286+ end )
287+
288+ updated_client = Functions . update_auth ( client , new_token )
289+
290+ assert { :ok , response } =
291+ Functions . invoke ( updated_client , "test-function" , http_client: @ mock )
292+
293+ assert response . body == % { "success" => true }
294+ end
295+ end
296+
297+ describe "access_token option in invoke/3" do
298+ test "overrides authorization header with custom token" , % { client: client } do
299+ custom_token = "custom_auth_token"
300+
301+ expect ( @ mock , :stream , fn request , _opts ->
302+ assert Request . get_header ( request , "authorization" ) == "Bearer #{ custom_token } "
303+
304+ { :ok ,
305+ % Finch.Response {
306+ status: 200 ,
307+ headers: % { "content-type" => "application/json" } ,
308+ body: ~s( {"authorized": true})
309+ } }
310+ end )
311+
312+ assert { :ok , response } =
313+ Functions . invoke ( client , "test-function" ,
314+ access_token: custom_token ,
315+ http_client: @ mock
316+ )
317+
318+ assert response . body == % { "authorized" => true }
319+ end
320+
321+ test "works with other options combined" , % { client: client } do
322+ custom_token = "combined_auth_token"
323+ custom_headers = % { "x-custom" => "value" }
324+ body_data = % { test: "data" }
325+
326+ expect ( @ mock , :stream , fn request , _opts ->
327+ assert Request . get_header ( request , "authorization" ) == "Bearer #{ custom_token } "
328+ assert Request . get_header ( request , "x-custom" ) == "value"
329+ assert Request . get_header ( request , "content-type" ) == "application/json"
330+
331+ { :ok ,
332+ % Finch.Response {
333+ status: 200 ,
334+ headers: % { "content-type" => "application/json" } ,
335+ body: ~s( {"success": true})
336+ } }
337+ end )
338+
339+ assert { :ok , response } =
340+ Functions . invoke ( client , "test-function" ,
341+ access_token: custom_token ,
342+ headers: custom_headers ,
343+ body: body_data ,
344+ http_client: @ mock
345+ )
346+
347+ assert response . body == % { "success" => true }
348+ end
349+
350+ test "original client remains unchanged after access_token override" , % { client: client } do
351+ original_token = client . access_token
352+ custom_token = "temporary_override_token"
353+
354+ expect ( @ mock , :stream , fn request , _opts ->
355+ assert Request . get_header ( request , "authorization" ) == "Bearer #{ custom_token } "
356+
357+ { :ok ,
358+ % Finch.Response {
359+ status: 200 ,
360+ headers: % { "content-type" => "application/json" } ,
361+ body: ~s( {"success": true})
362+ } }
363+ end )
364+
365+ assert { :ok , _response } =
366+ Functions . invoke ( client , "test-function" ,
367+ access_token: custom_token ,
368+ http_client: @ mock
369+ )
370+
371+ # Original client should be unchanged
372+ assert client . access_token == original_token
373+ end
374+
375+ test "nil access_token option uses original client token" , % { client: client } do
376+ expect ( @ mock , :stream , fn request , _opts ->
377+ assert Request . get_header ( request , "authorization" ) == "Bearer #{ client . access_token } "
378+
379+ { :ok ,
380+ % Finch.Response {
381+ status: 200 ,
382+ headers: % { "content-type" => "application/json" } ,
383+ body: ~s( {"success": true})
384+ } }
385+ end )
386+
387+ assert { :ok , response } =
388+ Functions . invoke ( client , "test-function" , access_token: nil , http_client: @ mock )
389+
390+ assert response . body == % { "success" => true }
391+ end
392+
393+ test "empty access_token option uses original client token" , % { client: client } do
394+ expect ( @ mock , :stream , fn request , _opts ->
395+ assert Request . get_header ( request , "authorization" ) == "Bearer #{ client . access_token } "
396+
397+ { :ok ,
398+ % Finch.Response {
399+ status: 200 ,
400+ headers: % { "content-type" => "application/json" } ,
401+ body: ~s( {"success": true})
402+ } }
403+ end )
404+
405+ assert { :ok , response } = Functions . invoke ( client , "test-function" , http_client: @ mock )
406+ assert response . body == % { "success" => true }
407+ end
408+ end
260409end
0 commit comments