| 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 'test_pub.dart'; |
| 12 import 'test_pub.dart'; |
| 12 import '../../../pkg/http/lib/http.dart' as http; | 13 import '../../../pkg/http/lib/http.dart' as http; |
| 13 import '../../../pkg/unittest/lib/unittest.dart'; | 14 import '../../../pkg/unittest/lib/unittest.dart'; |
| 14 import '../../pub/io.dart'; | 15 import '../../pub/io.dart'; |
| 15 import '../../pub/utils.dart'; | 16 import '../../pub/utils.dart'; |
| 16 | 17 |
| 17 main() { | 18 main() { |
| 18 setUp(() => normalPackage.scheduleCreate()); | 19 setUp(() => normalPackage.scheduleCreate()); |
| 19 | 20 |
| 20 test('with no credentials.json, authenticates and saves credentials.json', | 21 integration('with no credentials.json, authenticates and saves ' |
| 21 () { | 22 'credentials.json', () { |
| 22 var server = new ScheduledServer(); | 23 var server = new ScheduledServer(); |
| 23 var pub = startPubLish(server); | 24 var pub = startPubLish(server); |
| 24 confirmPublish(pub); | 25 confirmPublish(pub); |
| 25 authorizePub(pub, server); | 26 authorizePub(pub, server); |
| 26 | 27 |
| 27 server.handle('GET', '/packages/versions/new.json', (request, response) { | 28 server.handle('GET', '/packages/versions/new.json', (request, response) { |
| 28 expect(request.headers.value('authorization'), | 29 expect(request.headers.value('authorization'), |
| 29 equals('Bearer access token')); | 30 equals('Bearer access token')); |
| 30 | 31 |
| 31 response.outputStream.close(); | 32 response.outputStream.close(); |
| 32 }); | 33 }); |
| 33 | 34 |
| 34 pub.kill(); | 35 pub.kill(); |
| 35 | 36 |
| 36 credentialsFile(server, 'access token').scheduleValidate(); | 37 credentialsFile(server, 'access token').scheduleValidate(); |
| 37 | |
| 38 run(); | |
| 39 }); | 38 }); |
| 40 | 39 |
| 41 test('with a pre-existing credentials.json does not authenticate', () { | 40 integration('with a pre-existing credentials.json does not authenticate', () { |
| 42 var server = new ScheduledServer(); | 41 var server = new ScheduledServer(); |
| 43 credentialsFile(server, 'access token').scheduleCreate(); | 42 credentialsFile(server, 'access token').scheduleCreate(); |
| 44 var pub = startPubLish(server); | 43 var pub = startPubLish(server); |
| 45 confirmPublish(pub); | 44 confirmPublish(pub); |
| 46 | 45 |
| 47 server.handle('GET', '/packages/versions/new.json', (request, response) { | 46 server.handle('GET', '/packages/versions/new.json', (request, response) { |
| 48 expect(request.headers.value('authorization'), | 47 expect(request.headers.value('authorization'), |
| 49 equals('Bearer access token')); | 48 equals('Bearer access token')); |
| 50 | 49 |
| 51 response.outputStream.close(); | 50 response.outputStream.close(); |
| 52 }); | 51 }); |
| 53 | 52 |
| 54 pub.kill(); | 53 pub.kill(); |
| 55 | |
| 56 run(); | |
| 57 }); | 54 }); |
| 58 | 55 |
| 59 test('with an expired credentials.json, refreshes and saves the refreshed ' | 56 integration('with an expired credentials.json, refreshes and saves the ' |
| 60 'access token to credentials.json', () { | 57 'refreshed access token to credentials.json', () { |
| 61 var server = new ScheduledServer(); | 58 var server = new ScheduledServer(); |
| 62 credentialsFile(server, 'access token', | 59 credentialsFile(server, 'access token', |
| 63 refreshToken: 'refresh token', | 60 refreshToken: 'refresh token', |
| 64 expiration: new Date.now().subtract(new Duration(hours: 1))) | 61 expiration: new Date.now().subtract(new Duration(hours: 1))) |
| 65 .scheduleCreate(); | 62 .scheduleCreate(); |
| 66 | 63 |
| 67 var pub = startPubLish(server); | 64 var pub = startPubLish(server); |
| 68 confirmPublish(pub); | 65 confirmPublish(pub); |
| 69 | 66 |
| 70 server.handle('POST', '/token', (request, response) { | 67 server.handle('POST', '/token', (request, response) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 86 expect(request.headers.value('authorization'), | 83 expect(request.headers.value('authorization'), |
| 87 equals('Bearer new access token')); | 84 equals('Bearer new access token')); |
| 88 | 85 |
| 89 response.outputStream.close(); | 86 response.outputStream.close(); |
| 90 }); | 87 }); |
| 91 | 88 |
| 92 pub.shouldExit(); | 89 pub.shouldExit(); |
| 93 | 90 |
| 94 credentialsFile(server, 'new access token', refreshToken: 'refresh token') | 91 credentialsFile(server, 'new access token', refreshToken: 'refresh token') |
| 95 .scheduleValidate(); | 92 .scheduleValidate(); |
| 96 | |
| 97 run(); | |
| 98 }); | 93 }); |
| 99 | 94 |
| 100 test('with an expired credentials.json without a refresh token, ' | 95 integration('with an expired credentials.json without a refresh token, ' |
| 101 'authenticates again and saves credentials.json', () { | 96 'authenticates again and saves credentials.json', () { |
| 102 var server = new ScheduledServer(); | 97 var server = new ScheduledServer(); |
| 103 credentialsFile(server, 'access token', | 98 credentialsFile(server, 'access token', |
| 104 expiration: new Date.now().subtract(new Duration(hours: 1))) | 99 expiration: new Date.now().subtract(new Duration(hours: 1))) |
| 105 .scheduleCreate(); | 100 .scheduleCreate(); |
| 106 | 101 |
| 107 var pub = startPubLish(server); | 102 var pub = startPubLish(server); |
| 108 confirmPublish(pub); | 103 confirmPublish(pub); |
| 109 | 104 |
| 110 expectLater(pub.nextErrLine(), equals("Pub's authorization to upload " | 105 expectLater(pub.nextErrLine(), equals("Pub's authorization to upload " |
| 111 "packages has expired and can't be automatically refreshed.")); | 106 "packages has expired and can't be automatically refreshed.")); |
| 112 authorizePub(pub, server, "new access token"); | 107 authorizePub(pub, server, "new access token"); |
| 113 | 108 |
| 114 server.handle('GET', '/packages/versions/new.json', (request, response) { | 109 server.handle('GET', '/packages/versions/new.json', (request, response) { |
| 115 expect(request.headers.value('authorization'), | 110 expect(request.headers.value('authorization'), |
| 116 equals('Bearer new access token')); | 111 equals('Bearer new access token')); |
| 117 | 112 |
| 118 response.outputStream.close(); | 113 response.outputStream.close(); |
| 119 }); | 114 }); |
| 120 | 115 |
| 121 pub.kill(); | 116 pub.kill(); |
| 122 | 117 |
| 123 credentialsFile(server, 'new access token').scheduleValidate(); | 118 credentialsFile(server, 'new access token').scheduleValidate(); |
| 124 | |
| 125 run(); | |
| 126 }); | 119 }); |
| 127 | 120 |
| 128 test('with a malformed credentials.json, authenticates again and saves ' | 121 integration('with a malformed credentials.json, authenticates again and ' |
| 129 'credentials.json', () { | 122 'saves credentials.json', () { |
| 130 var server = new ScheduledServer(); | 123 var server = new ScheduledServer(); |
| 131 dir(cachePath, [ | 124 dir(cachePath, [ |
| 132 file('credentials.json', '{bad json') | 125 file('credentials.json', '{bad json') |
| 133 ]).scheduleCreate(); | 126 ]).scheduleCreate(); |
| 134 | 127 |
| 135 var pub = startPubLish(server); | 128 var pub = startPubLish(server); |
| 136 confirmPublish(pub); | 129 confirmPublish(pub); |
| 137 authorizePub(pub, server, "new access token"); | 130 authorizePub(pub, server, "new access token"); |
| 138 | 131 |
| 139 server.handle('GET', '/packages/versions/new.json', (request, response) { | 132 server.handle('GET', '/packages/versions/new.json', (request, response) { |
| 140 expect(request.headers.value('authorization'), | 133 expect(request.headers.value('authorization'), |
| 141 equals('Bearer new access token')); | 134 equals('Bearer new access token')); |
| 142 | 135 |
| 143 response.outputStream.close(); | 136 response.outputStream.close(); |
| 144 }); | 137 }); |
| 145 | 138 |
| 146 pub.kill(); | 139 pub.kill(); |
| 147 | 140 |
| 148 credentialsFile(server, 'new access token').scheduleValidate(); | 141 credentialsFile(server, 'new access token').scheduleValidate(); |
| 149 | |
| 150 run(); | |
| 151 }); | 142 }); |
| 152 | 143 |
| 153 test('with server-rejected credentials, authenticates again and saves ' | 144 integration('with server-rejected credentials, authenticates again and saves ' |
| 154 'credentials.json', () { | 145 'credentials.json', () { |
| 155 var server = new ScheduledServer(); | 146 var server = new ScheduledServer(); |
| 156 credentialsFile(server, 'access token').scheduleCreate(); | 147 credentialsFile(server, 'access token').scheduleCreate(); |
| 157 var pub = startPubLish(server); | 148 var pub = startPubLish(server); |
| 158 | 149 |
| 159 confirmPublish(pub); | 150 confirmPublish(pub); |
| 160 | 151 |
| 161 server.handle('GET', '/packages/versions/new.json', (request, response) { | 152 server.handle('GET', '/packages/versions/new.json', (request, response) { |
| 162 response.statusCode = 401; | 153 response.statusCode = 401; |
| 163 response.headers.set('www-authenticate', 'Bearer error="invalid_token",' | 154 response.headers.set('www-authenticate', 'Bearer error="invalid_token",' |
| 164 ' error_description="your token sucks"'); | 155 ' error_description="your token sucks"'); |
| 165 response.outputStream.writeString(json.stringify({ | 156 response.outputStream.writeString(json.stringify({ |
| 166 'error': {'message': 'your token sucks'} | 157 'error': {'message': 'your token sucks'} |
| 167 })); | 158 })); |
| 168 response.outputStream.close(); | 159 response.outputStream.close(); |
| 169 }); | 160 }); |
| 170 | 161 |
| 171 expectLater(pub.nextErrLine(), equals('OAuth2 authorization failed (your ' | 162 expectLater(pub.nextErrLine(), equals('OAuth2 authorization failed (your ' |
| 172 'token sucks).')); | 163 'token sucks).')); |
| 173 // TODO(rnystrom): The confirm line is run together with this one because | 164 // TODO(rnystrom): The confirm line is run together with this one because |
| 174 // in normal usage, the user will have entered a newline on stdin which | 165 // in normal usage, the user will have entered a newline on stdin which |
| 175 // gets echoed to the terminal. Do something better here? | 166 // gets echoed to the terminal. Do something better here? |
| 176 expectLater(pub.nextLine(), equals( | 167 expectLater(pub.nextLine(), equals( |
| 177 'Looks great! Are you ready to upload your package (y/n)? ' | 168 'Looks great! Are you ready to upload your package (y/n)? ' |
| 178 'Pub needs your authorization to upload packages on your behalf.')); | 169 'Pub needs your authorization to upload packages on your behalf.')); |
| 179 pub.kill(); | 170 pub.kill(); |
| 180 run(); | |
| 181 }); | 171 }); |
| 182 } | 172 } |
| 183 | 173 |
| 184 void authorizePub(ScheduledProcess pub, ScheduledServer server, | 174 void authorizePub(ScheduledProcess pub, ScheduledServer server, |
| 185 [String accessToken="access token"]) { | 175 [String accessToken="access token"]) { |
| 186 // TODO(rnystrom): The confirm line is run together with this one because | 176 // TODO(rnystrom): The confirm line is run together with this one because |
| 187 // in normal usage, the user will have entered a newline on stdin which | 177 // in normal usage, the user will have entered a newline on stdin which |
| 188 // gets echoed to the terminal. Do something better here? | 178 // gets echoed to the terminal. Do something better here? |
| 189 expectLater(pub.nextLine(), equals( | 179 expectLater(pub.nextLine(), equals( |
| 190 'Looks great! Are you ready to upload your package (y/n)? ' | 180 'Looks great! Are you ready to upload your package (y/n)? ' |
| (...skipping 24 matching lines...) Expand all Loading... |
| 215 | 205 |
| 216 response.headers.contentType = new ContentType("application", "json"); | 206 response.headers.contentType = new ContentType("application", "json"); |
| 217 response.outputStream.writeString(json.stringify({ | 207 response.outputStream.writeString(json.stringify({ |
| 218 "access_token": accessToken, | 208 "access_token": accessToken, |
| 219 "token_type": "bearer" | 209 "token_type": "bearer" |
| 220 })); | 210 })); |
| 221 response.outputStream.close(); | 211 response.outputStream.close(); |
| 222 }); | 212 }); |
| 223 }); | 213 }); |
| 224 } | 214 } |
| OLD | NEW |