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