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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: pkg/oauth2/test/credentials_test.dart
diff --git a/pkg/oauth2/test/credentials_test.dart b/pkg/oauth2/test/credentials_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..4aca43bdbfc53a906105f4c216b7cf4137d5807e
--- /dev/null
+++ b/pkg/oauth2/test/credentials_test.dart
@@ -0,0 +1,104 @@
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library credentials_test;
+
+import 'dart:io';
+import 'dart:json';
+import 'dart:uri';
+
+import '../../unittest/lib/unittest.dart';
+import '../../http/lib/http.dart' as http;
+import '../lib/oauth2.dart' as oauth2;
+import 'utils.dart';
+
+final Uri tokenEndpoint = new Uri.fromString('http://example.com/token');
+
+ExpectClient httpClient;
+
+void createHttpClient() {
+ httpClient = new ExpectClient();
+}
+
+void main() {
+ 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.
+
+ test('is not expired if no expiration exists', () {
+ var credentials = new oauth2.Credentials('access token');
+ expect(credentials.isExpired, isFalse);
+ });
+
+ test('is not expired if the expiration is in the future', () {
+ var expiration = new Date.now().add(new Duration(hours: 1));
+ var credentials = new oauth2.Credentials(
+ 'access token', null, null, null, expiration);
+ expect(credentials.isExpired, isFalse);
+ });
+
+ test('is expired if the expiration is in the past', () {
+ var expiration = new Date.now().subtract(new Duration(hours: 1));
+ var credentials = new oauth2.Credentials(
+ 'access token', null, null, null, expiration);
+ expect(credentials.isExpired, isTrue);
+ });
+
+ test("can't refresh without a refresh token", () {
+ var credentials = new oauth2.Credentials(
+ 'access token', null, tokenEndpoint);
+ expect(credentials.canRefresh, false);
+ expect(credentials.refresh('identifier', 'secret', httpClient: httpClient),
+ throwsStateError);
+ });
+
+ test("can't refresh without a token endpoint", () {
+ var credentials = new oauth2.Credentials('access token', 'refresh token');
+ expect(credentials.canRefresh, false);
+ expect(credentials.refresh('identifier', 'secret', httpClient: httpClient),
+ throwsStateError);
+ });
+
+ test("can refresh with a refresh token and a token endpoint", () {
+ var credentials = new oauth2.Credentials(
+ 'access token', 'refresh token', tokenEndpoint, ['scope1', 'scope2']);
+ expect(credentials.canRefresh, true);
+
+ httpClient.expectRequest((request) {
+ expect(request.method, equals('POST'));
+ expect(request.url.toString(), equals(tokenEndpoint.toString()));
+ expect(request.bodyFields, equals({
+ "grant_type": "refresh_token",
+ "refresh_token": "refresh token",
+ "scope": "scope1 scope2",
+ "client_id": "identifier",
+ "client_secret": "secret"
+ }));
+
+ return new Future.immediate(new http.Response(JSON.stringify({
+ 'access_token': 'new access token',
+ 'token_type': 'bearer'
+ }), 200, headers: {'content-type': 'application/json'}));
+ });
+
+
+ expect(credentials.refresh('identifier', 'secret', httpClient: httpClient)
+ .transform((credentials) {
+ expect(credentials.accessToken, equals('new access token'));
+ }), completes);
+ });
+
+ test("fromJson(toJson) should load the same credentials", () {
+ var expiration = new Date.now().subtract(new Duration(hours: 1));
+ var credentials = new oauth2.Credentials(
+ 'access token', 'refresh token', tokenEndpoint, ['scope1', 'scope2'],
+ expiration);
+ var reloaded = new oauth2.Credentials.fromJson(credentials.toJson());
+
+ expect(reloaded.accessToken, equals(credentials.accessToken));
+ expect(reloaded.refreshToken, equals(credentials.refreshToken));
+ expect(reloaded.tokenEndpoint.toString(),
+ equals(credentials.tokenEndpoint.toString()));
+ expect(reloaded.scopes, equals(credentials.scopes));
+ expect(reloaded.expiration, equals(credentials.expiration));
+ });
+}

Powered by Google App Engine
This is Rietveld 408576698