Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(969)

Side by Side Diff: pkg/oauth2/test/credentials_test.dart

Issue 11420025: Add a package for authenticating via OAuth2. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Refresh token fixes and code review changes Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 credentials_test;
6
7 import 'dart:io';
8 import 'dart:json';
9 import 'dart:uri';
10
11 import '../../unittest/lib/unittest.dart';
12 import '../../http/lib/http.dart' as http;
13 import '../lib/oauth2.dart' as oauth2;
14 import 'utils.dart';
15
16 final Uri tokenEndpoint = new Uri.fromString('http://example.com/token');
17
18 ExpectClient httpClient;
19
20 void main() {
21 setUp(() => httpClient = new ExpectClient());
22
23 test('is not expired if no expiration exists', () {
24 var credentials = new oauth2.Credentials('access token');
25 expect(credentials.isExpired, isFalse);
26 });
27
28 test('is not expired if the expiration is in the future', () {
29 var expiration = new Date.now().add(new Duration(hours: 1));
30 var credentials = new oauth2.Credentials(
31 'access token', null, null, null, expiration);
32 expect(credentials.isExpired, isFalse);
33 });
34
35 test('is expired if the expiration is in the past', () {
36 var expiration = new Date.now().subtract(new Duration(hours: 1));
37 var credentials = new oauth2.Credentials(
38 'access token', null, null, null, expiration);
39 expect(credentials.isExpired, isTrue);
40 });
41
42 test("can't refresh without a refresh token", () {
43 var credentials = new oauth2.Credentials(
44 'access token', null, tokenEndpoint);
45 expect(credentials.canRefresh, false);
46 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient),
47 throwsStateError);
48 });
49
50 test("can't refresh without a token endpoint", () {
51 var credentials = new oauth2.Credentials('access token', 'refresh token');
52 expect(credentials.canRefresh, false);
53 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient),
54 throwsStateError);
55 });
56
57 test("can refresh with a refresh token and a token endpoint", () {
58 var credentials = new oauth2.Credentials(
59 'access token', 'refresh token', tokenEndpoint, ['scope1', 'scope2']);
60 expect(credentials.canRefresh, true);
61
62 httpClient.expectRequest((request) {
63 expect(request.method, equals('POST'));
64 expect(request.url.toString(), equals(tokenEndpoint.toString()));
65 expect(request.bodyFields, equals({
66 "grant_type": "refresh_token",
67 "refresh_token": "refresh token",
68 "scope": "scope1 scope2",
69 "client_id": "identifier",
70 "client_secret": "secret"
71 }));
72
73 return new Future.immediate(new http.Response(JSON.stringify({
74 'access_token': 'new access token',
75 'token_type': 'bearer',
76 'refresh_token': 'new refresh token'
77 }), 200, headers: {'content-type': 'application/json'}));
78 });
79
80
81 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient)
82 .transform((credentials) {
83 expect(credentials.accessToken, equals('new access token'));
84 expect(credentials.refreshToken, equals('new refresh token'));
85 }), completes);
86 });
87
88 test("uses the old refresh token if a new one isn't provided", () {
89 var credentials = new oauth2.Credentials(
90 'access token', 'refresh token', tokenEndpoint);
91 expect(credentials.canRefresh, true);
92
93 httpClient.expectRequest((request) {
94 expect(request.method, equals('POST'));
95 expect(request.url.toString(), equals(tokenEndpoint.toString()));
96 expect(request.bodyFields, equals({
97 "grant_type": "refresh_token",
98 "refresh_token": "refresh token",
99 "client_id": "identifier",
100 "client_secret": "secret"
101 }));
102
103 return new Future.immediate(new http.Response(JSON.stringify({
104 'access_token': 'new access token',
105 'token_type': 'bearer'
106 }), 200, headers: {'content-type': 'application/json'}));
107 });
108
109
110 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient)
111 .transform((credentials) {
112 expect(credentials.accessToken, equals('new access token'));
113 expect(credentials.refreshToken, equals('refresh token'));
114 }), completes);
115 });
116
117 group("fromJson", () {
118 oauth2.Credentials fromMap(Map map) =>
119 new oauth2.Credentials.fromJson(JSON.stringify(map));
120
121 test("should load the same credentials from toJson", () {
122 var expiration = new Date.now().subtract(new Duration(hours: 1));
123 var credentials = new oauth2.Credentials(
124 'access token', 'refresh token', tokenEndpoint, ['scope1', 'scope2'],
125 expiration);
126 var reloaded = new oauth2.Credentials.fromJson(credentials.toJson());
127
128 expect(reloaded.accessToken, equals(credentials.accessToken));
129 expect(reloaded.refreshToken, equals(credentials.refreshToken));
130 expect(reloaded.tokenEndpoint.toString(),
131 equals(credentials.tokenEndpoint.toString()));
132 expect(reloaded.scopes, equals(credentials.scopes));
133 expect(reloaded.expiration, equals(credentials.expiration));
134 });
135
136 test("should throw a FormatException for invalid JSON", () {
137 expect(() => new oauth2.Credentials.fromJson("foo bar"),
138 throwsFormatException);
139 });
140
141 test("should throw a FormatException for JSON that's not a map", () {
142 expect(() => new oauth2.Credentials.fromJson("null"),
143 throwsFormatException);
144 });
145
146 test("should throw a FormatException if there is no accessToken", () {
147 expect(() => fromMap({}), throwsFormatException);
148 });
149
150 test("should throw a FormatException if accessToken is not a string", () {
151 expect(() => fromMap({"accessToken": 12}), throwsFormatException);
152 });
153
154 test("should throw a FormatException if refreshToken is not a string", () {
155 expect(() => fromMap({"accessToken": "foo", "refreshToken": 12}),
156 throwsFormatException);
157 });
158
159 test("should throw a FormatException if tokenEndpoint is not a string", () {
160 expect(() => fromMap({"accessToken": "foo", "tokenEndpoint": 12}),
161 throwsFormatException);
162 });
163
164 test("should throw a FormatException if scopes is not a list", () {
165 expect(() => fromMap({"accessToken": "foo", "scopes": 12}),
166 throwsFormatException);
167 });
168
169 test("should throw a FormatException if expiration is not an int", () {
170 expect(() => fromMap({"accessToken": "foo", "expiration": "12"}),
171 throwsFormatException);
172 });
173 });
174 }
OLDNEW
« no previous file with comments | « pkg/oauth2/test/client_test.dart ('k') | pkg/oauth2/test/handle_access_token_response_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698