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
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ dart test test/specific_test.dart -n "test name pattern"

Build tests on the `supabase_test` package instead of writing mocks by hand:

- Answer HTTP calls with `MockSupabaseHttpClient` and its `stub`, `stubHandler`, `stubError` and `stubTable`/`stubRpc`/`stubStorage*` helpers. Do not subclass `BaseClient` in a test. Match methods with `HttpMethod.post.value` and friends rather than raw strings.
- Answer HTTP calls with `MockSupabaseHttpClient` and its `stub`, `stubHandler`, `stubError` and `stubTable`/`stubRpc`/`stubStorage*` helpers. Do not subclass `BaseClient` in a test. Match methods with `HttpMethod.post` and friends.
- Assert on what was sent with `requests` and `requestsTo(path, method:)`.
- Put a client into a signed-in state with `signInTestUser`, and build server payloads with `testUserJson`, `testSessionResponseJson` and `unsignedTestJwt`. Do not hand-assemble JWTs or session JSON.
- Construct clients with `testSupabaseClient` where a whole `SupabaseClient` is needed.
Expand Down
22 changes: 20 additions & 2 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ it also takes care of several of the database changes further down. After it com
[the changes the compiler will not catch](#changes-the-compiler-will-not-catch), and then every
breaking change grouped by area: [packages and setup](#packages-and-setup),
[database](#database), [auth](#auth), [realtime](#realtime), [storage](#storage),
[errors](#errors), [requests, retries and tokens](#requests-retries-and-tokens) and
[shared types](#shared-types).
[errors](#errors), [requests, retries and tokens](#requests-retries-and-tokens),
[shared types](#shared-types) and [testing](#testing).

### Install the package skills

Expand Down Expand Up @@ -2048,3 +2048,21 @@ unaffected. For `functions.invoke`, two things changed:
(3 to 5). This is not a compile error, so replace any persisted `index` with `name`.

The enum also exposes `value`, the uppercase wire form, in place of `method.name.toUpperCase()`.

### Testing

#### `supabase_test` matches methods with `HttpMethod` and takes the `initializeTestSupabase` names

The `method` parameter of `MockSupabaseHttpClient.requestsTo` and of every `stub*` method is an
`HttpMethod` instead of a `String`. `testSupabaseClient` takes the same parameter names as
`initializeTestSupabase` from `package:supabase_flutter/testing.dart`.

| Before | After |
| ---------------------------------------- | ------------------------------------------------------ |
| `method: 'POST'` | `method: HttpMethod.post` |
| `testSupabaseClient(apiKey: key)` | `testSupabaseClient(publishableKey: key)` |
| `testSupabaseClient(realtime: realtime)` | `testSupabaseClient(realtimeTransport: realtime.call)` |

Both helpers now default to an `sb_publishable_` key instead of an unsigned anon JWT. A signed-out
edge function call therefore no longer sends an `Authorization` header, as with a production
publishable key. Pass a JWT as `publishableKey` if a test relies on the old headers.
4 changes: 2 additions & 2 deletions packages/supabase/test/client_authorization_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void main() {
);

final signIn = httpClient
.requestsTo('/auth/v1/token', method: HttpMethod.post.value)
.requestsTo('/auth/v1/token', method: HttpMethod.post)
.single;
expect(
signIn.headers[HttpHeader.authorization],
Expand Down Expand Up @@ -92,7 +92,7 @@ void main() {
await supabase.auth.getUser();

final request = httpClient
.requestsTo('/auth/v1/user', method: HttpMethod.get.value)
.requestsTo('/auth/v1/user', method: HttpMethod.get)
.single;
expect(
request.headers[HttpHeader.authorization],
Expand Down
7 changes: 5 additions & 2 deletions packages/supabase/test/realtime_access_token_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ void main() {

setUp(() {
httpClient = MockSupabaseHttpClient();
supabase = testSupabaseClient(httpClient: httpClient, apiKey: apiKey);
supabase = testSupabaseClient(
httpClient: httpClient,
publishableKey: apiKey,
);
});

tearDown(() => supabase.dispose());
Expand Down Expand Up @@ -64,7 +67,7 @@ void main() {
final aal2Token = accessTokenWith({'aal': 'aal2'});
httpClient.stub(
testSessionResponseJson(accessToken: aal2Token),
method: HttpMethod.post.value,
method: HttpMethod.post,
path: '/auth/v1/factors/factor-id/verify',
);

Expand Down
5 changes: 4 additions & 1 deletion packages/supabase/test/stream_insert_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ void main() {
setUp(() {
httpClient = MockSupabaseHttpClient();
realtime = MockRealtimeTransport();
supabase = testSupabaseClient(httpClient: httpClient, realtime: realtime);
supabase = testSupabaseClient(
httpClient: httpClient,
realtimeTransport: realtime.call,
);
});

tearDown(() async {
Expand Down
5 changes: 4 additions & 1 deletion packages/supabase/test/stream_order_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ void main() {
setUp(() {
httpClient = MockSupabaseHttpClient();
realtime = MockRealtimeTransport();
supabase = testSupabaseClient(httpClient: httpClient, realtime: realtime);
supabase = testSupabaseClient(
httpClient: httpClient,
realtimeTransport: realtime.call,
);
});

tearDown(() async {
Expand Down
5 changes: 4 additions & 1 deletion packages/supabase/test/stream_select_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ void main() {
setUp(() {
httpClient = MockSupabaseHttpClient();
realtime = MockRealtimeTransport();
supabase = testSupabaseClient(httpClient: httpClient, realtime: realtime);
supabase = testSupabaseClient(
httpClient: httpClient,
realtimeTransport: realtime.call,
);
});

tearDown(() async {
Expand Down
2 changes: 1 addition & 1 deletion packages/supabase/test/typed_postgres_changes_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void main() {

setUp(() {
realtime = MockRealtimeTransport();
supabase = testSupabaseClient(realtime: realtime);
supabase = testSupabaseClient(realtimeTransport: realtime.call);
channel = supabase.channel('todos');
});

Expand Down
5 changes: 4 additions & 1 deletion packages/supabase/test/typed_table_schema_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ void main() {
setUp(() {
httpClient = MockSupabaseHttpClient();
realtime = MockRealtimeTransport();
supabase = testSupabaseClient(httpClient: httpClient, realtime: realtime);
supabase = testSupabaseClient(
httpClient: httpClient,
realtimeTransport: realtime.call,
);
});

tearDown(() async {
Expand Down
11 changes: 5 additions & 6 deletions packages/supabase_auth/test/admin_sso_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:supabase_auth/supabase_auth.dart';
import 'package:supabase_common/supabase_common.dart';
import 'package:test/test.dart';

import 'utils.dart';
Expand Down Expand Up @@ -229,7 +228,7 @@ void main() {
final request = mockClient
.requestsTo(
'/admin/sso/providers',
method: HttpMethod.get.value,
method: HttpMethod.get,
)
.single;
expect(request.queryParameters, {'resource_id_prefix': 'ac'});
Expand All @@ -250,7 +249,7 @@ void main() {
final request = mockClient
.requestsTo(
'/admin/sso/providers',
method: HttpMethod.post.value,
method: HttpMethod.post,
)
.single;
expect(request.jsonBody, {
Expand All @@ -268,7 +267,7 @@ void main() {
expect(
mockClient.requestsTo(
'/admin/sso/providers/resource_acme',
method: HttpMethod.get.value,
method: HttpMethod.get,
),
hasLength(1),
);
Expand All @@ -288,7 +287,7 @@ void main() {
final request = mockClient
.requestsTo(
'/admin/sso/providers/$_providerId',
method: HttpMethod.put.value,
method: HttpMethod.put,
)
.single;
expect(request.jsonBody, {
Expand All @@ -306,7 +305,7 @@ void main() {
expect(
mockClient.requestsTo(
'/admin/sso/providers/$_providerId',
method: HttpMethod.delete.value,
method: HttpMethod.delete,
),
hasLength(1),
);
Expand Down
3 changes: 1 addition & 2 deletions packages/supabase_auth/test/admin_update_factor_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:supabase_auth/supabase_auth.dart';
import 'package:supabase_common/supabase_common.dart';
import 'package:test/test.dart';

import 'utils.dart';
Expand Down Expand Up @@ -38,7 +37,7 @@ void main() {

final requests = httpClient.requestsTo(
'/admin/users/$userId/factors/$factorId',
method: HttpMethod.put.value,
method: HttpMethod.put,
);
expect(requests, hasLength(1));
expect(requests.single.jsonBody, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:async';

import 'package:supabase_auth/supabase_auth.dart';
import 'package:supabase_common/supabase_common.dart';
import 'package:test/test.dart';

import 'utils.dart';
Expand All @@ -28,7 +27,7 @@ void main() {
void stubRefreshRejection(String code) {
httpClient.stub(
rejection(code),
method: HttpMethod.post.value,
method: HttpMethod.post,
path: '/token',
query: {'grant_type': 'refresh_token'},
statusCode: 400,
Expand Down Expand Up @@ -152,7 +151,7 @@ void main() {
headers: apiVersionHeaders,
);
},
method: HttpMethod.post.value,
method: HttpMethod.post,
path: '/token',
query: {'grant_type': 'refresh_token'},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void main() {
}) {
httpClient.stub(
body,
method: HttpMethod.post.value,
method: HttpMethod.post,
path: '/token',
query: refreshQuery,
statusCode: statusCode,
Expand All @@ -80,7 +80,7 @@ void main() {
void stubRefreshHtml(int statusCode, {int? times}) {
httpClient.stubText(
htmlPage,
method: HttpMethod.post.value,
method: HttpMethod.post,
path: '/token',
query: refreshQuery,
statusCode: statusCode,
Expand Down
3 changes: 1 addition & 2 deletions packages/supabase_auth/test/update_user_race_test.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:async';

import 'package:supabase_auth/supabase_auth.dart';
import 'package:supabase_common/supabase_common.dart';
import 'package:test/test.dart';

import 'utils.dart';
Expand All @@ -24,7 +23,7 @@ void main() {
testUserJson(email: 'updated@email.com'),
);
},
method: HttpMethod.put.value,
method: HttpMethod.put,
path: '/user',
);
client = AuthClient(
Expand Down
25 changes: 7 additions & 18 deletions packages/supabase_flutter/lib/testing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
///
/// [initializeTestSupabase] initializes [Supabase] the way a test needs it:
/// in-memory persistence, no token auto refresh, no deep link detection, and
/// an unsigned test key. Answer the HTTP traffic with a
/// a test publishable key. Answer the HTTP traffic with a
/// `MockSupabaseHttpClient` from `package:supabase_test`, and realtime with
/// its `MockRealtimeTransport`:
///
Expand All @@ -25,20 +25,17 @@
/// ```
library;

import 'dart:convert';

import 'package:http/http.dart';
import 'package:meta/meta.dart';

import 'supabase_flutter.dart';

/// The publishable key [initializeTestSupabase] uses by default: an unsigned
/// JWT carrying the `anon` role, which the mocked services never verify.
/// The publishable key [initializeTestSupabase] uses by default.
///
/// It carries the `sb_publishable_` prefix, so signed-out requests carry the
/// headers a production publishable key produces.
@visibleForTesting
final String testPublishableKey = _unsignedJwt({
'role': 'anon',
'iss': 'supabase_flutter_testing',
});
const testPublishableKey = 'sb_publishable_supabase_flutter_testing';

/// Initializes [Supabase] for a test and returns the instance.
///
Expand All @@ -49,7 +46,7 @@ final String testPublishableKey = _unsignedJwt({
/// touched, and defaults [publishableKey] to [testPublishableKey]. Pass a
/// `MockSupabaseHttpClient` as [httpClient] to answer the requests, and the
/// `call` method of a `MockRealtimeTransport` as [realtimeTransport] to
/// answer the realtime traffic.
/// answer the realtime traffic, the same way `testSupabaseClient` takes them.
///
/// Dispose the instance when the test ends, for example with
/// `addTearDown(supabase.dispose)`, so the next test can initialize again.
Expand All @@ -76,11 +73,3 @@ Future<Supabase> initializeTestSupabase({
),
);
}

String _unsignedJwt(Map<String, dynamic> claims) {
final header = base64Url.encode(
utf8.encode(jsonEncode({'alg': 'HS256', 'typ': 'JWT'})),
);
final payload = base64Url.encode(utf8.encode(jsonEncode(claims)));
return '$header.$payload.AAAA';
}
16 changes: 8 additions & 8 deletions packages/supabase_flutter/test/restore_credential_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,25 +107,25 @@ void main() {
httpClient = MockSupabaseHttpClient()
..stub(
_registrationOptions(),
method: HttpMethod.post.value,
method: HttpMethod.post,
path: _registrationOptionsPath,
)
..stub(
_passkeyJson('Google Password Manager'),
method: HttpMethod.post.value,
method: HttpMethod.post,
path: _registrationVerifyPath,
)
..stubHandler(
(request) {
final body = request.jsonBody as Map<String, dynamic>;
return jsonResponse(_passkeyJson(body['friendly_name'] as String));
},
method: HttpMethod.patch.value,
method: HttpMethod.patch,
path: _passkeyPath,
)
..stub(
_authenticationOptions,
method: HttpMethod.post.value,
method: HttpMethod.post,
path: _authenticationOptionsPath,
)
..stub(
Expand All @@ -135,7 +135,7 @@ void main() {
'role': 'authenticated',
}),
),
method: HttpMethod.post.value,
method: HttpMethod.post,
path: _authenticationVerifyPath,
);
client = AuthClient(
Expand Down Expand Up @@ -186,7 +186,7 @@ void main() {
test('uses a custom friendly name for the key and account label', () async {
httpClient.stub(
_registrationOptions(withUserName: false),
method: HttpMethod.post.value,
method: HttpMethod.post,
path: _registrationOptionsPath,
);
final restore = _FakeRestoreCredential();
Expand Down Expand Up @@ -224,7 +224,7 @@ void main() {
'error_code': 'validation_failed',
'msg': 'Invalid credential',
},
method: HttpMethod.post.value,
method: HttpMethod.post,
path: _registrationVerifyPath,
statusCode: 400,
);
Expand All @@ -249,7 +249,7 @@ void main() {
'error_code': 'unexpected_failure',
'msg': 'Database error',
},
method: HttpMethod.patch.value,
method: HttpMethod.patch,
path: _passkeyPath,
statusCode: 500,
);
Expand Down
Loading
Loading