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