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({}), |
100 (e) => e is StateError); | 94 throwsStateError); |
nweiz
2014/03/05 22:38:08
Indent this correctly.
kevmoo
2014/03/06 00:01:42
Done.
| |
101 }); | 95 }); |
102 | 96 |
103 test("can't be called twice", () { | 97 test("can't be called twice", () { |
104 grant.getAuthorizationUrl(redirectUrl); | 98 grant.getAuthorizationUrl(redirectUrl); |
105 expectFutureThrows( | 99 expect( |
106 grant.handleAuthorizationResponse({'code': 'auth code'}), | 100 grant.handleAuthorizationResponse({'code': 'auth code'}), |
107 (e) => e is FormatException); | 101 throwsFormatException); |
108 expectFutureThrows( | 102 expect( |
109 grant.handleAuthorizationResponse({'code': 'auth code'}), | 103 grant.handleAuthorizationResponse({'code': 'auth code'}), |
110 (e) => e is StateError); | 104 throwsStateError); |
111 }); | 105 }); |
112 | 106 |
113 test('must have a state parameter if the authorization URL did', () { | 107 test('must have a state parameter if the authorization URL did', () { |
114 grant.getAuthorizationUrl(redirectUrl, state: 'state'); | 108 grant.getAuthorizationUrl(redirectUrl, state: 'state'); |
115 expectFutureThrows( | 109 expect( |
116 grant.handleAuthorizationResponse({'code': 'auth code'}), | 110 grant.handleAuthorizationResponse({'code': 'auth code'}), |
117 (e) => e is FormatException); | 111 throwsFormatException); |
118 }); | 112 }); |
119 | 113 |
120 test('must have the same state parameter the authorization URL did', () { | 114 test('must have the same state parameter the authorization URL did', () { |
121 grant.getAuthorizationUrl(redirectUrl, state: 'state'); | 115 grant.getAuthorizationUrl(redirectUrl, state: 'state'); |
122 expectFutureThrows(grant.handleAuthorizationResponse({ | 116 expect(grant.handleAuthorizationResponse({ |
123 'code': 'auth code', | 117 'code': 'auth code', |
124 'state': 'other state' | 118 'state': 'other state' |
125 }), (e) => e is FormatException); | 119 }), throwsFormatException); |
126 }); | 120 }); |
127 | 121 |
128 test('must have a code parameter', () { | 122 test('must have a code parameter', () { |
129 grant.getAuthorizationUrl(redirectUrl); | 123 grant.getAuthorizationUrl(redirectUrl); |
130 expectFutureThrows(grant.handleAuthorizationResponse({}), | 124 expect(grant.handleAuthorizationResponse({}), |
131 (e) => e is FormatException); | 125 throwsFormatException); |
nweiz
2014/03/05 22:38:08
Indent this correctly.
kevmoo
2014/03/06 00:01:42
Done.
| |
132 }); | 126 }); |
133 | 127 |
134 test('with an error parameter throws an AuthorizationException', () { | 128 test('with an error parameter throws an AuthorizationException', () { |
135 grant.getAuthorizationUrl(redirectUrl); | 129 grant.getAuthorizationUrl(redirectUrl); |
136 expectFutureThrows( | 130 expect( |
137 grant.handleAuthorizationResponse({'error': 'invalid_request'}), | 131 grant.handleAuthorizationResponse({'error': 'invalid_request'}), |
138 (e) => e is oauth2.AuthorizationException); | 132 throwsA((e) => e is oauth2.AuthorizationException)); |
139 }); | 133 }); |
140 | 134 |
141 test('sends an authorization code request', () { | 135 test('sends an authorization code request', () { |
142 grant.getAuthorizationUrl(redirectUrl); | 136 grant.getAuthorizationUrl(redirectUrl); |
143 client.expectRequest((request) { | 137 client.expectRequest((request) { |
144 expect(request.method, equals('POST')); | 138 expect(request.method, equals('POST')); |
145 expect(request.url.toString(), equals(grant.tokenEndpoint.toString())); | 139 expect(request.url.toString(), equals(grant.tokenEndpoint.toString())); |
146 expect(request.bodyFields, equals({ | 140 expect(request.bodyFields, equals({ |
147 'grant_type': 'authorization_code', | 141 'grant_type': 'authorization_code', |
148 'code': 'auth code', | 142 'code': 'auth code', |
149 'redirect_uri': redirectUrl.toString(), | 143 'redirect_uri': redirectUrl.toString(), |
150 'client_id': 'identifier', | 144 'client_id': 'identifier', |
151 'client_secret': 'secret' | 145 'client_secret': 'secret' |
152 })); | 146 })); |
153 | 147 |
154 return new Future.value(new http.Response(JSON.encode({ | 148 return new Future.value(new http.Response(JSON.encode({ |
155 'access_token': 'access token', | 149 'access_token': 'access token', |
156 'token_type': 'bearer', | 150 'token_type': 'bearer', |
157 }), 200, headers: {'content-type': 'application/json'})); | 151 }), 200, headers: {'content-type': 'application/json'})); |
158 }); | 152 }); |
159 | 153 |
160 grant.handleAuthorizationResponse({'code': 'auth code'}) | 154 return grant.handleAuthorizationResponse({'code': 'auth code'}) |
161 .then(expectAsync1((client) { | 155 .then((client) { |
162 expect(client.credentials.accessToken, equals('access token')); | 156 expect(client.credentials.accessToken, equals('access token')); |
163 })); | 157 }); |
nweiz
2014/03/05 22:38:08
These days I'd write this as
expect(grant.han
kevmoo
2014/03/06 00:01:42
Done.
| |
164 }); | 158 }); |
165 }); | 159 }); |
166 | 160 |
167 group('.handleAuthorizationCode', () { | 161 group('.handleAuthorizationCode', () { |
168 setUp(createGrant); | 162 setUp(createGrant); |
169 | 163 |
170 test("can't be called before .getAuthorizationUrl", () { | 164 test("can't be called before .getAuthorizationUrl", () { |
171 expectFutureThrows( | 165 expect( |
172 grant.handleAuthorizationCode('auth code'), | 166 grant.handleAuthorizationCode('auth code'), |
173 (e) => e is StateError); | 167 throwsStateError); |
174 }); | 168 }); |
175 | 169 |
176 test("can't be called twice", () { | 170 test("can't be called twice", () { |
177 grant.getAuthorizationUrl(redirectUrl); | 171 grant.getAuthorizationUrl(redirectUrl); |
178 expectFutureThrows( | 172 expect( |
179 grant.handleAuthorizationCode('auth code'), | 173 grant.handleAuthorizationCode('auth code'), |
180 (e) => e is FormatException); | 174 throwsFormatException); |
181 expectFutureThrows(grant.handleAuthorizationCode('auth code'), | 175 expect(grant.handleAuthorizationCode('auth code'), |
182 (e) => e is StateError); | 176 throwsStateError); |
nweiz
2014/03/05 22:38:08
Indent this correctly.
kevmoo
2014/03/06 00:01:42
Done.
| |
183 }); | 177 }); |
184 | 178 |
185 test('sends an authorization code request', () { | 179 test('sends an authorization code request', () { |
186 grant.getAuthorizationUrl(redirectUrl); | 180 grant.getAuthorizationUrl(redirectUrl); |
187 client.expectRequest((request) { | 181 client.expectRequest((request) { |
188 expect(request.method, equals('POST')); | 182 expect(request.method, equals('POST')); |
189 expect(request.url.toString(), equals(grant.tokenEndpoint.toString())); | 183 expect(request.url.toString(), equals(grant.tokenEndpoint.toString())); |
190 expect(request.bodyFields, equals({ | 184 expect(request.bodyFields, equals({ |
191 'grant_type': 'authorization_code', | 185 'grant_type': 'authorization_code', |
192 'code': 'auth code', | 186 'code': 'auth code', |
193 'redirect_uri': redirectUrl.toString(), | 187 'redirect_uri': redirectUrl.toString(), |
194 'client_id': 'identifier', | 188 'client_id': 'identifier', |
195 'client_secret': 'secret' | 189 'client_secret': 'secret' |
196 })); | 190 })); |
197 | 191 |
198 return new Future.value(new http.Response(JSON.encode({ | 192 return new Future.value(new http.Response(JSON.encode({ |
199 'access_token': 'access token', | 193 'access_token': 'access token', |
200 'token_type': 'bearer', | 194 'token_type': 'bearer', |
201 }), 200, headers: {'content-type': 'application/json'})); | 195 }), 200, headers: {'content-type': 'application/json'})); |
202 }); | 196 }); |
203 | 197 |
204 expect(grant.handleAuthorizationCode('auth code'), | 198 expect(grant.handleAuthorizationCode('auth code'), |
205 completion(predicate((client) { | 199 completion(predicate((client) { |
206 expect(client.credentials.accessToken, equals('access token')); | 200 expect(client.credentials.accessToken, equals('access token')); |
207 return true; | 201 return true; |
208 }))); | 202 }))); |
209 }); | 203 }); |
210 }); | 204 }); |
211 } | 205 } |
OLD | NEW |