OLD | NEW |
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' as json; | 8 import 'dart:json' as json; |
9 import 'dart:uri'; | 9 import 'dart:uri'; |
10 | 10 |
| 11 import 'test_pub.dart'; |
11 import '../../../pkg/http/lib/http.dart' as http; | 12 import '../../../pkg/http/lib/http.dart' as http; |
12 import '../../../pkg/scheduled_test/lib/scheduled_process.dart'; | 13 import '../../../pkg/unittest/lib/unittest.dart'; |
13 import '../../../pkg/scheduled_test/lib/scheduled_test.dart'; | |
14 import '../../../pkg/scheduled_test/lib/scheduled_server.dart'; | |
15 | |
16 import '../../pub/io.dart'; | 14 import '../../pub/io.dart'; |
17 import '../../pub/utils.dart'; | 15 import '../../pub/utils.dart'; |
18 import 'descriptor.dart' as d; | |
19 import 'test_pub.dart'; | |
20 | |
21 import 'dart:async'; | |
22 | 16 |
23 main() { | 17 main() { |
24 setUp(() => d.validPackage.create()); | 18 setUp(() => normalPackage.scheduleCreate()); |
25 | 19 |
26 integration('with no credentials.json, authenticates and saves ' | 20 integration('with no credentials.json, authenticates and saves ' |
27 'credentials.json', () { | 21 'credentials.json', () { |
28 var server = new ScheduledServer(); | 22 var server = new ScheduledServer(); |
29 var pub = startPublish(server); | 23 var pub = startPubLish(server); |
30 confirmPublish(pub); | 24 confirmPublish(pub); |
31 authorizePub(pub, server); | 25 authorizePub(pub, server); |
32 | 26 |
33 server.handle('GET', '/packages/versions/new.json', (request) { | 27 server.handle('GET', '/packages/versions/new.json', (request, response) { |
34 expect(request.headers.value('authorization'), | 28 expect(request.headers.value('authorization'), |
35 equals('Bearer access token')); | 29 equals('Bearer access token')); |
36 | 30 |
37 request.response.close(); | 31 response.close(); |
38 }); | 32 }); |
39 | 33 |
40 // After we give pub an invalid response, it should crash. We wait for it to | 34 // After we give pub an invalid response, it should crash. We wait for it to |
41 // do so rather than killing it so it'll write out the credentials file. | 35 // do so rather than killing it so it'll write out the credentials file. |
42 pub.shouldExit(1); | 36 pub.shouldExit(1); |
43 | 37 |
44 d.credentialsFile(server, 'access token').validate(); | 38 credentialsFile(server, 'access token').scheduleValidate(); |
45 }); | 39 }); |
46 | 40 |
47 integration('with a pre-existing credentials.json does not authenticate', () { | 41 integration('with a pre-existing credentials.json does not authenticate', () { |
48 var server = new ScheduledServer(); | 42 var server = new ScheduledServer(); |
49 d.credentialsFile(server, 'access token').create(); | 43 credentialsFile(server, 'access token').scheduleCreate(); |
50 var pub = startPublish(server); | 44 var pub = startPubLish(server); |
51 confirmPublish(pub); | 45 confirmPublish(pub); |
52 | 46 |
53 server.handle('GET', '/packages/versions/new.json', (request) { | 47 server.handle('GET', '/packages/versions/new.json', (request, response) { |
54 expect(request.headers.value('authorization'), | 48 expect(request.headers.value('authorization'), |
55 equals('Bearer access token')); | 49 equals('Bearer access token')); |
56 | 50 |
57 request.response.close(); | 51 response.close(); |
58 }); | 52 }); |
59 | 53 |
60 pub.kill(); | 54 pub.kill(); |
61 }); | 55 }); |
62 | 56 |
63 integration('with an expired credentials.json, refreshes and saves the ' | 57 integration('with an expired credentials.json, refreshes and saves the ' |
64 'refreshed access token to credentials.json', () { | 58 'refreshed access token to credentials.json', () { |
65 var server = new ScheduledServer(); | 59 var server = new ScheduledServer(); |
66 d.credentialsFile(server, 'access token', | 60 credentialsFile(server, 'access token', |
67 refreshToken: 'refresh token', | 61 refreshToken: 'refresh token', |
68 expiration: new DateTime.now().subtract(new Duration(hours: 1))) | 62 expiration: new DateTime.now().subtract(new Duration(hours: 1))) |
69 .create(); | 63 .scheduleCreate(); |
70 | 64 |
71 var pub = startPublish(server); | 65 var pub = startPubLish(server); |
72 confirmPublish(pub); | 66 confirmPublish(pub); |
73 | 67 |
74 server.handle('POST', '/token', (request) { | 68 server.handle('POST', '/token', (request, response) { |
75 return new ByteStream(request).toBytes().then((bytes) { | 69 return new ByteStream(request).toBytes().then((bytes) { |
76 var body = new String.fromCharCodes(bytes); | 70 var body = new String.fromCharCodes(bytes); |
77 expect(body, matches( | 71 expect(body, matches( |
78 new RegExp(r'(^|&)refresh_token=refresh\+token(&|$)'))); | 72 new RegExp(r'(^|&)refresh_token=refresh\+token(&|$)'))); |
79 | 73 |
80 request.response.headers.contentType = | 74 response.headers.contentType = new ContentType("application", "json"); |
81 new ContentType("application", "json"); | 75 response.write(json.stringify({ |
82 request.response.write(json.stringify({ | |
83 "access_token": "new access token", | 76 "access_token": "new access token", |
84 "token_type": "bearer" | 77 "token_type": "bearer" |
85 })); | 78 })); |
86 request.response.close(); | 79 response.close(); |
87 }); | 80 }); |
88 }); | 81 }); |
89 | 82 |
90 server.handle('GET', '/packages/versions/new.json', (request) { | 83 server.handle('GET', '/packages/versions/new.json', (request, response) { |
91 expect(request.headers.value('authorization'), | 84 expect(request.headers.value('authorization'), |
92 equals('Bearer new access token')); | 85 equals('Bearer new access token')); |
93 | 86 |
94 request.response.close(); | 87 response.close(); |
95 }); | 88 }); |
96 | 89 |
97 pub.shouldExit(); | 90 pub.shouldExit(); |
98 | 91 |
99 d.credentialsFile(server, 'new access token', refreshToken: 'refresh token') | 92 credentialsFile(server, 'new access token', refreshToken: 'refresh token') |
100 .validate(); | 93 .scheduleValidate(); |
101 }); | 94 }); |
102 | 95 |
103 integration('with an expired credentials.json without a refresh token, ' | 96 integration('with an expired credentials.json without a refresh token, ' |
104 'authenticates again and saves credentials.json', () { | 97 'authenticates again and saves credentials.json', () { |
105 var server = new ScheduledServer(); | 98 var server = new ScheduledServer(); |
106 d.credentialsFile(server, 'access token', | 99 credentialsFile(server, 'access token', |
107 expiration: new DateTime.now().subtract(new Duration(hours: 1))) | 100 expiration: new DateTime.now().subtract(new Duration(hours: 1))) |
108 .create(); | 101 .scheduleCreate(); |
109 | 102 |
110 var pub = startPublish(server); | 103 var pub = startPubLish(server); |
111 confirmPublish(pub); | 104 confirmPublish(pub); |
112 | 105 |
113 expect(pub.nextErrLine(), completion(equals("Pub's authorization to upload " | 106 expectLater(pub.nextErrLine(), equals("Pub's authorization to upload " |
114 "packages has expired and can't be automatically refreshed."))); | 107 "packages has expired and can't be automatically refreshed.")); |
115 authorizePub(pub, server, "new access token"); | 108 authorizePub(pub, server, "new access token"); |
116 | 109 |
117 server.handle('GET', '/packages/versions/new.json', (request) { | 110 server.handle('GET', '/packages/versions/new.json', (request, response) { |
118 expect(request.headers.value('authorization'), | 111 expect(request.headers.value('authorization'), |
119 equals('Bearer new access token')); | 112 equals('Bearer new access token')); |
120 | 113 |
121 request.response.close(); | 114 response.close(); |
122 }); | 115 }); |
123 | 116 |
124 // After we give pub an invalid response, it should crash. We wait for it to | 117 // After we give pub an invalid response, it should crash. We wait for it to |
125 // do so rather than killing it so it'll write out the credentials file. | 118 // do so rather than killing it so it'll write out the credentials file. |
126 pub.shouldExit(1); | 119 pub.shouldExit(1); |
127 | 120 |
128 d.credentialsFile(server, 'new access token').validate(); | 121 credentialsFile(server, 'new access token').scheduleValidate(); |
129 }); | 122 }); |
130 | 123 |
131 integration('with a malformed credentials.json, authenticates again and ' | 124 integration('with a malformed credentials.json, authenticates again and ' |
132 'saves credentials.json', () { | 125 'saves credentials.json', () { |
133 var server = new ScheduledServer(); | 126 var server = new ScheduledServer(); |
134 d.dir(cachePath, [ | 127 dir(cachePath, [ |
135 d.file('credentials.json', '{bad json') | 128 file('credentials.json', '{bad json') |
136 ]).create(); | 129 ]).scheduleCreate(); |
137 | 130 |
138 var pub = startPublish(server); | 131 var pub = startPubLish(server); |
139 confirmPublish(pub); | 132 confirmPublish(pub); |
140 authorizePub(pub, server, "new access token"); | 133 authorizePub(pub, server, "new access token"); |
141 | 134 |
142 server.handle('GET', '/packages/versions/new.json', (request) { | 135 server.handle('GET', '/packages/versions/new.json', (request, response) { |
143 expect(request.headers.value('authorization'), | 136 expect(request.headers.value('authorization'), |
144 equals('Bearer new access token')); | 137 equals('Bearer new access token')); |
145 | 138 |
146 request.response.close(); | 139 response.close(); |
147 }); | 140 }); |
148 | 141 |
| 142 |
149 // After we give pub an invalid response, it should crash. We wait for it to | 143 // After we give pub an invalid response, it should crash. We wait for it to |
150 // do so rather than killing it so it'll write out the credentials file. | 144 // do so rather than killing it so it'll write out the credentials file. |
151 pub.shouldExit(1); | 145 pub.shouldExit(1); |
152 | 146 |
153 d.credentialsFile(server, 'new access token').validate(); | 147 credentialsFile(server, 'new access token').scheduleValidate(); |
154 }); | 148 }); |
155 | 149 |
156 // Regression test for issue 8849. | 150 // Regression test for issue 8849. |
157 integration('with a server-rejected refresh token, authenticates again and ' | 151 integration('with a server-rejected refresh token, authenticates again and ' |
158 'saves credentials.json', () { | 152 'saves credentials.json', () { |
159 var server = new ScheduledServer(); | 153 var server = new ScheduledServer(); |
160 d.credentialsFile(server, 'access token', | 154 credentialsFile(server, 'access token', |
161 refreshToken: 'bad refresh token', | 155 refreshToken: 'bad refresh token', |
162 expiration: new DateTime.now().subtract(new Duration(hours: 1))) | 156 expiration: new DateTime.now().subtract(new Duration(hours: 1))) |
163 .create(); | 157 .scheduleCreate(); |
164 | 158 |
165 var pub = startPublish(server); | 159 var pub = startPubLish(server); |
166 confirmPublish(pub); | 160 confirmPublish(pub); |
167 | 161 |
168 server.handle('POST', '/token', (request) { | 162 server.handle('POST', '/token', (request, response) { |
169 return new ByteStream(request).toBytes().then((bytes) { | 163 return new ByteStream(request).toBytes().then((bytes) { |
170 var response = request.response; | |
171 response.statusCode = 400; | 164 response.statusCode = 400; |
172 response.reasonPhrase = 'Bad request'; | 165 response.reasonPhrase = 'Bad request'; |
173 response.headers.contentType = new ContentType("application", "json"); | 166 response.headers.contentType = new ContentType("application", "json"); |
174 response.write(json.stringify({"error": "invalid_request"})); | 167 response.write(json.stringify({"error": "invalid_request"})); |
175 response.close(); | 168 response.close(); |
176 }); | 169 }); |
177 }); | 170 }); |
178 | 171 |
179 authorizePub(pub, server, 'new access token'); | 172 authorizePub(pub, server, 'new access token'); |
180 | 173 |
181 server.handle('GET', '/packages/versions/new.json', (request) { | 174 server.handle('GET', '/packages/versions/new.json', (request, response) { |
182 expect(request.headers.value('authorization'), | 175 expect(request.headers.value('authorization'), |
183 equals('Bearer new access token')); | 176 equals('Bearer new access token')); |
184 | 177 |
185 request.response.close(); | 178 response.close(); |
186 }); | 179 }); |
187 | 180 |
188 pub.kill(); | 181 pub.kill(); |
189 }); | 182 }); |
190 | 183 |
191 integration('with server-rejected credentials, authenticates again and saves ' | 184 integration('with server-rejected credentials, authenticates again and saves ' |
192 'credentials.json', () { | 185 'credentials.json', () { |
193 var server = new ScheduledServer(); | 186 var server = new ScheduledServer(); |
194 d.credentialsFile(server, 'access token').create(); | 187 credentialsFile(server, 'access token').scheduleCreate(); |
195 var pub = startPublish(server); | 188 var pub = startPubLish(server); |
196 | 189 |
197 confirmPublish(pub); | 190 confirmPublish(pub); |
198 | 191 |
199 server.handle('GET', '/packages/versions/new.json', (request) { | 192 server.handle('GET', '/packages/versions/new.json', (request, response) { |
200 var response = request.response; | |
201 response.statusCode = 401; | 193 response.statusCode = 401; |
202 response.headers.set('www-authenticate', 'Bearer error="invalid_token",' | 194 response.headers.set('www-authenticate', 'Bearer error="invalid_token",' |
203 ' error_description="your token sucks"'); | 195 ' error_description="your token sucks"'); |
204 response.write(json.stringify({ | 196 response.write(json.stringify({ |
205 'error': {'message': 'your token sucks'} | 197 'error': {'message': 'your token sucks'} |
206 })); | 198 })); |
207 response.close(); | 199 response.close(); |
208 }); | 200 }); |
209 | 201 |
210 expect(pub.nextErrLine(), completion(equals('OAuth2 authorization failed ' | 202 expectLater(pub.nextErrLine(), equals('OAuth2 authorization failed (your ' |
211 '(your token sucks).'))); | 203 'token sucks).')); |
212 // TODO(rnystrom): The confirm line is run together with this one because | 204 // TODO(rnystrom): The confirm line is run together with this one because |
213 // in normal usage, the user will have entered a newline on stdin which | 205 // in normal usage, the user will have entered a newline on stdin which |
214 // gets echoed to the terminal. Do something better here? | 206 // gets echoed to the terminal. Do something better here? |
215 expect(pub.nextLine(), completion(equals( | 207 expectLater(pub.nextLine(), equals( |
216 'Looks great! Are you ready to upload your package (y/n)? ' | 208 'Looks great! Are you ready to upload your package (y/n)? ' |
217 'Pub needs your authorization to upload packages on your behalf.'))); | 209 'Pub needs your authorization to upload packages on your behalf.')); |
218 pub.kill(); | 210 pub.kill(); |
219 }); | 211 }); |
220 } | 212 } |
221 | 213 |
222 void authorizePub(ScheduledProcess pub, ScheduledServer server, | 214 void authorizePub(ScheduledProcess pub, ScheduledServer server, |
223 [String accessToken="access token"]) { | 215 [String accessToken="access token"]) { |
224 // TODO(rnystrom): The confirm line is run together with this one because | 216 // TODO(rnystrom): The confirm line is run together with this one because |
225 // in normal usage, the user will have entered a newline on stdin which | 217 // in normal usage, the user will have entered a newline on stdin which |
226 // gets echoed to the terminal. Do something better here? | 218 // gets echoed to the terminal. Do something better here? |
227 expect(pub.nextLine(), completion(equals( | 219 expectLater(pub.nextLine(), equals( |
228 'Looks great! Are you ready to upload your package (y/n)? ' | 220 'Looks great! Are you ready to upload your package (y/n)? ' |
229 'Pub needs your authorization to upload packages on your behalf.'))); | 221 'Pub needs your authorization to upload packages on your behalf.')); |
230 | 222 |
231 expect(pub.nextLine().then((line) { | 223 expectLater(pub.nextLine().then((line) { |
232 var match = new RegExp(r'[?&]redirect_uri=([0-9a-zA-Z%+-]+)[$&]') | 224 var match = new RegExp(r'[?&]redirect_uri=([0-9a-zA-Z%+-]+)[$&]') |
233 .firstMatch(line); | 225 .firstMatch(line); |
234 expect(match, isNotNull); | 226 expect(match, isNotNull); |
235 | 227 |
236 var redirectUrl = Uri.parse(decodeUriComponent(match.group(1))); | 228 var redirectUrl = Uri.parse(decodeUriComponent(match.group(1))); |
237 redirectUrl = addQueryParameters(redirectUrl, {'code': 'access code'}); | 229 redirectUrl = addQueryParameters(redirectUrl, {'code': 'access code'}); |
238 return (new http.Request('GET', redirectUrl)..followRedirects = false) | 230 return (new http.Request('GET', redirectUrl)..followRedirects = false) |
239 .send(); | 231 .send(); |
240 }).then((response) { | 232 }).then((response) { |
241 expect(response.headers['location'], | 233 expect(response.headers['location'], |
242 equals('http://pub.dartlang.org/authorized')); | 234 equals('http://pub.dartlang.org/authorized')); |
243 }), completes); | 235 }), anything); |
244 | 236 |
245 handleAccessTokenRequest(server, accessToken); | 237 handleAccessTokenRequest(server, accessToken); |
246 } | 238 } |
247 | 239 |
248 void handleAccessTokenRequest(ScheduledServer server, String accessToken) { | 240 void handleAccessTokenRequest(ScheduledServer server, String accessToken) { |
249 server.handle('POST', '/token', (request) { | 241 server.handle('POST', '/token', (request, response) { |
250 return new ByteStream(request).toBytes().then((bytes) { | 242 return new ByteStream(request).toBytes().then((bytes) { |
251 var body = new String.fromCharCodes(bytes); | 243 var body = new String.fromCharCodes(bytes); |
252 expect(body, matches(new RegExp(r'(^|&)code=access\+code(&|$)'))); | 244 expect(body, matches(new RegExp(r'(^|&)code=access\+code(&|$)'))); |
253 | 245 |
254 request.response.headers.contentType = | 246 response.headers.contentType = new ContentType("application", "json"); |
255 new ContentType("application", "json"); | 247 response.write(json.stringify({ |
256 request.response.write(json.stringify({ | |
257 "access_token": accessToken, | 248 "access_token": accessToken, |
258 "token_type": "bearer" | 249 "token_type": "bearer" |
259 })); | 250 })); |
260 request.response.close(); | 251 response.close(); |
261 }); | 252 }); |
262 }); | 253 }); |
263 } | 254 } |
264 | 255 |
OLD | NEW |