Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fake test context #7437

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
81 changes: 81 additions & 0 deletions pkg/pub_integration/lib/src/fake_test_context.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:io';

import 'package:_pub_shared/pubapi.dart';
import 'package:http/http.dart' as http;
import 'package:path/path.dart' as p;
import 'fake_test_context_provider.dart';
import 'test_scenario.dart';

/// Create a [TestContext] for running integration [TestScenario]'s against
/// the fake server.
///
/// Returns a [TestContext] and a cleanup function.
Future<
({
TestContext context,
Future<void> Function() close,
})> createFakeTestContext() async {
final provider = await TestContextProvider.start();
final c = http.Client();
final tempDir = await Directory.systemTemp.createTemp('pub-integration-');

final userA = await provider.createTestUser(email: '[email protected]');
// TODO: publish _dummy_pkg to the fake server and make sure that it's
// owned by userA! Probably we can't just publish it here.
// Though it'd be ideal to use a prepopulated TestProfile thing.

return (
context: TestContext(
pubHostedUrl: provider.pubHostedUrl,
client: c,
publicApi: PubApiClient(provider.pubHostedUrl, client: c),
adminApi: PubApiClient(provider.pubHostedUrl,
client: _HttpClientWithAuthorization(c, () async {
// TODO: Return fake OIDC id_token for the admin user
throw UnimplementedError('Duplicate logic from test_models.dart');
})),
adminServiceAccount: TestServiceAccount(
email: '[email protected]',
getIdToken: () async {
// TODO: Return fake OIDC id_token for the admin user
throw UnimplementedError('Duplicate logic from test_models.dart');
}),
userA: userA,
userB: await provider.createTestUser(email: '[email protected]'),
testPackage: '_dummy_pkg',
tempDir: tempDir.path,
dartSdkRoot: p.dirname(p.dirname(Platform.resolvedExecutable)),
),
close: () async {
c.close();
await provider.close();
await tempDir.delete(recursive: true);
},
);
}

class _HttpClientWithAuthorization extends http.BaseClient {
final http.Client _client;
final Future<String> Function() _getBearerToken;

_HttpClientWithAuthorization(
this._client,
this._getBearerToken,
);

@override
Future<http.StreamedResponse> send(http.BaseRequest request) async {
final token = await _getBearerToken();
request.headers['Authorization'] = 'Bearer $token';
return await _client.send(request);
}

@override
void close() {
super.close();
}
}
61 changes: 40 additions & 21 deletions pkg/pub_integration/lib/src/test_scenario.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,70 +6,90 @@ import 'dart:async';

import 'package:_pub_shared/pubapi.dart';
import 'package:http/http.dart' as http;
import 'package:meta/meta.dart';
import 'package:puppeteer/puppeteer.dart';

@sealed
abstract class TestContext {
/// Interface that provides all the information and access required to run an
/// integration [TestScenario].
final class TestContext {
TestContext({
required this.pubHostedUrl,
required this.client,
required this.publicApi,
required this.adminApi,
required this.adminServiceAccount,
required this.userA,
required this.userB,
required this.testPackage,
required this.tempDir,
required this.dartSdkRoot,
});

/// `PUB_HOSTED_URL` for the server to be tested.
String get pubHostedUrl;
final String pubHostedUrl;

/// An [http.Client] that can be used for making requests.
///
/// This [client] will not retry failed requests, it is the responsibility to
/// retry requests.
http.Client get client;
final http.Client client;

/// A API client for accessing the API without authentication.
PubApiClient get publicApi;
final PubApiClient publicApi;

/// A API client for accessing the API with admin authentication.
///
/// Using this [PubApiClient] is equivalent to making a request authenticated
/// with [adminServiceAccount].
PubApiClient get adminApi;
final PubApiClient adminApi;

/// Service account that can call admin API on pub.dev
TestServiceAccount get adminServiceAccount;
final TestServiceAccount adminServiceAccount;

/// A user that can be used for testing.
///
/// This user is owner of testPackage;
TestUser get userA;
final TestUser userA;

/// A user that can be used for testing.
TestUser get userB;
final TestUser userB;

/// Name of a package that is owned by [userA], and which may be used for
/// testing.
String get testPackage => '_dummy_pkg';
final String testPackage;

/// Temporary directory which will be deleted after test are completed.
String get tempDir;
final String tempDir;

/// Path to the Dart SDK root folder to be used for testing.
String get dartSdkRoot;
final String dartSdkRoot;
}

@sealed
abstract class TestServiceAccount {
/// Interface that provides all the information and access required to run an
/// integration [TestScenario].
final class TestServiceAccount {
final Future<String> Function() _getIdToken;

TestServiceAccount({
required this.email,
required Future<String> Function() getIdToken,
}) : _getIdToken = getIdToken;

/// Get the identifier for this service account.
String get email;
final String email;

/// Get `id_token` impersonating this service account with audience set to
/// `https://pub.dev`.
///
/// Valid for at-least 30 minutes.
Future<String> getIdToken();
Future<String> getIdToken() => _getIdToken();
}

typedef WithBrowserPageCallbackFn = Future<T> Function<T>(
Future<T> Function(Page page) fn);
typedef ReadLatestEmailFn = FutureOr<String> Function();
typedef CreateCredentialsFn = FutureOr<Map<String, Object?>> Function();

@sealed
class TestUser {
final class TestUser {
/// The email of the given test user.
final String email;

Expand Down Expand Up @@ -99,8 +119,7 @@ class TestUser {
});
}

@sealed
class TestScenario {
final class TestScenario {
/// Human readable title for this test scenario.
final String title;

Expand Down