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
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using FluentAssertions.Execution;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Storage.Tests.Support;
using Supabase.Storage;
using Supabase.Storage.Common;
using Supabase.Storage.Exceptions;
using Supabase.Storage.ListAnalyticsBuckets;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;

namespace Storage.Tests.ListAnalyticsBuckets;

/// <summary>
/// Contract tests for listing analytics buckets: the request the client puts on the wire (method, path, headers,
/// the query built from <see cref="ListAnalyticsBucketsOptions"/>) and how each response the Iceberg bucket route
/// can give is surfaced. Bodies are pinned from the storage route source (<c>GET /iceberg/bucket</c>), not observed
/// on a running service.
/// </summary>
[TestClass]
[TestCategory("Contract")]
public class ListAnalyticsBucketsContractTests
{
private const string BucketPath = "/storage/v1/iceberg/bucket";
private const string ListedBody = "[{\"id\":\"analytics-data\",\"name\":\"analytics-data\",\"created_at\":\"2026-11-24T10:00:00.000Z\",\"updated_at\":\"2026-11-25T11:30:00.000Z\"},{\"id\":\"events\",\"name\":\"events\",\"created_at\":\"2026-11-24T10:00:00.000Z\",\"updated_at\":\"2026-11-24T10:00:00.000Z\"}]";
private WireMockServer server = null!;
private Client client = null!;

[TestInitialize]
public void TestInitialize()
{
this.server = WireMockServer.Start();
this.client = new Client($"{this.server.Url}/storage/v1", new Dictionary<string, string>
{
{ "Authorization", "Bearer service-key" }
});
}

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

[TestMethod]
public async Task ListBucketsAsync_ShouldSendBodylessGetToIcebergBucketRoute()
{
this.Respond(200, ListedBody);
await this.client.Analytics.ListBucketsAsync();
var request = this.server.LogEntries.Should().ContainSingle().Which.RequestMessage!;
using (new AssertionScope())
{
request.Method.Should().Be("GET", "the Iceberg bucket route lists with GET /bucket");
request.Path.Should().Be(BucketPath);
request.Body.Should().BeNullOrEmpty("the options travel in the query string");
request.Headers!["Authorization"].Should().ContainSingle().Which.Should().Be("Bearer service-key");
}
}

[TestMethod]
public async Task ListBucketsAsync_ShouldSendNoQuery_GivenNoOptions()
{
this.Respond(200, ListedBody);
await this.client.Analytics.ListBucketsAsync();
this.server.LogEntries.Should().ContainSingle().Which.RequestMessage!.Query
.Should().BeNullOrEmpty("the service owns the defaults: every bucket, sorted by name ascending");
}

[TestMethod]
public async Task ListBucketsAsync_ShouldSendNoQuery_GivenEmptyOptions()
{
this.Respond(200, ListedBody);
await this.client.Analytics.ListBucketsAsync(new ListAnalyticsBucketsOptions());
this.server.LogEntries.Should().ContainSingle().Which.RequestMessage!.Query
.Should().BeNullOrEmpty("an option left unset is not sent");
}

[TestMethod]
public async Task ListBucketsAsync_ShouldSendEveryOptionAsQueryParameter()
{
this.Respond(200, ListedBody);
await this.client.Analytics.ListBucketsAsync(new ListAnalyticsBucketsOptions
{
Limit = 10,
Offset = 20,
Sort = new AnalyticsBucketSort(AnalyticsBucketSortColumn.CreatedAt, ListSortDirection.Descending),
Search = "analytics",
});
this.SentQuery().Should().BeEquivalentTo(new Dictionary<string, string>
{
{ "limit", "10" },
{ "offset", "20" },
{ "sortColumn", "created_at" },
{ "sortOrder", "desc" },
{ "search", "analytics" },
}, "the route reads limit, offset, sortColumn, sortOrder and search from the query string");
}

[TestMethod]
[DataRow(AnalyticsBucketSortColumn.Name, "name")]
[DataRow(AnalyticsBucketSortColumn.CreatedAt, "created_at")]
[DataRow(AnalyticsBucketSortColumn.UpdatedAt, "updated_at")]
public async Task ListBucketsAsync_ShouldSendTheWireNameOfTheSortColumn(AnalyticsBucketSortColumn column, string expected)
{
this.Respond(200, ListedBody);
await this.client.Analytics.ListBucketsAsync(new ListAnalyticsBucketsOptions { Sort = new AnalyticsBucketSort(column) });
this.SentQuery()["sortColumn"].Should().Be(expected, "the route's sortColumn enum is the snake_case column name");
}

[TestMethod]
[DataRow(ListSortDirection.Ascending, "asc")]
[DataRow(ListSortDirection.Descending, "desc")]
public async Task ListBucketsAsync_ShouldSendTheWireNameOfTheSortDirection(ListSortDirection direction, string expected)
{
this.Respond(200, ListedBody);
await this.client.Analytics.ListBucketsAsync(new ListAnalyticsBucketsOptions { Sort = new AnalyticsBucketSort(AnalyticsBucketSortColumn.Name, direction) });
this.SentQuery()["sortOrder"].Should().Be(expected, "the route's sortOrder enum is asc or desc");
}

[TestMethod]
public async Task ListBucketsAsync_ShouldSortAscending_GivenSortWithoutDirection()
{
this.Respond(200, ListedBody);
await this.client.Analytics.ListBucketsAsync(new ListAnalyticsBucketsOptions { Sort = new AnalyticsBucketSort(AnalyticsBucketSortColumn.UpdatedAt) });
this.SentQuery()["sortOrder"].Should().Be("asc", "a sort defaults to ascending");
}

[TestMethod]
public async Task ListBucketsAsync_ShouldEscapeTheSearchTerm()
{
this.Respond(200, ListedBody);
await this.client.Analytics.ListBucketsAsync(new ListAnalyticsBucketsOptions { Search = "a&b=c d+e" });
this.SentQuery()["search"].Should().Be("a&b=c d+e", "the search term reaches the service as one query value");
}

[TestMethod]
public async Task ListBucketsAsync_ShouldReturnTheListedBuckets()
{
this.Respond(200, ListedBody);
var buckets = await this.client.Analytics.ListBucketsAsync();
buckets.Should().BeEquivalentTo(new[]
{
new AnalyticsBucket
{
Name = "analytics-data",
CreatedAt = new DateTimeOffset(2026, 11, 24, 10, 0, 0, TimeSpan.Zero),
UpdatedAt = new DateTimeOffset(2026, 11, 25, 11, 30, 0, TimeSpan.Zero),
},
new AnalyticsBucket
{
Name = "events",
CreatedAt = new DateTimeOffset(2026, 11, 24, 10, 0, 0, TimeSpan.Zero),
UpdatedAt = new DateTimeOffset(2026, 11, 24, 10, 0, 0, TimeSpan.Zero),
},
}, options => options.WithStrictOrdering(), "the service's order is kept");
}

[TestMethod]
public async Task ListBucketsAsync_ShouldReturnEmptyList_GivenNoBuckets()
{
this.Respond(200, "[]");
(await this.client.Analytics.ListBucketsAsync()).Should().BeEmpty();
}

[TestMethod]
public async Task ListBucketsAsync_ShouldReturnEmptyList_GivenNullBody()
{
this.Respond(200, "null");
(await this.client.Analytics.ListBucketsAsync()).Should().BeEmpty("a returned collection is never null");
}

[TestMethod]
public async Task ListBucketsAsync_ShouldThrowStorageException_GivenEmptySuccessBody()
{
this.Respond(200, "");
var act = () => this.client.Analytics.ListBucketsAsync();
await act.Should().ThrowAsync<SupabaseStorageException>("a success without a JSON array is not a listing");
}

[TestMethod]
public void ListBucketsAsync_ShouldNotResumeOnTheCallersContext()
{
this.Respond(200, ListedBody);
BlockingCaller.Completes(() => this.client.Analytics.ListBucketsAsync())
.Should().BeTrue("the SDK never resumes on the caller's synchronization context");
}

[TestMethod]
public async Task ListBucketsAsync_ShouldReadHeadersAtCallTime()
{
this.Respond(200, ListedBody);
var analytics = this.client.Analytics;
this.client.GetHeaders = () => new Dictionary<string, string> { { "apikey", "rotated" } };
await analytics.ListBucketsAsync();
this.server.LogEntries.Should().ContainSingle().Which.RequestMessage!.Headers!["apikey"]
.Should().ContainSingle().Which.Should().Be("rotated");
}

[TestMethod]
public async Task ListBucketsAsync_ShouldThrowStorageException_GivenRejectedPagination()
{
this.Respond(400, "{\"statusCode\":\"400\",\"error\":\"Bad Request\",\"message\":\"querystring/limit must be >= 1\"}");
var act = () => this.client.Analytics.ListBucketsAsync(new ListAnalyticsBucketsOptions { Limit = 0 });
(await act.Should().ThrowAsync<SupabaseStorageException>("the service validates the pagination range")).Which.StatusCode
.Should().Be(400);
}

[TestMethod]
public async Task ListBucketsAsync_ShouldThrowNotAuthorized_GivenUnauthorizedCaller()
{
this.Respond(401, "{\"message\":\"Unauthorized\",\"statusCode\":\"401\"}");
var act = () => this.client.Analytics.ListBucketsAsync();
(await act.Should().ThrowAsync<SupabaseStorageException>()).Which.Reason
.Should().Be(FailureHint.Reason.NotAuthorized);
}

[TestMethod]
public async Task ListBucketsAsync_ShouldThrowStorageException_GivenFeatureNotEnabled()
{
this.Respond(403, "{\"error\":{\"message\":\"Iceberg buckets are not enabled for this tenant\",\"type\":\"FeatureNotEnabled\",\"code\":403}}");
var act = () => this.client.Analytics.ListBucketsAsync();
(await act.Should().ThrowAsync<SupabaseStorageException>()).Which.StatusCode.Should().Be(403);
}

[TestMethod]
public async Task ListBucketsAsync_ShouldThrowInternal_GivenServerFailure()
{
this.Respond(500, "{\"error\":{\"message\":\"boom\",\"type\":\"InternalError\",\"code\":500}}");
var act = () => this.client.Analytics.ListBucketsAsync();
(await act.Should().ThrowAsync<SupabaseStorageException>()).Which.Reason
.Should().Be(FailureHint.Reason.Internal);
}

[TestMethod]
public async Task ListBucketsAsync_ShouldCancelTheRequest_GivenCanceledToken()
{
this.Respond(200, ListedBody);
using var cts = new CancellationTokenSource();
cts.Cancel();
var act = () => this.client.Analytics.ListBucketsAsync(cancellationToken: cts.Token);
await act.Should().ThrowAsync<OperationCanceledException>("the token must reach the HTTP call");
}

private IDictionary<string, string> SentQuery()
{
var query = this.server.LogEntries.Should().ContainSingle().Which.RequestMessage!.Query!;
var sent = new Dictionary<string, string>();
foreach (var parameter in query)
sent[parameter.Key] = parameter.Value.ToString();
return sent;
}

private void Respond(int status, string body) =>
this.server.Given(Request.Create().WithPath(BucketPath).UsingGet())
.RespondWith(Response.Create().WithStatusCode(status).WithHeader("Content-Type", "application/json").WithBody(body));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Supabase.Storage;
using Supabase.Storage.ListAnalyticsBuckets;

namespace Storage.Tests.ListAnalyticsBuckets;

/// <summary>
/// Unit tests for the only check <c>Analytics.ListBucketsAsync</c> makes before the wire. Pagination ranges are
/// validated by the service.
/// </summary>
[TestClass]
[TestCategory("Unit")]
public class ListAnalyticsBucketsTests
{
private const AnalyticsBucketSortColumn UndefinedColumn = (AnalyticsBucketSortColumn) 42;

[TestMethod]
public async Task ListBucketsAsync_ShouldThrowArgumentOutOfRange_GivenUndefinedSortColumn()
{
var sut = new Client("http://localhost:1/storage/v1");
var options = new ListAnalyticsBucketsOptions { Sort = new AnalyticsBucketSort(UndefinedColumn) };
(await FluentActions.Awaiting(() => sut.Analytics.ListBucketsAsync(options))
.Should().ThrowAsync<ArgumentOutOfRangeException>("the route only sorts by name, created_at or updated_at"))
.WithMessage("The sort column is not one the service can sort analytics buckets by.*");
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Text.Json.Serialization;

namespace Supabase.Storage.CreateAnalyticsBucket;
namespace Supabase.Storage.Common;

/// <summary>
/// An analytics bucket, the Apache Iceberg backed kind of Supabase Storage bucket.
Expand Down
25 changes: 24 additions & 1 deletion packages/Storage/Storage/Interfaces/IStorageAnalyticsApi.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Supabase.Core.Attributes;
using Supabase.Storage.CreateAnalyticsBucket;
using Supabase.Storage.Common;
using Supabase.Storage.ListAnalyticsBuckets;

namespace Supabase.Storage.Interfaces;

Expand All @@ -28,6 +30,27 @@ public interface IStorageAnalyticsApi
[ImplementedCapability("storage.analytics.create_analytics_bucket")]
Task<AnalyticsBucket> CreateBucketAsync(string name, CancellationToken cancellationToken = default);

/// <summary>
/// Lists the analytics buckets of the project.
/// </summary>
/// <param name="options">Pagination, sort and search options. Without them the service returns every bucket, sorted by name in ascending order.</param>
/// <param name="cancellationToken">Token used to cancel the request.</param>
/// <returns>The buckets, in the order the service returned them. Empty when there are none.</returns>
/// <exception cref="ArgumentOutOfRangeException">The sort column is not a defined <see cref="AnalyticsBucketSortColumn"/>.</exception>
/// <exception cref="Exceptions.SupabaseStorageException">The service rejected the request, for example because the pagination is out of range, the caller is not authorized or the feature is not enabled.</exception>
/// <example>
/// <code>
/// var storage = new Client(url, headers);
/// var buckets = await storage.Analytics.ListBucketsAsync(new ListAnalyticsBucketsOptions
/// {
/// Limit = 10,
/// Sort = new AnalyticsBucketSort(AnalyticsBucketSortColumn.CreatedAt, ListSortDirection.Descending),
/// });
/// </code>
/// </example>
[ImplementedCapability("storage.analytics.list_analytics_buckets")]
Task<IReadOnlyList<AnalyticsBucket>> ListBucketsAsync(ListAnalyticsBucketsOptions? options = null, CancellationToken cancellationToken = default);

/// <summary>
/// Deletes an empty analytics bucket.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.ComponentModel;

namespace Supabase.Storage.ListAnalyticsBuckets;

/// <summary>
/// The order analytics buckets are listed in.
/// </summary>
/// <param name="Column">The column to sort by.</param>
/// <param name="Direction">The sort direction. Ascending unless specified.</param>
public sealed record AnalyticsBucketSort(AnalyticsBucketSortColumn Column, ListSortDirection Direction = ListSortDirection.Ascending);
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Supabase.Storage.ListAnalyticsBuckets;

/// <summary>
/// A column analytics buckets can be sorted by.
/// </summary>
public enum AnalyticsBucketSortColumn
{
/// <summary>The bucket name.</summary>
Name,

/// <summary>When the bucket was created.</summary>
CreatedAt,

/// <summary>When the bucket was last updated.</summary>
UpdatedAt,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Supabase.Storage.ListAnalyticsBuckets;

/// <summary>
/// Pagination, sort and search options for listing analytics buckets. An option left unset is not sent, and the
/// service applies its default: every bucket, sorted by name in ascending order.
/// </summary>
public sealed record ListAnalyticsBucketsOptions
{
/// <summary>The maximum number of buckets to return. The service requires at least 1.</summary>
public int? Limit { get; init; }

/// <summary>The number of buckets to skip. The service requires at least 0.</summary>
public int? Offset { get; init; }

/// <summary>The order the buckets are listed in.</summary>
public AnalyticsBucketSort? Sort { get; init; }

/// <summary>Returns only the buckets whose name contains this text, compared case-sensitively.</summary>
public string? Search { get; init; }
}
Loading