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

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

Issue 812253002: Delete a bunch of packages that are now on GitHub. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Un-delete http Created 6 years 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:async';
8 import 'dart:convert';
9
10 import 'package:http/http.dart' as http;
11 import 'package:oauth2/oauth2.dart' as oauth2;
12 import 'package:unittest/unittest.dart';
13
14 import 'utils.dart';
15
16 final Uri tokenEndpoint = Uri.parse('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 DateTime.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 DateTime.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
47 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient),
48 throwsStateError);
49 });
50
51 test("can't refresh without a token endpoint", () {
52 var credentials = new oauth2.Credentials('access token', 'refresh token');
53 expect(credentials.canRefresh, false);
54
55 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient),
56 throwsStateError);
57 });
58
59 test("can refresh with a refresh token and a token endpoint", () {
60 var credentials = new oauth2.Credentials(
61 'access token', 'refresh token', tokenEndpoint, ['scope1', 'scope2']);
62 expect(credentials.canRefresh, true);
63
64 httpClient.expectRequest((request) {
65 expect(request.method, equals('POST'));
66 expect(request.url.toString(), equals(tokenEndpoint.toString()));
67 expect(request.bodyFields, equals({
68 "grant_type": "refresh_token",
69 "refresh_token": "refresh token",
70 "scope": "scope1 scope2",
71 "client_id": "identifier",
72 "client_secret": "secret"
73 }));
74
75 return new Future.value(new http.Response(JSON.encode({
76 'access_token': 'new access token',
77 'token_type': 'bearer',
78 'refresh_token': 'new refresh token'
79 }), 200, headers: {'content-type': 'application/json'}));
80 });
81
82
83 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient)
84 .then((credentials) {
85 expect(credentials.accessToken, equals('new access token'));
86 expect(credentials.refreshToken, equals('new refresh token'));
87 }), completes);
88 });
89
90 test("uses the old refresh token if a new one isn't provided", () {
91 var credentials = new oauth2.Credentials(
92 'access token', 'refresh token', tokenEndpoint);
93 expect(credentials.canRefresh, true);
94
95 httpClient.expectRequest((request) {
96 expect(request.method, equals('POST'));
97 expect(request.url.toString(), equals(tokenEndpoint.toString()));
98 expect(request.bodyFields, equals({
99 "grant_type": "refresh_token",
100 "refresh_token": "refresh token",
101 "client_id": "identifier",
102 "client_secret": "secret"
103 }));
104
105 return new Future.value(new http.Response(JSON.encode({
106 'access_token': 'new access token',
107 'token_type': 'bearer'
108 }), 200, headers: {'content-type': 'application/json'}));
109 });
110
111
112 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient)
113 .then((credentials) {
114 expect(credentials.accessToken, equals('new access token'));
115 expect(credentials.refreshToken, equals('refresh token'));
116 }), completes);
117 });
118
119 group("fromJson", () {
120 oauth2.Credentials fromMap(Map map) =>
121 new oauth2.Credentials.fromJson(JSON.encode(map));
122
123 test("should load the same credentials from toJson", () {
124 var expiration = new DateTime.now().subtract(new Duration(hours: 1));
125 var credentials = new oauth2.Credentials(
126 'access token', 'refresh token', tokenEndpoint, ['scope1', 'scope2'],
127 expiration);
128 var reloaded = new oauth2.Credentials.fromJson(credentials.toJson());
129
130 expect(reloaded.accessToken, equals(credentials.accessToken));
131 expect(reloaded.refreshToken, equals(credentials.refreshToken));
132 expect(reloaded.tokenEndpoint.toString(),
133 equals(credentials.tokenEndpoint.toString()));
134 expect(reloaded.scopes, equals(credentials.scopes));
135 expect(reloaded.expiration, equals(credentials.expiration));
136 });
137
138 test("should throw a FormatException for invalid JSON", () {
139 expect(() => new oauth2.Credentials.fromJson("foo bar"),
140 throwsFormatException);
141 });
142
143 test("should throw a FormatException for JSON that's not a map", () {
144 expect(() => new oauth2.Credentials.fromJson("null"),
145 throwsFormatException);
146 });
147
148 test("should throw a FormatException if there is no accessToken", () {
149 expect(() => fromMap({}), throwsFormatException);
150 });
151
152 test("should throw a FormatException if accessToken is not a string", () {
153 expect(() => fromMap({"accessToken": 12}), throwsFormatException);
154 });
155
156 test("should throw a FormatException if refreshToken is not a string", () {
157 expect(() => fromMap({"accessToken": "foo", "refreshToken": 12}),
158 throwsFormatException);
159 });
160
161 test("should throw a FormatException if tokenEndpoint is not a string", () {
162 expect(() => fromMap({"accessToken": "foo", "tokenEndpoint": 12}),
163 throwsFormatException);
164 });
165
166 test("should throw a FormatException if scopes is not a list", () {
167 expect(() => fromMap({"accessToken": "foo", "scopes": 12}),
168 throwsFormatException);
169 });
170
171 test("should throw a FormatException if expiration is not an int", () {
172 expect(() => fromMap({"accessToken": "foo", "expiration": "12"}),
173 throwsFormatException);
174 });
175 });
176 }
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