Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanoltman committed Feb 27, 2024
1 parent d617842 commit 80cd1f8
Showing 1 changed file with 79 additions and 2 deletions.
81 changes: 79 additions & 2 deletions googleapis_auth/test/oauth2_flows/auth_code_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ import '../test_utils.dart';

typedef RequestHandler = Future<Response> Function(Request _);

class CustomAuthEndpoints extends AuthEndpoints {
@override
Uri get authorizationEndpoint => Uri.https('example.com', '/auth');

@override
Uri get tokenEndpoint => Uri.https('example.com', '/token');
}

final _browserFlowRedirectMatcher = predicate<String>((object) {
if (object.startsWith('redirect_uri=')) {
final url = Uri.parse(
Expand All @@ -35,10 +43,13 @@ void main() {

// Validation + Responses from the authorization server.

RequestHandler successFullResponse({required bool manual}) =>
RequestHandler successFullResponse({
required bool manual,
AuthEndpoints authEndpoints = const GoogleAuthEndpoints(),
}) =>
(Request request) async {
expect(request.method, equals('POST'));
expect(request.url, googleOauth2TokenEndpoint);
expect(request.url, authEndpoints.tokenEndpoint);
expect(
request.headers['content-type']!,
startsWith('application/x-www-form-urlencoded'),
Expand Down Expand Up @@ -128,13 +139,50 @@ void main() {
return redirectUri;
}

Uri validateUserPromptUriWithCustomEndpoints(
String url, {
bool manual = false,
}) {
final uri = Uri.parse(url);
final authEndpoints = CustomAuthEndpoints();
expect(uri.scheme, authEndpoints.authorizationEndpoint.scheme);
expect(uri.authority, authEndpoints.authorizationEndpoint.authority);
expect(uri.path, authEndpoints.authorizationEndpoint.path);
expect(uri.queryParameters, {
'client_id': clientId.identifier,
'response_type': 'code',
'scope': 's1 s2',
'redirect_uri': isNotEmpty,
'code_challenge': hasLength(43),
'code_challenge_method': 'S256',
if (!manual) 'state': hasLength(32),
});

final redirectUri = Uri.parse(uri.queryParameters['redirect_uri']!);

if (manual) {
expect('$redirectUri', equals('urn:ietf:wg:oauth:2.0:oob'));
} else {
expect(uri.queryParameters['state'], isNotNull);
expect(redirectUri.scheme, equals('http'));
expect(redirectUri.host, equals('localhost'));
}

return redirectUri;
}

group('authorization-code-flow', () {
group('manual-copy-paste', () {
Future<String> manualUserPrompt(String url) async {
validateUserPromptUri(url, manual: true);
return 'mycode';
}

Future<String> manualUserPromptWithCustomEndpoints(String url) async {
validateUserPromptUriWithCustomEndpoints(url, manual: true);
return 'mycode';
}

test('successful', () async {
final flow = AuthorizationCodeGrantManualFlow(
authEndpoints,
Expand All @@ -146,6 +194,21 @@ void main() {
validateAccessCredentials(await flow.run());
});

test('successful (custom endpoints)', () async {
final authEndpoints = CustomAuthEndpoints();
final flow = AuthorizationCodeGrantManualFlow(
authEndpoints,
clientId,
scopes,
mockClient(
successFullResponse(manual: true, authEndpoints: authEndpoints),
expectClose: false,
),
manualUserPromptWithCustomEndpoints,
);
validateAccessCredentials(await flow.run());
});

test('user-exception', () async {
// We use a TransportException here for convenience.
Future<String> manualUserPromptError(String url) =>
Expand Down Expand Up @@ -227,6 +290,20 @@ void main() {
callRedirectionEndpoint(authCodeCall);
}

void userPromptCustomEndpoints(String url, AuthEndpoints endpoints) {
final redirectUri = validateUserPromptUriWithCustomEndpoints(url);
final authCodeCall = Uri(
scheme: redirectUri.scheme,
host: redirectUri.host,
port: redirectUri.port,
path: redirectUri.path,
queryParameters: {
'state': Uri.parse(url).queryParameters['state'],
'code': 'mycode',
});
callRedirectionEndpoint(authCodeCall);
}

void userPromptInvalidHttpVerb(String url) {
final redirectUri = validateUserPromptUri(url);
final authCodeCall = Uri(
Expand Down

0 comments on commit 80cd1f8

Please sign in to comment.