-
Notifications
You must be signed in to change notification settings - Fork 104
Google Sign In Service #495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8db84be
basic sign in functionality
4647c94
authentication service wip
effccce
moved auth out of widgets
004ab15
move auth service into build state
7b08a6f
auth service, user avatar widget
495490b
added tests
66bc9ab
new line at eof
3b22ad9
removed AuthenticationService interface
ea64d13
update readme
5b924c0
google sign in service test
65ebdab
google scope var
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,9 @@ | ||
| # Build Dashboard v2 Frontend | ||
|
|
||
| [Design Doc](https://flutter.dev/go/build-dashboard-v2) | ||
| [Design Doc](https://flutter.dev/go/build-dashboard-v2) | ||
|
|
||
| ## Running for web locally | ||
|
|
||
| `flutter run -d chrome --web-port=8080` | ||
|
|
||
| Must run on port 8080 for Google Sign In to work since that is the only enabled port for localhost. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // Copyright 2019 The Chromium Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:google_sign_in_all/google_sign_in_all.dart'; | ||
|
|
||
| /// Service class for interacting with Google Sign In authentication for Cocoon backend. | ||
| class GoogleSignInService { | ||
| /// Creates a new [GoogleSignIn]. | ||
| GoogleSignInService({GoogleSignIn googleSignIn}) | ||
| : _googleSignIn = googleSignIn ?? | ||
| setupGoogleSignIn( | ||
| scopes: <String>[ | ||
| 'https://www.googleapis.com/auth/userinfo.email', | ||
| 'https://www.googleapis.com/auth/userinfo.profile', | ||
| ], | ||
| webClientId: | ||
| '308150028417-vlj9mqlm3gk1d03fb0efif1fu5nagdtt.apps.googleusercontent.com', | ||
| ); | ||
|
|
||
| // TODO(chillers): Switch to official Flutter plugin when it supports web. | ||
| final GoogleSignIn _googleSignIn; | ||
|
|
||
| AuthCredentials _credentials; | ||
|
|
||
| GoogleAccount _user; | ||
|
|
||
| /// Whether or not the application has been signed in to. | ||
| bool get isAuthenticated => _credentials?.accessToken != null; | ||
|
|
||
| /// The profile photo url of the current user signed in. | ||
| String get avatarUrl => _user?.photoUrl; | ||
|
|
||
| /// The email of the current user signed in. | ||
| String get email => _user?.email; | ||
|
|
||
| /// Authentication token to be sent to Cocoon Backend to verify API calls. | ||
| String get accessToken => _credentials?.accessToken; | ||
|
|
||
| /// Initiate the Google Sign In process. | ||
| Future<void> signIn() async { | ||
| _credentials = await _googleSignIn.signIn(); | ||
| _user = await _googleSignIn.getCurrentUser(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // Copyright (c) 2019 The Chromium Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:mockito/mockito.dart'; | ||
| import 'package:test/test.dart' as test; | ||
|
|
||
| import 'package:app_flutter/build_dashboard.dart'; | ||
| import 'package:app_flutter/service/google_authentication.dart'; | ||
| import 'package:app_flutter/service/fake_cocoon.dart'; | ||
| import 'package:app_flutter/state/flutter_build.dart'; | ||
|
|
||
| void main() { | ||
| group('UserAvatar', () { | ||
| GoogleSignInService authService; | ||
|
|
||
| setUp(() { | ||
| authService = MockGoogleSignInService(); | ||
| }); | ||
|
|
||
| testWidgets('shows sign in button when not signed in', | ||
| (WidgetTester tester) async { | ||
| when(authService.isAuthenticated).thenReturn(false); | ||
| final FlutterBuildState buildState = FlutterBuildState( | ||
| authService: authService, cocoonService: FakeCocoonService()); | ||
| await tester.pumpWidget(MaterialApp( | ||
| home: UserAvatar( | ||
| buildState: buildState, | ||
| ), | ||
| )); | ||
|
|
||
| expect(find.text('Sign in'), findsOneWidget); | ||
| }); | ||
|
|
||
| testWidgets('sign in button activates google sign in when pressed', | ||
| (WidgetTester tester) async { | ||
| when(authService.isAuthenticated).thenReturn(false); | ||
| final FlutterBuildState buildState = FlutterBuildState( | ||
| authService: authService, cocoonService: FakeCocoonService()); | ||
| await tester.pumpWidget(MaterialApp( | ||
| home: UserAvatar( | ||
| buildState: buildState, | ||
| ), | ||
| )); | ||
|
|
||
| verifyNever(authService.signIn()); | ||
|
|
||
| await tester.tap(find.byType(UserAvatar)); | ||
|
|
||
| verify(authService.signIn()).called(1); | ||
| }); | ||
|
|
||
| testWidgets('shows user avatar when signed in', | ||
| (WidgetTester tester) async { | ||
| when(authService.isAuthenticated).thenReturn(true); | ||
| when(authService.avatarUrl).thenReturn('https://flutter.dev'); | ||
| final FlutterBuildState buildState = FlutterBuildState( | ||
| authService: authService, cocoonService: FakeCocoonService()); | ||
| await tester.pumpWidget(MaterialApp( | ||
| home: UserAvatar( | ||
| buildState: buildState, | ||
| ), | ||
| )); | ||
|
|
||
| expect(tester.takeException(), | ||
| const test.TypeMatcher<NetworkImageLoadException>()); | ||
| expect(find.byType(Image), findsOneWidget); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| /// Mock [GoogleSignInService] for testing interactions. | ||
| class MockGoogleSignInService extends Mock implements GoogleSignInService {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // Copyright 2019 The Chromium Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:google_sign_in_all/google_sign_in_all.dart'; | ||
| import 'package:mockito/mockito.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| import 'package:app_flutter/service/google_authentication.dart'; | ||
|
|
||
| void main() { | ||
| group('GoogleSignInService not signed in', () { | ||
| GoogleSignInService authService; | ||
|
|
||
| setUp(() { | ||
| authService = GoogleSignInService(googleSignIn: MockGoogleSignIn()); | ||
| }); | ||
|
|
||
| test('not authenticated', () { | ||
| expect(authService.isAuthenticated, false); | ||
| }); | ||
|
|
||
| test('no user information', () { | ||
| expect(authService.avatarUrl, null); | ||
| expect(authService.email, null); | ||
| expect(authService.accessToken, null); | ||
| }); | ||
| }); | ||
|
|
||
| group('GoogleSignInService sign in', () { | ||
| GoogleSignInService authService; | ||
| GoogleSignIn mockSignIn; | ||
|
|
||
| setUp(() { | ||
| mockSignIn = MockGoogleSignIn(); | ||
| final AuthCredentials fakeCredentials = FakeAuthCredentials(); | ||
| when(mockSignIn.signIn()) | ||
| .thenAnswer((_) => Future<AuthCredentials>.value(fakeCredentials)); | ||
| when(mockSignIn.getCurrentUser()).thenAnswer((_) => | ||
| Future<GoogleAccount>.value(GoogleAccount( | ||
| email: 'fake@fake.com', photoUrl: 'fake://fake.png'))); | ||
|
|
||
| authService = GoogleSignInService(googleSignIn: mockSignIn); | ||
| }); | ||
|
|
||
| test('is authenticated after successful sign in', () async { | ||
| await authService.signIn(); | ||
|
|
||
| expect(authService.isAuthenticated, true); | ||
| }); | ||
|
|
||
| test('there is user information after successful sign in', () async { | ||
| await authService.signIn(); | ||
|
|
||
| expect(authService.email, 'fake@fake.com'); | ||
| expect(authService.avatarUrl, 'fake://fake.png'); | ||
| expect(authService.accessToken, 'fake'); | ||
| }); | ||
|
|
||
| test('is not authenticated after failure in sign in', () async { | ||
| when(mockSignIn.signIn()) | ||
| .thenAnswer((_) => Future<AuthCredentials>.value(null)); | ||
| when(mockSignIn.getCurrentUser()) | ||
| .thenAnswer((_) => Future<GoogleAccount>.value(null)); | ||
|
|
||
| await authService.signIn(); | ||
|
|
||
| expect(authService.isAuthenticated, false); | ||
| expect(authService.email, null); | ||
| expect(authService.avatarUrl, null); | ||
| expect(authService.accessToken, null); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| /// Mock [GoogleSignIn] for testing interactions. | ||
| class MockGoogleSignIn extends Mock implements GoogleSignIn {} | ||
|
|
||
| /// Fake [AuthCredentials] for [MockGoogleSignIn]. | ||
| class FakeAuthCredentials implements AuthCredentials { | ||
| @override | ||
| final String accessToken = 'fake'; | ||
|
|
||
| @override | ||
| final String idToken = 'faker'; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe hoist these into private consts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added