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

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: Misc fixes 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 createHttpClient() {
21 httpClient = new ExpectClient();
22 }
23
24 void main() {
25 setUp(createHttpClient);
Bob Nystrom 2012/11/16 19:53:30 Why not just inline the function here?
nweiz 2012/11/17 01:06:27 Done.
26
27 test('is not expired if no expiration exists', () {
28 var credentials = new oauth2.Credentials('access token');
29 expect(credentials.isExpired, isFalse);
30 });
31
32 test('is not expired if the expiration is in the future', () {
33 var expiration = new Date.now().add(new Duration(hours: 1));
34 var credentials = new oauth2.Credentials(
35 'access token', null, null, null, expiration);
36 expect(credentials.isExpired, isFalse);
37 });
38
39 test('is expired if the expiration is in the past', () {
40 var expiration = new Date.now().subtract(new Duration(hours: 1));
41 var credentials = new oauth2.Credentials(
42 'access token', null, null, null, expiration);
43 expect(credentials.isExpired, isTrue);
44 });
45
46 test("can't refresh without a refresh token", () {
47 var credentials = new oauth2.Credentials(
48 'access token', null, tokenEndpoint);
49 expect(credentials.canRefresh, false);
50 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient),
51 throwsStateError);
52 });
53
54 test("can't refresh without a token endpoint", () {
55 var credentials = new oauth2.Credentials('access token', 'refresh token');
56 expect(credentials.canRefresh, false);
57 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient),
58 throwsStateError);
59 });
60
61 test("can refresh with a refresh token and a token endpoint", () {
62 var credentials = new oauth2.Credentials(
63 'access token', 'refresh token', tokenEndpoint, ['scope1', 'scope2']);
64 expect(credentials.canRefresh, true);
65
66 httpClient.expectRequest((request) {
67 expect(request.method, equals('POST'));
68 expect(request.url.toString(), equals(tokenEndpoint.toString()));
69 expect(request.bodyFields, equals({
70 "grant_type": "refresh_token",
71 "refresh_token": "refresh token",
72 "scope": "scope1 scope2",
73 "client_id": "identifier",
74 "client_secret": "secret"
75 }));
76
77 return new Future.immediate(new http.Response(JSON.stringify({
78 'access_token': 'new access token',
79 'token_type': 'bearer'
80 }), 200, headers: {'content-type': 'application/json'}));
81 });
82
83
84 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient)
85 .transform((credentials) {
86 expect(credentials.accessToken, equals('new access token'));
87 }), completes);
88 });
89
90 test("fromJson(toJson) should load the same credentials", () {
91 var expiration = new Date.now().subtract(new Duration(hours: 1));
92 var credentials = new oauth2.Credentials(
93 'access token', 'refresh token', tokenEndpoint, ['scope1', 'scope2'],
94 expiration);
95 var reloaded = new oauth2.Credentials.fromJson(credentials.toJson());
96
97 expect(reloaded.accessToken, equals(credentials.accessToken));
98 expect(reloaded.refreshToken, equals(credentials.refreshToken));
99 expect(reloaded.tokenEndpoint.toString(),
100 equals(credentials.tokenEndpoint.toString()));
101 expect(reloaded.scopes, equals(credentials.scopes));
102 expect(reloaded.expiration, equals(credentials.expiration));
103 });
104 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698