From 8dc9dd7555c317a257483c2a473158d77cccbf59 Mon Sep 17 00:00:00 2001 From: rekire Date: Sun, 31 Mar 2024 21:50:28 +0200 Subject: [PATCH] Add missing tests --- autologin/test/autologin_test.dart | 1 + autologin_linux/lib/autologin_linux.dart | 65 ++++++++++++++++- autologin_linux/lib/src/autologin_linux.dart | 55 --------------- .../test/autologin_linux_test.dart | 69 +++++++++++++++++++ 4 files changed, 134 insertions(+), 56 deletions(-) delete mode 100644 autologin_linux/lib/src/autologin_linux.dart diff --git a/autologin/test/autologin_test.dart b/autologin/test/autologin_test.dart index 2d0eb84..99c73c7 100644 --- a/autologin/test/autologin_test.dart +++ b/autologin/test/autologin_test.dart @@ -21,6 +21,7 @@ void main() { setUp(() { autologinPlatform = MockAutologinPlatform(); + autologinPlatform.setup(); // just for code coverage Credential? cache; String? cachedToken; when( diff --git a/autologin_linux/lib/autologin_linux.dart b/autologin_linux/lib/autologin_linux.dart index 1155b1c..e9c6804 100644 --- a/autologin_linux/lib/autologin_linux.dart +++ b/autologin_linux/lib/autologin_linux.dart @@ -1 +1,64 @@ -export 'src/autologin_linux.dart'; +import 'package:autologin_platform_interface/autologin_platform_interface.dart'; +import 'package:flutter/services.dart'; + +/// The Linux implementation of [AutologinPlatform]. +class AutologinLinux extends MethodChannelAutologin { + String? _appId; + String? _appName; + + /// Registers this class as the default instance of [AutologinPlatform] + static void registerWith() { + AutologinPlatform.instance = AutologinLinux(); + } + + @override + void setup({String? domain, String? appId, String? appName}) { + _appId = appId; + _appName = appName; + } + + @override + Future requestCredentials() async { + _validateArguments(); + final json = await methodChannel.invokeMethod>( + 'requestCredentials', + {'appId': _appId, 'appName': _appName}, + ); + if (json == null) { + return null; + } + return Credential.fromMap(json.map((key, value) => MapEntry(key.toString(), value))); + } + + @override + Future saveCredentials(Credential credential) async { + _validateArguments(); + return await methodChannel.invokeMethod( + 'saveCredentials', + { + ...credential.toJson(), + 'appId': _appId, + 'appName': _appName, + }, + ) == + true; + } + + void _validateArguments() { + final missingArgs = []; + if (_appId == null) { + missingArgs.add("'appId'"); + } + if (_appName == null) { + missingArgs.add("'appName'"); + } + if (missingArgs.isNotEmpty) { + const expectedCall = "AutologinPlugin.setup(appId: 'your.app.id', appName: 'Your app name')"; + throw PlatformException( + code: 'MissingArgument', + message: 'For the Linux platform you need to call $expectedCall before this call', + details: 'The missing argument${missingArgs.length > 1 ? 's are' : ' is'} ${missingArgs.join(' and ')}.', + ); + } + } +} diff --git a/autologin_linux/lib/src/autologin_linux.dart b/autologin_linux/lib/src/autologin_linux.dart deleted file mode 100644 index 62f5c02..0000000 --- a/autologin_linux/lib/src/autologin_linux.dart +++ /dev/null @@ -1,55 +0,0 @@ -import 'package:autologin_platform_interface/autologin_platform_interface.dart'; -import 'package:flutter/services.dart'; - -/// The Linux implementation of [AutologinPlatform]. -class AutologinLinux extends MethodChannelAutologin { - String? _appId; - String? _appName; - - /// Registers this class as the default instance of [AutologinPlatform] - static void registerWith() { - AutologinPlatform.instance = AutologinLinux(); - } - - @override - void setup({String? domain, String? appId, String? appName}) { - _appId = appId; - _appName = appName; - } - - @override - Future requestCredentials() async { - _validateArguments(); - final json = await methodChannel.invokeMethod>( - 'requestCredentials', - {'appId': _appId, 'appName': _appName}, - ); - if (json == null) { - return null; - } - return Credential.fromMap(json.map((key, value) => MapEntry(key.toString(), value))); - } - - @override - Future saveCredentials(Credential credential) async { - return false; - } - - void _validateArguments() { - final missingArgs = []; - if (_appId == null) { - missingArgs.add("'appId'"); - } - if (_appName == null) { - missingArgs.add("'appName'"); - } - if (missingArgs.isNotEmpty) { - const expectedCall = "AutologinPlugin.setup(appId: 'your.app.id', appName: 'Your app name')"; - throw PlatformException( - code: 'MissingArgument', - message: 'For the Linux platform you need to call $expectedCall before this call', - details: 'The missing argument${missingArgs.length > 1 ? 's are' : ' is'} ${missingArgs.join(' and ')}.', - ); - } - } -} diff --git a/autologin_linux/test/autologin_linux_test.dart b/autologin_linux/test/autologin_linux_test.dart index 76bdf97..449f590 100644 --- a/autologin_linux/test/autologin_linux_test.dart +++ b/autologin_linux/test/autologin_linux_test.dart @@ -87,6 +87,75 @@ void main() { }, ); + test( + 'saveCredentials returns expected value', + () async { + autologin.setup(appId: 'demo.id', appName: 'Test app'); + final report = await autologin.saveCredentials(SharedTests.expectedCredentials); + expect(report, equals(true)); + expect(utils.log, [ + isMethodCall( + 'saveCredentials', + arguments: { + ...SharedTests.expectedCredentials.toJson(), + 'appId': 'demo.id', + 'appName': 'Test app', + }, + ), + ]); + }, + ); + + test( + 'saveCredentials throws helpful errors', + () async { + const expectedMessage = "For the Linux platform you need to call AutologinPlugin.setup(appId: 'your.app.id', " + "appName: 'Your app name') before this call"; + + // All arguments missing + autologin.setup(); + expect( + autologin.saveCredentials(SharedTests.expectedCredentials), + throwsA( + predicate( + (e) => + e is PlatformException && + e.message == expectedMessage && + e.details == "The missing arguments are 'appId' and 'appName'.", + ), + ), + ); + + // app name missing + autologin.setup(appId: 'demo.id'); + expect( + autologin.saveCredentials(SharedTests.expectedCredentials), + throwsA( + predicate( + (e) => + e is PlatformException && + e.message == expectedMessage && + e.details == "The missing argument is 'appName'.", + ), + ), + ); + + // app id missing + autologin.setup(appName: 'test'); + expect( + autologin.saveCredentials(SharedTests.expectedCredentials), + throwsA( + predicate( + (e) => + e is PlatformException && + e.message == expectedMessage && + e.details == "The missing argument is 'appId'.", + ), + ), + ); + }, + ); + test('saveLoginToken returns false', utils.saveLoginTokenReturnsFalse); test('requestLoginToken returns nothing', () async {