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:io'; | |
8 import 'dart:json' as json; | |
9 | |
10 import 'package:scheduled_test/scheduled_process.dart'; | |
11 import 'package:scheduled_test/scheduled_server.dart'; | |
12 import 'package:scheduled_test/scheduled_test.dart'; | |
13 | |
14 import '../../pub/io.dart'; | |
15 import '../../pub/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 --server The package server on which the package is hosted | |
24 (defaults to "https://pub.dartlang.org") | |
25 | |
26 --package The package whose uploaders will be modified | |
27 (defaults to the current package) | |
28 '''; | |
29 | |
30 ScheduledProcess startPubUploader(ScheduledServer server, List<String> args) { | |
31 var tokenEndpoint = server.url.then((url) => | |
32 url.resolve('/token').toString()); | |
33 args = flatten(['uploader', '--server', tokenEndpoint, args]); | |
34 return startPub(args: args, tokenEndpoint: tokenEndpoint); | |
35 } | |
36 | |
37 main() { | |
38 initConfig(); | |
39 group('displays usage', () { | |
40 integration('when run with no arguments', () { | |
41 schedulePub(args: ['uploader'], | |
42 output: USAGE_STRING, exitCode: 64); | |
43 }); | |
44 | |
45 integration('when run with only a command', () { | |
46 schedulePub(args: ['uploader', 'add'], | |
47 output: USAGE_STRING, exitCode: 64); | |
48 }); | |
49 | |
50 integration('when run with an invalid command', () { | |
51 schedulePub(args: ['uploader', 'foo', 'email'], | |
52 output: USAGE_STRING, exitCode: 64); | |
53 }); | |
54 }); | |
55 | |
56 integration('adds an uploader', () { | |
57 var server = new ScheduledServer(); | |
58 d.credentialsFile(server, 'access token').create(); | |
59 var pub = startPubUploader(server, ['--package', 'pkg', 'add', 'email']); | |
60 | |
61 server.handle('POST', '/packages/pkg/uploaders.json', (request) { | |
62 expect(new ByteStream(request).toBytes().then((bodyBytes) { | |
63 expect(new String.fromCharCodes(bodyBytes), equals('email=email')); | |
64 | |
65 request.response.headers.contentType = | |
66 new ContentType("application", "json"); | |
67 request.response.write(json.stringify({ | |
68 'success': {'message': 'Good job!'} | |
69 })); | |
70 request.response.close(); | |
71 }), completes); | |
72 }); | |
73 | |
74 expect(pub.nextLine(), completion(equals('Good job!'))); | |
75 pub.shouldExit(0); | |
76 }); | |
77 | |
78 integration('removes an uploader', () { | |
79 var server = new ScheduledServer(); | |
80 d.credentialsFile(server, 'access token').create(); | |
81 var pub = startPubUploader(server, ['--package', 'pkg', 'remove', 'email']); | |
82 | |
83 server.handle('DELETE', '/packages/pkg/uploaders/email.json', (request) { | |
84 request.response.headers.contentType = | |
85 new ContentType("application", "json"); | |
86 request.response.write(json.stringify({ | |
87 'success': {'message': 'Good job!'} | |
88 })); | |
89 request.response.close(); | |
90 }); | |
91 | |
92 expect(pub.nextLine(), completion(equals('Good job!'))); | |
93 pub.shouldExit(0); | |
94 }); | |
95 | |
96 integration('defaults to the current package', () { | |
97 d.validPackage.create(); | |
98 | |
99 var server = new ScheduledServer(); | |
100 d.credentialsFile(server, 'access token').create(); | |
101 var pub = startPubUploader(server, ['add', 'email']); | |
102 | |
103 server.handle('POST', '/packages/test_pkg/uploaders.json', (request) { | |
104 request.response.headers.contentType = | |
105 new ContentType("application", "json"); | |
106 request.response.write(json.stringify({ | |
107 'success': {'message': 'Good job!'} | |
108 })); | |
109 request.response.close(); | |
110 }); | |
111 | |
112 expect(pub.nextLine(), completion(equals('Good job!'))); | |
113 pub.shouldExit(0); | |
114 }); | |
115 | |
116 integration('add provides an error', () { | |
117 var server = new ScheduledServer(); | |
118 d.credentialsFile(server, 'access token').create(); | |
119 var pub = startPubUploader(server, ['--package', 'pkg', 'add', 'email']); | |
120 | |
121 server.handle('POST', '/packages/pkg/uploaders.json', (request) { | |
122 request.response.statusCode = 400; | |
123 request.response.headers.contentType = | |
124 new ContentType("application", "json"); | |
125 request.response.write(json.stringify({ | |
126 'error': {'message': 'Bad job!'} | |
127 })); | |
128 request.response.close(); | |
129 }); | |
130 | |
131 expect(pub.nextErrLine(), completion(equals('Bad job!'))); | |
132 pub.shouldExit(1); | |
133 }); | |
134 | |
135 integration('remove provides an error', () { | |
136 var server = new ScheduledServer(); | |
137 d.credentialsFile(server, 'access token').create(); | |
138 var pub = startPubUploader(server, | |
139 ['--package', 'pkg', 'remove', 'e/mail']); | |
140 | |
141 server.handle('DELETE', '/packages/pkg/uploaders/e%2Fmail.json', (request) { | |
142 request.response.statusCode = 400; | |
143 request.response.headers.contentType = | |
144 new ContentType("application", "json"); | |
145 request.response.write(json.stringify({ | |
146 'error': {'message': 'Bad job!'} | |
147 })); | |
148 request.response.close(); | |
149 }); | |
150 | |
151 expect(pub.nextErrLine(), completion(equals('Bad job!'))); | |
152 pub.shouldExit(1); | |
153 }); | |
154 | |
155 integration('add provides invalid JSON', () { | |
156 var server = new ScheduledServer(); | |
157 d.credentialsFile(server, 'access token').create(); | |
158 var pub = startPubUploader(server, ['--package', 'pkg', 'add', 'email']); | |
159 | |
160 server.handle('POST', '/packages/pkg/uploaders.json', (request) { | |
161 request.response.write("{not json"); | |
162 request.response.close(); | |
163 }); | |
164 | |
165 expect(pub.nextErrLine(), completion(equals('Invalid server response:'))); | |
166 expect(pub.nextErrLine(), completion(equals('{not json'))); | |
167 pub.shouldExit(1); | |
168 }); | |
169 | |
170 integration('remove provides invalid JSON', () { | |
171 var server = new ScheduledServer(); | |
172 d.credentialsFile(server, 'access token').create(); | |
173 var pub = startPubUploader(server, ['--package', 'pkg', 'remove', 'email']); | |
174 | |
175 server.handle('DELETE', '/packages/pkg/uploaders/email.json', (request) { | |
176 request.response.write("{not json"); | |
177 request.response.close(); | |
178 }); | |
179 | |
180 expect(pub.nextErrLine(), completion(equals('Invalid server response:'))); | |
181 expect(pub.nextErrLine(), completion(equals('{not json'))); | |
182 pub.shouldExit(1); | |
183 }); | |
184 } | |
OLD | NEW |