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