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 |
(...skipping 13 matching lines...) Expand all Loading... | |
24 confirmPublish(pub); | 24 confirmPublish(pub); |
25 authorizePub(pub, server); | 25 authorizePub(pub, server); |
26 | 26 |
27 server.handle('GET', '/packages/versions/new.json', (request, response) { | 27 server.handle('GET', '/packages/versions/new.json', (request, response) { |
28 expect(request.headers.value('authorization'), | 28 expect(request.headers.value('authorization'), |
29 equals('Bearer access token')); | 29 equals('Bearer access token')); |
30 | 30 |
31 response.outputStream.close(); | 31 response.outputStream.close(); |
32 }); | 32 }); |
33 | 33 |
34 pub.kill(); | 34 // 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. | |
36 pub.shouldExit(1); | |
Bob Nystrom
2013/02/05 00:53:22
Maybe add a pub.shouldFail(1) method so you don't
nweiz
2013/02/05 00:57:18
I don't think that makes sense to put on the actua
| |
35 | 37 |
36 credentialsFile(server, 'access token').scheduleValidate(); | 38 credentialsFile(server, 'access token').scheduleValidate(); |
37 }); | 39 }); |
38 | 40 |
39 integration('with a pre-existing credentials.json does not authenticate', () { | 41 integration('with a pre-existing credentials.json does not authenticate', () { |
40 var server = new ScheduledServer(); | 42 var server = new ScheduledServer(); |
41 credentialsFile(server, 'access token').scheduleCreate(); | 43 credentialsFile(server, 'access token').scheduleCreate(); |
42 var pub = startPubLish(server); | 44 var pub = startPubLish(server); |
43 confirmPublish(pub); | 45 confirmPublish(pub); |
44 | 46 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
105 "packages has expired and can't be automatically refreshed.")); | 107 "packages has expired and can't be automatically refreshed.")); |
106 authorizePub(pub, server, "new access token"); | 108 authorizePub(pub, server, "new access token"); |
107 | 109 |
108 server.handle('GET', '/packages/versions/new.json', (request, response) { | 110 server.handle('GET', '/packages/versions/new.json', (request, response) { |
109 expect(request.headers.value('authorization'), | 111 expect(request.headers.value('authorization'), |
110 equals('Bearer new access token')); | 112 equals('Bearer new access token')); |
111 | 113 |
112 response.outputStream.close(); | 114 response.outputStream.close(); |
113 }); | 115 }); |
114 | 116 |
115 pub.kill(); | 117 // 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. | |
119 pub.shouldExit(1); | |
116 | 120 |
117 credentialsFile(server, 'new access token').scheduleValidate(); | 121 credentialsFile(server, 'new access token').scheduleValidate(); |
118 }); | 122 }); |
119 | 123 |
120 integration('with a malformed credentials.json, authenticates again and ' | 124 integration('with a malformed credentials.json, authenticates again and ' |
121 'saves credentials.json', () { | 125 'saves credentials.json', () { |
122 var server = new ScheduledServer(); | 126 var server = new ScheduledServer(); |
123 dir(cachePath, [ | 127 dir(cachePath, [ |
124 file('credentials.json', '{bad json') | 128 file('credentials.json', '{bad json') |
125 ]).scheduleCreate(); | 129 ]).scheduleCreate(); |
126 | 130 |
127 var pub = startPubLish(server); | 131 var pub = startPubLish(server); |
128 confirmPublish(pub); | 132 confirmPublish(pub); |
129 authorizePub(pub, server, "new access token"); | 133 authorizePub(pub, server, "new access token"); |
130 | 134 |
131 server.handle('GET', '/packages/versions/new.json', (request, response) { | 135 server.handle('GET', '/packages/versions/new.json', (request, response) { |
132 expect(request.headers.value('authorization'), | 136 expect(request.headers.value('authorization'), |
133 equals('Bearer new access token')); | 137 equals('Bearer new access token')); |
134 | 138 |
135 response.outputStream.close(); | 139 response.outputStream.close(); |
136 }); | 140 }); |
137 | 141 |
138 pub.kill(); | 142 |
143 // 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. | |
145 pub.shouldExit(1); | |
139 | 146 |
140 credentialsFile(server, 'new access token').scheduleValidate(); | 147 credentialsFile(server, 'new access token').scheduleValidate(); |
141 }); | 148 }); |
142 | 149 |
143 integration('with server-rejected credentials, authenticates again and saves ' | 150 integration('with server-rejected credentials, authenticates again and saves ' |
144 'credentials.json', () { | 151 'credentials.json', () { |
145 var server = new ScheduledServer(); | 152 var server = new ScheduledServer(); |
146 credentialsFile(server, 'access token').scheduleCreate(); | 153 credentialsFile(server, 'access token').scheduleCreate(); |
147 var pub = startPubLish(server); | 154 var pub = startPubLish(server); |
148 | 155 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
183 var match = new RegExp(r'[?&]redirect_uri=([0-9a-zA-Z%+-]+)[$&]') | 190 var match = new RegExp(r'[?&]redirect_uri=([0-9a-zA-Z%+-]+)[$&]') |
184 .firstMatch(line); | 191 .firstMatch(line); |
185 expect(match, isNotNull); | 192 expect(match, isNotNull); |
186 | 193 |
187 var redirectUrl = Uri.parse(decodeUriComponent(match.group(1))); | 194 var redirectUrl = Uri.parse(decodeUriComponent(match.group(1))); |
188 redirectUrl = addQueryParameters(redirectUrl, {'code': 'access code'}); | 195 redirectUrl = addQueryParameters(redirectUrl, {'code': 'access code'}); |
189 return (new http.Request('GET', redirectUrl)..followRedirects = false) | 196 return (new http.Request('GET', redirectUrl)..followRedirects = false) |
190 .send(); | 197 .send(); |
191 }).then((response) { | 198 }).then((response) { |
192 expect(response.headers['location'], | 199 expect(response.headers['location'], |
193 equals(['http://pub.dartlang.org/authorized'])); | 200 equals('http://pub.dartlang.org/authorized')); |
194 }), anything); | 201 }), anything); |
195 | 202 |
196 handleAccessTokenRequest(server, accessToken); | 203 handleAccessTokenRequest(server, accessToken); |
197 } | 204 } |
198 | 205 |
199 void handleAccessTokenRequest(ScheduledServer server, String accessToken) { | 206 void handleAccessTokenRequest(ScheduledServer server, String accessToken) { |
200 server.handle('POST', '/token', (request, response) { | 207 server.handle('POST', '/token', (request, response) { |
201 return wrapInputStream(request.inputStream).toBytes().then((bytes) { | 208 return wrapInputStream(request.inputStream).toBytes().then((bytes) { |
202 var body = new String.fromCharCodes(bytes); | 209 var body = new String.fromCharCodes(bytes); |
203 expect(body, matches(new RegExp(r'(^|&)code=access\+code(&|$)'))); | 210 expect(body, matches(new RegExp(r'(^|&)code=access\+code(&|$)'))); |
204 | 211 |
205 response.headers.contentType = new ContentType("application", "json"); | 212 response.headers.contentType = new ContentType("application", "json"); |
206 response.outputStream.writeString(json.stringify({ | 213 response.outputStream.writeString(json.stringify({ |
207 "access_token": accessToken, | 214 "access_token": accessToken, |
208 "token_type": "bearer" | 215 "token_type": "bearer" |
209 })); | 216 })); |
210 response.outputStream.close(); | 217 response.outputStream.close(); |
211 }); | 218 }); |
212 }); | 219 }); |
213 } | 220 } |
OLD | NEW |