Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(175)

Side by Side Diff: utils/tests/pub/oauth2_test.dart

Issue 11348268: Don't require users to copy-paste authorization codes when authorizing pub. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library oauth2_test; 5 library oauth2_test;
6 6
7 import 'dart:io'; 7 import 'dart:io';
8 import 'dart:json'; 8 import 'dart:json';
9 import 'dart:uri';
9 10
10 import 'test_pub.dart'; 11 import 'test_pub.dart';
12 import '../../../pkg/http/lib/http.dart' as http;
11 import '../../../pkg/unittest/lib/unittest.dart'; 13 import '../../../pkg/unittest/lib/unittest.dart';
12 import '../../pub/io.dart'; 14 import '../../pub/io.dart';
15 import '../../pub/utils.dart';
13 16
14 main() { 17 main() {
15 setUp(() => dir(appPath, [libPubspec("test_pkg", "1.0.0")]).scheduleCreate()); 18 setUp(() => dir(appPath, [libPubspec("test_pkg", "1.0.0")]).scheduleCreate());
16 19
17 test('with no credentials.json, authenticates and saves credentials.json', 20 test('with no credentials.json, authenticates and saves credentials.json',
18 () { 21 () {
19 var server = new ScheduledServer(); 22 var server = new ScheduledServer();
20 var pub = startPubLish(server); 23 var pub = startPubLish(server);
21 24 authorizePub(pub, server);
22 expectLater(pub.nextLine(), equals('Pub needs your '
23 'authorization to upload packages on your behalf.'));
24 pub.writeLine('access code');
25
26 handleAccessTokenRequest(server, "access token");
27 25
28 server.handle('GET', '/packages/versions/new.json', (request, response) { 26 server.handle('GET', '/packages/versions/new.json', (request, response) {
29 expect(request.headers.value('authorization'), 27 expect(request.headers.value('authorization'),
30 equals('Bearer access token')); 28 equals('Bearer access token'));
31 29
32 response.outputStream.close(); 30 response.outputStream.close();
33 }); 31 });
34 32
35 pub.kill(); 33 pub.kill();
36 34
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 'authenticates again and saves credentials.json', () { 98 'authenticates again and saves credentials.json', () {
101 var server = new ScheduledServer(); 99 var server = new ScheduledServer();
102 credentialsFile(server, 'access token', 100 credentialsFile(server, 'access token',
103 expiration: new Date.now().subtract(new Duration(hours: 1))) 101 expiration: new Date.now().subtract(new Duration(hours: 1)))
104 .scheduleCreate(); 102 .scheduleCreate();
105 103
106 var pub = startPubLish(server); 104 var pub = startPubLish(server);
107 105
108 expectLater(pub.nextErrLine(), equals("Pub's authorization to upload " 106 expectLater(pub.nextErrLine(), equals("Pub's authorization to upload "
109 "packages has expired and can't be automatically refreshed.")); 107 "packages has expired and can't be automatically refreshed."));
110 expectLater(pub.nextLine(), equals('Pub needs your ' 108 authorizePub(pub, server, "new access token");
111 'authorization to upload packages on your behalf.'));
112 pub.writeLine('access code');
113
114 handleAccessTokenRequest(server, "new access token");
115 109
116 server.handle('GET', '/packages/versions/new.json', (request, response) { 110 server.handle('GET', '/packages/versions/new.json', (request, response) {
117 expect(request.headers.value('authorization'), 111 expect(request.headers.value('authorization'),
118 equals('Bearer new access token')); 112 equals('Bearer new access token'));
119 113
120 response.outputStream.close(); 114 response.outputStream.close();
121 }); 115 });
122 116
123 pub.kill(); 117 pub.kill();
124 118
125 credentialsFile(server, 'new access token').scheduleValidate(); 119 credentialsFile(server, 'new access token').scheduleValidate();
126 120
127 run(); 121 run();
128 }); 122 });
129 123
130 test('with a malformed credentials.json, authenticates again and saves ' 124 test('with a malformed credentials.json, authenticates again and saves '
131 'credentials.json', () { 125 'credentials.json', () {
132 var server = new ScheduledServer(); 126 var server = new ScheduledServer();
133 dir(cachePath, [ 127 dir(cachePath, [
134 file('credentials.json', '{bad json') 128 file('credentials.json', '{bad json')
135 ]).scheduleCreate(); 129 ]).scheduleCreate();
136 130
137 var pub = startPubLish(server); 131 var pub = startPubLish(server);
138 132 authorizePub(pub, server, "new access token");
139 expectLater(pub.nextLine(), equals('Pub needs your '
140 'authorization to upload packages on your behalf.'));
141 pub.writeLine('access code');
142
143 handleAccessTokenRequest(server, "new access token");
144 133
145 server.handle('GET', '/packages/versions/new.json', (request, response) { 134 server.handle('GET', '/packages/versions/new.json', (request, response) {
146 expect(request.headers.value('authorization'), 135 expect(request.headers.value('authorization'),
147 equals('Bearer new access token')); 136 equals('Bearer new access token'));
148 137
149 response.outputStream.close(); 138 response.outputStream.close();
150 }); 139 });
151 140
152 pub.kill(); 141 pub.kill();
153 142
154 credentialsFile(server, 'new access token').scheduleValidate(); 143 credentialsFile(server, 'new access token').scheduleValidate();
155 144
156 run(); 145 run();
157 }); 146 });
158 } 147 }
159 148
149 void authorizePub(ScheduledProcess pub, ScheduledServer server,
150 [String accessToken="access token"]) {
151 expectLater(pub.nextLine(), equals('Pub needs your '
152 'authorization to upload packages on your behalf.'));
153
154 expectLater(pub.nextLine().chain((line) {
155 var match = new RegExp(r'[?&]redirect_uri=([0-9a-zA-Z%+-]+)[$&]')
156 .firstMatch(line);
157 expect(match, isNotNull);
158
159 var redirectUrl = new Uri.fromString(decodeUriComponent(match.group(1)));
160 redirectUrl = addQueryParameters(redirectUrl, {'code': 'access code'});
161 return (new http.Request('GET', redirectUrl)..followRedirects = false)
162 .send();
163 }).transform((response) {
164 expect(response.headers['location'],
165 equals(['http://pub.dartlang.org/authorized']));
166 }), anything);
167
168 handleAccessTokenRequest(server, accessToken);
169 }
170
160 void handleAccessTokenRequest(ScheduledServer server, String accessToken) { 171 void handleAccessTokenRequest(ScheduledServer server, String accessToken) {
161 server.handle('POST', '/token', (request, response) { 172 server.handle('POST', '/token', (request, response) {
162 return consumeInputStream(request.inputStream).transform((bytes) { 173 return consumeInputStream(request.inputStream).transform((bytes) {
163 var body = new String.fromCharCodes(bytes); 174 var body = new String.fromCharCodes(bytes);
164 expect(body, matches(new RegExp(r'(^|&)code=access%20code(&|$)'))); 175 expect(body, matches(new RegExp(r'(^|&)code=access%20code(&|$)')));
165 176
166 response.headers.contentType = new ContentType("application", "json"); 177 response.headers.contentType = new ContentType("application", "json");
167 response.outputStream.writeString(JSON.stringify({ 178 response.outputStream.writeString(JSON.stringify({
168 "access_token": accessToken, 179 "access_token": accessToken,
169 "token_type": "bearer" 180 "token_type": "bearer"
170 })); 181 }));
171 response.outputStream.close(); 182 response.outputStream.close();
172 }); 183 });
173 }); 184 });
174 } 185 }
OLDNEW
« utils/pub/utils.dart ('K') | « utils/pub/utils.dart ('k') | utils/tests/pub/test_pub.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698