signInTestUser function

  1. @visibleForTesting
Future<Session> signInTestUser(
  1. AuthClient auth, {
  2. String userId = testUserId,
  3. String email = 'fake1@email.com',
  4. String role = 'authenticated',
  5. Map<String, dynamic> claims = const {},
  6. DateTime? expiresAt,
})

Puts auth into a signed-in state without any network traffic, and returns the resulting session.

The session carries an unsigned access token holding userId, role, email and claims, and a user built by testUserJson, so code under test observes currentUser, currentSession and the Authorization header exactly as after a real sign-in. Subscribers of onAuthStateChange receive a tokenRefreshed event.

expiresAt defaults to an hour from now and must lie in the future; to exercise expiry handling, build an expired session from the fixtures directly instead.

final supabase = testSupabaseClient(httpClient: httpClient);
final session = await signInTestUser(supabase.auth);

expect(supabase.auth.currentUser?.id, session.user.id);

Implementation

@visibleForTesting
Future<Session> signInTestUser(
  AuthClient auth, {
  String userId = testUserId,
  String email = 'fake1@email.com',
  String role = 'authenticated',
  Map<String, dynamic> claims = const {},
  DateTime? expiresAt,
}) async {
  final expiry = expiresAt ?? DateTime.now().add(const Duration(hours: 1));
  final accessToken = unsignedTestJwt({
    'exp': expiry.millisecondsSinceEpoch ~/ 1000,
    'sub': userId,
    'role': role,
    'email': email,
    ...claims,
  });
  final response = await auth.recoverSession(
    jsonEncode(
      testSessionResponseJson(
        accessToken: accessToken,
        user: testUserJson(id: userId, email: email),
      ),
    ),
  );
  return response.session!;
}