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:io'; | 8 import 'dart:io'; |
8 import 'dart:json'; | 9 import 'dart:json' as JSON; |
9 import 'dart:uri'; | 10 import 'dart:uri'; |
10 | 11 |
11 import '../../unittest/lib/unittest.dart'; | 12 import '../../unittest/lib/unittest.dart'; |
12 import '../../http/lib/http.dart' as http; | 13 import '../../http/lib/http.dart' as http; |
13 import '../lib/oauth2.dart' as oauth2; | 14 import '../lib/oauth2.dart' as oauth2; |
14 import 'utils.dart'; | 15 import 'utils.dart'; |
15 | 16 |
16 final Uri tokenEndpoint = new Uri.fromString('http://example.com/token'); | 17 final Uri tokenEndpoint = new Uri.fromString('http://example.com/token'); |
17 | 18 |
18 ExpectClient httpClient; | 19 ExpectClient httpClient; |
(...skipping 17 matching lines...) Expand all Loading... |
36 var expiration = new Date.now().subtract(new Duration(hours: 1)); | 37 var expiration = new Date.now().subtract(new Duration(hours: 1)); |
37 var credentials = new oauth2.Credentials( | 38 var credentials = new oauth2.Credentials( |
38 'access token', null, null, null, expiration); | 39 'access token', null, null, null, expiration); |
39 expect(credentials.isExpired, isTrue); | 40 expect(credentials.isExpired, isTrue); |
40 }); | 41 }); |
41 | 42 |
42 test("can't refresh without a refresh token", () { | 43 test("can't refresh without a refresh token", () { |
43 var credentials = new oauth2.Credentials( | 44 var credentials = new oauth2.Credentials( |
44 'access token', null, tokenEndpoint); | 45 'access token', null, tokenEndpoint); |
45 expect(credentials.canRefresh, false); | 46 expect(credentials.canRefresh, false); |
46 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient), | 47 credentials.refresh('identifier', 'secret', httpClient: httpClient) |
47 throwsStateError); | 48 .catchError(expectAsync1((e) { |
| 49 expect(e.error is StateError, isTrue); |
| 50 })); |
48 }); | 51 }); |
49 | 52 |
50 test("can't refresh without a token endpoint", () { | 53 test("can't refresh without a token endpoint", () { |
51 var credentials = new oauth2.Credentials('access token', 'refresh token'); | 54 var credentials = new oauth2.Credentials('access token', 'refresh token'); |
52 expect(credentials.canRefresh, false); | 55 expect(credentials.canRefresh, false); |
53 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient), | 56 credentials.refresh('identifier', 'secret', httpClient: httpClient) |
54 throwsStateError); | 57 .catchError(expectAsync1((e) { |
| 58 expect(e.error is StateError, isTrue); |
| 59 })); |
55 }); | 60 }); |
56 | 61 |
57 test("can refresh with a refresh token and a token endpoint", () { | 62 test("can refresh with a refresh token and a token endpoint", () { |
58 var credentials = new oauth2.Credentials( | 63 var credentials = new oauth2.Credentials( |
59 'access token', 'refresh token', tokenEndpoint, ['scope1', 'scope2']); | 64 'access token', 'refresh token', tokenEndpoint, ['scope1', 'scope2']); |
60 expect(credentials.canRefresh, true); | 65 expect(credentials.canRefresh, true); |
61 | 66 |
62 httpClient.expectRequest((request) { | 67 httpClient.expectRequest((request) { |
63 expect(request.method, equals('POST')); | 68 expect(request.method, equals('POST')); |
64 expect(request.url.toString(), equals(tokenEndpoint.toString())); | 69 expect(request.url.toString(), equals(tokenEndpoint.toString())); |
65 expect(request.bodyFields, equals({ | 70 expect(request.bodyFields, equals({ |
66 "grant_type": "refresh_token", | 71 "grant_type": "refresh_token", |
67 "refresh_token": "refresh token", | 72 "refresh_token": "refresh token", |
68 "scope": "scope1 scope2", | 73 "scope": "scope1 scope2", |
69 "client_id": "identifier", | 74 "client_id": "identifier", |
70 "client_secret": "secret" | 75 "client_secret": "secret" |
71 })); | 76 })); |
72 | 77 |
73 return new Future.immediate(new http.Response(JSON.stringify({ | 78 return new Future.immediate(new http.Response(JSON.stringify({ |
74 'access_token': 'new access token', | 79 'access_token': 'new access token', |
75 'token_type': 'bearer', | 80 'token_type': 'bearer', |
76 'refresh_token': 'new refresh token' | 81 'refresh_token': 'new refresh token' |
77 }), 200, headers: {'content-type': 'application/json'})); | 82 }), 200, headers: {'content-type': 'application/json'})); |
78 }); | 83 }); |
79 | 84 |
80 | 85 |
81 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient) | 86 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient) |
82 .transform((credentials) { | 87 .then((credentials) { |
83 expect(credentials.accessToken, equals('new access token')); | 88 expect(credentials.accessToken, equals('new access token')); |
84 expect(credentials.refreshToken, equals('new refresh token')); | 89 expect(credentials.refreshToken, equals('new refresh token')); |
85 }), completes); | 90 }), completes); |
86 }); | 91 }); |
87 | 92 |
88 test("uses the old refresh token if a new one isn't provided", () { | 93 test("uses the old refresh token if a new one isn't provided", () { |
89 var credentials = new oauth2.Credentials( | 94 var credentials = new oauth2.Credentials( |
90 'access token', 'refresh token', tokenEndpoint); | 95 'access token', 'refresh token', tokenEndpoint); |
91 expect(credentials.canRefresh, true); | 96 expect(credentials.canRefresh, true); |
92 | 97 |
93 httpClient.expectRequest((request) { | 98 httpClient.expectRequest((request) { |
94 expect(request.method, equals('POST')); | 99 expect(request.method, equals('POST')); |
95 expect(request.url.toString(), equals(tokenEndpoint.toString())); | 100 expect(request.url.toString(), equals(tokenEndpoint.toString())); |
96 expect(request.bodyFields, equals({ | 101 expect(request.bodyFields, equals({ |
97 "grant_type": "refresh_token", | 102 "grant_type": "refresh_token", |
98 "refresh_token": "refresh token", | 103 "refresh_token": "refresh token", |
99 "client_id": "identifier", | 104 "client_id": "identifier", |
100 "client_secret": "secret" | 105 "client_secret": "secret" |
101 })); | 106 })); |
102 | 107 |
103 return new Future.immediate(new http.Response(JSON.stringify({ | 108 return new Future.immediate(new http.Response(JSON.stringify({ |
104 'access_token': 'new access token', | 109 'access_token': 'new access token', |
105 'token_type': 'bearer' | 110 'token_type': 'bearer' |
106 }), 200, headers: {'content-type': 'application/json'})); | 111 }), 200, headers: {'content-type': 'application/json'})); |
107 }); | 112 }); |
108 | 113 |
109 | 114 |
110 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient) | 115 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient) |
111 .transform((credentials) { | 116 .then((credentials) { |
112 expect(credentials.accessToken, equals('new access token')); | 117 expect(credentials.accessToken, equals('new access token')); |
113 expect(credentials.refreshToken, equals('refresh token')); | 118 expect(credentials.refreshToken, equals('refresh token')); |
114 }), completes); | 119 }), completes); |
115 }); | 120 }); |
116 | 121 |
117 group("fromJson", () { | 122 group("fromJson", () { |
118 oauth2.Credentials fromMap(Map map) => | 123 oauth2.Credentials fromMap(Map map) => |
119 new oauth2.Credentials.fromJson(JSON.stringify(map)); | 124 new oauth2.Credentials.fromJson(JSON.stringify(map)); |
120 | 125 |
121 test("should load the same credentials from toJson", () { | 126 test("should load the same credentials from toJson", () { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 expect(() => fromMap({"accessToken": "foo", "scopes": 12}), | 170 expect(() => fromMap({"accessToken": "foo", "scopes": 12}), |
166 throwsFormatException); | 171 throwsFormatException); |
167 }); | 172 }); |
168 | 173 |
169 test("should throw a FormatException if expiration is not an int", () { | 174 test("should throw a FormatException if expiration is not an int", () { |
170 expect(() => fromMap({"accessToken": "foo", "expiration": "12"}), | 175 expect(() => fromMap({"accessToken": "foo", "expiration": "12"}), |
171 throwsFormatException); | 176 throwsFormatException); |
172 }); | 177 }); |
173 }); | 178 }); |
174 } | 179 } |
OLD | NEW |