| 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; | |
| 6 | |
| 7 import 'dart:async'; | 5 import 'dart:async'; |
| 8 import 'dart:convert'; | 6 import 'dart:convert'; |
| 9 | 7 |
| 10 import 'package:test/test.dart'; | 8 import 'package:test/test.dart'; |
| 11 import 'package:http/http.dart' as http; | 9 import 'package:http/http.dart' as http; |
| 12 import 'package:oauth2/oauth2.dart' as oauth2; | 10 import 'package:oauth2/oauth2.dart' as oauth2; |
| 13 | 11 |
| 14 import 'utils.dart'; | 12 import 'utils.dart'; |
| 15 | 13 |
| 16 final redirectUrl = Uri.parse('http://example.com/redirect'); | 14 final redirectUrl = Uri.parse('http://example.com/redirect'); |
| 17 | 15 |
| 18 ExpectClient client; | 16 void main() { |
| 17 var client; |
| 18 var grant; |
| 19 setUp(() { |
| 20 client = new ExpectClient(); |
| 21 grant = new oauth2.AuthorizationCodeGrant( |
| 22 'identifier', |
| 23 'secret', |
| 24 Uri.parse('https://example.com/authorization'), |
| 25 Uri.parse('https://example.com/token'), |
| 26 httpClient: client); |
| 27 }); |
| 19 | 28 |
| 20 oauth2.AuthorizationCodeGrant grant; | |
| 21 | |
| 22 void createGrant() { | |
| 23 client = new ExpectClient(); | |
| 24 grant = new oauth2.AuthorizationCodeGrant( | |
| 25 'identifier', | |
| 26 'secret', | |
| 27 Uri.parse('https://example.com/authorization'), | |
| 28 Uri.parse('https://example.com/token'), | |
| 29 httpClient: client); | |
| 30 } | |
| 31 | |
| 32 void main() { | |
| 33 group('.getAuthorizationUrl', () { | 29 group('.getAuthorizationUrl', () { |
| 34 setUp(createGrant); | |
| 35 | |
| 36 test('builds the correct URL', () { | 30 test('builds the correct URL', () { |
| 37 expect(grant.getAuthorizationUrl(redirectUrl).toString(), | 31 expect(grant.getAuthorizationUrl(redirectUrl).toString(), |
| 38 equals('https://example.com/authorization' | 32 equals('https://example.com/authorization' |
| 39 '?response_type=code' | 33 '?response_type=code' |
| 40 '&client_id=identifier' | 34 '&client_id=identifier' |
| 41 '&redirect_uri=http%3A%2F%2Fexample.com%2Fredirect')); | 35 '&redirect_uri=http%3A%2F%2Fexample.com%2Fredirect')); |
| 42 }); | 36 }); |
| 43 | 37 |
| 44 test('builds the correct URL with scopes', () { | 38 test('builds the correct URL with scopes', () { |
| 45 var authorizationUrl = grant.getAuthorizationUrl( | 39 var authorizationUrl = grant.getAuthorizationUrl( |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 '&redirect_uri=http%3A%2F%2Fexample.com%2Fredirect')); | 74 '&redirect_uri=http%3A%2F%2Fexample.com%2Fredirect')); |
| 81 }); | 75 }); |
| 82 | 76 |
| 83 test("can't be called twice", () { | 77 test("can't be called twice", () { |
| 84 grant.getAuthorizationUrl(redirectUrl); | 78 grant.getAuthorizationUrl(redirectUrl); |
| 85 expect(() => grant.getAuthorizationUrl(redirectUrl), throwsStateError); | 79 expect(() => grant.getAuthorizationUrl(redirectUrl), throwsStateError); |
| 86 }); | 80 }); |
| 87 }); | 81 }); |
| 88 | 82 |
| 89 group('.handleAuthorizationResponse', () { | 83 group('.handleAuthorizationResponse', () { |
| 90 setUp(createGrant); | |
| 91 | |
| 92 test("can't be called before .getAuthorizationUrl", () { | 84 test("can't be called before .getAuthorizationUrl", () { |
| 93 expect(grant.handleAuthorizationResponse({}), throwsStateError); | 85 expect(grant.handleAuthorizationResponse({}), throwsStateError); |
| 94 }); | 86 }); |
| 95 | 87 |
| 96 test("can't be called twice", () { | 88 test("can't be called twice", () { |
| 97 grant.getAuthorizationUrl(redirectUrl); | 89 grant.getAuthorizationUrl(redirectUrl); |
| 98 expect(grant.handleAuthorizationResponse({'code': 'auth code'}), | 90 expect(grant.handleAuthorizationResponse({'code': 'auth code'}), |
| 99 throwsFormatException); | 91 throwsFormatException); |
| 100 expect(grant.handleAuthorizationResponse({'code': 'auth code'}), | 92 expect(grant.handleAuthorizationResponse({'code': 'auth code'}), |
| 101 throwsStateError); | 93 throwsStateError); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 }), 200, headers: {'content-type': 'application/json'})); | 138 }), 200, headers: {'content-type': 'application/json'})); |
| 147 }); | 139 }); |
| 148 | 140 |
| 149 expect(grant.handleAuthorizationResponse({'code': 'auth code'}) | 141 expect(grant.handleAuthorizationResponse({'code': 'auth code'}) |
| 150 .then((client) => client.credentials.accessToken), | 142 .then((client) => client.credentials.accessToken), |
| 151 completion(equals('access token'))); | 143 completion(equals('access token'))); |
| 152 }); | 144 }); |
| 153 }); | 145 }); |
| 154 | 146 |
| 155 group('.handleAuthorizationCode', () { | 147 group('.handleAuthorizationCode', () { |
| 156 setUp(createGrant); | |
| 157 | |
| 158 test("can't be called before .getAuthorizationUrl", () { | 148 test("can't be called before .getAuthorizationUrl", () { |
| 159 expect(grant.handleAuthorizationCode('auth code'), throwsStateError); | 149 expect(grant.handleAuthorizationCode('auth code'), throwsStateError); |
| 160 }); | 150 }); |
| 161 | 151 |
| 162 test("can't be called twice", () { | 152 test("can't be called twice", () { |
| 163 grant.getAuthorizationUrl(redirectUrl); | 153 grant.getAuthorizationUrl(redirectUrl); |
| 164 expect(grant.handleAuthorizationCode('auth code'), throwsFormatException); | 154 expect(grant.handleAuthorizationCode('auth code'), throwsFormatException); |
| 165 expect(grant.handleAuthorizationCode('auth code'), throwsStateError); | 155 expect(grant.handleAuthorizationCode('auth code'), throwsStateError); |
| 166 }); | 156 }); |
| 167 | 157 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 185 }); | 175 }); |
| 186 | 176 |
| 187 expect(grant.handleAuthorizationCode('auth code'), | 177 expect(grant.handleAuthorizationCode('auth code'), |
| 188 completion(predicate((client) { | 178 completion(predicate((client) { |
| 189 expect(client.credentials.accessToken, equals('access token')); | 179 expect(client.credentials.accessToken, equals('access token')); |
| 190 return true; | 180 return true; |
| 191 }))); | 181 }))); |
| 192 }); | 182 }); |
| 193 }); | 183 }); |
| 194 } | 184 } |
| OLD | NEW |