Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library oauth2_test; | |
| 6 | |
| 7 import 'dart:io'; | |
| 8 import 'dart:json'; | |
| 9 | |
| 10 import 'test_pub.dart'; | |
| 11 import '../../../pkg/unittest/lib/unittest.dart'; | |
| 12 import '../../pub/io.dart'; | |
| 13 | |
| 14 main() { | |
| 15 setUp(() => dir(appPath, [libPubspec("test_pkg", "1.0.0")]).scheduleCreate()); | |
| 16 | |
| 17 test('with no credentials.json, authenticates and saves credentials.json', | |
| 18 () { | |
| 19 var server = new ScheduledServer(); | |
| 20 var pub = startPubLish(server); | |
| 21 | |
| 22 expectLater(pub.nextLine(), equals('Pub needs your ' | |
| 23 'authorization to upload packages on your behalf.')); | |
| 24 pub.writeLine('access code'); | |
| 25 | |
| 26 handleAccessTokenRequest(server, "access token"); | |
| 27 | |
| 28 server.handle('GET', '/packages/versions/new.json', (request, response) { | |
|
Bob Nystrom
2012/11/27 21:00:53
These callbacks need to be wrapped in expectAsync
nweiz
2012/11/27 22:07:34
It's actually important that they're *not*. They'r
| |
| 29 expect(request.headers.value('authorization'), | |
| 30 equals('Bearer access token')); | |
| 31 | |
| 32 response.outputStream.close(); | |
| 33 }); | |
| 34 | |
| 35 pub.kill(); | |
| 36 | |
| 37 credentialsFile(server, 'access token').scheduleValidate(); | |
| 38 | |
| 39 run(); | |
| 40 }); | |
| 41 | |
| 42 test('with a pre-existing credentials.json does not authenticate', () { | |
| 43 var server = new ScheduledServer(); | |
| 44 credentialsFile(server, 'access token').scheduleCreate(); | |
| 45 var pub = startPubLish(server); | |
| 46 | |
| 47 server.handle('GET', '/packages/versions/new.json', (request, response) { | |
| 48 expect(request.headers.value('authorization'), | |
| 49 equals('Bearer access token')); | |
| 50 | |
| 51 response.outputStream.close(); | |
| 52 }); | |
| 53 | |
| 54 pub.kill(); | |
| 55 | |
| 56 run(); | |
| 57 }); | |
| 58 | |
| 59 test('with an expired credentials.json, refreshes and saves the refreshed ' | |
| 60 'access token to credentials.json', () { | |
| 61 var server = new ScheduledServer(); | |
| 62 credentialsFile(server, 'access token', | |
| 63 refreshToken: 'refresh token', | |
| 64 expiration: new Date.now().subtract(new Duration(hours: 1))) | |
| 65 .scheduleCreate(); | |
| 66 | |
| 67 var pub = startPubLish(server); | |
| 68 | |
| 69 server.handle('POST', '/token', (request, response) { | |
| 70 return consumeInputStream(request.inputStream).transform((bytes) { | |
| 71 var body = new String.fromCharCodes(bytes); | |
| 72 expect(body, matches( | |
| 73 new RegExp(r'(^|&)refresh_token=refresh%20token(&|$)'))); | |
| 74 | |
| 75 response.headers.contentType = new ContentType("application", "json"); | |
| 76 response.outputStream.writeString(JSON.stringify({ | |
| 77 "access_token": "new access token", | |
| 78 "token_type": "bearer" | |
| 79 })); | |
| 80 response.outputStream.close(); | |
| 81 }); | |
| 82 }); | |
| 83 | |
| 84 server.handle('GET', '/packages/versions/new.json', (request, response) { | |
| 85 expect(request.headers.value('authorization'), | |
| 86 equals('Bearer new access token')); | |
| 87 | |
| 88 response.outputStream.close(); | |
| 89 }); | |
| 90 | |
| 91 pub.shouldExit(); | |
| 92 | |
| 93 credentialsFile(server, 'new access token', refreshToken: 'refresh token') | |
| 94 .scheduleValidate(); | |
| 95 | |
| 96 run(); | |
| 97 }); | |
| 98 | |
| 99 test('with an expired credentials.json without a refresh token, ' | |
| 100 'authenticates again and saves credentials.json', () { | |
| 101 var server = new ScheduledServer(); | |
| 102 credentialsFile(server, 'access token', | |
| 103 expiration: new Date.now().subtract(new Duration(hours: 1))) | |
| 104 .scheduleCreate(); | |
| 105 | |
| 106 var pub = startPubLish(server); | |
| 107 | |
| 108 expectLater(pub.nextErrLine(), equals("Pub's authorization to upload " | |
| 109 "packages has expired and can't be automatically refreshed.")); | |
| 110 expectLater(pub.nextLine(), equals('Pub needs your ' | |
| 111 'authorization to upload packages on your behalf.')); | |
| 112 pub.writeLine('access code'); | |
| 113 | |
| 114 handleAccessTokenRequest(server, "new access token"); | |
| 115 | |
| 116 server.handle('GET', '/packages/versions/new.json', (request, response) { | |
| 117 expect(request.headers.value('authorization'), | |
| 118 equals('Bearer new access token')); | |
| 119 | |
| 120 response.outputStream.close(); | |
| 121 }); | |
| 122 | |
| 123 pub.kill(); | |
| 124 | |
| 125 credentialsFile(server, 'new access token').scheduleValidate(); | |
| 126 | |
| 127 run(); | |
| 128 }); | |
| 129 | |
| 130 test('with a malformed credentials.json, authenticates again and saves ' | |
| 131 'credentials.json', () { | |
| 132 var server = new ScheduledServer(); | |
| 133 dir(cachePath, [ | |
| 134 file('credentials.json', '{bad json') | |
| 135 ]).scheduleCreate(); | |
| 136 | |
| 137 var pub = startPubLish(server); | |
| 138 | |
| 139 expectLater(pub.nextLine(), equals('Pub needs your ' | |
| 140 'authorization to upload packages on your behalf.')); | |
| 141 pub.writeLine('access code'); | |
| 142 | |
| 143 handleAccessTokenRequest(server, "new access token"); | |
| 144 | |
| 145 server.handle('GET', '/packages/versions/new.json', (request, response) { | |
| 146 expect(request.headers.value('authorization'), | |
| 147 equals('Bearer new access token')); | |
| 148 | |
| 149 response.outputStream.close(); | |
| 150 }); | |
| 151 | |
| 152 pub.kill(); | |
| 153 | |
| 154 credentialsFile(server, 'new access token').scheduleValidate(); | |
| 155 | |
| 156 run(); | |
| 157 }); | |
| 158 } | |
| 159 | |
| 160 void handleAccessTokenRequest(ScheduledServer server, String accessToken) { | |
|
Bob Nystrom
2012/11/27 21:00:53
What's the difference between this and the code at
nweiz
2012/11/27 22:07:34
That code handles a refresh request, while this co
| |
| 161 server.handle('POST', '/token', (request, response) { | |
| 162 return consumeInputStream(request.inputStream).transform((bytes) { | |
| 163 var body = new String.fromCharCodes(bytes); | |
| 164 expect(body, matches(new RegExp(r'(^|&)code=access%20code(&|$)'))); | |
| 165 | |
| 166 response.headers.contentType = new ContentType("application", "json"); | |
| 167 response.outputStream.writeString(JSON.stringify({ | |
| 168 "access_token": accessToken, | |
| 169 "token_type": "bearer" | |
| 170 })); | |
| 171 response.outputStream.close(); | |
| 172 }); | |
| 173 }); | |
| 174 } | |
| OLD | NEW |