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'; | 8 import 'dart:json'; |
9 import 'dart:uri'; | 9 import 'dart:uri'; |
10 | 10 |
11 import 'test_pub.dart'; | 11 import 'test_pub.dart'; |
12 import '../../../pkg/http/lib/http.dart' as http; | 12 import '../../../pkg/http/lib/http.dart' as http; |
13 import '../../../pkg/unittest/lib/unittest.dart'; | 13 import '../../../pkg/unittest/lib/unittest.dart'; |
14 import '../../pub/io.dart'; | 14 import '../../pub/io.dart'; |
15 import '../../pub/utils.dart'; | 15 import '../../pub/utils.dart'; |
16 | 16 |
17 main() { | 17 main() { |
18 setUp(() => normalPackage.scheduleCreate()); | 18 setUp(() => normalPackage.scheduleCreate()); |
19 | 19 |
20 test('with no credentials.json, authenticates and saves credentials.json', | 20 test('with no credentials.json, authenticates and saves credentials.json', |
21 () { | 21 () { |
22 var server = new ScheduledServer(); | 22 var server = new ScheduledServer(); |
23 var pub = startPubLish(server); | 23 var pub = startPubLish(server); |
| 24 confirmPublish(pub); |
24 authorizePub(pub, server); | 25 authorizePub(pub, server); |
25 | 26 |
26 server.handle('GET', '/packages/versions/new.json', (request, response) { | 27 server.handle('GET', '/packages/versions/new.json', (request, response) { |
27 expect(request.headers.value('authorization'), | 28 expect(request.headers.value('authorization'), |
28 equals('Bearer access token')); | 29 equals('Bearer access token')); |
29 | 30 |
30 response.outputStream.close(); | 31 response.outputStream.close(); |
31 }); | 32 }); |
32 | 33 |
33 pub.kill(); | 34 pub.kill(); |
34 | 35 |
35 credentialsFile(server, 'access token').scheduleValidate(); | 36 credentialsFile(server, 'access token').scheduleValidate(); |
36 | 37 |
37 run(); | 38 run(); |
38 }); | 39 }); |
39 | 40 |
40 test('with a pre-existing credentials.json does not authenticate', () { | 41 test('with a pre-existing credentials.json does not authenticate', () { |
41 var server = new ScheduledServer(); | 42 var server = new ScheduledServer(); |
42 credentialsFile(server, 'access token').scheduleCreate(); | 43 credentialsFile(server, 'access token').scheduleCreate(); |
43 var pub = startPubLish(server); | 44 var pub = startPubLish(server); |
| 45 confirmPublish(pub); |
44 | 46 |
45 server.handle('GET', '/packages/versions/new.json', (request, response) { | 47 server.handle('GET', '/packages/versions/new.json', (request, response) { |
46 expect(request.headers.value('authorization'), | 48 expect(request.headers.value('authorization'), |
47 equals('Bearer access token')); | 49 equals('Bearer access token')); |
48 | 50 |
49 response.outputStream.close(); | 51 response.outputStream.close(); |
50 }); | 52 }); |
51 | 53 |
52 pub.kill(); | 54 pub.kill(); |
53 | 55 |
54 run(); | 56 run(); |
55 }); | 57 }); |
56 | 58 |
57 test('with an expired credentials.json, refreshes and saves the refreshed ' | 59 test('with an expired credentials.json, refreshes and saves the refreshed ' |
58 'access token to credentials.json', () { | 60 'access token to credentials.json', () { |
59 var server = new ScheduledServer(); | 61 var server = new ScheduledServer(); |
60 credentialsFile(server, 'access token', | 62 credentialsFile(server, 'access token', |
61 refreshToken: 'refresh token', | 63 refreshToken: 'refresh token', |
62 expiration: new Date.now().subtract(new Duration(hours: 1))) | 64 expiration: new Date.now().subtract(new Duration(hours: 1))) |
63 .scheduleCreate(); | 65 .scheduleCreate(); |
64 | 66 |
65 var pub = startPubLish(server); | 67 var pub = startPubLish(server); |
| 68 confirmPublish(pub); |
66 | 69 |
67 server.handle('POST', '/token', (request, response) { | 70 server.handle('POST', '/token', (request, response) { |
68 return consumeInputStream(request.inputStream).transform((bytes) { | 71 return consumeInputStream(request.inputStream).transform((bytes) { |
69 var body = new String.fromCharCodes(bytes); | 72 var body = new String.fromCharCodes(bytes); |
70 expect(body, matches( | 73 expect(body, matches( |
71 new RegExp(r'(^|&)refresh_token=refresh%20token(&|$)'))); | 74 new RegExp(r'(^|&)refresh_token=refresh%20token(&|$)'))); |
72 | 75 |
73 response.headers.contentType = new ContentType("application", "json"); | 76 response.headers.contentType = new ContentType("application", "json"); |
74 response.outputStream.writeString(JSON.stringify({ | 77 response.outputStream.writeString(JSON.stringify({ |
75 "access_token": "new access token", | 78 "access_token": "new access token", |
(...skipping 19 matching lines...) Expand all Loading... |
95 }); | 98 }); |
96 | 99 |
97 test('with an expired credentials.json without a refresh token, ' | 100 test('with an expired credentials.json without a refresh token, ' |
98 'authenticates again and saves credentials.json', () { | 101 'authenticates again and saves credentials.json', () { |
99 var server = new ScheduledServer(); | 102 var server = new ScheduledServer(); |
100 credentialsFile(server, 'access token', | 103 credentialsFile(server, 'access token', |
101 expiration: new Date.now().subtract(new Duration(hours: 1))) | 104 expiration: new Date.now().subtract(new Duration(hours: 1))) |
102 .scheduleCreate(); | 105 .scheduleCreate(); |
103 | 106 |
104 var pub = startPubLish(server); | 107 var pub = startPubLish(server); |
| 108 confirmPublish(pub); |
105 | 109 |
106 expectLater(pub.nextErrLine(), equals("Pub's authorization to upload " | 110 expectLater(pub.nextErrLine(), equals("Pub's authorization to upload " |
107 "packages has expired and can't be automatically refreshed.")); | 111 "packages has expired and can't be automatically refreshed.")); |
108 authorizePub(pub, server, "new access token"); | 112 authorizePub(pub, server, "new access token"); |
109 | 113 |
110 server.handle('GET', '/packages/versions/new.json', (request, response) { | 114 server.handle('GET', '/packages/versions/new.json', (request, response) { |
111 expect(request.headers.value('authorization'), | 115 expect(request.headers.value('authorization'), |
112 equals('Bearer new access token')); | 116 equals('Bearer new access token')); |
113 | 117 |
114 response.outputStream.close(); | 118 response.outputStream.close(); |
115 }); | 119 }); |
116 | 120 |
117 pub.kill(); | 121 pub.kill(); |
118 | 122 |
119 credentialsFile(server, 'new access token').scheduleValidate(); | 123 credentialsFile(server, 'new access token').scheduleValidate(); |
120 | 124 |
121 run(); | 125 run(); |
122 }); | 126 }); |
123 | 127 |
124 test('with a malformed credentials.json, authenticates again and saves ' | 128 test('with a malformed credentials.json, authenticates again and saves ' |
125 'credentials.json', () { | 129 'credentials.json', () { |
126 var server = new ScheduledServer(); | 130 var server = new ScheduledServer(); |
127 dir(cachePath, [ | 131 dir(cachePath, [ |
128 file('credentials.json', '{bad json') | 132 file('credentials.json', '{bad json') |
129 ]).scheduleCreate(); | 133 ]).scheduleCreate(); |
130 | 134 |
131 var pub = startPubLish(server); | 135 var pub = startPubLish(server); |
| 136 confirmPublish(pub); |
132 authorizePub(pub, server, "new access token"); | 137 authorizePub(pub, server, "new access token"); |
133 | 138 |
134 server.handle('GET', '/packages/versions/new.json', (request, response) { | 139 server.handle('GET', '/packages/versions/new.json', (request, response) { |
135 expect(request.headers.value('authorization'), | 140 expect(request.headers.value('authorization'), |
136 equals('Bearer new access token')); | 141 equals('Bearer new access token')); |
137 | 142 |
138 response.outputStream.close(); | 143 response.outputStream.close(); |
139 }); | 144 }); |
140 | 145 |
141 pub.kill(); | 146 pub.kill(); |
142 | 147 |
143 credentialsFile(server, 'new access token').scheduleValidate(); | 148 credentialsFile(server, 'new access token').scheduleValidate(); |
144 | 149 |
145 run(); | 150 run(); |
146 }); | 151 }); |
147 } | 152 } |
148 | 153 |
149 void authorizePub(ScheduledProcess pub, ScheduledServer server, | 154 void authorizePub(ScheduledProcess pub, ScheduledServer server, |
150 [String accessToken="access token"]) { | 155 [String accessToken="access token"]) { |
151 expectLater(pub.nextLine(), equals('Pub needs your ' | 156 // TODO(rnystrom): The confirm line is run together with this one because |
152 'authorization to upload packages on your behalf.')); | 157 // in normal usage, the user will have entered a newline on stdin which |
| 158 // gets echoed to the terminal. Do something better here? |
| 159 expectLater(pub.nextLine(), equals( |
| 160 'Looks great! Are you ready to upload your package (y/n)? ' |
| 161 'Pub needs your authorization to upload packages on your behalf.')); |
153 | 162 |
154 expectLater(pub.nextLine().chain((line) { | 163 expectLater(pub.nextLine().chain((line) { |
155 var match = new RegExp(r'[?&]redirect_uri=([0-9a-zA-Z%+-]+)[$&]') | 164 var match = new RegExp(r'[?&]redirect_uri=([0-9a-zA-Z%+-]+)[$&]') |
156 .firstMatch(line); | 165 .firstMatch(line); |
157 expect(match, isNotNull); | 166 expect(match, isNotNull); |
158 | 167 |
159 var redirectUrl = new Uri.fromString(decodeUriComponent(match.group(1))); | 168 var redirectUrl = new Uri.fromString(decodeUriComponent(match.group(1))); |
160 redirectUrl = addQueryParameters(redirectUrl, {'code': 'access code'}); | 169 redirectUrl = addQueryParameters(redirectUrl, {'code': 'access code'}); |
161 return (new http.Request('GET', redirectUrl)..followRedirects = false) | 170 return (new http.Request('GET', redirectUrl)..followRedirects = false) |
162 .send(); | 171 .send(); |
(...skipping 13 matching lines...) Expand all Loading... |
176 | 185 |
177 response.headers.contentType = new ContentType("application", "json"); | 186 response.headers.contentType = new ContentType("application", "json"); |
178 response.outputStream.writeString(JSON.stringify({ | 187 response.outputStream.writeString(JSON.stringify({ |
179 "access_token": accessToken, | 188 "access_token": accessToken, |
180 "token_type": "bearer" | 189 "token_type": "bearer" |
181 })); | 190 })); |
182 response.outputStream.close(); | 191 response.outputStream.close(); |
183 }); | 192 }); |
184 }); | 193 }); |
185 } | 194 } |
OLD | NEW |