OLD | NEW |
1 // Copyright (c) 2013, 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 library pub_uploader_test; | 1 library pub_uploader_test; |
6 | |
7 import 'dart:convert'; | 2 import 'dart:convert'; |
8 | |
9 import 'package:scheduled_test/scheduled_process.dart'; | 3 import 'package:scheduled_test/scheduled_process.dart'; |
10 import 'package:scheduled_test/scheduled_server.dart'; | 4 import 'package:scheduled_test/scheduled_server.dart'; |
11 import 'package:scheduled_test/scheduled_test.dart'; | 5 import 'package:scheduled_test/scheduled_test.dart'; |
12 import 'package:shelf/shelf.dart' as shelf; | 6 import 'package:shelf/shelf.dart' as shelf; |
13 | |
14 import '../lib/src/exit_codes.dart' as exit_codes; | 7 import '../lib/src/exit_codes.dart' as exit_codes; |
15 import '../lib/src/utils.dart'; | 8 import '../lib/src/utils.dart'; |
16 import 'descriptor.dart' as d; | 9 import 'descriptor.dart' as d; |
17 import 'test_pub.dart'; | 10 import 'test_pub.dart'; |
18 | |
19 final USAGE_STRING = ''' | 11 final USAGE_STRING = ''' |
20 Manage uploaders for a package on pub.dartlang.org. | 12 Manage uploaders for a package on pub.dartlang.org. |
21 | 13 |
22 Usage: pub uploader [options] {add/remove} <email> | 14 Usage: pub uploader [options] {add/remove} <email> |
23 -h, --help Print usage information for this command. | 15 -h, --help Print usage information for this command. |
24 --server The package server on which the package is hosted. | 16 --server The package server on which the package is hosted. |
25 (defaults to "https://pub.dartlang.org") | 17 (defaults to "https://pub.dartlang.org") |
26 | 18 |
27 --package The package whose uploaders will be modified. | 19 --package The package whose uploaders will be modified. |
28 (defaults to the current package) | 20 (defaults to the current package) |
29 | 21 |
30 Run "pub help" to see global options. | 22 Run "pub help" to see global options. |
31 See http://dartlang.org/tools/pub/cmd/pub-uploader.html for detailed documentati
on. | 23 See http://dartlang.org/tools/pub/cmd/pub-uploader.html for detailed documentati
on. |
32 '''; | 24 '''; |
33 | |
34 ScheduledProcess startPubUploader(ScheduledServer server, List<String> args) { | 25 ScheduledProcess startPubUploader(ScheduledServer server, List<String> args) { |
35 var tokenEndpoint = server.url.then((url) => | 26 var tokenEndpoint = |
36 url.resolve('/token').toString()); | 27 server.url.then((url) => url.resolve('/token').toString()); |
37 args = flatten(['uploader', '--server', tokenEndpoint, args]); | 28 args = flatten(['uploader', '--server', tokenEndpoint, args]); |
38 return startPub(args: args, tokenEndpoint: tokenEndpoint); | 29 return startPub(args: args, tokenEndpoint: tokenEndpoint); |
39 } | 30 } |
40 | |
41 main() { | 31 main() { |
42 initConfig(); | 32 initConfig(); |
43 group('displays usage', () { | 33 group('displays usage', () { |
44 integration('when run with no arguments', () { | 34 integration('when run with no arguments', () { |
45 schedulePub(args: ['uploader'], | 35 schedulePub( |
46 output: USAGE_STRING, exitCode: exit_codes.USAGE); | 36 args: ['uploader'], |
| 37 output: USAGE_STRING, |
| 38 exitCode: exit_codes.USAGE); |
47 }); | 39 }); |
48 | |
49 integration('when run with only a command', () { | 40 integration('when run with only a command', () { |
50 schedulePub(args: ['uploader', 'add'], | 41 schedulePub( |
51 output: USAGE_STRING, exitCode: exit_codes.USAGE); | 42 args: ['uploader', 'add'], |
| 43 output: USAGE_STRING, |
| 44 exitCode: exit_codes.USAGE); |
52 }); | 45 }); |
53 | |
54 integration('when run with an invalid command', () { | 46 integration('when run with an invalid command', () { |
55 schedulePub(args: ['uploader', 'foo', 'email'], | 47 schedulePub( |
56 output: USAGE_STRING, exitCode: exit_codes.USAGE); | 48 args: ['uploader', 'foo', 'email'], |
| 49 output: USAGE_STRING, |
| 50 exitCode: exit_codes.USAGE); |
57 }); | 51 }); |
58 }); | 52 }); |
59 | |
60 integration('adds an uploader', () { | 53 integration('adds an uploader', () { |
61 var server = new ScheduledServer(); | 54 var server = new ScheduledServer(); |
62 d.credentialsFile(server, 'access token').create(); | 55 d.credentialsFile(server, 'access token').create(); |
63 var pub = startPubUploader(server, ['--package', 'pkg', 'add', 'email']); | 56 var pub = startPubUploader(server, ['--package', 'pkg', 'add', 'email']); |
64 | |
65 server.handle('POST', '/api/packages/pkg/uploaders', (request) { | 57 server.handle('POST', '/api/packages/pkg/uploaders', (request) { |
66 return request.readAsString().then((body) { | 58 return request.readAsString().then((body) { |
67 expect(body, equals('email=email')); | 59 expect(body, equals('email=email')); |
68 | |
69 return new shelf.Response.ok(JSON.encode({ | 60 return new shelf.Response.ok(JSON.encode({ |
70 'success': {'message': 'Good job!'} | 61 'success': { |
71 }), headers: {'content-type': 'application/json'}); | 62 'message': 'Good job!' |
| 63 } |
| 64 }), headers: { |
| 65 'content-type': 'application/json' |
| 66 }); |
72 }); | 67 }); |
73 }); | 68 }); |
74 | |
75 pub.stdout.expect('Good job!'); | 69 pub.stdout.expect('Good job!'); |
76 pub.shouldExit(exit_codes.SUCCESS); | 70 pub.shouldExit(exit_codes.SUCCESS); |
77 }); | 71 }); |
78 | |
79 integration('removes an uploader', () { | 72 integration('removes an uploader', () { |
80 var server = new ScheduledServer(); | 73 var server = new ScheduledServer(); |
81 d.credentialsFile(server, 'access token').create(); | 74 d.credentialsFile(server, 'access token').create(); |
82 var pub = startPubUploader(server, ['--package', 'pkg', 'remove', 'email']); | 75 var pub = startPubUploader(server, ['--package', 'pkg', 'remove', 'email']); |
83 | |
84 server.handle('DELETE', '/api/packages/pkg/uploaders/email', (request) { | 76 server.handle('DELETE', '/api/packages/pkg/uploaders/email', (request) { |
85 return new shelf.Response.ok(JSON.encode({ | 77 return new shelf.Response.ok(JSON.encode({ |
86 'success': {'message': 'Good job!'} | 78 'success': { |
87 }), headers: {'content-type': 'application/json'}); | 79 'message': 'Good job!' |
| 80 } |
| 81 }), headers: { |
| 82 'content-type': 'application/json' |
| 83 }); |
88 }); | 84 }); |
89 | |
90 pub.stdout.expect('Good job!'); | 85 pub.stdout.expect('Good job!'); |
91 pub.shouldExit(exit_codes.SUCCESS); | 86 pub.shouldExit(exit_codes.SUCCESS); |
92 }); | 87 }); |
93 | |
94 integration('defaults to the current package', () { | 88 integration('defaults to the current package', () { |
95 d.validPackage.create(); | 89 d.validPackage.create(); |
96 | |
97 var server = new ScheduledServer(); | 90 var server = new ScheduledServer(); |
98 d.credentialsFile(server, 'access token').create(); | 91 d.credentialsFile(server, 'access token').create(); |
99 var pub = startPubUploader(server, ['add', 'email']); | 92 var pub = startPubUploader(server, ['add', 'email']); |
100 | |
101 server.handle('POST', '/api/packages/test_pkg/uploaders', (request) { | 93 server.handle('POST', '/api/packages/test_pkg/uploaders', (request) { |
102 return new shelf.Response.ok(JSON.encode({ | 94 return new shelf.Response.ok(JSON.encode({ |
103 'success': {'message': 'Good job!'} | 95 'success': { |
104 }), headers: {'content-type': 'application/json'}); | 96 'message': 'Good job!' |
| 97 } |
| 98 }), headers: { |
| 99 'content-type': 'application/json' |
| 100 }); |
105 }); | 101 }); |
106 | |
107 pub.stdout.expect('Good job!'); | 102 pub.stdout.expect('Good job!'); |
108 pub.shouldExit(exit_codes.SUCCESS); | 103 pub.shouldExit(exit_codes.SUCCESS); |
109 }); | 104 }); |
110 | |
111 integration('add provides an error', () { | 105 integration('add provides an error', () { |
112 var server = new ScheduledServer(); | 106 var server = new ScheduledServer(); |
113 d.credentialsFile(server, 'access token').create(); | 107 d.credentialsFile(server, 'access token').create(); |
114 var pub = startPubUploader(server, ['--package', 'pkg', 'add', 'email']); | 108 var pub = startPubUploader(server, ['--package', 'pkg', 'add', 'email']); |
115 | |
116 server.handle('POST', '/api/packages/pkg/uploaders', (request) { | 109 server.handle('POST', '/api/packages/pkg/uploaders', (request) { |
117 return new shelf.Response(400, | 110 return new shelf.Response(400, body: JSON.encode({ |
118 body: JSON.encode({'error': {'message': 'Bad job!'}}), | 111 'error': { |
119 headers: {'content-type': 'application/json'}); | 112 'message': 'Bad job!' |
| 113 } |
| 114 }), headers: { |
| 115 'content-type': 'application/json' |
| 116 }); |
120 }); | 117 }); |
121 | |
122 pub.stderr.expect('Bad job!'); | 118 pub.stderr.expect('Bad job!'); |
123 pub.shouldExit(1); | 119 pub.shouldExit(1); |
124 }); | 120 }); |
125 | |
126 integration('remove provides an error', () { | 121 integration('remove provides an error', () { |
127 var server = new ScheduledServer(); | 122 var server = new ScheduledServer(); |
128 d.credentialsFile(server, 'access token').create(); | 123 d.credentialsFile(server, 'access token').create(); |
129 var pub = startPubUploader(server, | 124 var pub = |
130 ['--package', 'pkg', 'remove', 'e/mail']); | 125 startPubUploader(server, ['--package', 'pkg', 'remove', 'e/mail']); |
131 | |
132 server.handle('DELETE', '/api/packages/pkg/uploaders/e%2Fmail', (request) { | 126 server.handle('DELETE', '/api/packages/pkg/uploaders/e%2Fmail', (request) { |
133 return new shelf.Response(400, | 127 return new shelf.Response(400, body: JSON.encode({ |
134 body: JSON.encode({'error': {'message': 'Bad job!'}}), | 128 'error': { |
135 headers: {'content-type': 'application/json'}); | 129 'message': 'Bad job!' |
| 130 } |
| 131 }), headers: { |
| 132 'content-type': 'application/json' |
| 133 }); |
136 }); | 134 }); |
137 | |
138 pub.stderr.expect('Bad job!'); | 135 pub.stderr.expect('Bad job!'); |
139 pub.shouldExit(1); | 136 pub.shouldExit(1); |
140 }); | 137 }); |
141 | |
142 integration('add provides invalid JSON', () { | 138 integration('add provides invalid JSON', () { |
143 var server = new ScheduledServer(); | 139 var server = new ScheduledServer(); |
144 d.credentialsFile(server, 'access token').create(); | 140 d.credentialsFile(server, 'access token').create(); |
145 var pub = startPubUploader(server, ['--package', 'pkg', 'add', 'email']); | 141 var pub = startPubUploader(server, ['--package', 'pkg', 'add', 'email']); |
146 | 142 server.handle( |
147 server.handle('POST', '/api/packages/pkg/uploaders', | 143 'POST', |
| 144 '/api/packages/pkg/uploaders', |
148 (request) => new shelf.Response.ok("{not json")); | 145 (request) => new shelf.Response.ok("{not json")); |
149 | 146 pub.stderr.expect(emitsLines('Invalid server response:\n' '{not json')); |
150 pub.stderr.expect(emitsLines( | |
151 'Invalid server response:\n' | |
152 '{not json')); | |
153 pub.shouldExit(1); | 147 pub.shouldExit(1); |
154 }); | 148 }); |
155 | |
156 integration('remove provides invalid JSON', () { | 149 integration('remove provides invalid JSON', () { |
157 var server = new ScheduledServer(); | 150 var server = new ScheduledServer(); |
158 d.credentialsFile(server, 'access token').create(); | 151 d.credentialsFile(server, 'access token').create(); |
159 var pub = startPubUploader(server, ['--package', 'pkg', 'remove', 'email']); | 152 var pub = startPubUploader(server, ['--package', 'pkg', 'remove', 'email']); |
160 | 153 server.handle( |
161 server.handle('DELETE', '/api/packages/pkg/uploaders/email', | 154 'DELETE', |
| 155 '/api/packages/pkg/uploaders/email', |
162 (request) => new shelf.Response.ok("{not json")); | 156 (request) => new shelf.Response.ok("{not json")); |
163 | 157 pub.stderr.expect(emitsLines('Invalid server response:\n' '{not json')); |
164 pub.stderr.expect(emitsLines( | |
165 'Invalid server response:\n' | |
166 '{not json')); | |
167 pub.shouldExit(1); | 158 pub.shouldExit(1); |
168 }); | 159 }); |
169 } | 160 } |
OLD | NEW |