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

Commit

Permalink
[extension_google_sign_in_as_googleapis_auth] Migrate to null safety (#…
Browse files Browse the repository at this point in the history
…3642)

Migrates to NNBD.
Replaces Mockito-based fakes with test's Fake.
  • Loading branch information
stuartmorgan authored Feb 26, 2021
1 parent ad650f9 commit aead5ac
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ 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
# See https://dart.dev/tools/pub/dependencies#version-constraints
# 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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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';
import 'package:test/fake.dart';

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

class MockGoogleSignInAuthentication extends Mock
implements GoogleSignInAuthentication {}
class FakeGoogleSignIn extends Fake implements GoogleSignIn {
final List<String> scopes = SIGN_IN_FAKE_SCOPES;
}

const SOME_FAKE_ACCESS_TOKEN = 'this-is-something-not-null';
const SOME_FAKE_SCOPES = ['some-scope', 'another-scope'];
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(
Expand All @@ -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));
});
}

0 comments on commit aead5ac

Please sign in to comment.