From 4d200d1da215a00349138a3f499f72871baa8ff4 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Tue, 23 Feb 2021 12:19:17 -0500 Subject: [PATCH 1/4] [extension_google_sign_in_as_googleapis_auth] NNDB Migrates to NNBD. Replaces Mockito-based fakes with test's Fake. --- .../CHANGELOG.md | 5 ++++ ...ion_google_sign_in_as_googleapis_auth.dart | 15 ++++++---- .../pubspec.yaml | 16 +++++----- ...oogle_sign_in_as_googleapis_auth_test.dart | 30 +++++++++---------- 4 files changed, 38 insertions(+), 28 deletions(-) diff --git a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/CHANGELOG.md b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/CHANGELOG.md index 4afb1a0e98bf..5e29f340599b 100644 --- a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/CHANGELOG.md +++ b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/CHANGELOG.md @@ -1,3 +1,8 @@ +## 2.0.0 + +* Migrate to null safety. +* Fixes the requested scopes to use the `GoogleSignIn` instance's `scopes`. + ## 1.0.4 * Update the example app: remove the deprecated `RaisedButton` and `FlatButton` widgets. diff --git a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/lib/extension_google_sign_in_as_googleapis_auth.dart b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/lib/extension_google_sign_in_as_googleapis_auth.dart index eec45cc0e89a..8c8ede5eee1a 100644 --- a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/lib/extension_google_sign_in_as_googleapis_auth.dart +++ b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/lib/extension_google_sign_in_as_googleapis_auth.dart @@ -15,15 +15,20 @@ import 'package:http/http.dart' as http; /// client that can be used with the rest of the `googleapis` libraries. extension GoogleApisGoogleSignInAuth on GoogleSignIn { /// Retrieve a `googleapis` authenticated client. - Future authenticatedClient({ - @visibleForTesting GoogleSignInAuthentication debugAuthentication, - @visibleForTesting List debugScopes = const [], + Future authenticatedClient({ + @visibleForTesting GoogleSignInAuthentication? debugAuthentication, + @visibleForTesting List? debugScopes, }) async { - final auth = debugAuthentication ?? await currentUser.authentication; + final GoogleSignInAuthentication? auth = + debugAuthentication ?? await currentUser?.authentication; + final String? oathTokenString = auth?.accessToken; + if (oathTokenString == null) { + return null; + } final credentials = googleapis_auth.AccessCredentials( googleapis_auth.AccessToken( 'Bearer', - auth.accessToken, + oathTokenString, // We don't know when the token expires, so we assume "never" DateTime.now().toUtc().add(Duration(days: 365)), ), diff --git a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/pubspec.yaml b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/pubspec.yaml index 9da5f0baa848..6798b7f18e42 100644 --- a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/pubspec.yaml +++ b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/pubspec.yaml @@ -6,23 +6,23 @@ name: extension_google_sign_in_as_googleapis_auth description: A bridge package between google_sign_in and googleapis_auth, to create Authenticated Clients from google_sign_in user credentials. -version: 1.0.4 +version: 2.0.0 homepage: https://github.com/flutter/plugins/google_sign_in/extension_google_sign_in_as_googleapis_auth dependencies: flutter: sdk: flutter - google_sign_in: ^4.4.1 - googleapis_auth: ^0.2.11+1 - meta: ^1.1.8 - http: ^0.12.1 + google_sign_in: ^5.0.0-nullsafety + googleapis_auth: ^1.0.0 + meta: ^1.3.0 + http: ^0.13.0 dev_dependencies: - mockito: ^4.1.1 - pedantic: ^1.9.0 + pedantic: ^1.10.0 + test: ^1.16.3 flutter_test: sdk: flutter environment: - sdk: ">=2.7.0 <3.0.0" + sdk: ">=2.12.0-259.9.beta <3.0.0" flutter: ">=1.12.13+hotfix.4" diff --git a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/test/extension_google_sign_in_as_googleapis_auth_test.dart b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/test/extension_google_sign_in_as_googleapis_auth_test.dart index 9f86703d4bb8..563cf92200a8 100644 --- a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/test/extension_google_sign_in_as_googleapis_auth_test.dart +++ b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/test/extension_google_sign_in_as_googleapis_auth_test.dart @@ -7,25 +7,25 @@ import 'package:google_sign_in/google_sign_in.dart'; import 'package:googleapis_auth/auth.dart' as auth; import 'package:extension_google_sign_in_as_googleapis_auth/extension_google_sign_in_as_googleapis_auth.dart'; -import 'package:mockito/mockito.dart'; import 'package:flutter_test/flutter_test.dart'; - -// Mocks so I don't have to prepare all the GoogleSignIn environment. -class MockGoogleSignIn extends Mock implements GoogleSignIn {} - -class MockGoogleSignInAuthentication extends Mock - implements GoogleSignInAuthentication {} +import 'package:test/fake.dart'; const SOME_FAKE_ACCESS_TOKEN = 'this-is-something-not-null'; const SOME_FAKE_SCOPES = ['some-scope', 'another-scope']; -void main() { - GoogleSignIn signIn = MockGoogleSignIn(); - final authMock = MockGoogleSignInAuthentication(); +// Mocks so I don't have to prepare all the GoogleSignIn environment. +class FakeGoogleSignIn extends Fake implements GoogleSignIn { + final List scopes = []; +} - setUp(() { - when(authMock.accessToken).thenReturn(SOME_FAKE_ACCESS_TOKEN); - }); +class FakeGoogleSignInAuthentication extends Fake + implements GoogleSignInAuthentication { + final String accessToken = SOME_FAKE_ACCESS_TOKEN; +} + +void main() { + GoogleSignIn signIn = FakeGoogleSignIn(); + final authMock = FakeGoogleSignInAuthentication(); test('authenticatedClient returns an authenticated client', () async { final client = await signIn.authenticatedClient( @@ -36,10 +36,10 @@ void main() { test('authenticatedClient returned client contains the passed-in credentials', () async { - final client = await signIn.authenticatedClient( + final client = (await signIn.authenticatedClient( debugAuthentication: authMock, debugScopes: SOME_FAKE_SCOPES, - ); + ))!; expect(client.credentials.accessToken.data, equals(SOME_FAKE_ACCESS_TOKEN)); expect(client.credentials.scopes, equals(SOME_FAKE_SCOPES)); }); From 062b7ffbdc58310e0c39a6b7e99a94eb292fbc97 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Fri, 26 Feb 2021 10:05:35 -0500 Subject: [PATCH 2/4] Comment cleanup --- .../extension_google_sign_in_as_googleapis_auth/pubspec.yaml | 2 +- .../test/extension_google_sign_in_as_googleapis_auth_test.dart | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/pubspec.yaml b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/pubspec.yaml index 6798b7f18e42..7d86b67196d0 100644 --- a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/pubspec.yaml +++ b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/pubspec.yaml @@ -12,7 +12,7 @@ homepage: https://github.com/flutter/plugins/google_sign_in/extension_google_sig dependencies: flutter: sdk: flutter - google_sign_in: ^5.0.0-nullsafety + google_sign_in: ^5.0.0 googleapis_auth: ^1.0.0 meta: ^1.3.0 http: ^0.13.0 diff --git a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/test/extension_google_sign_in_as_googleapis_auth_test.dart b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/test/extension_google_sign_in_as_googleapis_auth_test.dart index 563cf92200a8..6a4103ab080d 100644 --- a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/test/extension_google_sign_in_as_googleapis_auth_test.dart +++ b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/test/extension_google_sign_in_as_googleapis_auth_test.dart @@ -13,7 +13,6 @@ import 'package:test/fake.dart'; const SOME_FAKE_ACCESS_TOKEN = 'this-is-something-not-null'; const SOME_FAKE_SCOPES = ['some-scope', 'another-scope']; -// Mocks so I don't have to prepare all the GoogleSignIn environment. class FakeGoogleSignIn extends Fake implements GoogleSignIn { final List scopes = []; } From 899098daca0c88d0d7105afcbe94a1ec5589fa40 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Fri, 26 Feb 2021 10:12:09 -0500 Subject: [PATCH 3/4] Update example dependencies to allow building --- .../example/lib/main.dart | 3 ++- .../example/pubspec.yaml | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/example/lib/main.dart b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/example/lib/main.dart index 597ab563ae5b..0ec62a832648 100755 --- a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/example/lib/main.dart +++ b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/example/lib/main.dart @@ -56,7 +56,8 @@ class SignInDemoState extends State { _contactText = 'Loading contact info...'; }); - final peopleApi = PeopleApi(await _googleSignIn.authenticatedClient()); + final peopleApi = + PeopleServiceApi(await _googleSignIn.authenticatedClient()); final response = await peopleApi.people.connections.list( 'people/me', personFields: 'names', diff --git a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/example/pubspec.yaml b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/example/pubspec.yaml index 48dfef644a5c..d3b428d190c9 100755 --- a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/example/pubspec.yaml +++ b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/example/pubspec.yaml @@ -4,7 +4,7 @@ description: Example of Google Sign-In plugin and googleapis. dependencies: flutter: sdk: flutter - google_sign_in: ^4.4.1 + google_sign_in: ^5.0.0 extension_google_sign_in_as_googleapis_auth: # When depending on this package from a real application you should use: # extension_google_sign_in_as_googleapis_auth: ^x.y.z @@ -12,10 +12,10 @@ dependencies: # The example app is bundled with the plugin so we use a path dependency on # the parent directory to use the current plugin's version. path: ../ - googleapis: ^0.55.0 + googleapis: ^1.0.0 dev_dependencies: - pedantic: ^1.8.0 + pedantic: ^1.10.0 integration_test: path: ../../../integration_test flutter_driver: From d6ee1f39e886c7179a80130d98bdc7a9cdae08e2 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Fri, 26 Feb 2021 10:22:00 -0500 Subject: [PATCH 4/4] Add test --- ..._google_sign_in_as_googleapis_auth_test.dart | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/test/extension_google_sign_in_as_googleapis_auth_test.dart b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/test/extension_google_sign_in_as_googleapis_auth_test.dart index 6a4103ab080d..508f366eacde 100644 --- a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/test/extension_google_sign_in_as_googleapis_auth_test.dart +++ b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/test/extension_google_sign_in_as_googleapis_auth_test.dart @@ -11,10 +11,11 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:test/fake.dart'; const SOME_FAKE_ACCESS_TOKEN = 'this-is-something-not-null'; -const SOME_FAKE_SCOPES = ['some-scope', 'another-scope']; +const DEBUG_FAKE_SCOPES = ['some-scope', 'another-scope']; +const SIGN_IN_FAKE_SCOPES = ['some-scope', 'another-scope']; class FakeGoogleSignIn extends Fake implements GoogleSignIn { - final List scopes = []; + final List scopes = SIGN_IN_FAKE_SCOPES; } class FakeGoogleSignInAuthentication extends Fake @@ -33,13 +34,21 @@ void main() { expect(client, isA()); }); + test('authenticatedClient uses GoogleSignIn scopes by default', () async { + final client = (await signIn.authenticatedClient( + debugAuthentication: authMock, + ))!; + expect(client.credentials.accessToken.data, equals(SOME_FAKE_ACCESS_TOKEN)); + expect(client.credentials.scopes, equals(SIGN_IN_FAKE_SCOPES)); + }); + test('authenticatedClient returned client contains the passed-in credentials', () async { final client = (await signIn.authenticatedClient( debugAuthentication: authMock, - debugScopes: SOME_FAKE_SCOPES, + debugScopes: DEBUG_FAKE_SCOPES, ))!; expect(client.credentials.accessToken.data, equals(SOME_FAKE_ACCESS_TOKEN)); - expect(client.credentials.scopes, equals(SOME_FAKE_SCOPES)); + expect(client.credentials.scopes, equals(DEBUG_FAKE_SCOPES)); }); }