| 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:async'; | |
| 8 import 'dart:convert'; | |
| 9 | |
| 10 import 'package:unittest/unittest.dart'; | |
| 11 import 'package:http/http.dart' as http; | |
| 12 import 'package:oauth2/oauth2.dart' as oauth2; | |
| 13 | |
| 14 import 'utils.dart'; | |
| 15 | |
| 16 final redirectUrl = Uri.parse('http://example.com/redirect'); | |
| 17 | |
| 18 ExpectClient client; | |
| 19 | |
| 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', () { | |
| 34 setUp(createGrant); | |
| 35 | |
| 36 test('builds the correct URL', () { | |
| 37 expect(grant.getAuthorizationUrl(redirectUrl).toString(), | |
| 38 equals('https://example.com/authorization' | |
| 39 '?response_type=code' | |
| 40 '&client_id=identifier' | |
| 41 '&redirect_uri=http%3A%2F%2Fexample.com%2Fredirect')); | |
| 42 }); | |
| 43 | |
| 44 test('builds the correct URL with scopes', () { | |
| 45 var authorizationUrl = grant.getAuthorizationUrl( | |
| 46 redirectUrl, scopes: ['scope', 'other/scope']); | |
| 47 expect(authorizationUrl.toString(), | |
| 48 equals('https://example.com/authorization' | |
| 49 '?response_type=code' | |
| 50 '&client_id=identifier' | |
| 51 '&redirect_uri=http%3A%2F%2Fexample.com%2Fredirect' | |
| 52 '&scope=scope+other%2Fscope')); | |
| 53 }); | |
| 54 | |
| 55 test('builds the correct URL with state', () { | |
| 56 var authorizationUrl = grant.getAuthorizationUrl( | |
| 57 redirectUrl, state: 'state'); | |
| 58 expect(authorizationUrl.toString(), | |
| 59 equals('https://example.com/authorization' | |
| 60 '?response_type=code' | |
| 61 '&client_id=identifier' | |
| 62 '&redirect_uri=http%3A%2F%2Fexample.com%2Fredirect' | |
| 63 '&state=state')); | |
| 64 }); | |
| 65 | |
| 66 test('merges with existing query parameters', () { | |
| 67 grant = new oauth2.AuthorizationCodeGrant( | |
| 68 'identifier', | |
| 69 'secret', | |
| 70 Uri.parse('https://example.com/authorization?query=value'), | |
| 71 Uri.parse('https://example.com/token'), | |
| 72 httpClient: client); | |
| 73 | |
| 74 var authorizationUrl = grant.getAuthorizationUrl(redirectUrl); | |
| 75 expect(authorizationUrl.toString(), | |
| 76 equals('https://example.com/authorization' | |
| 77 '?query=value' | |
| 78 '&response_type=code' | |
| 79 '&client_id=identifier' | |
| 80 '&redirect_uri=http%3A%2F%2Fexample.com%2Fredirect')); | |
| 81 }); | |
| 82 | |
| 83 test("can't be called twice", () { | |
| 84 grant.getAuthorizationUrl(redirectUrl); | |
| 85 expect(() => grant.getAuthorizationUrl(redirectUrl), throwsStateError); | |
| 86 }); | |
| 87 }); | |
| 88 | |
| 89 group('.handleAuthorizationResponse', () { | |
| 90 setUp(createGrant); | |
| 91 | |
| 92 test("can't be called before .getAuthorizationUrl", () { | |
| 93 expect(grant.handleAuthorizationResponse({}), throwsStateError); | |
| 94 }); | |
| 95 | |
| 96 test("can't be called twice", () { | |
| 97 grant.getAuthorizationUrl(redirectUrl); | |
| 98 expect(grant.handleAuthorizationResponse({'code': 'auth code'}), | |
| 99 throwsFormatException); | |
| 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( | |
| 126 grant.handleAuthorizationResponse({'error': 'invalid_request'}), | |
| 127 throwsA((e) => e is oauth2.AuthorizationException)); | |
| 128 }); | |
| 129 | |
| 130 test('sends an authorization code request', () { | |
| 131 grant.getAuthorizationUrl(redirectUrl); | |
| 132 client.expectRequest((request) { | |
| 133 expect(request.method, equals('POST')); | |
| 134 expect(request.url.toString(), equals(grant.tokenEndpoint.toString())); | |
| 135 expect(request.bodyFields, equals({ | |
| 136 'grant_type': 'authorization_code', | |
| 137 'code': 'auth code', | |
| 138 'redirect_uri': redirectUrl.toString(), | |
| 139 'client_id': 'identifier', | |
| 140 'client_secret': 'secret' | |
| 141 })); | |
| 142 | |
| 143 return new Future.value(new http.Response(JSON.encode({ | |
| 144 'access_token': 'access token', | |
| 145 'token_type': 'bearer', | |
| 146 }), 200, headers: {'content-type': 'application/json'})); | |
| 147 }); | |
| 148 | |
| 149 expect(grant.handleAuthorizationResponse({'code': 'auth code'}) | |
| 150 .then((client) => client.credentials.accessToken), | |
| 151 completion(equals('access token'))); | |
| 152 }); | |
| 153 }); | |
| 154 | |
| 155 group('.handleAuthorizationCode', () { | |
| 156 setUp(createGrant); | |
| 157 | |
| 158 test("can't be called before .getAuthorizationUrl", () { | |
| 159 expect(grant.handleAuthorizationCode('auth code'), throwsStateError); | |
| 160 }); | |
| 161 | |
| 162 test("can't be called twice", () { | |
| 163 grant.getAuthorizationUrl(redirectUrl); | |
| 164 expect(grant.handleAuthorizationCode('auth code'), throwsFormatException); | |
| 165 expect(grant.handleAuthorizationCode('auth code'), throwsStateError); | |
| 166 }); | |
| 167 | |
| 168 test('sends an authorization code request', () { | |
| 169 grant.getAuthorizationUrl(redirectUrl); | |
| 170 client.expectRequest((request) { | |
| 171 expect(request.method, equals('POST')); | |
| 172 expect(request.url.toString(), equals(grant.tokenEndpoint.toString())); | |
| 173 expect(request.bodyFields, equals({ | |
| 174 'grant_type': 'authorization_code', | |
| 175 'code': 'auth code', | |
| 176 'redirect_uri': redirectUrl.toString(), | |
| 177 'client_id': 'identifier', | |
| 178 'client_secret': 'secret' | |
| 179 })); | |
| 180 | |
| 181 return new Future.value(new http.Response(JSON.encode({ | |
| 182 'access_token': 'access token', | |
| 183 'token_type': 'bearer', | |
| 184 }), 200, headers: {'content-type': 'application/json'})); | |
| 185 }); | |
| 186 | |
| 187 expect(grant.handleAuthorizationCode('auth code'), | |
| 188 completion(predicate((client) { | |
| 189 expect(client.credentials.accessToken, equals('access token')); | |
| 190 return true; | |
| 191 }))); | |
| 192 }); | |
| 193 }); | |
| 194 } | |
| OLD | NEW |