diff --git a/packages/auth/amplify_auth_cognito/example/integration_test/custom_authorizer_test.dart b/packages/auth/amplify_auth_cognito/example/integration_test/custom_authorizer_test.dart index 26105ee0a1..0273075bef 100644 --- a/packages/auth/amplify_auth_cognito/example/integration_test/custom_authorizer_test.dart +++ b/packages/auth/amplify_auth_cognito/example/integration_test/custom_authorizer_test.dart @@ -64,79 +64,84 @@ void main() { }); asyncTest('can invoke with HTTP client', (_) async { - // ignore_for_file: avoid_print - print('TestTestTest - 1'); - final username = generateUsername(); - final password = generatePassword(); + List checkpoints = []; + try { + checkpoints.add('TestTestTest - 1'); + final username = generateUsername(); + final password = generatePassword(); - print('TestTestTest - 2'); - final signUpRes = await Amplify.Auth.signUp( - username: username, - password: password, - ); - expect(signUpRes.isSignUpComplete, isTrue); + checkpoints.add('TestTestTest - 2'); + final signUpRes = await Amplify.Auth.signUp( + username: username, + password: password, + ); + expect(signUpRes.isSignUpComplete, isTrue); - print('TestTestTest - 3'); - final signInRes = await Amplify.Auth.signIn( - username: username, - password: password, - ); - expect(signInRes.isSignedIn, isTrue); + checkpoints.add('TestTestTest - 3'); + final signInRes = await Amplify.Auth.signIn( + username: username, + password: password, + ); + expect(signInRes.isSignedIn, isTrue); - print('TestTestTest - 4'); - final session = - await Amplify.Auth.fetchAuthSession() as CognitoAuthSession; - expect(session.userPoolTokensResult.valueOrNull, isNotNull); + checkpoints.add('TestTestTest - 4'); + final session = + await Amplify.Auth.fetchAuthSession() as CognitoAuthSession; + expect(session.userPoolTokensResult.valueOrNull, isNotNull); - print('TestTestTest - 5'); - final apiUrl = config.api!.awsPlugin!.values - .singleWhere((e) => e.endpointType == EndpointType.rest) - .endpoint; + checkpoints.add('TestTestTest - 5'); + final apiUrl = config.api!.awsPlugin!.values + .singleWhere((e) => e.endpointType == EndpointType.rest) + .endpoint; - print('TestTestTest - 6'); - // Verifies invocation with the ID token. Invocation with an access - // token requires integration with a resource server/OAuth and is, thus, - // not tested. - // - // https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-enable-cognito-user-pool.html + checkpoints.add('TestTestTest - 6'); + // Verifies invocation with the ID token. Invocation with an access + // token requires integration with a resource server/OAuth and is, thus, + // not tested. + // + // https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-enable-cognito-user-pool.html - final request = AWSStreamedHttpRequest.post( - Uri.parse(apiUrl).replace( - queryParameters: queryParameters, - ), - headers: { - AWSHeaders.accept: 'application/json;charset=utf-8', - AWSHeaders.authorization: - session.userPoolTokensResult.value.idToken.raw, - ...customHeaders, - }, - body: HttpPayload.json({'request': 'hello'}), - ); - print('TestTestTest - 7'); - final resp = await client.send(request).response; - print('TestTestTest - 8'); - final body = await resp.decodeBody(); - print('TestTestTest - 9'); - expect(resp.statusCode, 200, reason: body); - expect( - jsonDecode(body), - equals({'response': 'hello'}), - ); - print('TestTestTest - 10'); - customHeaders.forEach((key, value) { - expect( - resp.headers, - containsPair(key, value), + final request = AWSStreamedHttpRequest.post( + Uri.parse(apiUrl).replace( + queryParameters: queryParameters, + ), + headers: { + AWSHeaders.accept: 'application/json;charset=utf-8', + AWSHeaders.authorization: + session.userPoolTokensResult.value.idToken.raw, + ...customHeaders, + }, + body: HttpPayload.json({'request': 'hello'}), ); - }); - print('TestTestTest - 11'); - queryParameters.forEach((key, value) { + checkpoints.add('TestTestTest - 7'); + final resp = await client.send(request).response; + checkpoints.add('TestTestTest - 8'); + final body = await resp.decodeBody(); + checkpoints.add('TestTestTest - 9'); + expect(resp.statusCode, 200, reason: body); expect( - resp.headers, - containsPair('x-query-$key', value), + jsonDecode(body), + equals({'response': 'hello'}), ); - }); - print('TestTestTest - 12'); + checkpoints.add('TestTestTest - 10'); + customHeaders.forEach((key, value) { + expect( + resp.headers, + containsPair(key, value), + ); + }); + checkpoints.add('TestTestTest - 11'); + queryParameters.forEach((key, value) { + expect( + resp.headers, + containsPair('x-query-$key', value), + ); + }); + checkpoints.add('TestTestTest - 12'); + } catch (e) { + checkpoints.add('TestTestTest - Error: $e'); + throw Exception('$e - CheckPoints1 = $checkpoints'); + } }); asyncTest('can invoke with API plugin', (_) async { @@ -220,59 +225,81 @@ void main() { }); asyncTest('can invoke with HTTP client', (_) async { - final cognitoPlugin = Amplify.Auth.getPlugin( - AmplifyAuthCognito.pluginKey, - ); - final session = await cognitoPlugin.fetchAuthSession(); - expect(session.credentialsResult.valueOrNull, isNotNull); + List checkpoints = []; + try { + checkpoints.add('TestTestTest - 1'); + final cognitoPlugin = Amplify.Auth.getPlugin( + AmplifyAuthCognito.pluginKey, + ); + checkpoints.add('TestTestTest - 2'); + final session = await cognitoPlugin.fetchAuthSession(); + checkpoints.add('TestTestTest - 3'); + expect(session.credentialsResult.valueOrNull, isNotNull); + checkpoints.add('TestTestTest - 4'); - final restApi = config.api!.awsPlugin!.values - .singleWhere((e) => e.endpointType == EndpointType.rest); - final apiUrl = Uri.parse(restApi.endpoint); + final restApi = config.api!.awsPlugin!.values.singleWhere( + (e) => e.endpointType == EndpointType.rest); + checkpoints.add('TestTestTest - 5'); + final apiUrl = Uri.parse(restApi.endpoint); + checkpoints.add('TestTestTest - 6'); - final payload = jsonEncode({'request': 'hello'}); - final request = AWSHttpRequest.post( - apiUrl.replace( - queryParameters: queryParameters, - ), - headers: const { - AWSHeaders.accept: 'application/json;charset=utf-8', - ...customHeaders, - }, - body: utf8.encode(payload), - ); - final signer = AWSSigV4Signer( - credentialsProvider: AWSCredentialsProvider( - session.credentialsResult.value, - ), - ); - final scope = AWSCredentialScope( - region: restApi.region, - service: AWSService.apiGatewayManagementApi, - ); - final signedRequest = await signer.sign( - request, - credentialScope: scope, - ); - final resp = await client.send(signedRequest).response; - final body = await resp.decodeBody(); - expect(resp.statusCode, 200, reason: body); - expect( - jsonDecode(body), - equals({'response': 'hello'}), - ); - customHeaders.forEach((key, value) { - expect( - resp.headers, - containsPair(key, value), + final payload = jsonEncode({'request': 'hello'}); + final request = AWSHttpRequest.post( + apiUrl.replace( + queryParameters: queryParameters, + ), + headers: const { + AWSHeaders.accept: 'application/json;charset=utf-8', + ...customHeaders, + }, + body: utf8.encode(payload), + ); + checkpoints.add('TestTestTest - 7'); + final signer = AWSSigV4Signer( + credentialsProvider: AWSCredentialsProvider( + session.credentialsResult.value, + ), + ); + checkpoints.add('TestTestTest - 8'); + final scope = AWSCredentialScope( + region: restApi.region, + service: AWSService.apiGatewayManagementApi, ); - }); - queryParameters.forEach((key, value) { + checkpoints.add('TestTestTest - 9'); + final signedRequest = await signer.sign( + request, + credentialScope: scope, + ); + checkpoints.add('TestTestTest - 10'); + final resp = await client.send(signedRequest).response; + checkpoints.add('TestTestTest - 11'); + final body = await resp.decodeBody(); + checkpoints.add('TestTestTest - 12'); + expect(resp.statusCode, 200, reason: body); + checkpoints.add('TestTestTest - 13'); expect( - resp.headers, - containsPair('x-query-$key', value), + jsonDecode(body), + equals({'response': 'hello'}), ); - }); + checkpoints.add('TestTestTest - 14'); + customHeaders.forEach((key, value) { + expect( + resp.headers, + containsPair(key, value), + ); + }); + checkpoints.add('TestTestTest - 15'); + queryParameters.forEach((key, value) { + expect( + resp.headers, + containsPair('x-query-$key', value), + ); + }); + checkpoints.add('TestTestTest - 16'); + } catch (e) { + checkpoints.add('TestTestTest - Error: $e'); + throw Exception('$e - CheckPoints2 = $checkpoints'); + } }); asyncTest('can invoke with API plugin', (_) async {