Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions SupabaseTests/StatelessClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
57 changes: 53 additions & 4 deletions SupabaseTests/SupabaseClientCompositionTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -37,13 +38,14 @@ private static Supabase.Client UrlClient(SupabaseOptions options = null) =>
private static Supabase.Client DiClient(
IGotrueClient<User, Session> auth = null,
IRealtimeClient<RealtimeSocket, RealtimeChannel> realtime = null,
IPostgrestClient postgrest = null) =>
IPostgrestClient postgrest = null,
SupabaseOptions options = null) =>
new(auth ?? Substitute.For<IGotrueClient<User, Session>>(),
realtime ?? RealtimeSubstitute(),
Substitute.For<IFunctionsClient>(),
postgrest ?? Substitute.For<IPostgrestClient>(),
Substitute.For<IStorageClient<Bucket, FileObject>>(),
new SupabaseOptions());
options ?? new SupabaseOptions());

// The umbrella writes Options.PostgrestClient during construction, so the substitute needs real Options.
private static IRealtimeClient<RealtimeSocket, RealtimeChannel> RealtimeSubstitute()
Expand Down Expand Up @@ -174,6 +176,30 @@ public void SupabaseClient_ShouldNotForwardTokenToRealtime_GivenAuthStateCarries
realtime.DidNotReceive().SetAuth(Arg.Any<string>());
}

[TestMethod]
public async Task SupabaseClient_ShouldRetrieveSessionAndConnectRealtime_GivenAutoConnectEnabled()
{
var auth = Substitute.For<IGotrueClient<User, Session>>();
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<IGotrueClient<User, Session>>();
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()
{
Expand All @@ -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<Models.Channel>().Should().NotBeNull();
var table = UrlClient().From<Models.Channel>();
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]
Expand Down
Loading