| 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 10 matching lines...) Expand all Loading... |
| 21 'credentials.json', () { | 21 'credentials.json', () { |
| 22 var server = new ScheduledServer(); | 22 var server = new ScheduledServer(); |
| 23 var pub = startPubLish(server); | 23 var pub = startPubLish(server); |
| 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.close(); |
| 32 }); | 32 }); |
| 33 | 33 |
| 34 // 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 |
| 35 // 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. |
| 36 pub.shouldExit(1); | 36 pub.shouldExit(1); |
| 37 | 37 |
| 38 credentialsFile(server, 'access token').scheduleValidate(); | 38 credentialsFile(server, 'access token').scheduleValidate(); |
| 39 }); | 39 }); |
| 40 | 40 |
| 41 integration('with a pre-existing credentials.json does not authenticate', () { | 41 integration('with a pre-existing credentials.json does not authenticate', () { |
| 42 var server = new ScheduledServer(); | 42 var server = new ScheduledServer(); |
| 43 credentialsFile(server, 'access token').scheduleCreate(); | 43 credentialsFile(server, 'access token').scheduleCreate(); |
| 44 var pub = startPubLish(server); | 44 var pub = startPubLish(server); |
| 45 confirmPublish(pub); | 45 confirmPublish(pub); |
| 46 | 46 |
| 47 server.handle('GET', '/packages/versions/new.json', (request, response) { | 47 server.handle('GET', '/packages/versions/new.json', (request, response) { |
| 48 expect(request.headers.value('authorization'), | 48 expect(request.headers.value('authorization'), |
| 49 equals('Bearer access token')); | 49 equals('Bearer access token')); |
| 50 | 50 |
| 51 response.outputStream.close(); | 51 response.close(); |
| 52 }); | 52 }); |
| 53 | 53 |
| 54 pub.kill(); | 54 pub.kill(); |
| 55 }); | 55 }); |
| 56 | 56 |
| 57 integration('with an expired credentials.json, refreshes and saves the ' | 57 integration('with an expired credentials.json, refreshes and saves the ' |
| 58 'refreshed access token to credentials.json', () { | 58 'refreshed access token to credentials.json', () { |
| 59 var server = new ScheduledServer(); | 59 var server = new ScheduledServer(); |
| 60 credentialsFile(server, 'access token', | 60 credentialsFile(server, 'access token', |
| 61 refreshToken: 'refresh token', | 61 refreshToken: 'refresh token', |
| 62 expiration: new DateTime.now().subtract(new Duration(hours: 1))) | 62 expiration: new DateTime.now().subtract(new Duration(hours: 1))) |
| 63 .scheduleCreate(); | 63 .scheduleCreate(); |
| 64 | 64 |
| 65 var pub = startPubLish(server); | 65 var pub = startPubLish(server); |
| 66 confirmPublish(pub); | 66 confirmPublish(pub); |
| 67 | 67 |
| 68 server.handle('POST', '/token', (request, response) { | 68 server.handle('POST', '/token', (request, response) { |
| 69 return wrapInputStream(request.inputStream).toBytes().then((bytes) { | 69 return new ByteStream(request).toBytes().then((bytes) { |
| 70 var body = new String.fromCharCodes(bytes); | 70 var body = new String.fromCharCodes(bytes); |
| 71 expect(body, matches( | 71 expect(body, matches( |
| 72 new RegExp(r'(^|&)refresh_token=refresh\+token(&|$)'))); | 72 new RegExp(r'(^|&)refresh_token=refresh\+token(&|$)'))); |
| 73 | 73 |
| 74 response.headers.contentType = new ContentType("application", "json"); | 74 response.headers.contentType = new ContentType("application", "json"); |
| 75 response.outputStream.writeString(json.stringify({ | 75 response.addString(json.stringify({ |
| 76 "access_token": "new access token", | 76 "access_token": "new access token", |
| 77 "token_type": "bearer" | 77 "token_type": "bearer" |
| 78 })); | 78 })); |
| 79 response.outputStream.close(); | 79 response.close(); |
| 80 }); | 80 }); |
| 81 }); | 81 }); |
| 82 | 82 |
| 83 server.handle('GET', '/packages/versions/new.json', (request, response) { | 83 server.handle('GET', '/packages/versions/new.json', (request, response) { |
| 84 expect(request.headers.value('authorization'), | 84 expect(request.headers.value('authorization'), |
| 85 equals('Bearer new access token')); | 85 equals('Bearer new access token')); |
| 86 | 86 |
| 87 response.outputStream.close(); | 87 response.close(); |
| 88 }); | 88 }); |
| 89 | 89 |
| 90 pub.shouldExit(); | 90 pub.shouldExit(); |
| 91 | 91 |
| 92 credentialsFile(server, 'new access token', refreshToken: 'refresh token') | 92 credentialsFile(server, 'new access token', refreshToken: 'refresh token') |
| 93 .scheduleValidate(); | 93 .scheduleValidate(); |
| 94 }); | 94 }); |
| 95 | 95 |
| 96 integration('with an expired credentials.json without a refresh token, ' | 96 integration('with an expired credentials.json without a refresh token, ' |
| 97 'authenticates again and saves credentials.json', () { | 97 'authenticates again and saves credentials.json', () { |
| 98 var server = new ScheduledServer(); | 98 var server = new ScheduledServer(); |
| 99 credentialsFile(server, 'access token', | 99 credentialsFile(server, 'access token', |
| 100 expiration: new DateTime.now().subtract(new Duration(hours: 1))) | 100 expiration: new DateTime.now().subtract(new Duration(hours: 1))) |
| 101 .scheduleCreate(); | 101 .scheduleCreate(); |
| 102 | 102 |
| 103 var pub = startPubLish(server); | 103 var pub = startPubLish(server); |
| 104 confirmPublish(pub); | 104 confirmPublish(pub); |
| 105 | 105 |
| 106 expectLater(pub.nextErrLine(), equals("Pub's authorization to upload " | 106 expectLater(pub.nextErrLine(), equals("Pub's authorization to upload " |
| 107 "packages has expired and can't be automatically refreshed.")); | 107 "packages has expired and can't be automatically refreshed.")); |
| 108 authorizePub(pub, server, "new access token"); | 108 authorizePub(pub, server, "new access token"); |
| 109 | 109 |
| 110 server.handle('GET', '/packages/versions/new.json', (request, response) { | 110 server.handle('GET', '/packages/versions/new.json', (request, response) { |
| 111 expect(request.headers.value('authorization'), | 111 expect(request.headers.value('authorization'), |
| 112 equals('Bearer new access token')); | 112 equals('Bearer new access token')); |
| 113 | 113 |
| 114 response.outputStream.close(); | 114 response.close(); |
| 115 }); | 115 }); |
| 116 | 116 |
| 117 // 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 |
| 118 // 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. |
| 119 pub.shouldExit(1); | 119 pub.shouldExit(1); |
| 120 | 120 |
| 121 credentialsFile(server, 'new access token').scheduleValidate(); | 121 credentialsFile(server, 'new access token').scheduleValidate(); |
| 122 }); | 122 }); |
| 123 | 123 |
| 124 integration('with a malformed credentials.json, authenticates again and ' | 124 integration('with a malformed credentials.json, authenticates again and ' |
| 125 'saves credentials.json', () { | 125 'saves credentials.json', () { |
| 126 var server = new ScheduledServer(); | 126 var server = new ScheduledServer(); |
| 127 dir(cachePath, [ | 127 dir(cachePath, [ |
| 128 file('credentials.json', '{bad json') | 128 file('credentials.json', '{bad json') |
| 129 ]).scheduleCreate(); | 129 ]).scheduleCreate(); |
| 130 | 130 |
| 131 var pub = startPubLish(server); | 131 var pub = startPubLish(server); |
| 132 confirmPublish(pub); | 132 confirmPublish(pub); |
| 133 authorizePub(pub, server, "new access token"); | 133 authorizePub(pub, server, "new access token"); |
| 134 | 134 |
| 135 server.handle('GET', '/packages/versions/new.json', (request, response) { | 135 server.handle('GET', '/packages/versions/new.json', (request, response) { |
| 136 expect(request.headers.value('authorization'), | 136 expect(request.headers.value('authorization'), |
| 137 equals('Bearer new access token')); | 137 equals('Bearer new access token')); |
| 138 | 138 |
| 139 response.outputStream.close(); | 139 response.close(); |
| 140 }); | 140 }); |
| 141 | 141 |
| 142 | 142 |
| 143 // 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 |
| 144 // 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. |
| 145 pub.shouldExit(1); | 145 pub.shouldExit(1); |
| 146 | 146 |
| 147 credentialsFile(server, 'new access token').scheduleValidate(); | 147 credentialsFile(server, 'new access token').scheduleValidate(); |
| 148 }); | 148 }); |
| 149 | 149 |
| 150 integration('with server-rejected credentials, authenticates again and saves ' | 150 integration('with server-rejected credentials, authenticates again and saves ' |
| 151 'credentials.json', () { | 151 'credentials.json', () { |
| 152 var server = new ScheduledServer(); | 152 var server = new ScheduledServer(); |
| 153 credentialsFile(server, 'access token').scheduleCreate(); | 153 credentialsFile(server, 'access token').scheduleCreate(); |
| 154 var pub = startPubLish(server); | 154 var pub = startPubLish(server); |
| 155 | 155 |
| 156 confirmPublish(pub); | 156 confirmPublish(pub); |
| 157 | 157 |
| 158 server.handle('GET', '/packages/versions/new.json', (request, response) { | 158 server.handle('GET', '/packages/versions/new.json', (request, response) { |
| 159 response.statusCode = 401; | 159 response.statusCode = 401; |
| 160 response.headers.set('www-authenticate', 'Bearer error="invalid_token",' | 160 response.headers.set('www-authenticate', 'Bearer error="invalid_token",' |
| 161 ' error_description="your token sucks"'); | 161 ' error_description="your token sucks"'); |
| 162 response.outputStream.writeString(json.stringify({ | 162 response.addString(json.stringify({ |
| 163 'error': {'message': 'your token sucks'} | 163 'error': {'message': 'your token sucks'} |
| 164 })); | 164 })); |
| 165 response.outputStream.close(); | 165 response.close(); |
| 166 }); | 166 }); |
| 167 | 167 |
| 168 expectLater(pub.nextErrLine(), equals('OAuth2 authorization failed (your ' | 168 expectLater(pub.nextErrLine(), equals('OAuth2 authorization failed (your ' |
| 169 'token sucks).')); | 169 'token sucks).')); |
| 170 // TODO(rnystrom): The confirm line is run together with this one because | 170 // TODO(rnystrom): The confirm line is run together with this one because |
| 171 // in normal usage, the user will have entered a newline on stdin which | 171 // in normal usage, the user will have entered a newline on stdin which |
| 172 // gets echoed to the terminal. Do something better here? | 172 // gets echoed to the terminal. Do something better here? |
| 173 expectLater(pub.nextLine(), equals( | 173 expectLater(pub.nextLine(), equals( |
| 174 'Looks great! Are you ready to upload your package (y/n)? ' | 174 'Looks great! Are you ready to upload your package (y/n)? ' |
| 175 'Pub needs your authorization to upload packages on your behalf.')); | 175 'Pub needs your authorization to upload packages on your behalf.')); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 198 }).then((response) { | 198 }).then((response) { |
| 199 expect(response.headers['location'], | 199 expect(response.headers['location'], |
| 200 equals('http://pub.dartlang.org/authorized')); | 200 equals('http://pub.dartlang.org/authorized')); |
| 201 }), anything); | 201 }), anything); |
| 202 | 202 |
| 203 handleAccessTokenRequest(server, accessToken); | 203 handleAccessTokenRequest(server, accessToken); |
| 204 } | 204 } |
| 205 | 205 |
| 206 void handleAccessTokenRequest(ScheduledServer server, String accessToken) { | 206 void handleAccessTokenRequest(ScheduledServer server, String accessToken) { |
| 207 server.handle('POST', '/token', (request, response) { | 207 server.handle('POST', '/token', (request, response) { |
| 208 return wrapInputStream(request.inputStream).toBytes().then((bytes) { | 208 return new ByteStream(request).toBytes().then((bytes) { |
| 209 var body = new String.fromCharCodes(bytes); | 209 var body = new String.fromCharCodes(bytes); |
| 210 expect(body, matches(new RegExp(r'(^|&)code=access\+code(&|$)'))); | 210 expect(body, matches(new RegExp(r'(^|&)code=access\+code(&|$)'))); |
| 211 | 211 |
| 212 response.headers.contentType = new ContentType("application", "json"); | 212 response.headers.contentType = new ContentType("application", "json"); |
| 213 response.outputStream.writeString(json.stringify({ | 213 response.addString(json.stringify({ |
| 214 "access_token": accessToken, | 214 "access_token": accessToken, |
| 215 "token_type": "bearer" | 215 "token_type": "bearer" |
| 216 })); | 216 })); |
| 217 response.outputStream.close(); | 217 response.close(); |
| 218 }); | 218 }); |
| 219 }); | 219 }); |
| 220 } | 220 } |
| OLD | NEW |