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

Commit aead5ac

Browse files
[extension_google_sign_in_as_googleapis_auth] Migrate to null safety (#3642)
Migrates to NNBD. Replaces Mockito-based fakes with test's Fake.
1 parent ad650f9 commit aead5ac

File tree

6 files changed

+53
-34
lines changed

6 files changed

+53
-34
lines changed

packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.0.0
2+
3+
* Migrate to null safety.
4+
* Fixes the requested scopes to use the `GoogleSignIn` instance's `scopes`.
5+
16
## 1.0.4
27

38
* Update the example app: remove the deprecated `RaisedButton` and `FlatButton` widgets.

packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/example/lib/main.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ class SignInDemoState extends State<SignInDemo> {
5656
_contactText = 'Loading contact info...';
5757
});
5858

59-
final peopleApi = PeopleApi(await _googleSignIn.authenticatedClient());
59+
final peopleApi =
60+
PeopleServiceApi(await _googleSignIn.authenticatedClient());
6061
final response = await peopleApi.people.connections.list(
6162
'people/me',
6263
personFields: 'names',

packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/example/pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ description: Example of Google Sign-In plugin and googleapis.
44
dependencies:
55
flutter:
66
sdk: flutter
7-
google_sign_in: ^4.4.1
7+
google_sign_in: ^5.0.0
88
extension_google_sign_in_as_googleapis_auth:
99
# When depending on this package from a real application you should use:
1010
# extension_google_sign_in_as_googleapis_auth: ^x.y.z
1111
# See https://dart.dev/tools/pub/dependencies#version-constraints
1212
# The example app is bundled with the plugin so we use a path dependency on
1313
# the parent directory to use the current plugin's version.
1414
path: ../
15-
googleapis: ^0.55.0
15+
googleapis: ^1.0.0
1616

1717
dev_dependencies:
18-
pedantic: ^1.8.0
18+
pedantic: ^1.10.0
1919
integration_test:
2020
path: ../../../integration_test
2121
flutter_driver:

packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/lib/extension_google_sign_in_as_googleapis_auth.dart

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,20 @@ import 'package:http/http.dart' as http;
1515
/// client that can be used with the rest of the `googleapis` libraries.
1616
extension GoogleApisGoogleSignInAuth on GoogleSignIn {
1717
/// Retrieve a `googleapis` authenticated client.
18-
Future<googleapis_auth.AuthClient> authenticatedClient({
19-
@visibleForTesting GoogleSignInAuthentication debugAuthentication,
20-
@visibleForTesting List<String> debugScopes = const [],
18+
Future<googleapis_auth.AuthClient?> authenticatedClient({
19+
@visibleForTesting GoogleSignInAuthentication? debugAuthentication,
20+
@visibleForTesting List<String>? debugScopes,
2121
}) async {
22-
final auth = debugAuthentication ?? await currentUser.authentication;
22+
final GoogleSignInAuthentication? auth =
23+
debugAuthentication ?? await currentUser?.authentication;
24+
final String? oathTokenString = auth?.accessToken;
25+
if (oathTokenString == null) {
26+
return null;
27+
}
2328
final credentials = googleapis_auth.AccessCredentials(
2429
googleapis_auth.AccessToken(
2530
'Bearer',
26-
auth.accessToken,
31+
oathTokenString,
2732
// We don't know when the token expires, so we assume "never"
2833
DateTime.now().toUtc().add(Duration(days: 365)),
2934
),

packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/pubspec.yaml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@
66

77
name: extension_google_sign_in_as_googleapis_auth
88
description: A bridge package between google_sign_in and googleapis_auth, to create Authenticated Clients from google_sign_in user credentials.
9-
version: 1.0.4
9+
version: 2.0.0
1010
homepage: https://github.com/flutter/plugins/google_sign_in/extension_google_sign_in_as_googleapis_auth
1111

1212
dependencies:
1313
flutter:
1414
sdk: flutter
15-
google_sign_in: ^4.4.1
16-
googleapis_auth: ^0.2.11+1
17-
meta: ^1.1.8
18-
http: ^0.12.1
15+
google_sign_in: ^5.0.0
16+
googleapis_auth: ^1.0.0
17+
meta: ^1.3.0
18+
http: ^0.13.0
1919

2020
dev_dependencies:
21-
mockito: ^4.1.1
22-
pedantic: ^1.9.0
21+
pedantic: ^1.10.0
22+
test: ^1.16.3
2323
flutter_test:
2424
sdk: flutter
2525

2626
environment:
27-
sdk: ">=2.7.0 <3.0.0"
27+
sdk: ">=2.12.0-259.9.beta <3.0.0"
2828
flutter: ">=1.12.13+hotfix.4"

packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/test/extension_google_sign_in_as_googleapis_auth_test.dart

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@
77
import 'package:google_sign_in/google_sign_in.dart';
88
import 'package:googleapis_auth/auth.dart' as auth;
99
import 'package:extension_google_sign_in_as_googleapis_auth/extension_google_sign_in_as_googleapis_auth.dart';
10-
import 'package:mockito/mockito.dart';
1110
import 'package:flutter_test/flutter_test.dart';
11+
import 'package:test/fake.dart';
1212

13-
// Mocks so I don't have to prepare all the GoogleSignIn environment.
14-
class MockGoogleSignIn extends Mock implements GoogleSignIn {}
13+
const SOME_FAKE_ACCESS_TOKEN = 'this-is-something-not-null';
14+
const DEBUG_FAKE_SCOPES = <String>['some-scope', 'another-scope'];
15+
const SIGN_IN_FAKE_SCOPES = <String>['some-scope', 'another-scope'];
1516

16-
class MockGoogleSignInAuthentication extends Mock
17-
implements GoogleSignInAuthentication {}
17+
class FakeGoogleSignIn extends Fake implements GoogleSignIn {
18+
final List<String> scopes = SIGN_IN_FAKE_SCOPES;
19+
}
1820

19-
const SOME_FAKE_ACCESS_TOKEN = 'this-is-something-not-null';
20-
const SOME_FAKE_SCOPES = ['some-scope', 'another-scope'];
21+
class FakeGoogleSignInAuthentication extends Fake
22+
implements GoogleSignInAuthentication {
23+
final String accessToken = SOME_FAKE_ACCESS_TOKEN;
24+
}
2125

2226
void main() {
23-
GoogleSignIn signIn = MockGoogleSignIn();
24-
final authMock = MockGoogleSignInAuthentication();
25-
26-
setUp(() {
27-
when(authMock.accessToken).thenReturn(SOME_FAKE_ACCESS_TOKEN);
28-
});
27+
GoogleSignIn signIn = FakeGoogleSignIn();
28+
final authMock = FakeGoogleSignInAuthentication();
2929

3030
test('authenticatedClient returns an authenticated client', () async {
3131
final client = await signIn.authenticatedClient(
@@ -34,13 +34,21 @@ void main() {
3434
expect(client, isA<auth.AuthClient>());
3535
});
3636

37+
test('authenticatedClient uses GoogleSignIn scopes by default', () async {
38+
final client = (await signIn.authenticatedClient(
39+
debugAuthentication: authMock,
40+
))!;
41+
expect(client.credentials.accessToken.data, equals(SOME_FAKE_ACCESS_TOKEN));
42+
expect(client.credentials.scopes, equals(SIGN_IN_FAKE_SCOPES));
43+
});
44+
3745
test('authenticatedClient returned client contains the passed-in credentials',
3846
() async {
39-
final client = await signIn.authenticatedClient(
47+
final client = (await signIn.authenticatedClient(
4048
debugAuthentication: authMock,
41-
debugScopes: SOME_FAKE_SCOPES,
42-
);
49+
debugScopes: DEBUG_FAKE_SCOPES,
50+
))!;
4351
expect(client.credentials.accessToken.data, equals(SOME_FAKE_ACCESS_TOKEN));
44-
expect(client.credentials.scopes, equals(SOME_FAKE_SCOPES));
52+
expect(client.credentials.scopes, equals(DEBUG_FAKE_SCOPES));
4553
});
4654
}

0 commit comments

Comments
 (0)