Skip to content

Commit

Permalink
Refactor proposal: use unnamed callback function in with*HttpPubApiCl…
Browse files Browse the repository at this point in the history
…ient methods. (#8490)
  • Loading branch information
isoos authored Jan 24, 2025
1 parent b38a4ca commit f15ec13
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 91 deletions.
6 changes: 3 additions & 3 deletions app/lib/fake/backend/fake_auth_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,10 @@ Future<String> _acquireCsrfToken({
///
/// The [email] is used to create an HTTP session and the related CSRF token is
/// extracted from the session, both are sent alongside the requests.
Future<R> withFakeAuthHttpPubApiClient<R>({
Future<R> withFakeAuthHttpPubApiClient<R>(
Future<R> Function(PubApiClient client) fn, {
required String email,
List<String>? scopes,
required Future<R> Function(PubApiClient client) fn,
String? pubHostedUrl,
Set<String>? experimental,
}) async {
Expand All @@ -440,8 +440,8 @@ Future<R> withFakeAuthHttpPubApiClient<R>({
sessionId: sessionId,
csrfToken: csrfToken,
pubHostedUrl: pubHostedUrl,
fn: fn,
experimental: experimental,
fn,
);
}

Expand Down
12 changes: 6 additions & 6 deletions app/lib/tool/test_profile/importer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Future<void> importProfile({
email: firstMemberEmail,
scopes: [webmasterScope],
pubHostedUrl: pubHostedUrl,
fn: (client) async {
(client) async {
try {
await client.createPublisher(p.name);
} on RequestException catch (e) {
Expand Down Expand Up @@ -98,7 +98,7 @@ Future<void> importProfile({
bearerToken: createFakeAuthTokenForEmail(uploaderEmail,
audience: activeConfiguration.pubClientAudience),
pubHostedUrl: pubHostedUrl,
fn: (client) => client.uploadPackageBytes(bytes),
(client) => client.uploadPackageBytes(bytes),
);
published = true;
} catch (e, st) {
Expand All @@ -123,7 +123,7 @@ Future<void> importProfile({
await withFakeAuthHttpPubApiClient(
email: activeEmail!,
pubHostedUrl: pubHostedUrl,
fn: (client) async {
(client) async {
// update publisher
if (testPackage.publisher != null) {
await client.setPackagePublisher(
Expand Down Expand Up @@ -155,7 +155,7 @@ Future<void> importProfile({
bearerToken:
createFakeServiceAccountToken(email: adminUserEmail ?? activeEmail),
pubHostedUrl: pubHostedUrl,
fn: (client) async {
(client) async {
await client.adminPostAssignedTags(
packageName,
PatchAssignedTags(
Expand All @@ -173,7 +173,7 @@ Future<void> importProfile({
await withFakeAuthHttpPubApiClient(
email: u.email,
pubHostedUrl: pubHostedUrl,
fn: (client) async {
(client) async {
// creates user (regardless of likes being specified)
await client.listPackageLikes();

Expand All @@ -197,7 +197,7 @@ Future<void> importProfile({
await withFakeAuthHttpPubApiClient(
email: userEmail,
pubHostedUrl: pubHostedUrl,
fn: (client) async {
(client) async {
await client.likePackage(p.name);
},
);
Expand Down
4 changes: 2 additions & 2 deletions app/lib/tool/utils/pub_api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ class _FakeTimeClient implements http.Client {
///
/// If [bearerToken], [sessionId] or [csrfToken] is specified, the corresponding
/// HTTP header will be sent alongside the request.
Future<R> withHttpPubApiClient<R>({
Future<R> withHttpPubApiClient<R>(
Future<R> Function(PubApiClient client) fn, {
String? bearerToken,
String? sessionId,
String? csrfToken,
String? pubHostedUrl,
Set<String>? experimental,
required Future<R> Function(PubApiClient client) fn,
}) async {
final httpClient = httpClientWithAuthorization(
tokenProvider: () async => bearerToken,
Expand Down
18 changes: 8 additions & 10 deletions app/test/account/consent_backend_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void main() {
await withFakeAuthHttpPubApiClient(
email: adminEmail,
pubHostedUrl: activeConfiguration.primarySiteUri.toString(),
fn: (client) async {
(client) async {
await client.invitePackageUploader(
'oxygen', InviteUploaderRequest(email: userAtPubDevEmail));
},
Expand Down Expand Up @@ -309,15 +309,13 @@ void main() {

group('Sanity check', () {
testWithProfile('consent parameter length', fn: () async {
await withFakeAuthHttpPubApiClient(
email: adminAtPubDevEmail,
fn: (c) async {
await expectApiException(
c.consentInfo('abcd' * 500),
status: 400,
code: 'InvalidInput',
);
});
await withFakeAuthHttpPubApiClient(email: adminAtPubDevEmail, (c) async {
await expectApiException(
c.consentInfo('abcd' * 500),
status: 400,
code: 'InvalidInput',
);
});
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion app/test/admin/exported_api_sync_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void main() {
}) async {
await withHttpPubApiClient(
bearerToken: siteAdminToken,
fn: (api) async {
(api) async {
await api.adminInvokeAction(
'exported-api-sync',
AdminInvokeActionArguments(arguments: {
Expand Down
2 changes: 1 addition & 1 deletion app/test/admin/moderate_package_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void main() {
group('Moderate package', () {
Future<ModerationCase> _report(String package) async {
await withHttpPubApiClient(
fn: (client) async {
(client) async {
await client.postReport(ReportForm(
email: '[email protected]',
subject: 'package:$package',
Expand Down
2 changes: 1 addition & 1 deletion app/test/admin/moderate_package_version_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void main() {
group('Moderate package version', () {
Future<ModerationCase> _report(String package, String version) async {
await withHttpPubApiClient(
fn: (client) async {
(client) async {
await client.postReport(ReportForm(
email: '[email protected]',
subject: 'package-version:$package/$version',
Expand Down
2 changes: 1 addition & 1 deletion app/test/admin/moderate_publisher_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void main() {
group('Moderate Publisher', () {
Future<ModerationCase> _report(String publisherId) async {
await withHttpPubApiClient(
fn: (client) async {
(client) async {
await client.postReport(ReportForm(
email: '[email protected]',
subject: 'publisher:$publisherId',
Expand Down
2 changes: 1 addition & 1 deletion app/test/admin/moderate_user_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void main() {
group('Moderate User', () {
Future<ModerationCase> _report(String package) async {
await withHttpPubApiClient(
fn: (client) async {
(client) async {
await client.postReport(account_api.ReportForm(
email: '[email protected]',
subject: 'package:$package',
Expand Down
2 changes: 1 addition & 1 deletion app/test/admin/moderation_case_resolve_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void main() {
required bool? apply,
}) async {
await withHttpPubApiClient(
fn: (client) async {
(client) async {
await client.postReport(ReportForm(
email: '[email protected]',
caseId: appealCaseId,
Expand Down
2 changes: 1 addition & 1 deletion app/test/admin/moderation_transparency_metrics_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void main() {
String? caseId,
}) async {
await withHttpPubApiClient(
fn: (client) async {
(client) async {
await client.postReport(account_api.ReportForm(
email: email ?? '[email protected]',
subject: 'package:$package',
Expand Down
32 changes: 16 additions & 16 deletions app/test/frontend/handlers/report_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void main() {
group('Report API test', () {
testWithProfile('unauthenticated email missing', fn: () async {
await withHttpPubApiClient(
fn: (client) async {
(client) async {
await expectApiException(
client.postReport(ReportForm(
message: 'Problem.',
Expand All @@ -100,7 +100,7 @@ void main() {
await withHttpPubApiClient(
sessionId: sessionId,
csrfToken: csrfToken,
fn: (client) async {
(client) async {
await expectApiException(
client.postReport(ReportForm(
email: '[email protected]',
Expand All @@ -118,7 +118,7 @@ void main() {

testWithProfile('subject missing', fn: () async {
await withHttpPubApiClient(
fn: (client) async {
(client) async {
await expectApiException(
client.postReport(ReportForm(
email: '[email protected]',
Expand All @@ -135,7 +135,7 @@ void main() {

testWithProfile('subject is invalid', fn: () async {
await withHttpPubApiClient(
fn: (client) async {
(client) async {
await expectApiException(
client.postReport(ReportForm(
email: '[email protected]',
Expand All @@ -153,7 +153,7 @@ void main() {

testWithProfile('package missing', fn: () async {
await withHttpPubApiClient(
fn: (client) async {
(client) async {
await expectApiException(
client.postReport(ReportForm(
email: '[email protected]',
Expand All @@ -171,7 +171,7 @@ void main() {

testWithProfile('version missing', fn: () async {
await withHttpPubApiClient(
fn: (client) async {
(client) async {
await expectApiException(
client.postReport(ReportForm(
email: '[email protected]',
Expand All @@ -189,7 +189,7 @@ void main() {

testWithProfile('publisher missing', fn: () async {
await withHttpPubApiClient(
fn: (client) async {
(client) async {
await expectApiException(
client.postReport(ReportForm(
email: '[email protected]',
Expand All @@ -212,7 +212,7 @@ void main() {
await withHttpPubApiClient(
sessionId: sessionId,
csrfToken: csrfToken,
fn: (client) async {
(client) async {
await expectApiException(
client.postReport(ReportForm(
subject: 'package:oxygen',
Expand All @@ -230,7 +230,7 @@ void main() {

testWithProfile('unauthenticated report success', fn: () async {
await withHttpPubApiClient(
fn: (client) async {
(client) async {
final msg = await client.postReport(ReportForm(
email: '[email protected]',
subject: 'package:oxygen',
Expand Down Expand Up @@ -259,7 +259,7 @@ void main() {
await withHttpPubApiClient(
sessionId: sessionId,
csrfToken: csrfToken,
fn: (client) async {
(client) async {
final msg = await client.postReport(ReportForm(
message: 'Huston, we have a problem.',
subject: 'package:oxygen',
Expand Down Expand Up @@ -308,7 +308,7 @@ void main() {

testWithProfile('failure: case does not exists', fn: () async {
await withHttpPubApiClient(
fn: (client) async {
(client) async {
await expectApiException(
client.postReport(ReportForm(
email: '[email protected]',
Expand All @@ -327,7 +327,7 @@ void main() {
testWithProfile('failure: case is not closed', fn: () async {
await _prepareApplied(status: ModerationStatus.pending);
await withHttpPubApiClient(
fn: (client) async {
(client) async {
await expectApiException(
client.postReport(ReportForm(
email: '[email protected]',
Expand All @@ -346,7 +346,7 @@ void main() {
testWithProfile('failure: subject is not on the case', fn: () async {
await _prepareApplied();
await withHttpPubApiClient(
fn: (client) async {
(client) async {
await expectApiException(
client.postReport(ReportForm(
email: '[email protected]',
Expand All @@ -371,7 +371,7 @@ void main() {

// first report: success
await withHttpPubApiClient(
fn: (client) async {
(client) async {
final msg = await client.postReport(ReportForm(
email: '[email protected]',
subject: 'package-version:oxygen/1.2.0',
Expand Down Expand Up @@ -400,7 +400,7 @@ void main() {
);

// second report: rejected
await withHttpPubApiClient(fn: (client) async {
await withHttpPubApiClient((client) async {
await expectApiException(
client.postReport(ReportForm(
email: '[email protected]',
Expand All @@ -423,7 +423,7 @@ void main() {

await withFakeAuthHttpPubApiClient(
email: '[email protected]',
fn: (client) async {
(client) async {
final msg = await client.postReport(ReportForm(
subject: 'package-version:oxygen/1.2.0',
caseId: 'case/1',
Expand Down
Loading

0 comments on commit f15ec13

Please sign in to comment.