diff --git a/SupabaseTests/StatelessClientTests.cs b/SupabaseTests/StatelessClientTests.cs index 3028a2d8..ce424086 100644 --- a/SupabaseTests/StatelessClientTests.cs +++ b/SupabaseTests/StatelessClientTests.cs @@ -71,14 +71,20 @@ public void GetRestOptions_ShouldMergeDeveloperSuppliedHeaders_GivenCustomHeader } [TestMethod] - public void Functions_ShouldReturnClient_GivenUrlAndKey() + public void Functions_ShouldCarryDeveloperHeadersToClient_GivenCustomHeader() { - Functions(SupabaseUrl, "my-key", FormatOptions()).Should().NotBeNull(); + var options = FormatOptions(); + options.Headers["X-Custom"] = "custom-value"; + Functions(SupabaseUrl, "my-key", options).GetHeaders!().Should().ContainKey("X-Custom") + .WhoseValue.Should().Be("custom-value", "developer headers must reach the composed functions client"); } [TestMethod] - public void Storage_ShouldReturnClient_GivenUrlAndKey() + public void Storage_ShouldCarryDeveloperHeadersToClient_GivenCustomHeader() { - Storage(SupabaseUrl, "my-key", FormatOptions()).Should().NotBeNull(); + var options = FormatOptions(); + options.Headers["X-Custom"] = "custom-value"; + Storage(SupabaseUrl, "my-key", options).Headers.Should().ContainKey("X-Custom") + .WhoseValue.Should().Be("custom-value", "developer headers must reach the composed storage client"); } } diff --git a/SupabaseTests/SupabaseClientCompositionTests.cs b/SupabaseTests/SupabaseClientCompositionTests.cs index 8b91467a..77fbe342 100644 --- a/SupabaseTests/SupabaseClientCompositionTests.cs +++ b/SupabaseTests/SupabaseClientCompositionTests.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.Net.WebSockets; +using System.Threading.Tasks; using FluentAssertions; using FluentAssertions.Execution; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -37,13 +38,14 @@ private static Supabase.Client UrlClient(SupabaseOptions options = null) => private static Supabase.Client DiClient( IGotrueClient auth = null, IRealtimeClient realtime = null, - IPostgrestClient postgrest = null) => + IPostgrestClient postgrest = null, + SupabaseOptions options = null) => new(auth ?? Substitute.For>(), realtime ?? RealtimeSubstitute(), Substitute.For(), postgrest ?? Substitute.For(), Substitute.For>(), - new SupabaseOptions()); + options ?? new SupabaseOptions()); // The umbrella writes Options.PostgrestClient during construction, so the substitute needs real Options. private static IRealtimeClient RealtimeSubstitute() @@ -174,6 +176,30 @@ public void SupabaseClient_ShouldNotForwardTokenToRealtime_GivenAuthStateCarries realtime.DidNotReceive().SetAuth(Arg.Any()); } + [TestMethod] + public async Task SupabaseClient_ShouldRetrieveSessionAndConnectRealtime_GivenAutoConnectEnabled() + { + var auth = Substitute.For>(); + var realtime = RealtimeSubstitute(); + var client = DiClient(auth: auth, realtime: realtime, + options: new SupabaseOptions { AutoConnectRealtime = true }); + await client.InitializeAsync(); + await auth.Received().RetrieveSessionAsync(); + await realtime.Received().ConnectAsync(); + } + + [TestMethod] + public async Task SupabaseClient_ShouldNotConnectRealtime_GivenAutoConnectDisabled() + { + var auth = Substitute.For>(); + var realtime = RealtimeSubstitute(); + var client = DiClient(auth: auth, realtime: realtime, + options: new SupabaseOptions { AutoConnectRealtime = false }); + await client.InitializeAsync(); + await auth.Received().RetrieveSessionAsync(); + await realtime.DidNotReceive().ConnectAsync(); + } + [TestMethod] public void SupabaseClient_ShouldDelegateRpcToPostgrest() { @@ -183,9 +209,32 @@ public void SupabaseClient_ShouldDelegateRpcToPostgrest() } [TestMethod] - public void SupabaseClient_ShouldReturnQueryableTable_GivenFrom() + public void SupabaseClient_ShouldConfigurePostgrestWithSchema_GivenCustomSchema() + { + var client = UrlClient(new SupabaseOptions { AutoConnectRealtime = false, Schema = "custom" }); + client.Postgrest.Options.Schema.Should().Be("custom", + "the umbrella must pass the developer's schema down to the Postgrest client it builds"); + } + + [TestMethod] + public void SupabaseClient_ShouldConfigureAuthFromUrlAndOptions_GivenAutoRefreshDisabled() + { + var client = UrlClient(new SupabaseOptions { AutoConnectRealtime = false, AutoRefreshToken = false }); + using (new AssertionScope()) + { + client.Auth.Options.Url.Should().Be("http://localhost/auth/v1", + "the auth client must be built against the derived auth url, not the Gotrue default"); + client.Auth.Options.AutoRefreshToken.Should().BeFalse( + "the developer's AutoRefreshToken choice must reach the auth client"); + } + } + + [TestMethod] + public void SupabaseClient_ShouldWireTableToClientAuthHeaders_GivenFrom() { - UrlClient().From().Should().NotBeNull(); + var table = UrlClient().From(); + table.GetHeaders!().Should().ContainKey("apiKey").WhoseValue.Should().Be("test-key", + "queries built from the umbrella must carry its auth headers, or every request is unauthenticated"); } [TestMethod]