[extension_google_sign_in_as_googleapis_auth] Migrate to null safety (#3642)

Migrates to NNBD.
Replaces Mockito-based fakes with test's Fake.
This commit is contained in:
stuartmorgan
2021-02-26 10:19:07 -08:00
committed by GitHub
parent 0621213616
commit abb8933c25
6 changed files with 54 additions and 35 deletions

View File

@ -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 ## 1.0.4
* Update the example app: remove the deprecated `RaisedButton` and `FlatButton` widgets. * Update the example app: remove the deprecated `RaisedButton` and `FlatButton` widgets.

View File

@ -56,7 +56,8 @@ class SignInDemoState extends State<SignInDemo> {
_contactText = 'Loading contact info...'; _contactText = 'Loading contact info...';
}); });
final peopleApi = PeopleApi(await _googleSignIn.authenticatedClient()); final peopleApi =
PeopleServiceApi(await _googleSignIn.authenticatedClient());
final response = await peopleApi.people.connections.list( final response = await peopleApi.people.connections.list(
'people/me', 'people/me',
personFields: 'names', personFields: 'names',

View File

@ -4,7 +4,7 @@ description: Example of Google Sign-In plugin and googleapis.
dependencies: dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
google_sign_in: ^4.4.1 google_sign_in: ^5.0.0
extension_google_sign_in_as_googleapis_auth: extension_google_sign_in_as_googleapis_auth:
# When depending on this package from a real application you should use: # When depending on this package from a real application you should use:
# extension_google_sign_in_as_googleapis_auth: ^x.y.z # 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 example app is bundled with the plugin so we use a path dependency on
# the parent directory to use the current plugin's version. # the parent directory to use the current plugin's version.
path: ../ path: ../
googleapis: ^0.55.0 googleapis: ^1.0.0
dev_dependencies: dev_dependencies:
pedantic: ^1.8.0 pedantic: ^1.10.0
integration_test: integration_test:
path: ../../../integration_test path: ../../../integration_test
flutter_driver: flutter_driver:

View File

@ -15,15 +15,20 @@ import 'package:http/http.dart' as http;
/// client that can be used with the rest of the `googleapis` libraries. /// client that can be used with the rest of the `googleapis` libraries.
extension GoogleApisGoogleSignInAuth on GoogleSignIn { extension GoogleApisGoogleSignInAuth on GoogleSignIn {
/// Retrieve a `googleapis` authenticated client. /// Retrieve a `googleapis` authenticated client.
Future<googleapis_auth.AuthClient> authenticatedClient({ Future<googleapis_auth.AuthClient?> authenticatedClient({
@visibleForTesting GoogleSignInAuthentication debugAuthentication, @visibleForTesting GoogleSignInAuthentication? debugAuthentication,
@visibleForTesting List<String> debugScopes = const [], @visibleForTesting List<String>? debugScopes,
}) async { }) 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( final credentials = googleapis_auth.AccessCredentials(
googleapis_auth.AccessToken( googleapis_auth.AccessToken(
'Bearer', 'Bearer',
auth.accessToken, oathTokenString,
// We don't know when the token expires, so we assume "never" // We don't know when the token expires, so we assume "never"
DateTime.now().toUtc().add(Duration(days: 365)), DateTime.now().toUtc().add(Duration(days: 365)),
), ),

View File

@ -6,23 +6,23 @@
name: extension_google_sign_in_as_googleapis_auth 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. 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 homepage: https://github.com/flutter/plugins/google_sign_in/extension_google_sign_in_as_googleapis_auth
dependencies: dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
google_sign_in: ^4.4.1 google_sign_in: ^5.0.0
googleapis_auth: ^0.2.11+1 googleapis_auth: ^1.0.0
meta: ^1.1.8 meta: ^1.3.0
http: ^0.12.1 http: ^0.13.0
dev_dependencies: dev_dependencies:
mockito: ^4.1.1 pedantic: ^1.10.0
pedantic: ^1.9.0 test: ^1.16.3
flutter_test: flutter_test:
sdk: flutter sdk: flutter
environment: environment:
sdk: ">=2.7.0 <3.0.0" sdk: ">=2.12.0-259.9.beta <3.0.0"
flutter: ">=1.12.13+hotfix.4" flutter: ">=1.12.13+hotfix.4"

View File

@ -7,25 +7,25 @@
import 'package:google_sign_in/google_sign_in.dart'; import 'package:google_sign_in/google_sign_in.dart';
import 'package:googleapis_auth/auth.dart' as auth; 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: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'; import 'package:flutter_test/flutter_test.dart';
import 'package:test/fake.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 {}
const SOME_FAKE_ACCESS_TOKEN = 'this-is-something-not-null'; const SOME_FAKE_ACCESS_TOKEN = 'this-is-something-not-null';
const SOME_FAKE_SCOPES = ['some-scope', 'another-scope']; const DEBUG_FAKE_SCOPES = <String>['some-scope', 'another-scope'];
const SIGN_IN_FAKE_SCOPES = <String>['some-scope', 'another-scope'];
class FakeGoogleSignIn extends Fake implements GoogleSignIn {
final List<String> scopes = SIGN_IN_FAKE_SCOPES;
}
class FakeGoogleSignInAuthentication extends Fake
implements GoogleSignInAuthentication {
final String accessToken = SOME_FAKE_ACCESS_TOKEN;
}
void main() { void main() {
GoogleSignIn signIn = MockGoogleSignIn(); GoogleSignIn signIn = FakeGoogleSignIn();
final authMock = MockGoogleSignInAuthentication(); final authMock = FakeGoogleSignInAuthentication();
setUp(() {
when(authMock.accessToken).thenReturn(SOME_FAKE_ACCESS_TOKEN);
});
test('authenticatedClient returns an authenticated client', () async { test('authenticatedClient returns an authenticated client', () async {
final client = await signIn.authenticatedClient( final client = await signIn.authenticatedClient(
@ -34,13 +34,21 @@ void main() {
expect(client, isA<auth.AuthClient>()); expect(client, isA<auth.AuthClient>());
}); });
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', test('authenticatedClient returned client contains the passed-in credentials',
() async { () async {
final client = await signIn.authenticatedClient( final client = (await signIn.authenticatedClient(
debugAuthentication: authMock, debugAuthentication: authMock,
debugScopes: SOME_FAKE_SCOPES, debugScopes: DEBUG_FAKE_SCOPES,
); ))!;
expect(client.credentials.accessToken.data, equals(SOME_FAKE_ACCESS_TOKEN)); 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));
}); });
} }