diff --git a/packages/flutter_auth_core/lib/src/flutter_auth.dart b/packages/flutter_auth_core/lib/src/flutter_auth.dart index 6861b72..c02acd1 100644 --- a/packages/flutter_auth_core/lib/src/flutter_auth.dart +++ b/packages/flutter_auth_core/lib/src/flutter_auth.dart @@ -1,11 +1,7 @@ -import 'package:flutter_auth_core/src/utils/flutter_auth_exception_code.dart'; -import 'package:flutter_auth_core/src/utils/flutter_auth_exception_message.dart'; import 'package:flutter_webview_plugin/flutter_webview_plugin.dart'; -import 'utils/flutter_auth_webview.dart'; -import 'package:meta/meta.dart' show required, visibleForOverriding; +import 'package:meta/meta.dart' show visibleForOverriding; import 'package:flutter/material.dart'; -import 'package:flutter/widgets.dart'; import 'package:flutter_auth_core/flutter_auth_core.dart'; // ignore: public_member_api_docs @@ -14,21 +10,19 @@ class FlutterAuth { final String clientSecret; final String callbackUrl; final bool clearCache; - final String userAgent; + final String? userAgent; /// Returns an instance of [FlutterAuth]. - FlutterAuth( - {@required this.clientId, - @required this.clientSecret, - @required this.callbackUrl, - this.clearCache = false, - this.userAgent}) { - assert(clientId != null && clientId.isNotEmpty, - 'ClientId may not be null or empty.'); - assert(clientSecret != null && clientSecret.isNotEmpty, - 'ClientSecret may not be null or empty.'); - assert(callbackUrl != null && callbackUrl.isNotEmpty, - 'CallbackUrl may not be null or empty.'); + FlutterAuth({ + required this.clientId, + required this.clientSecret, + required this.callbackUrl, + this.clearCache = false, + this.userAgent, + }) { + assert(clientId.isNotEmpty, 'ClientId may not be empty.'); + assert(clientSecret.isNotEmpty, 'ClientSecret may not be empty.'); + assert(callbackUrl.isNotEmpty, 'CallbackUrl may not be empty.'); } @visibleForOverriding @@ -50,8 +44,9 @@ class FlutterAuth { @visibleForTesting // ignore: public_member_api_docs Future openLoginPageWithWebview( - BuildContext context, String url) async { - assert(context != null && url != null && url.isNotEmpty); + BuildContext? context, String url) async { + assert(context != null); + assert(url.isNotEmpty); var authorizedResult; try { @@ -73,14 +68,17 @@ class FlutterAuth { @visibleForTesting // ignore: public_member_api_docs - Future navigateToWebview(BuildContext context, String url) async { - return Navigator.of(context).push(MaterialPageRoute( + Future navigateToWebview(BuildContext? context, String url) async { + return Navigator.of(context!).push( + MaterialPageRoute( builder: (context) => FlutterAuthWebview( - url: url, - redirectUrl: callbackUrl, - userAgent: userAgent, - clearCache: clearCache, - ))); + url: url, + redirectUrl: callbackUrl, + userAgent: userAgent, + clearCache: clearCache, + ), + ), + ); } @visibleForTesting diff --git a/packages/flutter_auth_core/lib/src/flutter_auth_exception.dart b/packages/flutter_auth_core/lib/src/flutter_auth_exception.dart index 8e282bf..2290b5d 100644 --- a/packages/flutter_auth_core/lib/src/flutter_auth_exception.dart +++ b/packages/flutter_auth_core/lib/src/flutter_auth_exception.dart @@ -9,10 +9,10 @@ class FlutterAuthException { FlutterAuthException({this.code, this.message, this.details}); /// The error code - final FlutterAuthExceptionCode code; + final FlutterAuthExceptionCode? code; /// Description of the error thrown - final String message; + final String? message; /// Additional details of the error thrown final dynamic details; diff --git a/packages/flutter_auth_core/lib/src/flutter_auth_result.dart b/packages/flutter_auth_core/lib/src/flutter_auth_result.dart index 5538f2b..5746674 100644 --- a/packages/flutter_auth_core/lib/src/flutter_auth_result.dart +++ b/packages/flutter_auth_core/lib/src/flutter_auth_result.dart @@ -8,10 +8,10 @@ class FlutterAuthResult { FlutterAuthResult({this.token, this.secret}) : assert(token != null); /// The token obtained after the user has successfully logged in. - final String token; + final String? token; /// The secret obtained after the user has successfully logged in. - final String secret; + final String? secret; /// Returns the current instance as a [Map]. Map get asMap { diff --git a/packages/flutter_auth_core/lib/src/utils/flutter_auth_webview.dart b/packages/flutter_auth_core/lib/src/utils/flutter_auth_webview.dart index 01caefc..ada3d04 100644 --- a/packages/flutter_auth_core/lib/src/utils/flutter_auth_webview.dart +++ b/packages/flutter_auth_core/lib/src/utils/flutter_auth_webview.dart @@ -9,13 +9,13 @@ class FlutterAuthWebview extends StatefulWidget { final String redirectUrl; final bool clearCache; final String title; - final String userAgent; + final String? userAgent; // ignore: public_member_api_docs const FlutterAuthWebview( - {Key key, - @required this.url, - @required this.redirectUrl, + {Key? key, + required this.url, + required this.redirectUrl, this.userAgent, this.clearCache = true, this.title = ""}) @@ -29,9 +29,9 @@ class _FlutterAuthWebviewState extends State { final FlutterWebviewPlugin _wv = FlutterWebviewPlugin(); // On urlChanged stream - StreamSubscription _onUrlChanged; + late StreamSubscription _onUrlChanged; - StreamSubscription _onHttpError; + late StreamSubscription _onHttpError; static const String _userAgentMacOSX = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"; diff --git a/packages/flutter_auth_core/pubspec.yaml b/packages/flutter_auth_core/pubspec.yaml index 939095b..468cc95 100644 --- a/packages/flutter_auth_core/pubspec.yaml +++ b/packages/flutter_auth_core/pubspec.yaml @@ -4,19 +4,19 @@ version: 0.0.4 homepage: https://github.com/StackTiger/flutter_auth/tree/master/packages/flutter_auth_core environment: - sdk: ">=2.7.0 <3.0.0" + sdk: ">=2.16.0 <3.0.0" dependencies: flutter: sdk: flutter - quiver: ">=2.0.0 <3.0.0" - meta: ^1.1.8 - url_launcher: ^5.5.0 - http: ^0.12.2 - oauth1: ^1.0.4 - flutter_webview_plugin: ^0.3.11 + flutter_webview_plugin: ^0.4.0 + http: ^0.13.4 + meta: ^1.7.0 + oauth1: ^2.0.0 + quiver: ^3.0.1+1 + url_launcher: ^6.0.20 dev_dependencies: flutter_test: sdk: flutter - mockito: ^4.1.1 + mockito: ^5.1.0 diff --git a/packages/flutter_auth_core/test/flutter_auth_test.dart b/packages/flutter_auth_core/test/flutter_auth_test.dart index b3770f8..379de52 100644 --- a/packages/flutter_auth_core/test/flutter_auth_test.dart +++ b/packages/flutter_auth_core/test/flutter_auth_test.dart @@ -14,7 +14,7 @@ const kAuthorizedResultUrl = 'test-authorized-result-url'; BuildContext kMockBuildContext = MockBuildContext(); void main() { - TestFlutterAuth testFlutterAuth; + late TestFlutterAuth testFlutterAuth; TestWidgetsFlutterBinding.ensureInitialized(); setUp(() { @@ -45,31 +45,31 @@ void main() { try { FlutterAuth( callbackUrl: kCallbackUrl, - clientId: null, + clientId: '', clientSecret: kClientSecret); } on AssertionError catch (e) { - expect(e.message, 'ClientId may not be null or empty.'); + expect(e.message, 'ClientId may not be empty.'); } }); test('throws AssertionError if clientSecret is null', () { try { FlutterAuth( - callbackUrl: kCallbackUrl, clientId: kClientId, clientSecret: null); + callbackUrl: kCallbackUrl, clientId: kClientId, clientSecret: ''); } on AssertionError catch (e) { - expect(e.message, 'ClientSecret may not be null or empty.'); + expect(e.message, 'ClientSecret may not be empty.'); } }); test('throws AssertionError if callbackUrl is null', () { try { FlutterAuth( - callbackUrl: null, + callbackUrl: '', clientId: kClientId, clientSecret: kClientSecret, ); } on AssertionError catch (e) { - expect(e.message, 'CallbackUrl may not be null or empty.'); + expect(e.message, 'CallbackUrl may not be empty.'); } }); }); @@ -111,7 +111,7 @@ void main() { test('throws AssertionError if url is null', () { expect( () => - testFlutterAuth.openLoginPageWithWebview(kMockBuildContext, null), + testFlutterAuth.openLoginPageWithWebview(kMockBuildContext, ''), throwsAssertionError); }); @@ -135,7 +135,7 @@ class TestFlutterAuth extends FlutterAuth { userAgent: kUserAgent); @override - navigateToWebview(BuildContext context, String url) { + navigateToWebview(BuildContext? context, String url) { return Future.value('mock-url'); } diff --git a/packages/github_auth/example/android/app/src/main/AndroidManifest.xml b/packages/github_auth/example/android/app/src/main/AndroidManifest.xml index 86fd5aa..926a1e1 100644 --- a/packages/github_auth/example/android/app/src/main/AndroidManifest.xml +++ b/packages/github_auth/example/android/app/src/main/AndroidManifest.xml @@ -6,7 +6,7 @@ additional functionality it is fine to subclass or reimplement FlutterApplication and put your custom class here. --> =2.1.0 <3.0.0" + sdk: ">=2.16.0 <3.0.0" diff --git a/packages/github_auth/example/test_driver/app_test.dart b/packages/github_auth/example/test_driver/app_test.dart index c1a756d..95780f9 100644 --- a/packages/github_auth/example/test_driver/app_test.dart +++ b/packages/github_auth/example/test_driver/app_test.dart @@ -1,9 +1,8 @@ +import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_driver/flutter_driver.dart'; -import 'package:test/test.dart'; - void main() { group('Github Auth App', () { - final titleTextFinder = find.byValueKey('title'); + final titleTextFinder = find.text('title'); FlutterDriver driver; diff --git a/packages/github_auth/lib/src/github_api_error.dart b/packages/github_auth/lib/src/github_api_error.dart index d1b9aa6..0d4758c 100644 --- a/packages/github_auth/lib/src/github_api_error.dart +++ b/packages/github_auth/lib/src/github_api_error.dart @@ -1,9 +1,9 @@ // https://docs.github.com/en/free-pro-team@latest/developers/apps/authorizing-oauth-apps#error-codes-for-the-device-flow class GithubAPIError { - String code; - String message; - String uri; + String? code; + String? message; + String? uri; GithubAPIError({this.code, this.message, this.uri}); diff --git a/packages/github_auth/lib/src/github_auth.dart b/packages/github_auth/lib/src/github_auth.dart index 4fef8b7..193be0b 100644 --- a/packages/github_auth/lib/src/github_auth.dart +++ b/packages/github_auth/lib/src/github_auth.dart @@ -34,20 +34,25 @@ class GithubAuth extends FlutterAuth { final bool clearCache; /// The user agent to be used for the Webview - final String userAgent; + final String? userAgent; @visibleForTesting // ignore: public_member_api_docs final client = http.Client(); GithubAuth( - {@required this.clientId, - @required this.clientSecret, - @required this.callbackUrl, + {required this.clientId, + required this.clientSecret, + required this.callbackUrl, this.scope = "user,gist,user:email", this.allowSignUp = true, this.clearCache = false, - this.userAgent}); + this.userAgent}) + : super( + clientId: clientId, + clientSecret: clientSecret, + callbackUrl: callbackUrl, + ); @visibleForTesting @override @@ -55,7 +60,7 @@ class GithubAuth extends FlutterAuth { Future loginComplete(Uri authorizedResultUrl) async { FlutterAuthResult result; // exchange for access token - String code = authorizedResultUrl.queryParameters[kCodeConstant]; + String? code = authorizedResultUrl.queryParameters[kCodeConstant]; if (code == null || code.isEmpty) { throw FlutterAuthException( @@ -78,7 +83,8 @@ class GithubAuth extends FlutterAuth { @visibleForTesting Future getAccessToken(String code) async { - var response = await client.post("$kApiEndpointAccessToken", headers: { + http.Response response = + await client.post(Uri.parse(kApiEndpointAccessToken), headers: { kAcceptConstant: kAcceptJsonConstant, }, body: { kClientIdConstant: clientId, @@ -87,7 +93,7 @@ class GithubAuth extends FlutterAuth { }); if (response.statusCode == 200) { - var body = json.decode(utf8.decode(response.bodyBytes)); + Map body = json.decode(utf8.decode(response.bodyBytes)); var error = body[kErrorConstant]; if (error != null) { throw GithubAPIError.parse(body); diff --git a/packages/github_auth/lib/src/utils/constants.dart b/packages/github_auth/lib/src/utils/constants.dart index d8e873d..fdd0832 100644 --- a/packages/github_auth/lib/src/utils/constants.dart +++ b/packages/github_auth/lib/src/utils/constants.dart @@ -1,11 +1,31 @@ -const kApiBaseEndpoint = 'https://github.com/login/oauth'; -const kApiEndpointAccessToken = '$kApiBaseEndpoint/access_token'; -const kApiEndpointAuthorize = '$kApiBaseEndpoint/authorize'; - -const kCodeConstant = 'code'; -const kAccessTokenConstant = 'access_token'; -const kClientSecretConstant = 'client_secret'; -const kClientIdConstant = "client_id"; -const kAcceptConstant = "Accept"; -const kAcceptJsonConstant = "application/json"; -const kErrorConstant = "error"; +/// Global constant for Github OAuth Login base url +const String kApiBaseEndpoint = 'https://github.com/login/oauth'; + +/// Global constant for Github OAuth Login access token endpoint +const String kApiEndpointAccessToken = '$kApiBaseEndpoint/access_token'; + +/// Global constant for Github OAuth Login authorize endpoint +const String kApiEndpointAuthorize = '$kApiBaseEndpoint/authorize'; + +/// Global key constant for Github OAuth Login that to be +/// passed to the API call to get the access token +/// while logging in with [GithubAuth.clientId] and [GithubAuth.clientSecret]. +const String kCodeConstant = 'code'; + +/// Global key constant for to get the access token from the json response. +const String kAccessTokenConstant = 'access_token'; + +/// Global Key constant to put the client secret in the request body. +const String kClientSecretConstant = 'client_secret'; + +/// Global Key constant to put the client id in the request body. +const String kClientIdConstant = "client_id"; + +/// Global Key constant to put the headers in the request body. +const String kAcceptConstant = "Accept"; + +/// Global Value constant to put the headers in the request body. +const String kAcceptJsonConstant = "application/json"; + +/// Global Key constant to get the error from the json response. +const String kErrorConstant = "error"; diff --git a/packages/github_auth/pubspec.yaml b/packages/github_auth/pubspec.yaml index d3db13b..0077956 100644 --- a/packages/github_auth/pubspec.yaml +++ b/packages/github_auth/pubspec.yaml @@ -4,18 +4,19 @@ version: 0.0.4 homepage: https://github.com/StackTiger/flutter_auth/tree/master/packages/github_auth environment: - sdk: ">=2.7.0 <3.0.0" - flutter: ">=1.10.0" + sdk: ">=2.16.0 <3.0.0" + flutter: ">=2.10.0" dependencies: - flutter_auth_core: ^0.0.0 flutter: sdk: flutter - quiver: ">=2.0.0 <3.0.0" - meta: ^1.1.8 - http: ^0.12.2 + flutter_auth_core: + path: ../flutter_auth_core + http: ^0.13.4 + meta: ^1.7.0 + quiver: ^3.0.1+1 dev_dependencies: flutter_test: sdk: flutter - mockito: ^4.1.1 + mockito: ^5.1.0 diff --git a/packages/github_auth/test/github_auth_test.dart b/packages/github_auth/test/github_auth_test.dart index f3f798d..66b6183 100644 --- a/packages/github_auth/test/github_auth_test.dart +++ b/packages/github_auth/test/github_auth_test.dart @@ -1,5 +1,3 @@ -import 'dart:async'; - import 'package:flutter/material.dart'; import 'package:flutter_auth_core/flutter_auth_core.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -9,7 +7,7 @@ import 'package:mockito/mockito.dart'; import 'package:http/http.dart' as http; BuildContext kMockBuildContext = MockBuildContext(); -MockHttpClient kClient; +late MockHttpClient kClient; const kCallbackUrl = 'githubsdk://'; const kClientId = 'test-consumer-key'; @@ -21,7 +19,7 @@ const kToken = 'test-token'; void main() { TestWidgetsFlutterBinding.ensureInitialized(); - GithubAuth githubAuth; + late GithubAuth githubAuth; setUp(() { githubAuth = TestGithubAuth(); @@ -85,9 +83,9 @@ void main() { }); group('loginComplete()', () { - String mockCode; - String mockAccessToken; - Uri mockAuthorizedResultUrl; + late String mockCode; + late String mockAccessToken; + late Uri mockAuthorizedResultUrl; setUp(() { mockCode = 'test-code'; @@ -98,7 +96,7 @@ void main() { test('return a [FlutterAuthResult] with [FlutterAuthStatus] success', () async { - when(kClient.post(kApiEndpointAccessToken, + when(kClient.post(Uri.parse(kApiEndpointAccessToken), headers: anyNamed('headers'), body: anyNamed('body'))) .thenAnswer((_) async => http.Response('{"access_token": "$mockAccessToken"}', 200)); @@ -128,7 +126,7 @@ void main() { test( 'returns a [FlutterAuthResult] with [FlutterAuthStatus] error if statusCode != 200', () async { - when(kClient.post(kApiEndpointAccessToken, + when(kClient.post(Uri.parse(kApiEndpointAccessToken), headers: anyNamed('headers'), body: anyNamed('body'))) .thenAnswer((_) async => http.Response('mock error', 400)); @@ -145,7 +143,7 @@ void main() { test( 'return a [FlutterAuthResult] with FlutterAuthStatus.error if [accessToken] is null ', () async { - when(kClient.post(kApiEndpointAccessToken, + when(kClient.post(Uri.parse(kApiEndpointAccessToken), headers: anyNamed('headers'), body: anyNamed('body'))) .thenAnswer((_) async => http.Response('mock error', 400)); diff --git a/packages/twitter_auth/lib/src/twitter_api_error.dart b/packages/twitter_auth/lib/src/twitter_api_error.dart index 21862ba..9694cab 100644 --- a/packages/twitter_auth/lib/src/twitter_api_error.dart +++ b/packages/twitter_auth/lib/src/twitter_api_error.dart @@ -1,8 +1,8 @@ // https://developer.twitter.com/ja/docs/basics/response-codes class TwitterAPIError { - String code; - String message; + String? code; + String? message; TwitterAPIError({this.code, this.message}); diff --git a/packages/twitter_auth/lib/src/twitter_auth.dart b/packages/twitter_auth/lib/src/twitter_auth.dart index df50780..f287d57 100644 --- a/packages/twitter_auth/lib/src/twitter_auth.dart +++ b/packages/twitter_auth/lib/src/twitter_auth.dart @@ -2,7 +2,6 @@ import 'package:oauth1/oauth1.dart' as oauth1; import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_auth_core/flutter_auth_core.dart'; -import 'package:meta/meta.dart' show required, visibleForTesting; import 'package:oauth1/oauth1.dart'; import 'package:twitter_auth/src/utils/exception.dart'; import 'utils/constants.dart'; @@ -25,13 +24,13 @@ class TwitterAuth extends FlutterAuth { final bool clearCache; /// The user agent to be used for the Webview - final String userAgent; + final String? userAgent; - ClientCredentials _clientCredentials; + late ClientCredentials _clientCredentials; @visibleForTesting // ignore: public_member_api_docs - Authorization authorization; + late Authorization authorization; static final dynamic _client = oauth1.Platform( kApiEndpointRequestToken, @@ -41,15 +40,18 @@ class TwitterAuth extends FlutterAuth { /// Returns an instance of [TwitterAuth]. TwitterAuth( - {@required this.clientId, - @required this.clientSecret, - @required this.callbackUrl, + {required this.clientId, + required this.clientSecret, + required this.callbackUrl, this.clearCache = false, - this.userAgent}) { - assert(clientId != null && clientId.isNotEmpty, - 'Consumer key may not be null or empty.'); - assert(clientSecret != null && clientSecret.isNotEmpty, - 'Consumer secret may not be null or empty.'); + this.userAgent}) + : super( + clientId: clientId, + clientSecret: clientSecret, + callbackUrl: callbackUrl) { + assert(clientId.isNotEmpty, 'Consumer key may not be null or empty.'); + assert( + clientSecret.isNotEmpty, 'Consumer secret may not be null or empty.'); _clientCredentials = ClientCredentials(clientId, clientSecret); } diff --git a/packages/twitter_auth/lib/src/utils/exception.dart b/packages/twitter_auth/lib/src/utils/exception.dart index 0fa42cd..59deaa2 100644 --- a/packages/twitter_auth/lib/src/utils/exception.dart +++ b/packages/twitter_auth/lib/src/utils/exception.dart @@ -13,14 +13,15 @@ dynamic parseLoginException(error) { FlutterAuthException exception; if (error is SocketException) { - if (error.message != null && error.message.contains("Failed host lookup")) { + if (error.message.contains("Failed host lookup")) { exception = FlutterAuthException( code: FlutterAuthExceptionCode.network, message: 'No network', details: error.toString()); } - } else if (error is StateError) { - final apiError = parseTwitterApiError(error); + } + if (error is StateError) { + final apiError = parseTwitterApiError(error as StateError); exception = FlutterAuthException( code: FlutterAuthExceptionCode.login, @@ -38,7 +39,7 @@ dynamic parseLoginException(error) { } TwitterAPIError parseTwitterApiError(StateError error) { - List errors = XmlElement.parseString( + List? errors = XmlElement.parseString( error.message, returnElementsNamed: ['error'], start: 0, diff --git a/packages/twitter_auth/pubspec.yaml b/packages/twitter_auth/pubspec.yaml index d03f060..a30cc46 100644 --- a/packages/twitter_auth/pubspec.yaml +++ b/packages/twitter_auth/pubspec.yaml @@ -4,20 +4,21 @@ version: 0.0.4 homepage: https://github.com/StackTiger/flutter_auth/tree/master/packages/twitter_auth environment: - sdk: ">=2.7.0 <3.0.0" - flutter: ">=1.10.0" + sdk: ">=2.16.0 <3.0.0" + flutter: ">=2.10.0" dependencies: - flutter_auth_core: ^0.0.0 flutter: sdk: flutter - quiver: ">=2.0.0 <3.0.0" + flutter_auth_core: + path: packages/flutter_auth_core + http: ^0.13.4 meta: ^1.1.8 - http: ^0.12.2 - oauth1: ^1.0.4 - xml_parser: ^0.1.2 + oauth1: ^2.0.0 + quiver: ^3.0.1+1 + xml_parser: ^1.0.0 dev_dependencies: flutter_test: sdk: flutter - mockito: ^4.1.1 + mockito: ^5.1.0 diff --git a/packages/twitter_auth/test/twitter_auth_test.dart b/packages/twitter_auth/test/twitter_auth_test.dart index 6342fc3..4c3d5bd 100644 --- a/packages/twitter_auth/test/twitter_auth_test.dart +++ b/packages/twitter_auth/test/twitter_auth_test.dart @@ -1,5 +1,3 @@ -import 'dart:async'; - import 'package:flutter/material.dart'; import 'package:flutter_auth_core/flutter_auth_core.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -16,10 +14,10 @@ const kToken = 'test-token'; const kAuthorizedResultUrl = 'test-authorized-result-url'; BuildContext kMockBuildContext = MockBuildContext(); -MockOAuth1 kOauth1; +late MockOAuth1 kOauth1; void main() { - TwitterAuth twitterAuth; + late TwitterAuth twitterAuth; TestWidgetsFlutterBinding.ensureInitialized(); @@ -63,9 +61,10 @@ void main() { }); test('login()', () async { - when(kOauth1.requestTemporaryCredentials(any)).thenAnswer((_) { + when(kOauth1.requestTemporaryCredentials('oob')).thenAnswer((_) { return Future.value( - AuthorizationResponse(Credentials('x', 'x'), null)); + AuthorizationResponse(Credentials('x', 'x'), {}), + ); }); final result = await twitterAuth.login(kMockBuildContext); @@ -77,10 +76,10 @@ void main() { }); group('loginComplete()', () { - String mockOauthToken; - String mockOauthVerifier; - String mockOauthSecret; - Uri mockAuthorizedResultUrl; + late String mockOauthToken; + late String mockOauthVerifier; + late String mockOauthSecret; + late Uri mockAuthorizedResultUrl; setUp(() { mockOauthToken = 'test-oauth-token'; @@ -92,15 +91,17 @@ void main() { test('return a [FlutterAuthResult] with [FlutterAuthStatus] success', () async { - when(kOauth1.requestTokenCredentials(any, any)).thenAnswer((_) => - Future.value(AuthorizationResponse( - Credentials(mockOauthToken, mockOauthSecret), null))); + when(kOauth1.requestTokenCredentials( + Credentials(any.toString(), any.toString()), any.toString())) + .thenAnswer((_) => Future.value(AuthorizationResponse( + Credentials(mockOauthToken, mockOauthSecret), {}))); final result = await twitterAuth.loginComplete(mockAuthorizedResultUrl); - final capturedCredentials = - verify(kOauth1.requestTokenCredentials(captureAny, mockOauthVerifier)) - .captured[0] as Credentials; + final capturedCredentials = verify(kOauth1.requestTokenCredentials( + Credentials(captureAny.toString(), captureAny.toString()), + mockOauthVerifier)) + .captured[0] as Credentials; expect(capturedCredentials.token, mockOauthToken); expect(capturedCredentials.tokenSecret, ''); @@ -112,14 +113,16 @@ void main() { test( 'returns a [FlutterAuthResult] with [FlutterAuthStatus] error if [requestTokenCredentials] errors', () async { - when(kOauth1.requestTokenCredentials(any, any)) + when(kOauth1.requestTokenCredentials( + Credentials(any.toString(), any.toString()), any.toString())) .thenAnswer((_) => new Future.error('mock error')); try { await twitterAuth.loginComplete(mockAuthorizedResultUrl); } on FlutterAuthException catch (e) { - final capturedCredentials = verify( - kOauth1.requestTokenCredentials(captureAny, mockOauthVerifier)) + final capturedCredentials = verify(kOauth1.requestTokenCredentials( + Credentials(captureAny.toString(), captureAny.toString()), + mockOauthVerifier)) .captured[0] as Credentials; expect(capturedCredentials.token, mockOauthToken); @@ -143,7 +146,8 @@ void main() { expect(e.code, FlutterAuthExceptionCode.login); expect(e.message, 'oauth token is null'); - verifyNever(kOauth1.requestTokenCredentials(any, any)); + verifyNever(kOauth1.requestTokenCredentials( + Credentials(any.toString(), any.toString()), any.toString())); } catch (_) {} }); }); @@ -152,8 +156,16 @@ void main() { class MockBuildContext extends Mock implements BuildContext {} class MockOAuth1 extends Mock implements oauth1.Authorization { + Platform _platform = Platform( + 'https://api.twitter.com/oauth/request_token', + 'https://api.twitter.com/oauth/authorize', + 'https://api.twitter.com/oauth/access_token', + SignatureMethods.hmacSha1); MockOAuth1() { - Authorization(ClientCredentials(kClientId, kClientSecret), null); + Authorization( + ClientCredentials(kClientId, kClientSecret), + _platform + ); } }