Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library authorization_code_grant_test; | |
| 6 | |
| 7 import 'dart:io'; | |
| 8 import 'dart:json'; | |
| 9 import 'dart:uri'; | |
| 10 | |
| 11 import '../../unittest/lib/unittest.dart'; | |
| 12 import '../../http/lib/http.dart' as http; | |
| 13 import '../../http/lib/testing.dart'; | |
| 14 import '../lib/oauth2.dart' as oauth2; | |
| 15 import 'utils.dart'; | |
| 16 | |
| 17 final Uri redirectUrl = new Uri.fromString('http://example.com/redirect'); | |
|
Bob Nystrom
2012/11/16 19:53:30
Ditch the annotation.
nweiz
2012/11/17 01:06:27
Done.
| |
| 18 | |
| 19 ExpectClient client; | |
| 20 | |
| 21 AuthorizationCodeGrant grant; | |
| 22 | |
| 23 void createGrant() { | |
| 24 client = new ExpectClient(); | |
| 25 grant = new oauth2.AuthorizationCodeGrant( | |
| 26 'identifier', | |
| 27 'secret', | |
| 28 new Uri.fromString('https://example.com/authorization'), | |
| 29 new Uri.fromString('https://example.com/token'), | |
| 30 httpClient: client); | |
| 31 } | |
| 32 | |
| 33 void main() { | |
| 34 group('.getAuthorizationUrl', () { | |
| 35 setUp(createGrant); | |
| 36 | |
| 37 test('builds the correct URL', () { | |
| 38 expect(grant.getAuthorizationUrl(redirectUrl).toString(), | |
| 39 equals('https://example.com/authorization' | |
| 40 '?response_type=code' | |
| 41 '&client_id=identifier' | |
| 42 '&redirect_uri=http%3A%2F%2Fexample.com%2Fredirect')); | |
| 43 }); | |
| 44 | |
| 45 test('builds the correct URL with scopes', () { | |
| 46 var authorizationUrl = grant.getAuthorizationUrl( | |
| 47 redirectUrl, scopes: ['scope', 'other/scope']); | |
| 48 expect(authorizationUrl.toString(), | |
| 49 equals('https://example.com/authorization' | |
| 50 '?response_type=code' | |
| 51 '&client_id=identifier' | |
| 52 '&redirect_uri=http%3A%2F%2Fexample.com%2Fredirect' | |
| 53 '&scope=scope%20other%2Fscope')); | |
| 54 }); | |
| 55 | |
| 56 test('builds the correct URL with state', () { | |
| 57 var authorizationUrl = grant.getAuthorizationUrl( | |
| 58 redirectUrl, state: 'state'); | |
| 59 expect(authorizationUrl.toString(), | |
| 60 equals('https://example.com/authorization' | |
| 61 '?response_type=code' | |
| 62 '&client_id=identifier' | |
| 63 '&redirect_uri=http%3A%2F%2Fexample.com%2Fredirect' | |
| 64 '&state=state')); | |
| 65 }); | |
| 66 | |
| 67 test('merges with existing query parameters', () { | |
| 68 grant = new oauth2.AuthorizationCodeGrant( | |
| 69 'identifier', | |
| 70 'secret', | |
| 71 new Uri.fromString('https://example.com/authorization?query=value'), | |
| 72 new Uri.fromString('https://example.com/token'), | |
| 73 httpClient: client); | |
| 74 | |
| 75 var authorizationUrl = grant.getAuthorizationUrl(redirectUrl); | |
| 76 expect(authorizationUrl.toString(), | |
| 77 equals('https://example.com/authorization' | |
| 78 '?query=value' | |
| 79 '&response_type=code' | |
| 80 '&client_id=identifier' | |
| 81 '&redirect_uri=http%3A%2F%2Fexample.com%2Fredirect')); | |
|
Bob Nystrom
2012/11/16 19:53:30
Since you're using a map at some point (if I recal
nweiz
2012/11/17 01:06:27
All our maps are constructed as map literals, so t
Bob Nystrom
2012/11/19 21:37:10
Works for me.
| |
| 82 }); | |
| 83 | |
| 84 test("can't be called twice", () { | |
| 85 grant.getAuthorizationUrl(redirectUrl); | |
| 86 expect(() => grant.getAuthorizationUrl(redirectUrl), throwsStateError); | |
| 87 }); | |
| 88 }); | |
| 89 | |
| 90 group('.handleAuthorizationResponse', () { | |
| 91 setUp(createGrant); | |
| 92 | |
| 93 test("can't be called before .getAuthorizationUrl", () { | |
| 94 expect(grant.handleAuthorizationResponse({}), throwsStateError); | |
| 95 }); | |
| 96 | |
| 97 test("can't be called twice", () { | |
| 98 grant.getAuthorizationUrl(redirectUrl); | |
| 99 grant.handleAuthorizationResponse({'code': 'auth code'}); | |
| 100 expect(grant.handleAuthorizationResponse({'code': 'auth code'}), | |
| 101 throwsStateError); | |
| 102 }); | |
| 103 | |
| 104 test('must have a state parameter if the authorization URL did', () { | |
| 105 grant.getAuthorizationUrl(redirectUrl, state: 'state'); | |
| 106 expect(grant.handleAuthorizationResponse({'code': 'auth code'}), | |
| 107 throwsFormatException); | |
| 108 }); | |
| 109 | |
| 110 test('must have the same state parameter the authorization URL did', () { | |
| 111 grant.getAuthorizationUrl(redirectUrl, state: 'state'); | |
| 112 expect(grant.handleAuthorizationResponse({ | |
| 113 'code': 'auth code', | |
| 114 'state': 'other state' | |
| 115 }), throwsFormatException); | |
| 116 }); | |
| 117 | |
| 118 test('must have a code parameter', () { | |
| 119 grant.getAuthorizationUrl(redirectUrl); | |
| 120 expect(grant.handleAuthorizationResponse({}), throwsFormatException); | |
| 121 }); | |
| 122 | |
| 123 test('with an error parameter throws an AuthorizationException', () { | |
| 124 grant.getAuthorizationUrl(redirectUrl); | |
| 125 expect(grant.handleAuthorizationResponse({'error': 'invalid_request'}), | |
| 126 throwsAuthorizationException); | |
| 127 }); | |
| 128 | |
| 129 test('sends an authorization code request', () { | |
| 130 grant.getAuthorizationUrl(redirectUrl); | |
| 131 client.expectRequest((request) { | |
| 132 expect(request.method, equals('POST')); | |
| 133 expect(request.url.toString(), equals(grant.tokenEndpoint.toString())); | |
| 134 expect(request.bodyFields, equals({ | |
| 135 'grant_type': 'authorization_code', | |
| 136 'code': 'auth code', | |
| 137 'redirect_uri': redirectUrl.toString(), | |
| 138 'client_id': 'identifier', | |
| 139 'client_secret': 'secret' | |
| 140 })); | |
| 141 | |
| 142 return new Future.immediate(new http.Response(JSON.stringify({ | |
| 143 'access_token': 'access token', | |
| 144 'token_type': 'bearer', | |
| 145 }), 200, headers: {'content-type': 'application/json'})); | |
| 146 }); | |
| 147 | |
| 148 expect(grant.handleAuthorizationResponse({'code': 'auth code'}), | |
| 149 completion(predicate((client) { | |
| 150 expect(client.credentials.accessToken, equals('access token')); | |
| 151 return true; | |
| 152 }))); | |
| 153 }); | |
| 154 }); | |
| 155 | |
| 156 group('.handleAuthorizationCode', () { | |
| 157 setUp(createGrant); | |
| 158 | |
| 159 test("can't be called before .getAuthorizationUrl", () { | |
| 160 expect(grant.handleAuthorizationCode('auth code'), throwsStateError); | |
| 161 }); | |
| 162 | |
| 163 test("can't be called twice", () { | |
| 164 grant.getAuthorizationUrl(redirectUrl); | |
| 165 grant.handleAuthorizationCode('auth code'); | |
| 166 expect(grant.handleAuthorizationCode('auth code'), | |
| 167 throwsStateError); | |
| 168 }); | |
| 169 | |
| 170 test('sends an authorization code request', () { | |
| 171 grant.getAuthorizationUrl(redirectUrl); | |
| 172 client.expectRequest((request) { | |
| 173 expect(request.method, equals('POST')); | |
| 174 expect(request.url.toString(), equals(grant.tokenEndpoint.toString())); | |
| 175 expect(request.bodyFields, equals({ | |
| 176 'grant_type': 'authorization_code', | |
| 177 'code': 'auth code', | |
| 178 'redirect_uri': redirectUrl.toString(), | |
| 179 'client_id': 'identifier', | |
| 180 'client_secret': 'secret' | |
| 181 })); | |
| 182 | |
| 183 return new Future.immediate(new http.Response(JSON.stringify({ | |
| 184 'access_token': 'access token', | |
| 185 'token_type': 'bearer', | |
| 186 }), 200, headers: {'content-type': 'application/json'})); | |
| 187 }); | |
| 188 | |
| 189 expect(grant.handleAuthorizationCode('auth code'), | |
| 190 completion(predicate((client) { | |
| 191 expect(client.credentials.accessToken, equals('access token')); | |
| 192 return true; | |
| 193 }))); | |
| 194 }); | |
| 195 }); | |
| 196 } | |
| OLD | NEW |