| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library authorization_code_grant_test; | 5 library authorization_code_grant_test; |
| 6 | 6 |
| 7 import 'dart:async'; |
| 7 import 'dart:io'; | 8 import 'dart:io'; |
| 8 import 'dart:json'; | 9 import 'dart:json' as JSON; |
| 9 import 'dart:uri'; | 10 import 'dart:uri'; |
| 10 | 11 |
| 11 import '../../unittest/lib/unittest.dart'; | 12 import '../../unittest/lib/unittest.dart'; |
| 12 import '../../http/lib/http.dart' as http; | 13 import '../../http/lib/http.dart' as http; |
| 13 import '../../http/lib/testing.dart'; | 14 import '../../http/lib/testing.dart'; |
| 14 import '../lib/oauth2.dart' as oauth2; | 15 import '../lib/oauth2.dart' as oauth2; |
| 15 import 'utils.dart'; | 16 import 'utils.dart'; |
| 16 | 17 |
| 17 final redirectUrl = new Uri.fromString('http://example.com/redirect'); | 18 final redirectUrl = new Uri.fromString('http://example.com/redirect'); |
| 18 | 19 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 expect(authorizationUrl.toString(), | 83 expect(authorizationUrl.toString(), |
| 83 equals('https://example.com/authorization' | 84 equals('https://example.com/authorization' |
| 84 '?query=value' | 85 '?query=value' |
| 85 '&response_type=code' | 86 '&response_type=code' |
| 86 '&client_id=identifier' | 87 '&client_id=identifier' |
| 87 '&redirect_uri=http%3A%2F%2Fexample.com%2Fredirect')); | 88 '&redirect_uri=http%3A%2F%2Fexample.com%2Fredirect')); |
| 88 }); | 89 }); |
| 89 | 90 |
| 90 test("can't be called twice", () { | 91 test("can't be called twice", () { |
| 91 grant.getAuthorizationUrl(redirectUrl); | 92 grant.getAuthorizationUrl(redirectUrl); |
| 92 expectFutureThrows(grant.getAuthorizationUrl(redirectUrl), | 93 expect(() => grant.getAuthorizationUrl(redirectUrl), throwsStateError); |
| 93 (e) => e is StateError); | |
| 94 }); | 94 }); |
| 95 }); | 95 }); |
| 96 | 96 |
| 97 group('.handleAuthorizationResponse', () { | 97 group('.handleAuthorizationResponse', () { |
| 98 setUp(createGrant); | 98 setUp(createGrant); |
| 99 | 99 |
| 100 test("can't be called before .getAuthorizationUrl", () { | 100 test("can't be called before .getAuthorizationUrl", () { |
| 101 expectFutureThrows(grant.handleAuthorizationResponse({}), | 101 expectFutureThrows(grant.handleAuthorizationResponse({}), |
| 102 (e) => e is StateError); | 102 (e) => e is StateError); |
| 103 }); | 103 }); |
| 104 | 104 |
| 105 test("can't be called twice", () { | 105 test("can't be called twice", () { |
| 106 grant.getAuthorizationUrl(redirectUrl); | 106 grant.getAuthorizationUrl(redirectUrl); |
| 107 grant.handleAuthorizationResponse({'code': 'auth code'}); | 107 expectFutureThrows( |
| 108 grant.handleAuthorizationResponse({'code': 'auth code'}), |
| 109 (e) => e is FormatException); |
| 108 expectFutureThrows( | 110 expectFutureThrows( |
| 109 grant.handleAuthorizationResponse({'code': 'auth code'}), | 111 grant.handleAuthorizationResponse({'code': 'auth code'}), |
| 110 (e) => e is StateError); | 112 (e) => e is StateError); |
| 111 }); | 113 }); |
| 112 | 114 |
| 113 test('must have a state parameter if the authorization URL did', () { | 115 test('must have a state parameter if the authorization URL did', () { |
| 114 grant.getAuthorizationUrl(redirectUrl, state: 'state'); | 116 grant.getAuthorizationUrl(redirectUrl, state: 'state'); |
| 115 expectFutureThrows( | 117 expectFutureThrows( |
| 116 grant.handleAuthorizationResponse({'code': 'auth code'}), | 118 grant.handleAuthorizationResponse({'code': 'auth code'}), |
| 117 (e) => e is FormatException); | 119 (e) => e is FormatException); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 128 test('must have a code parameter', () { | 130 test('must have a code parameter', () { |
| 129 grant.getAuthorizationUrl(redirectUrl); | 131 grant.getAuthorizationUrl(redirectUrl); |
| 130 expectFutureThrows(grant.handleAuthorizationResponse({}), | 132 expectFutureThrows(grant.handleAuthorizationResponse({}), |
| 131 (e) => e is FormatException); | 133 (e) => e is FormatException); |
| 132 }); | 134 }); |
| 133 | 135 |
| 134 test('with an error parameter throws an AuthorizationException', () { | 136 test('with an error parameter throws an AuthorizationException', () { |
| 135 grant.getAuthorizationUrl(redirectUrl); | 137 grant.getAuthorizationUrl(redirectUrl); |
| 136 expectFutureThrows( | 138 expectFutureThrows( |
| 137 grant.handleAuthorizationResponse({'error': 'invalid_request'}), | 139 grant.handleAuthorizationResponse({'error': 'invalid_request'}), |
| 138 (e) => e is AuthorizationException); | 140 (e) => e is oauth2.AuthorizationException); |
| 139 }); | 141 }); |
| 140 | 142 |
| 141 test('sends an authorization code request', () { | 143 test('sends an authorization code request', () { |
| 142 grant.getAuthorizationUrl(redirectUrl); | 144 grant.getAuthorizationUrl(redirectUrl); |
| 143 client.expectRequest((request) { | 145 client.expectRequest((request) { |
| 144 expect(request.method, equals('POST')); | 146 expect(request.method, equals('POST')); |
| 145 expect(request.url.toString(), equals(grant.tokenEndpoint.toString())); | 147 expect(request.url.toString(), equals(grant.tokenEndpoint.toString())); |
| 146 expect(request.bodyFields, equals({ | 148 expect(request.bodyFields, equals({ |
| 147 'grant_type': 'authorization_code', | 149 'grant_type': 'authorization_code', |
| 148 'code': 'auth code', | 150 'code': 'auth code', |
| 149 'redirect_uri': redirectUrl.toString(), | 151 'redirect_uri': redirectUrl.toString(), |
| 150 'client_id': 'identifier', | 152 'client_id': 'identifier', |
| 151 'client_secret': 'secret' | 153 'client_secret': 'secret' |
| 152 })); | 154 })); |
| 153 | 155 |
| 154 return new Future.immediate(new http.Response(JSON.stringify({ | 156 return new Future.immediate(new http.Response(JSON.stringify({ |
| 155 'access_token': 'access token', | 157 'access_token': 'access token', |
| 156 'token_type': 'bearer', | 158 'token_type': 'bearer', |
| 157 }), 200, headers: {'content-type': 'application/json'})); | 159 }), 200, headers: {'content-type': 'application/json'})); |
| 158 }); | 160 }); |
| 159 | 161 |
| 160 expect(grant.handleAuthorizationResponse({'code': 'auth code'}), | 162 grant.handleAuthorizationResponse({'code': 'auth code'}) |
| 161 completion(predicate((client) { | 163 .then(expectAsync1((client) { |
| 162 expect(client.credentials.accessToken, equals('access token')); | 164 expect(client.credentials.accessToken, equals('access token')); |
| 163 return true; | 165 })); |
| 164 }))); | |
| 165 }); | 166 }); |
| 166 }); | 167 }); |
| 167 | 168 |
| 168 group('.handleAuthorizationCode', () { | 169 group('.handleAuthorizationCode', () { |
| 169 setUp(createGrant); | 170 setUp(createGrant); |
| 170 | 171 |
| 171 test("can't be called before .getAuthorizationUrl", () { | 172 test("can't be called before .getAuthorizationUrl", () { |
| 172 expect(grant.handleAuthorizationCode('auth code'), throwsStateError); | 173 expectFutureThrows( |
| 174 grant.handleAuthorizationCode('auth code'), |
| 175 (e) => e is StateError); |
| 173 }); | 176 }); |
| 174 | 177 |
| 175 test("can't be called twice", () { | 178 test("can't be called twice", () { |
| 176 grant.getAuthorizationUrl(redirectUrl); | 179 grant.getAuthorizationUrl(redirectUrl); |
| 177 grant.handleAuthorizationCode('auth code'); | 180 expectFutureThrows( |
| 181 grant.handleAuthorizationCode('auth code'), |
| 182 (e) => e is FormatException); |
| 178 expectFutureThrows(grant.handleAuthorizationCode('auth code'), | 183 expectFutureThrows(grant.handleAuthorizationCode('auth code'), |
| 179 (e) => e is StateError); | 184 (e) => e is StateError); |
| 180 }); | 185 }); |
| 181 | 186 |
| 182 test('sends an authorization code request', () { | 187 test('sends an authorization code request', () { |
| 183 grant.getAuthorizationUrl(redirectUrl); | 188 grant.getAuthorizationUrl(redirectUrl); |
| 184 client.expectRequest((request) { | 189 client.expectRequest((request) { |
| 185 expect(request.method, equals('POST')); | 190 expect(request.method, equals('POST')); |
| 186 expect(request.url.toString(), equals(grant.tokenEndpoint.toString())); | 191 expect(request.url.toString(), equals(grant.tokenEndpoint.toString())); |
| 187 expect(request.bodyFields, equals({ | 192 expect(request.bodyFields, equals({ |
| (...skipping 11 matching lines...) Expand all Loading... |
| 199 }); | 204 }); |
| 200 | 205 |
| 201 expect(grant.handleAuthorizationCode('auth code'), | 206 expect(grant.handleAuthorizationCode('auth code'), |
| 202 completion(predicate((client) { | 207 completion(predicate((client) { |
| 203 expect(client.credentials.accessToken, equals('access token')); | 208 expect(client.credentials.accessToken, equals('access token')); |
| 204 return true; | 209 return true; |
| 205 }))); | 210 }))); |
| 206 }); | 211 }); |
| 207 }); | 212 }); |
| 208 } | 213 } |
| OLD | NEW |