mirror of
https://github.com/flutter/packages.git
synced 2025-06-30 23:03:11 +08:00
[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:
@ -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.
|
||||
|
@ -56,7 +56,8 @@ class SignInDemoState extends State<SignInDemo> {
|
||||
_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',
|
||||
|
@ -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:
|
||||
|
@ -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<googleapis_auth.AuthClient> authenticatedClient({
|
||||
@visibleForTesting GoogleSignInAuthentication debugAuthentication,
|
||||
@visibleForTesting List<String> debugScopes = const [],
|
||||
Future<googleapis_auth.AuthClient?> authenticatedClient({
|
||||
@visibleForTesting GoogleSignInAuthentication? debugAuthentication,
|
||||
@visibleForTesting List<String>? 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)),
|
||||
),
|
||||
|
16
pubspec.yaml
16
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
|
||||
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"
|
||||
|
@ -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'];
|
||||
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() {
|
||||
GoogleSignIn signIn = MockGoogleSignIn();
|
||||
final authMock = MockGoogleSignInAuthentication();
|
||||
|
||||
setUp(() {
|
||||
when(authMock.accessToken).thenReturn(SOME_FAKE_ACCESS_TOKEN);
|
||||
});
|
||||
GoogleSignIn signIn = FakeGoogleSignIn();
|
||||
final authMock = FakeGoogleSignInAuthentication();
|
||||
|
||||
test('authenticatedClient returns an authenticated client', () async {
|
||||
final client = await signIn.authenticatedClient(
|
||||
@ -34,13 +34,21 @@ void main() {
|
||||
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',
|
||||
() async {
|
||||
final client = await signIn.authenticatedClient(
|
||||
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));
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user