|
| 1 | +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:_pub_shared/data/admin_api.dart'; |
| 6 | +import 'package:clock/clock.dart'; |
| 7 | +import 'package:pub_dev/fake/backend/fake_auth_provider.dart'; |
| 8 | +import 'package:pub_dev/package/backend.dart'; |
| 9 | +import 'package:pub_dev/package/models.dart'; |
| 10 | +import 'package:pub_dev/shared/datastore.dart'; |
| 11 | +import 'package:test/test.dart'; |
| 12 | + |
| 13 | +import '../package/backend_test_utils.dart'; |
| 14 | +import '../shared/handlers_test_utils.dart'; |
| 15 | +import '../shared/test_models.dart'; |
| 16 | +import '../shared/test_services.dart'; |
| 17 | + |
| 18 | +void main() { |
| 19 | + group('Reserve package', () { |
| 20 | + Future<void> _reserve( |
| 21 | + String package, { |
| 22 | + List<String>? emails, |
| 23 | + }) async { |
| 24 | + final api = createPubApiClient(authToken: siteAdminToken); |
| 25 | + await api.adminInvokeAction( |
| 26 | + 'package-reserve', |
| 27 | + AdminInvokeActionArguments(arguments: { |
| 28 | + 'package': package, |
| 29 | + if (emails != null) 'emails': emails.join(','), |
| 30 | + }), |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + testWithProfile('cannot reserve existing package', fn: () async { |
| 35 | + await expectApiException( |
| 36 | + _reserve('oxygen'), |
| 37 | + code: 'InvalidInput', |
| 38 | + status: 400, |
| 39 | + message: 'Package `oxygen` exists.', |
| 40 | + ); |
| 41 | + }); |
| 42 | + |
| 43 | + testWithProfile('cannot reserve ModeratedPackage', fn: () async { |
| 44 | + await dbService.commit(inserts: [ |
| 45 | + ModeratedPackage() |
| 46 | + ..id = 'pkg' |
| 47 | + ..name = 'pkg' |
| 48 | + ..moderated = clock.now() |
| 49 | + ..uploaders = <String>[] |
| 50 | + ..versions = <String>['1.0.0'] |
| 51 | + ]); |
| 52 | + await expectApiException( |
| 53 | + _reserve('pkg'), |
| 54 | + code: 'InvalidInput', |
| 55 | + status: 400, |
| 56 | + message: 'ModeratedPackage `pkg` exists.', |
| 57 | + ); |
| 58 | + }); |
| 59 | + |
| 60 | + testWithProfile('prevents non-whitelisted publishing', fn: () async { |
| 61 | + await _reserve('pkg'); |
| 62 | + |
| 63 | + final pubspecContent = generatePubspecYaml('pkg', '1.0.0'); |
| 64 | + final bytes = await packageArchiveBytes(pubspecContent: pubspecContent); |
| 65 | + await expectApiException( |
| 66 | + createPubApiClient(authToken: adminClientToken) |
| 67 | + .uploadPackageBytes(bytes), |
| 68 | + code: 'PackageRejected', |
| 69 | + status: 400, |
| 70 | + message: 'Package name pkg is reserved.', |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + testWithProfile('allows whitelisted publishing', fn: () async { |
| 75 | + await _reserve('pkg'); |
| 76 | + // update email addresses in a second request |
| 77 | + await _reserve( 'pkg', emails : [ '[email protected]']); |
| 78 | + |
| 79 | + final pubspecContent = generatePubspecYaml('pkg', '1.0.0'); |
| 80 | + final bytes = await packageArchiveBytes(pubspecContent: pubspecContent); |
| 81 | + await createPubApiClient(authToken: adminClientToken) |
| 82 | + .uploadPackageBytes(bytes); |
| 83 | + |
| 84 | + final rp = await packageBackend.lookupReservedPackage('pkg'); |
| 85 | + expect(rp, isNull); |
| 86 | + }); |
| 87 | + |
| 88 | + testWithProfile('allows Dart-team publishing', fn: () async { |
| 89 | + await _reserve('pkg'); |
| 90 | + |
| 91 | + final pubspecContent = generatePubspecYaml('pkg', '1.0.0'); |
| 92 | + final bytes = await packageArchiveBytes(pubspecContent: pubspecContent); |
| 93 | + await createPubApiClient( |
| 94 | + authToken : createFakeAuthTokenForEmail( '[email protected]', |
| 95 | + audience: 'fake-client-audience')) |
| 96 | + .uploadPackageBytes(bytes); |
| 97 | + |
| 98 | + final rp = await packageBackend.lookupReservedPackage('pkg'); |
| 99 | + expect(rp, isNull); |
| 100 | + }); |
| 101 | + }); |
| 102 | +} |
0 commit comments