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

Side by Side Diff: pkg/oauth2/test/handle_access_token_response_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
« no previous file with comments | « pkg/oauth2/test/credentials_test.dart ('k') | pkg/oauth2/test/utils.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 handle_access_token_response_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 '../lib/src/handle_access_token_response.dart';
15 import 'utils.dart';
16
17 final Uri tokenEndpoint = new Uri.fromString("https://example.com/token");
18
19 final Date startTime = new Date.now();
20
21 Credentials handle(http.Response response) =>
22 handleAccessTokenResponse(response, tokenEndpoint, startTime, ["scope"]);
23
24 void main() {
25 group('an error response', () {
26 oauth2.Credentials handleError(
27 {String body: '{"error": "invalid_request"}',
28 int statusCode: 400,
29 Map<String, String> headers:
30 const {"content-type": "application/json"}}) =>
31 handle(new http.Response(body, statusCode, headers: headers));
32
33 test('causes an AuthorizationException', () {
34 expect(() => handleError(), throwsAuthorizationException);
35 });
36
37 test('with a 401 code causes an AuthorizationException', () {
38 expect(() => handleError(statusCode: 401), throwsAuthorizationException);
39 });
40
41 test('with an unexpected code causes a FormatException', () {
42 expect(() => handleError(statusCode: 500),
43 throwsFormatException);
44 });
45
46 test('with no content-type causes a FormatException', () {
47 expect(() => handleError(headers: {}), throwsFormatException);
48 });
49
50 test('with a non-JSON content-type causes a FormatException', () {
51 expect(() => handleError(headers: {
52 'content-type': 'text/plain'
53 }), throwsFormatException);
54 });
55
56 test('with a JSON content-type and charset causes an '
57 'AuthorizationException', () {
58 expect(() => handleError(headers: {
59 'content-type': 'application/json; charset=UTF-8'
60 }), throwsAuthorizationException);
61 });
62
63 test('with invalid JSON causes a FormatException', () {
64 expect(() => handleError(body: 'not json'),
65 throwsFormatException);
66 });
67
68 test('with a non-string error causes a FormatException', () {
69 expect(() => handleError(body: '{"error": 12}'),
70 throwsFormatException);
71 });
72
73 test('with a non-string error_description causes a FormatException', () {
74 expect(() => handleError(body: JSON.stringify({
75 "error": "invalid_request",
76 "error_description": 12
77 })), throwsFormatException);
78 });
79
80 test('with a non-string error_uri causes a FormatException', () {
81 expect(() => handleError(body: JSON.stringify({
82 "error": "invalid_request",
83 "error_uri": 12
84 })), throwsFormatException);
85 });
86
87 test('with a string error_description causes a AuthorizationException', () {
88 expect(() => handleError(body: JSON.stringify({
89 "error": "invalid_request",
90 "error_description": "description"
91 })), throwsAuthorizationException);
92 });
93
94 test('with a string error_uri causes a AuthorizationException', () {
95 expect(() => handleError(body: JSON.stringify({
96 "error": "invalid_request",
97 "error_uri": "http://example.com/error"
98 })), throwsAuthorizationException);
99 });
100 });
101
102 group('a success response', () {
103 oauth2.Credentials handleSuccess(
104 {String contentType: "application/json",
105 String accessToken: 'access token',
106 String tokenType: 'bearer',
107 String expiresIn,
108 String refreshToken,
109 String scope}) {
110 return handle(new http.Response(JSON.stringify({
111 'access_token': accessToken,
112 'token_type': tokenType,
113 'expires_in': expiresIn,
114 'refresh_token': refreshToken,
115 'scope': scope
116 }), 200, headers: {'content-type': contentType}));
117 }
118
119 test('returns the correct credentials', () {
120 var credentials = handleSuccess();
121 expect(credentials.accessToken, equals('access token'));
122 expect(credentials.tokenEndpoint.toString(),
123 equals(tokenEndpoint.toString()));
124 });
125
126 test('with no content-type causes a FormatException', () {
127 expect(() => handleSuccess(contentType: null), throwsFormatException);
128 });
129
130 test('with a non-JSON content-type causes a FormatException', () {
131 expect(() => handleSuccess(contentType: 'text/plain'),
132 throwsFormatException);
133 });
134
135 test('with a JSON content-type and charset returns the correct '
136 'credentials', () {
137 var credentials = handleSuccess(
138 contentType: 'application/json; charset=UTF-8');
139 expect(credentials.accessToken, equals('access token'));
140 });
141
142 test('with a null access token throws a FormatException', () {
143 expect(() => handleSuccess(accessToken: null), throwsFormatException);
144 });
145
146 test('with a non-string access token throws a FormatException', () {
147 expect(() => handleSuccess(accessToken: 12), throwsFormatException);
148 });
149
150 test('with a null token type throws a FormatException', () {
151 expect(() => handleSuccess(tokenType: null), throwsFormatException);
152 });
153
154 test('with a non-string token type throws a FormatException', () {
155 expect(() => handleSuccess(tokenType: 12), throwsFormatException);
156 });
157
158 test('with a non-"bearer" token type throws a FormatException', () {
159 expect(() => handleSuccess(tokenType: "mac"), throwsFormatException);
160 });
161
162 test('with a non-int expires-in throws a FormatException', () {
163 expect(() => handleSuccess(expiresIn: "whenever"), throwsFormatException);
164 });
165
166 test('with expires-in sets the expiration to ten seconds earlier than the '
167 'server says', () {
168 var credentials = handleSuccess(expiresIn: 100);
169 expect(credentials.expiration.millisecondsSinceEpoch,
170 startTime.millisecondsSinceEpoch + 90 * 1000);
171 });
172
173 test('with a non-string refresh token throws a FormatException', () {
174 expect(() => handleSuccess(refreshToken: 12), throwsFormatException);
175 });
176
177 test('with a refresh token sets the refresh token', () {
178 var credentials = handleSuccess(refreshToken: "refresh me");
179 expect(credentials.refreshToken, equals("refresh me"));
180 });
181
182 test('with a non-string scope throws a FormatException', () {
183 expect(() => handleSuccess(scope: 12), throwsFormatException);
184 });
185
186 test('with a scope sets the scopes', () {
187 var credentials = handleSuccess(scope: "scope1 scope2");
188 expect(credentials.scopes, equals(["scope1", "scope2"]));
189 });
190 });
191 }
OLDNEW
« no previous file with comments | « pkg/oauth2/test/credentials_test.dart ('k') | pkg/oauth2/test/utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698