Chromium Code Reviews| 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:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 | 9 |
| 10 import 'package:unittest/unittest.dart'; | 10 import 'package:unittest/unittest.dart'; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 void createGrant() { | 22 void createGrant() { |
| 23 client = new ExpectClient(); | 23 client = new ExpectClient(); |
| 24 grant = new oauth2.AuthorizationCodeGrant( | 24 grant = new oauth2.AuthorizationCodeGrant( |
| 25 'identifier', | 25 'identifier', |
| 26 'secret', | 26 'secret', |
| 27 Uri.parse('https://example.com/authorization'), | 27 Uri.parse('https://example.com/authorization'), |
| 28 Uri.parse('https://example.com/token'), | 28 Uri.parse('https://example.com/token'), |
| 29 httpClient: client); | 29 httpClient: client); |
| 30 } | 30 } |
| 31 | 31 |
| 32 void expectFutureThrows(future, predicate) { | |
| 33 future.catchError(expectAsync1((error) { | |
| 34 expect(predicate(error), isTrue); | |
| 35 })); | |
| 36 } | |
| 37 | |
| 38 void main() { | 32 void main() { |
| 39 group('.getAuthorizationUrl', () { | 33 group('.getAuthorizationUrl', () { |
| 40 setUp(createGrant); | 34 setUp(createGrant); |
| 41 | 35 |
| 42 test('builds the correct URL', () { | 36 test('builds the correct URL', () { |
| 43 expect(grant.getAuthorizationUrl(redirectUrl).toString(), | 37 expect(grant.getAuthorizationUrl(redirectUrl).toString(), |
| 44 equals('https://example.com/authorization' | 38 equals('https://example.com/authorization' |
| 45 '?response_type=code' | 39 '?response_type=code' |
| 46 '&client_id=identifier' | 40 '&client_id=identifier' |
| 47 '&redirect_uri=http%3A%2F%2Fexample.com%2Fredirect')); | 41 '&redirect_uri=http%3A%2F%2Fexample.com%2Fredirect')); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 89 test("can't be called twice", () { | 83 test("can't be called twice", () { |
| 90 grant.getAuthorizationUrl(redirectUrl); | 84 grant.getAuthorizationUrl(redirectUrl); |
| 91 expect(() => grant.getAuthorizationUrl(redirectUrl), throwsStateError); | 85 expect(() => grant.getAuthorizationUrl(redirectUrl), throwsStateError); |
| 92 }); | 86 }); |
| 93 }); | 87 }); |
| 94 | 88 |
| 95 group('.handleAuthorizationResponse', () { | 89 group('.handleAuthorizationResponse', () { |
| 96 setUp(createGrant); | 90 setUp(createGrant); |
| 97 | 91 |
| 98 test("can't be called before .getAuthorizationUrl", () { | 92 test("can't be called before .getAuthorizationUrl", () { |
| 99 expectFutureThrows(grant.handleAuthorizationResponse({}), | 93 expect(grant.handleAuthorizationResponse({}), throwsStateError); |
| 100 (e) => e is StateError); | |
| 101 }); | 94 }); |
| 102 | 95 |
| 103 test("can't be called twice", () { | 96 test("can't be called twice", () { |
| 104 grant.getAuthorizationUrl(redirectUrl); | 97 grant.getAuthorizationUrl(redirectUrl); |
| 105 expectFutureThrows( | 98 expect(grant.handleAuthorizationResponse({'code': 'auth code'}), |
| 106 grant.handleAuthorizationResponse({'code': 'auth code'}), | 99 throwsFormatException); |
| 107 (e) => e is FormatException); | 100 expect(grant.handleAuthorizationResponse({'code': 'auth code'}), |
| 108 expectFutureThrows( | 101 throwsStateError); |
| 109 grant.handleAuthorizationResponse({'code': 'auth code'}), | |
| 110 (e) => e is StateError); | |
| 111 }); | 102 }); |
| 112 | 103 |
| 113 test('must have a state parameter if the authorization URL did', () { | 104 test('must have a state parameter if the authorization URL did', () { |
| 114 grant.getAuthorizationUrl(redirectUrl, state: 'state'); | 105 grant.getAuthorizationUrl(redirectUrl, state: 'state'); |
| 115 expectFutureThrows( | 106 expect(grant.handleAuthorizationResponse({'code': 'auth code'}), |
| 116 grant.handleAuthorizationResponse({'code': 'auth code'}), | 107 throwsFormatException); |
| 117 (e) => e is FormatException); | |
| 118 }); | 108 }); |
| 119 | 109 |
| 120 test('must have the same state parameter the authorization URL did', () { | 110 test('must have the same state parameter the authorization URL did', () { |
| 121 grant.getAuthorizationUrl(redirectUrl, state: 'state'); | 111 grant.getAuthorizationUrl(redirectUrl, state: 'state'); |
| 122 expectFutureThrows(grant.handleAuthorizationResponse({ | 112 expect(grant.handleAuthorizationResponse({ |
| 123 'code': 'auth code', | 113 'code': 'auth code', |
| 124 'state': 'other state' | 114 'state': 'other state' |
| 125 }), (e) => e is FormatException); | 115 }), throwsFormatException); |
| 126 }); | 116 }); |
| 127 | 117 |
| 128 test('must have a code parameter', () { | 118 test('must have a code parameter', () { |
| 129 grant.getAuthorizationUrl(redirectUrl); | 119 grant.getAuthorizationUrl(redirectUrl); |
| 130 expectFutureThrows(grant.handleAuthorizationResponse({}), | 120 expect(grant.handleAuthorizationResponse({}), throwsFormatException); |
| 131 (e) => e is FormatException); | |
| 132 }); | 121 }); |
| 133 | 122 |
| 134 test('with an error parameter throws an AuthorizationException', () { | 123 test('with an error parameter throws an AuthorizationException', () { |
| 135 grant.getAuthorizationUrl(redirectUrl); | 124 grant.getAuthorizationUrl(redirectUrl); |
| 136 expectFutureThrows( | 125 expect( |
| 137 grant.handleAuthorizationResponse({'error': 'invalid_request'}), | 126 grant.handleAuthorizationResponse({'error': 'invalid_request'}), |
| 138 (e) => e is oauth2.AuthorizationException); | 127 throwsA((e) => e is oauth2.AuthorizationException)); |
| 139 }); | 128 }); |
| 140 | 129 |
| 141 test('sends an authorization code request', () { | 130 test('sends an authorization code request', () { |
| 142 grant.getAuthorizationUrl(redirectUrl); | 131 grant.getAuthorizationUrl(redirectUrl); |
| 143 client.expectRequest((request) { | 132 client.expectRequest((request) { |
| 144 expect(request.method, equals('POST')); | 133 expect(request.method, equals('POST')); |
| 145 expect(request.url.toString(), equals(grant.tokenEndpoint.toString())); | 134 expect(request.url.toString(), equals(grant.tokenEndpoint.toString())); |
| 146 expect(request.bodyFields, equals({ | 135 expect(request.bodyFields, equals({ |
| 147 'grant_type': 'authorization_code', | 136 'grant_type': 'authorization_code', |
| 148 'code': 'auth code', | 137 'code': 'auth code', |
| 149 'redirect_uri': redirectUrl.toString(), | 138 'redirect_uri': redirectUrl.toString(), |
| 150 'client_id': 'identifier', | 139 'client_id': 'identifier', |
| 151 'client_secret': 'secret' | 140 'client_secret': 'secret' |
| 152 })); | 141 })); |
| 153 | 142 |
| 154 return new Future.value(new http.Response(JSON.encode({ | 143 return new Future.value(new http.Response(JSON.encode({ |
| 155 'access_token': 'access token', | 144 'access_token': 'access token', |
| 156 'token_type': 'bearer', | 145 'token_type': 'bearer', |
| 157 }), 200, headers: {'content-type': 'application/json'})); | 146 }), 200, headers: {'content-type': 'application/json'})); |
| 158 }); | 147 }); |
| 159 | 148 |
| 160 grant.handleAuthorizationResponse({'code': 'auth code'}) | 149 expect(grant.handleAuthorizationResponse({'code': 'auth code'}) |
| 161 .then(expectAsync1((client) { | 150 .then((client) => client.credentials.accessToken), |
| 162 expect(client.credentials.accessToken, equals('access token')); | 151 completion(equals('access token'))); |
|
nweiz
2014/03/06 00:12:17
Indent this +2 spaces. You can also indent the pre
| |
| 163 })); | |
| 164 }); | 152 }); |
| 165 }); | 153 }); |
| 166 | 154 |
| 167 group('.handleAuthorizationCode', () { | 155 group('.handleAuthorizationCode', () { |
| 168 setUp(createGrant); | 156 setUp(createGrant); |
| 169 | 157 |
| 170 test("can't be called before .getAuthorizationUrl", () { | 158 test("can't be called before .getAuthorizationUrl", () { |
| 171 expectFutureThrows( | 159 expect(grant.handleAuthorizationCode('auth code'), throwsStateError); |
| 172 grant.handleAuthorizationCode('auth code'), | |
| 173 (e) => e is StateError); | |
| 174 }); | 160 }); |
| 175 | 161 |
| 176 test("can't be called twice", () { | 162 test("can't be called twice", () { |
| 177 grant.getAuthorizationUrl(redirectUrl); | 163 grant.getAuthorizationUrl(redirectUrl); |
| 178 expectFutureThrows( | 164 expect(grant.handleAuthorizationCode('auth code'), throwsFormatException); |
| 179 grant.handleAuthorizationCode('auth code'), | 165 expect(grant.handleAuthorizationCode('auth code'), throwsStateError); |
| 180 (e) => e is FormatException); | |
| 181 expectFutureThrows(grant.handleAuthorizationCode('auth code'), | |
| 182 (e) => e is StateError); | |
| 183 }); | 166 }); |
| 184 | 167 |
| 185 test('sends an authorization code request', () { | 168 test('sends an authorization code request', () { |
| 186 grant.getAuthorizationUrl(redirectUrl); | 169 grant.getAuthorizationUrl(redirectUrl); |
| 187 client.expectRequest((request) { | 170 client.expectRequest((request) { |
| 188 expect(request.method, equals('POST')); | 171 expect(request.method, equals('POST')); |
| 189 expect(request.url.toString(), equals(grant.tokenEndpoint.toString())); | 172 expect(request.url.toString(), equals(grant.tokenEndpoint.toString())); |
| 190 expect(request.bodyFields, equals({ | 173 expect(request.bodyFields, equals({ |
| 191 'grant_type': 'authorization_code', | 174 'grant_type': 'authorization_code', |
| 192 'code': 'auth code', | 175 'code': 'auth code', |
| 193 'redirect_uri': redirectUrl.toString(), | 176 'redirect_uri': redirectUrl.toString(), |
| 194 'client_id': 'identifier', | 177 'client_id': 'identifier', |
| 195 'client_secret': 'secret' | 178 'client_secret': 'secret' |
| 196 })); | 179 })); |
| 197 | 180 |
| 198 return new Future.value(new http.Response(JSON.encode({ | 181 return new Future.value(new http.Response(JSON.encode({ |
| 199 'access_token': 'access token', | 182 'access_token': 'access token', |
| 200 'token_type': 'bearer', | 183 'token_type': 'bearer', |
| 201 }), 200, headers: {'content-type': 'application/json'})); | 184 }), 200, headers: {'content-type': 'application/json'})); |
| 202 }); | 185 }); |
| 203 | 186 |
| 204 expect(grant.handleAuthorizationCode('auth code'), | 187 expect(grant.handleAuthorizationCode('auth code'), |
| 205 completion(predicate((client) { | 188 completion(predicate((client) { |
| 206 expect(client.credentials.accessToken, equals('access token')); | 189 expect(client.credentials.accessToken, equals('access token')); |
| 207 return true; | 190 return true; |
| 208 }))); | 191 }))); |
| 209 }); | 192 }); |
| 210 }); | 193 }); |
| 211 } | 194 } |
| OLD | NEW |