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