Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
[google_sign_in_web] Ensure plugin throws PlatformExceptions (#2943)
Browse files Browse the repository at this point in the history
Instead of throwing JS objects, or attempting to access undefined properties.

This change also migrates tests to the `integration_test` package.
  • Loading branch information
ditman committed Sep 14, 2020
1 parent 4eb849a commit 56410f5
Show file tree
Hide file tree
Showing 24 changed files with 477 additions and 157 deletions.
7 changes: 7 additions & 0 deletions packages/google_sign_in/google_sign_in_web/CHANGELOG.md
@@ -1,3 +1,10 @@
## 0.9.2

* Throw PlatformExceptions from where the GMaps SDK may throw exceptions: `init()` and `signIn()`.
* Add two new JS-interop types to be able to unwrap JS errors in release mode.
* Align the fields of the thrown PlatformExceptions with the mobile version.
* Migrate tests to run with `flutter drive`

## 0.9.1+2

* Update package:e2e reference to use the local version in the flutter/plugins
Expand Down
6 changes: 1 addition & 5 deletions packages/google_sign_in/google_sign_in_web/README.md
Expand Up @@ -98,11 +98,7 @@ See the [google_sign_in.dart](https://github.com/flutter/plugins/blob/master/pac

Tests are a crucial to contributions to this package. All new contributions should be reasonably tested.

In order to run tests in this package, do:

```
flutter test --platform chrome -j1
```
**Check the [`test/README.md` file](https://github.com/flutter/plugins/blob/master/packages/google_sign_in/google_sign_in_web/test/README.md)** for more information on how to run tests on this package.

Contributions to this package are welcome. Read the [Contributing to Flutter Plugins](https://github.com/flutter/plugins/blob/master/CONTRIBUTING.md) guide to get started.

Expand Down
Expand Up @@ -105,16 +105,17 @@ class GoogleSignInPlugin extends GoogleSignInPlatform {
// state of the authentication, i.e: if you logout elsewhere...

isAuthInitialized.complete();
}), allowInterop((dynamic reason) {
}), allowInterop((auth2.GoogleAuthInitFailureError reason) {
// onError
throw PlatformException(
code: 'google_sign_in',
message: reason.error,
details: reason.details,
);
isAuthInitialized.completeError(PlatformException(
code: reason.error,
message: reason.details,
details:
'https://developers.google.com/identity/sign-in/web/reference#error_codes',
));
}));

return null;
return _isAuthInitialized;
}

@override
Expand All @@ -128,8 +129,16 @@ class GoogleSignInPlugin extends GoogleSignInPlatform {
@override
Future<GoogleSignInUserData> signIn() async {
await initialized;

return gapiUserToPluginUserData(await auth2.getAuthInstance().signIn());
try {
return gapiUserToPluginUserData(await auth2.getAuthInstance().signIn());
} on auth2.GoogleAuthSignInError catch (reason) {
throw PlatformException(
code: reason.error,
message: 'Exception raised from GoogleAuth.signIn()',
details:
'https://developers.google.com/identity/sign-in/web/reference#error_codes_2',
);
}
}

@override
Expand Down
Expand Up @@ -16,6 +16,23 @@ import "package:js/js_util.dart" show promiseToFuture;
/// <reference types="gapi" />
@anonymous
@JS()
class GoogleAuthInitFailureError {
external String get error;
external set error(String value);

external String get details;
external set details(String value);
}

@anonymous
@JS()
class GoogleAuthSignInError {
external String get error;
external set error(String value);
}

// Module gapi.auth2
/// GoogleAuth is a singleton class that provides methods to allow the user to sign in with a Google account,
/// get the user's current sign-in status, get specific data from the user's Google profile,
Expand All @@ -30,7 +47,7 @@ class GoogleAuth {
/// Calls the onInit function when the GoogleAuth object is fully initialized, or calls the onFailure function if
/// initialization fails.
external dynamic then(dynamic onInit(GoogleAuth googleAuth),
[dynamic onFailure(dynamic /*{error: string, details: string}*/ reason)]);
[dynamic onFailure(GoogleAuthInitFailureError reason)]);

/// Signs out all accounts from the application.
external dynamic signOut();
Expand Down
4 changes: 3 additions & 1 deletion packages/google_sign_in/google_sign_in_web/pubspec.yaml
Expand Up @@ -2,7 +2,7 @@ name: google_sign_in_web
description: Flutter plugin for Google Sign-In, a secure authentication system
for signing in with a Google account on Android, iOS and Web.
homepage: https://github.com/flutter/plugins/tree/master/packages/google_sign_in/google_sign_in_web
version: 0.9.1+2
version: 0.9.2

flutter:
plugin:
Expand All @@ -26,6 +26,8 @@ dev_dependencies:
google_sign_in: ^4.0.14
pedantic: ^1.8.0
mockito: ^4.1.1
integration_test:
path: ../../integration_test

environment:
sdk: ">=2.6.0 <3.0.0"
Expand Down
17 changes: 17 additions & 0 deletions packages/google_sign_in/google_sign_in_web/test/README.md
@@ -0,0 +1,17 @@
# Running browser_tests

Make sure you have updated to the latest Flutter master.

1. Check what version of Chrome is running on the machine you're running tests on.

2. Download and install driver for that version from here:
* <https://chromedriver.chromium.org/downloads>

3. Start the driver using `chromedriver --port=4444`

4. Change into the `test` directory of your clone.

5. Run tests: `flutter drive -d web-server --browser-name=chrome --target=test_driver/TEST_NAME_integration.dart`, or (in Linux):

* Single: `./run_test test_driver/TEST_NAME_integration.dart`
* All: `./run_test`
83 changes: 0 additions & 83 deletions packages/google_sign_in/google_sign_in_web/test/auth2_test.dart

This file was deleted.

This file was deleted.

22 changes: 22 additions & 0 deletions packages/google_sign_in/google_sign_in_web/test/lib/main.dart
@@ -0,0 +1,22 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';

void main() {
runApp(MyApp());
}

/// App for testing
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return Text('Testing... Look at the console output for results!');
}
}
24 changes: 24 additions & 0 deletions packages/google_sign_in/google_sign_in_web/test/pubspec.yaml
@@ -0,0 +1,24 @@
name: regular_integration_tests
publish_to: none

environment:
sdk: ">=2.2.2 <3.0.0"

dependencies:
flutter:
sdk: flutter

dev_dependencies:
google_sign_in: ^4.5.3
flutter_driver:
sdk: flutter
flutter_test:
sdk: flutter
http: ^0.12.2
mockito: ^4.1.1
integration_test:
path: ../../../integration_test

dependency_overrides:
google_sign_in_web:
path: ../
17 changes: 17 additions & 0 deletions packages/google_sign_in/google_sign_in_web/test/run_test
@@ -0,0 +1,17 @@
#!/usr/bin/bash
if pgrep -lf chromedriver > /dev/null; then
echo "chromedriver is running."

if [ $# -eq 0 ]; then
echo "No target specified, running all tests..."
find test_driver/ -iname *_integration.dart | xargs -n1 -i -t flutter drive -d web-server --web-port=7357 --browser-name=chrome --target='{}'
else
echo "Running test target: $1..."
set -x
flutter drive -d web-server --web-port=7357 --browser-name=chrome --target=$1
fi

else
echo "chromedriver is not running."
fi

0 comments on commit 56410f5

Please sign in to comment.