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
65 changes: 65 additions & 0 deletions packages/Gotrue/Gotrue.Tests/Stateless/StatelessSignOutTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#region

using System.Threading.Tasks;
using FluentAssertions;
using Gotrue.Tests.Support;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Supabase.Gotrue;
using Supabase.Gotrue.Exceptions;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using static Supabase.Gotrue.Constants;
using static Supabase.Gotrue.StatelessClient;

#endregion

namespace Gotrue.Tests.Stateless;

/// <summary>
/// Stateless sign-out sends the requested scope to <c>/logout</c>, global by default, and throws when the request fails.
/// </summary>
[TestClass]
[TestCategory("Contract")]
public class StatelessSignOutTests
{
private MockGotrueServer server = null!;

[TestInitialize]
public void TestInitialize() => this.server = new MockGotrueServer();

[TestCleanup]
public void TestCleanup() => this.server.Dispose();

[TestMethod]
[DataRow(SignOutScope.Global, "global")]
[DataRow(SignOutScope.Local, "local")]
[DataRow(SignOutScope.Others, "others")]
public async Task SignOut_ShouldSendTheRequestedScope(SignOutScope scope, string expected)
{
this.StubLogout(200);
await new StatelessClient().SignOut("user-access-token", this.Options(), scope);
this.server.VerifySingleReceivedRequest().WithQueryParam("scope", expected);
}

[TestMethod]
public async Task SignOut_ShouldSendGlobalScope_GivenNoScope()
{
this.StubLogout(200);
await new StatelessClient().SignOut("user-access-token", this.Options());
this.server.VerifySingleReceivedRequest().WithQueryParam("scope", "global");
}

[TestMethod]
public async Task SignOut_ShouldThrow_GivenErrorResponse()
{
this.StubLogout(401);
var signOut = () => new StatelessClient().SignOut("user-access-token", this.Options(), SignOutScope.Local);
await signOut.Should().ThrowAsync<GotrueException>();
}

private void StubLogout(int statusCode) =>
this.server.Given(Request.Create().WithPath("/logout").UsingPost())
.RespondWith(Response.Create().WithStatusCode(statusCode));

private StatelessClientOptions Options() => new() { Url = this.server.Url };
}
6 changes: 6 additions & 0 deletions packages/Gotrue/Gotrue/Interfaces/IGotrueStatelessClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ public interface IGotrueStatelessClient<TUser, TSession>
/// <returns></returns>
Task<bool> SignOut(string accessToken, StatelessClientOptions options);

/// <summary>
/// Signs out the user from the sessions in the given scope.
/// JWT tokens will still be valid for stateless auth until they expire.
/// </summary>
Task SignOut(string accessToken, StatelessClientOptions options, SignOutScope scope);

/// <summary>
/// Signs up a user
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions packages/Gotrue/Gotrue/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,5 @@ Supabase.Gotrue.Interfaces.IGotrueApi<TUser, TSession>.GetUserAsync(string! jwt,
Supabase.Gotrue.Interfaces.IGotrueClient<TUser, TSession>.GetClaimsAsync(string? jwt = null, Supabase.Gotrue.Claims.GetClaimsOptions? options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<Supabase.Gotrue.Claims.GetClaimsResponse!>!
Supabase.Gotrue.SignUpOptions.CaptchaToken.get -> string?
Supabase.Gotrue.SignUpOptions.CaptchaToken.set -> void
Supabase.Gotrue.Interfaces.IGotrueStatelessClient<TUser, TSession>.SignOut(string! accessToken, Supabase.Gotrue.StatelessClient.StatelessClientOptions! options, Supabase.Gotrue.Constants.SignOutScope scope) -> System.Threading.Tasks.Task!
Supabase.Gotrue.StatelessClient.SignOut(string! accessToken, Supabase.Gotrue.StatelessClient.StatelessClientOptions! options, Supabase.Gotrue.Constants.SignOutScope scope) -> System.Threading.Tasks.Task!
10 changes: 8 additions & 2 deletions packages/Gotrue/Gotrue/StatelessClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,17 @@ public async Task<bool> SignIn(string email, StatelessClientOptions options, Sig
/// <inheritdoc />
public async Task<bool> SignOut(string accessToken, StatelessClientOptions options)
{
var result = await this.GetApi(options).SignOut(accessToken);
result.ResponseMessage?.EnsureSuccessStatusCode();
await this.SignOut(accessToken, options, SignOutScope.Global).ConfigureAwait(false);
return true;
}

/// <inheritdoc />
public async Task SignOut(string accessToken, StatelessClientOptions options, SignOutScope scope)
{
var result = await this.GetApi(options).SignOut(accessToken, scope).ConfigureAwait(false);
result.ResponseMessage?.EnsureSuccessStatusCode();
}

/// <inheritdoc />
public async Task<Session?> VerifyOTP(string phone, string otpToken, StatelessClientOptions options, MobileOtpType type = MobileOtpType.SMS)
{
Expand Down
Loading