Index: utils/tests/pub/pub_uploader_test.dart |
diff --git a/utils/tests/pub/pub_uploader_test.dart b/utils/tests/pub/pub_uploader_test.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..7fb7565f5bceb6bcf8e3a91a91e31dc6dd391329 |
--- /dev/null |
+++ b/utils/tests/pub/pub_uploader_test.dart |
@@ -0,0 +1,186 @@ |
+// Copyright (c) 2012, 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. |
+ |
+library pub_uploader_test; |
+ |
+import 'dart:io'; |
+import 'dart:json'; |
+ |
+import 'test_pub.dart'; |
+import '../../../pkg/unittest/lib/unittest.dart'; |
+import '../../pub/utils.dart'; |
+import '../../pub/io.dart'; |
+ |
+final USAGE_STRING = ''' |
+ Manage uploaders for a package on pub.dartlang.org. |
+ |
+ Usage: pub uploader [options] {add/remove} <email> |
+ --server The package server on which the package is hosted |
+ (defaults to "https://pub.dartlang.org") |
+ |
+ --package The package whose uploaders will be modified |
+ (defaults to the current package) |
+ '''; |
+ |
+ScheduledProcess startPubUploader(ScheduledServer server, List<String> args) { |
+ var tokenEndpoint = server.url.transform((url) => |
+ url.resolve('/token').toString()); |
+ args = flatten(['uploader', '--server', tokenEndpoint, args]); |
+ return startPub(args: args, tokenEndpoint: tokenEndpoint); |
+} |
+ |
+main() { |
+ group('displays usage', () { |
+ test('when run with no arguments', () => |
+ runPub(args: ['uploader'], output: USAGE_STRING, exitCode: 64)); |
+ |
+ test('when run with only a command', () => |
+ runPub(args: ['uploader', 'add'], output: USAGE_STRING, exitCode: 64)); |
+ |
+ test('when run with an invalid command', () => runPub( |
+ args: ['uploader', 'foo', 'email'], |
+ output: USAGE_STRING, exitCode: 64)); |
+ }); |
+ |
+ test('adds an uploader', () { |
+ var server = new ScheduledServer(); |
+ credentialsFile(server, 'access token').scheduleCreate(); |
+ var pub = startPubUploader(server, ['--package', 'pkg', 'add', 'email']); |
+ |
+ server.handle('POST', '/packages/pkg/uploaders.json', (request, response) { |
+ expect(consumeInputStream(request.inputStream).transform((bodyBytes) { |
+ expect(new String.fromCharCodes(bodyBytes), equals('email=email')); |
+ |
+ response.headers.contentType = new ContentType("application", "json"); |
+ response.outputStream.writeString(JSON.stringify({ |
+ 'success': {'message': 'Good job!'} |
+ })); |
+ response.outputStream.close(); |
+ }), completes); |
+ }); |
+ |
+ expectLater(pub.nextLine(), equals('Good job!')); |
+ pub.shouldExit(0); |
+ |
+ run(); |
+ }); |
+ |
+ test('removes an uploader', () { |
+ var server = new ScheduledServer(); |
+ credentialsFile(server, 'access token').scheduleCreate(); |
+ var pub = startPubUploader(server, ['--package', 'pkg', 'remove', 'email']); |
+ |
+ server.handle('DELETE', '/packages/pkg/uploaders/email.json', |
+ (request, response) { |
+ response.headers.contentType = new ContentType("application", "json"); |
+ response.outputStream.writeString(JSON.stringify({ |
+ 'success': {'message': 'Good job!'} |
+ })); |
+ response.outputStream.close(); |
+ }); |
+ |
+ expectLater(pub.nextLine(), equals('Good job!')); |
+ pub.shouldExit(0); |
+ |
+ run(); |
+ }); |
+ |
+ test('defaults to the current package', () { |
+ normalPackage.scheduleCreate(); |
+ |
+ var server = new ScheduledServer(); |
+ credentialsFile(server, 'access token').scheduleCreate(); |
+ var pub = startPubUploader(server, ['add', 'email']); |
+ |
+ server.handle('POST', '/packages/test_pkg/uploaders.json', |
+ (request, response) { |
+ response.headers.contentType = new ContentType("application", "json"); |
+ response.outputStream.writeString(JSON.stringify({ |
+ 'success': {'message': 'Good job!'} |
+ })); |
+ response.outputStream.close(); |
+ }); |
+ |
+ expectLater(pub.nextLine(), equals('Good job!')); |
+ pub.shouldExit(0); |
+ |
+ run(); |
+ }); |
+ |
+ test('add provides an error', () { |
+ var server = new ScheduledServer(); |
+ credentialsFile(server, 'access token').scheduleCreate(); |
+ var pub = startPubUploader(server, ['--package', 'pkg', 'add', 'email']); |
+ |
+ server.handle('POST', '/packages/pkg/uploaders.json', (request, response) { |
+ response.statusCode = 400; |
+ response.headers.contentType = new ContentType("application", "json"); |
+ response.outputStream.writeString(JSON.stringify({ |
+ 'error': {'message': 'Bad job!'} |
+ })); |
+ response.outputStream.close(); |
+ }); |
+ |
+ expectLater(pub.nextErrLine(), equals('Bad job!')); |
+ pub.shouldExit(1); |
+ |
+ run(); |
+ }); |
+ |
+ test('remove provides an error', () { |
+ var server = new ScheduledServer(); |
+ credentialsFile(server, 'access token').scheduleCreate(); |
+ var pub = startPubUploader(server, ['--package', 'pkg', 'remove', 'email']); |
+ |
+ server.handle('DELETE', '/packages/pkg/uploaders/email.json', |
+ (request, response) { |
+ response.statusCode = 400; |
+ response.headers.contentType = new ContentType("application", "json"); |
+ response.outputStream.writeString(JSON.stringify({ |
+ 'error': {'message': 'Bad job!'} |
+ })); |
+ response.outputStream.close(); |
+ }); |
+ |
+ expectLater(pub.nextErrLine(), equals('Bad job!')); |
+ pub.shouldExit(1); |
+ |
+ run(); |
+ }); |
+ |
+ test('add provides invalid JSON', () { |
+ var server = new ScheduledServer(); |
+ credentialsFile(server, 'access token').scheduleCreate(); |
+ var pub = startPubUploader(server, ['--package', 'pkg', 'add', 'email']); |
+ |
+ server.handle('POST', '/packages/pkg/uploaders.json', (request, response) { |
+ response.outputStream.writeString("{not json"); |
+ response.outputStream.close(); |
+ }); |
+ |
+ expectLater(pub.nextErrLine(), equals('Invalid server response:')); |
+ expectLater(pub.nextErrLine(), equals('{not json')); |
+ pub.shouldExit(1); |
+ |
+ run(); |
+ }); |
+ |
+ test('remove provides invalid JSON', () { |
+ var server = new ScheduledServer(); |
+ credentialsFile(server, 'access token').scheduleCreate(); |
+ var pub = startPubUploader(server, ['--package', 'pkg', 'remove', 'email']); |
+ |
+ server.handle('DELETE', '/packages/pkg/uploaders/email.json', |
+ (request, response) { |
+ response.outputStream.writeString("{not json"); |
+ response.outputStream.close(); |
+ }); |
+ |
+ expectLater(pub.nextErrLine(), equals('Invalid server response:')); |
+ expectLater(pub.nextErrLine(), equals('{not json')); |
+ pub.shouldExit(1); |
+ |
+ run(); |
+ }); |
+} |