| 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 import 'dart:convert'; | 6 import 'dart:convert'; |
| 7 | 7 |
| 8 import 'package:http/http.dart' as http; | 8 import 'package:http/http.dart' as http; |
| 9 import 'package:oauth2/oauth2.dart' as oauth2; | 9 import 'package:oauth2/oauth2.dart' as oauth2; |
| 10 import 'package:test/test.dart'; | 10 import 'package:test/test.dart'; |
| 11 | 11 |
| 12 import 'utils.dart'; | 12 import 'utils.dart'; |
| 13 | 13 |
| 14 final Uri tokenEndpoint = Uri.parse('http://example.com/token'); | 14 final Uri tokenEndpoint = Uri.parse('http://example.com/token'); |
| 15 | 15 |
| 16 void main() { | 16 void main() { |
| 17 var httpClient; | 17 var httpClient; |
| 18 setUp(() => httpClient = new ExpectClient()); | 18 setUp(() => httpClient = new ExpectClient()); |
| 19 | 19 |
| 20 test('is not expired if no expiration exists', () { | 20 test('is not expired if no expiration exists', () { |
| 21 var credentials = new oauth2.Credentials('access token'); | 21 var credentials = new oauth2.Credentials('access token'); |
| 22 expect(credentials.isExpired, isFalse); | 22 expect(credentials.isExpired, isFalse); |
| 23 }); | 23 }); |
| 24 | 24 |
| 25 test('is not expired if the expiration is in the future', () { | 25 test('is not expired if the expiration is in the future', () { |
| 26 var expiration = new DateTime.now().add(new Duration(hours: 1)); | 26 var expiration = new DateTime.now().add(new Duration(hours: 1)); |
| 27 var credentials = new oauth2.Credentials( | 27 var credentials = new oauth2.Credentials( |
| 28 'access token', null, null, null, expiration); | 28 'access token', expiration: expiration); |
| 29 expect(credentials.isExpired, isFalse); | 29 expect(credentials.isExpired, isFalse); |
| 30 }); | 30 }); |
| 31 | 31 |
| 32 test('is expired if the expiration is in the past', () { | 32 test('is expired if the expiration is in the past', () { |
| 33 var expiration = new DateTime.now().subtract(new Duration(hours: 1)); | 33 var expiration = new DateTime.now().subtract(new Duration(hours: 1)); |
| 34 var credentials = new oauth2.Credentials( | 34 var credentials = new oauth2.Credentials( |
| 35 'access token', null, null, null, expiration); | 35 'access token', expiration: expiration); |
| 36 expect(credentials.isExpired, isTrue); | 36 expect(credentials.isExpired, isTrue); |
| 37 }); | 37 }); |
| 38 | 38 |
| 39 test("can't refresh without a refresh token", () { | 39 test("can't refresh without a refresh token", () { |
| 40 var credentials = new oauth2.Credentials( | 40 var credentials = new oauth2.Credentials( |
| 41 'access token', null, tokenEndpoint); | 41 'access token', tokenEndpoint: tokenEndpoint); |
| 42 expect(credentials.canRefresh, false); | 42 expect(credentials.canRefresh, false); |
| 43 | 43 |
| 44 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient), | 44 expect(credentials.refresh( |
| 45 identifier: 'identifier', |
| 46 secret: 'secret', |
| 47 httpClient: httpClient), |
| 45 throwsStateError); | 48 throwsStateError); |
| 46 }); | 49 }); |
| 47 | 50 |
| 48 test("can't refresh without a token endpoint", () { | 51 test("can't refresh without a token endpoint", () { |
| 49 var credentials = new oauth2.Credentials('access token', 'refresh token'); | 52 var credentials = new oauth2.Credentials( |
| 53 'access token', refreshToken: 'refresh token'); |
| 50 expect(credentials.canRefresh, false); | 54 expect(credentials.canRefresh, false); |
| 51 | 55 |
| 52 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient), | 56 expect(credentials.refresh( |
| 57 identifier: 'identifier', |
| 58 secret: 'secret', |
| 59 httpClient: httpClient), |
| 53 throwsStateError); | 60 throwsStateError); |
| 54 }); | 61 }); |
| 55 | 62 |
| 56 test("can refresh with a refresh token and a token endpoint", () async { | 63 test("can refresh with a refresh token and a token endpoint", () async { |
| 57 var credentials = new oauth2.Credentials( | 64 var credentials = new oauth2.Credentials( |
| 58 'access token', 'refresh token', tokenEndpoint, ['scope1', 'scope2']); | 65 'access token', |
| 66 refreshToken: 'refresh token', |
| 67 tokenEndpoint: tokenEndpoint, |
| 68 scopes: ['scope1', 'scope2']); |
| 59 expect(credentials.canRefresh, true); | 69 expect(credentials.canRefresh, true); |
| 60 | 70 |
| 61 httpClient.expectRequest((request) { | 71 httpClient.expectRequest((request) { |
| 72 expect(request.method, equals('POST')); |
| 73 expect(request.url.toString(), equals(tokenEndpoint.toString())); |
| 74 expect(request.bodyFields, equals({ |
| 75 "grant_type": "refresh_token", |
| 76 "refresh_token": "refresh token", |
| 77 "scope": "scope1 scope2" |
| 78 })); |
| 79 expect(request.headers, containsPair( |
| 80 "Authorization", |
| 81 "Basic aWQlQzMlQUJudCVDNCVBQmZpZXI6cyVDMyVBQmNyZXQ=")); |
| 82 |
| 83 return new Future.value(new http.Response(JSON.encode({ |
| 84 'access_token': 'new access token', |
| 85 'token_type': 'bearer', |
| 86 'refresh_token': 'new refresh token' |
| 87 }), 200, headers: {'content-type': 'application/json'})); |
| 88 }); |
| 89 |
| 90 credentials = await credentials.refresh( |
| 91 identifier: 'idëntīfier', |
| 92 secret: 'sëcret', |
| 93 httpClient: httpClient); |
| 94 expect(credentials.accessToken, equals('new access token')); |
| 95 expect(credentials.refreshToken, equals('new refresh token')); |
| 96 }); |
| 97 |
| 98 test("can refresh without a client secret", () async { |
| 99 var credentials = new oauth2.Credentials( |
| 100 'access token', |
| 101 refreshToken: 'refresh token', |
| 102 tokenEndpoint: tokenEndpoint, |
| 103 scopes: ['scope1', 'scope2']); |
| 104 expect(credentials.canRefresh, true); |
| 105 |
| 106 httpClient.expectRequest((request) { |
| 62 expect(request.method, equals('POST')); | 107 expect(request.method, equals('POST')); |
| 63 expect(request.url.toString(), equals(tokenEndpoint.toString())); | 108 expect(request.url.toString(), equals(tokenEndpoint.toString())); |
| 64 expect(request.bodyFields, equals({ | 109 expect(request.bodyFields, equals({ |
| 65 "grant_type": "refresh_token", | 110 "grant_type": "refresh_token", |
| 66 "refresh_token": "refresh token", | 111 "refresh_token": "refresh token", |
| 67 "scope": "scope1 scope2", | 112 "scope": "scope1 scope2", |
| 68 "client_id": "identifier", | 113 "client_id": "identifier" |
| 69 "client_secret": "secret" | |
| 70 })); | 114 })); |
| 71 | 115 |
| 72 return new Future.value(new http.Response(JSON.encode({ | 116 return new Future.value(new http.Response(JSON.encode({ |
| 73 'access_token': 'new access token', | 117 'access_token': 'new access token', |
| 74 'token_type': 'bearer', | 118 'token_type': 'bearer', |
| 75 'refresh_token': 'new refresh token' | 119 'refresh_token': 'new refresh token' |
| 76 }), 200, headers: {'content-type': 'application/json'})); | 120 }), 200, headers: {'content-type': 'application/json'})); |
| 77 }); | 121 }); |
| 78 | 122 |
| 79 | 123 |
| 80 credentials = await credentials.refresh('identifier', 'secret', | 124 credentials = await credentials.refresh( |
| 125 identifier: 'identifier', |
| 81 httpClient: httpClient); | 126 httpClient: httpClient); |
| 82 expect(credentials.accessToken, equals('new access token')); | 127 expect(credentials.accessToken, equals('new access token')); |
| 83 expect(credentials.refreshToken, equals('new refresh token')); | 128 expect(credentials.refreshToken, equals('new refresh token')); |
| 84 }); | 129 }); |
| 85 | 130 |
| 86 test("uses the old refresh token if a new one isn't provided", () async { | 131 test("can refresh without client authentication", () async { |
| 87 var credentials = new oauth2.Credentials( | 132 var credentials = new oauth2.Credentials( |
| 88 'access token', 'refresh token', tokenEndpoint); | 133 'access token', |
| 134 refreshToken: 'refresh token', |
| 135 tokenEndpoint: tokenEndpoint, |
| 136 scopes: ['scope1', 'scope2']); |
| 89 expect(credentials.canRefresh, true); | 137 expect(credentials.canRefresh, true); |
| 90 | 138 |
| 91 httpClient.expectRequest((request) { | 139 httpClient.expectRequest((request) { |
| 92 expect(request.method, equals('POST')); | 140 expect(request.method, equals('POST')); |
| 93 expect(request.url.toString(), equals(tokenEndpoint.toString())); | 141 expect(request.url.toString(), equals(tokenEndpoint.toString())); |
| 94 expect(request.bodyFields, equals({ | 142 expect(request.bodyFields, equals({ |
| 95 "grant_type": "refresh_token", | 143 "grant_type": "refresh_token", |
| 96 "refresh_token": "refresh token", | 144 "refresh_token": "refresh token", |
| 97 "client_id": "identifier", | 145 "scope": "scope1 scope2" |
| 98 "client_secret": "secret" | |
| 99 })); | 146 })); |
| 100 | 147 |
| 101 return new Future.value(new http.Response(JSON.encode({ | 148 return new Future.value(new http.Response(JSON.encode({ |
| 102 'access_token': 'new access token', | 149 'access_token': 'new access token', |
| 150 'token_type': 'bearer', |
| 151 'refresh_token': 'new refresh token' |
| 152 }), 200, headers: {'content-type': 'application/json'})); |
| 153 }); |
| 154 |
| 155 |
| 156 credentials = await credentials.refresh(httpClient: httpClient); |
| 157 expect(credentials.accessToken, equals('new access token')); |
| 158 expect(credentials.refreshToken, equals('new refresh token')); |
| 159 }); |
| 160 |
| 161 test("uses the old refresh token if a new one isn't provided", () async { |
| 162 var credentials = new oauth2.Credentials( |
| 163 'access token', |
| 164 refreshToken: 'refresh token', |
| 165 tokenEndpoint: tokenEndpoint); |
| 166 expect(credentials.canRefresh, true); |
| 167 |
| 168 httpClient.expectRequest((request) { |
| 169 expect(request.method, equals('POST')); |
| 170 expect(request.url.toString(), equals(tokenEndpoint.toString())); |
| 171 expect(request.bodyFields, equals({ |
| 172 "grant_type": "refresh_token", |
| 173 "refresh_token": "refresh token" |
| 174 })); |
| 175 expect(request.headers, containsPair( |
| 176 "Authorization", |
| 177 "Basic aWQlQzMlQUJudCVDNCVBQmZpZXI6cyVDMyVBQmNyZXQ=")); |
| 178 |
| 179 return new Future.value(new http.Response(JSON.encode({ |
| 180 'access_token': 'new access token', |
| 103 'token_type': 'bearer' | 181 'token_type': 'bearer' |
| 104 }), 200, headers: {'content-type': 'application/json'})); | 182 }), 200, headers: {'content-type': 'application/json'})); |
| 105 }); | 183 }); |
| 106 | 184 |
| 107 | 185 |
| 108 credentials = await credentials.refresh('identifier', 'secret', | 186 credentials = await credentials.refresh( |
| 187 identifier: 'idëntīfier', |
| 188 secret: 'sëcret', |
| 109 httpClient: httpClient); | 189 httpClient: httpClient); |
| 110 expect(credentials.accessToken, equals('new access token')); | 190 expect(credentials.accessToken, equals('new access token')); |
| 111 expect(credentials.refreshToken, equals('refresh token')); | 191 expect(credentials.refreshToken, equals('refresh token')); |
| 112 }); | 192 }); |
| 113 | 193 |
| 194 test("uses form-field authentication if basicAuth is false", () async { |
| 195 var credentials = new oauth2.Credentials( |
| 196 'access token', |
| 197 refreshToken: 'refresh token', |
| 198 tokenEndpoint: tokenEndpoint, |
| 199 scopes: ['scope1', 'scope2']); |
| 200 expect(credentials.canRefresh, true); |
| 201 |
| 202 httpClient.expectRequest((request) { |
| 203 expect(request.method, equals('POST')); |
| 204 expect(request.url.toString(), equals(tokenEndpoint.toString())); |
| 205 expect(request.bodyFields, equals({ |
| 206 "grant_type": "refresh_token", |
| 207 "refresh_token": "refresh token", |
| 208 "scope": "scope1 scope2", |
| 209 "client_id": "idëntīfier", |
| 210 "client_secret": "sëcret" |
| 211 })); |
| 212 |
| 213 return new Future.value(new http.Response(JSON.encode({ |
| 214 'access_token': 'new access token', |
| 215 'token_type': 'bearer', |
| 216 'refresh_token': 'new refresh token' |
| 217 }), 200, headers: {'content-type': 'application/json'})); |
| 218 }); |
| 219 |
| 220 credentials = await credentials.refresh( |
| 221 identifier: 'idëntīfier', |
| 222 secret: 'sëcret', |
| 223 basicAuth: false, |
| 224 httpClient: httpClient); |
| 225 expect(credentials.accessToken, equals('new access token')); |
| 226 expect(credentials.refreshToken, equals('new refresh token')); |
| 227 }); |
| 228 |
| 114 group("fromJson", () { | 229 group("fromJson", () { |
| 115 oauth2.Credentials fromMap(Map map) => | 230 oauth2.Credentials fromMap(Map map) => |
| 116 new oauth2.Credentials.fromJson(JSON.encode(map)); | 231 new oauth2.Credentials.fromJson(JSON.encode(map)); |
| 117 | 232 |
| 118 test("should load the same credentials from toJson", () { | 233 test("should load the same credentials from toJson", () { |
| 119 var expiration = new DateTime.now().subtract(new Duration(hours: 1)); | 234 var expiration = new DateTime.now().subtract(new Duration(hours: 1)); |
| 120 var credentials = new oauth2.Credentials( | 235 var credentials = new oauth2.Credentials( |
| 121 'access token', 'refresh token', tokenEndpoint, ['scope1', 'scope2'], | 236 'access token', |
| 122 expiration); | 237 refreshToken: 'refresh token', |
| 238 tokenEndpoint: tokenEndpoint, |
| 239 scopes: ['scope1', 'scope2'], |
| 240 expiration: expiration); |
| 123 var reloaded = new oauth2.Credentials.fromJson(credentials.toJson()); | 241 var reloaded = new oauth2.Credentials.fromJson(credentials.toJson()); |
| 124 | 242 |
| 125 expect(reloaded.accessToken, equals(credentials.accessToken)); | 243 expect(reloaded.accessToken, equals(credentials.accessToken)); |
| 126 expect(reloaded.refreshToken, equals(credentials.refreshToken)); | 244 expect(reloaded.refreshToken, equals(credentials.refreshToken)); |
| 127 expect(reloaded.tokenEndpoint.toString(), | 245 expect(reloaded.tokenEndpoint.toString(), |
| 128 equals(credentials.tokenEndpoint.toString())); | 246 equals(credentials.tokenEndpoint.toString())); |
| 129 expect(reloaded.scopes, equals(credentials.scopes)); | 247 expect(reloaded.scopes, equals(credentials.scopes)); |
| 130 expect(reloaded.expiration, equals(credentials.expiration)); | 248 expect(reloaded.expiration, equals(credentials.expiration)); |
| 131 }); | 249 }); |
| 132 | 250 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 162 expect(() => fromMap({"accessToken": "foo", "scopes": 12}), | 280 expect(() => fromMap({"accessToken": "foo", "scopes": 12}), |
| 163 throwsFormatException); | 281 throwsFormatException); |
| 164 }); | 282 }); |
| 165 | 283 |
| 166 test("should throw a FormatException if expiration is not an int", () { | 284 test("should throw a FormatException if expiration is not an int", () { |
| 167 expect(() => fromMap({"accessToken": "foo", "expiration": "12"}), | 285 expect(() => fromMap({"accessToken": "foo", "expiration": "12"}), |
| 168 throwsFormatException); | 286 throwsFormatException); |
| 169 }); | 287 }); |
| 170 }); | 288 }); |
| 171 } | 289 } |
| OLD | NEW |