Chromium Code Reviews| 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 credentials_test; | 5 library credentials_test; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 | 9 |
| 10 import 'package:http/http.dart' as http; | 10 import 'package:http/http.dart' as http; |
| 11 import 'package:oauth2/oauth2.dart' as oauth2; | 11 import 'package:oauth2/oauth2.dart' as oauth2; |
| 12 import 'package:unittest/unittest.dart'; | 12 import 'package:unittest/unittest.dart'; |
| 13 | 13 |
| 14 import 'utils.dart'; | 14 import 'utils.dart'; |
| 15 | 15 |
| 16 final Uri tokenEndpoint = Uri.parse('http://example.com/token'); | 16 final Uri tokenEndpoint = Uri.parse('http://example.com/token'); |
| 17 | 17 |
| 18 ExpectClient httpClient; | 18 void main() { |
| 19 ExpectClient httpClient; | |
|
nweiz
2014/03/05 22:38:08
If you're going to move this into [main], get rid
| |
| 19 | 20 |
| 20 void main() { | |
| 21 setUp(() => httpClient = new ExpectClient()); | 21 setUp(() => httpClient = new ExpectClient()); |
| 22 | 22 |
| 23 test('is not expired if no expiration exists', () { | 23 test('is not expired if no expiration exists', () { |
| 24 var credentials = new oauth2.Credentials('access token'); | 24 var credentials = new oauth2.Credentials('access token'); |
| 25 expect(credentials.isExpired, isFalse); | 25 expect(credentials.isExpired, isFalse); |
| 26 }); | 26 }); |
| 27 | 27 |
| 28 test('is not expired if the expiration is in the future', () { | 28 test('is not expired if the expiration is in the future', () { |
| 29 var expiration = new DateTime.now().add(new Duration(hours: 1)); | 29 var expiration = new DateTime.now().add(new Duration(hours: 1)); |
| 30 var credentials = new oauth2.Credentials( | 30 var credentials = new oauth2.Credentials( |
| 31 'access token', null, null, null, expiration); | 31 'access token', null, null, null, expiration); |
| 32 expect(credentials.isExpired, isFalse); | 32 expect(credentials.isExpired, isFalse); |
| 33 }); | 33 }); |
| 34 | 34 |
| 35 test('is expired if the expiration is in the past', () { | 35 test('is expired if the expiration is in the past', () { |
| 36 var expiration = new DateTime.now().subtract(new Duration(hours: 1)); | 36 var expiration = new DateTime.now().subtract(new Duration(hours: 1)); |
| 37 var credentials = new oauth2.Credentials( | 37 var credentials = new oauth2.Credentials( |
| 38 'access token', null, null, null, expiration); | 38 'access token', null, null, null, expiration); |
| 39 expect(credentials.isExpired, isTrue); | 39 expect(credentials.isExpired, isTrue); |
| 40 }); | 40 }); |
| 41 | 41 |
| 42 test("can't refresh without a refresh token", () { | 42 test("can't refresh without a refresh token", () { |
| 43 var credentials = new oauth2.Credentials( | 43 var credentials = new oauth2.Credentials( |
| 44 'access token', null, tokenEndpoint); | 44 'access token', null, tokenEndpoint); |
| 45 expect(credentials.canRefresh, false); | 45 expect(credentials.canRefresh, false); |
| 46 credentials.refresh('identifier', 'secret', httpClient: httpClient) | 46 |
| 47 .catchError(expectAsync1((error) { | 47 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient), |
| 48 expect(error is StateError, isTrue); | 48 throwsStateError); |
| 49 })); | |
| 50 }); | 49 }); |
| 51 | 50 |
| 52 test("can't refresh without a token endpoint", () { | 51 test("can't refresh without a token endpoint", () { |
| 53 var credentials = new oauth2.Credentials('access token', 'refresh token'); | 52 var credentials = new oauth2.Credentials('access token', 'refresh token'); |
| 54 expect(credentials.canRefresh, false); | 53 expect(credentials.canRefresh, false); |
| 55 credentials.refresh('identifier', 'secret', httpClient: httpClient) | 54 |
| 56 .catchError(expectAsync1((error) { | 55 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient), |
| 57 expect(error is StateError, isTrue); | 56 throwsStateError); |
| 58 })); | |
| 59 }); | 57 }); |
| 60 | 58 |
| 61 test("can refresh with a refresh token and a token endpoint", () { | 59 test("can refresh with a refresh token and a token endpoint", () { |
| 62 var credentials = new oauth2.Credentials( | 60 var credentials = new oauth2.Credentials( |
| 63 'access token', 'refresh token', tokenEndpoint, ['scope1', 'scope2']); | 61 'access token', 'refresh token', tokenEndpoint, ['scope1', 'scope2']); |
| 64 expect(credentials.canRefresh, true); | 62 expect(credentials.canRefresh, true); |
| 65 | 63 |
| 66 httpClient.expectRequest((request) { | 64 httpClient.expectRequest((request) { |
| 67 expect(request.method, equals('POST')); | 65 expect(request.method, equals('POST')); |
| 68 expect(request.url.toString(), equals(tokenEndpoint.toString())); | 66 expect(request.url.toString(), equals(tokenEndpoint.toString())); |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 169 expect(() => fromMap({"accessToken": "foo", "scopes": 12}), | 167 expect(() => fromMap({"accessToken": "foo", "scopes": 12}), |
| 170 throwsFormatException); | 168 throwsFormatException); |
| 171 }); | 169 }); |
| 172 | 170 |
| 173 test("should throw a FormatException if expiration is not an int", () { | 171 test("should throw a FormatException if expiration is not an int", () { |
| 174 expect(() => fromMap({"accessToken": "foo", "expiration": "12"}), | 172 expect(() => fromMap({"accessToken": "foo", "expiration": "12"}), |
| 175 throwsFormatException); | 173 throwsFormatException); |
| 176 }); | 174 }); |
| 177 }); | 175 }); |
| 178 } | 176 } |
| OLD | NEW |