Skip to content

Commit

Permalink
Add missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rekire committed Mar 31, 2024
1 parent e4dcac4 commit 8dc9dd7
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 56 deletions.
1 change: 1 addition & 0 deletions autologin/test/autologin_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ void main() {

setUp(() {
autologinPlatform = MockAutologinPlatform();
autologinPlatform.setup(); // just for code coverage
Credential? cache;
String? cachedToken;
when(
Expand Down
65 changes: 64 additions & 1 deletion autologin_linux/lib/autologin_linux.dart
Original file line number Diff line number Diff line change
@@ -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<Credential?> requestCredentials() async {
_validateArguments();
final json = await methodChannel.invokeMethod<Map<Object?, Object?>>(
'requestCredentials',
{'appId': _appId, 'appName': _appName},
);
if (json == null) {
return null;
}
return Credential.fromMap(json.map((key, value) => MapEntry(key.toString(), value)));
}

@override
Future<bool> saveCredentials(Credential credential) async {
_validateArguments();
return await methodChannel.invokeMethod<bool>(
'saveCredentials',
{
...credential.toJson(),
'appId': _appId,
'appName': _appName,
},
) ==
true;
}

void _validateArguments() {
final missingArgs = <String>[];
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 ')}.',
);
}
}
}
55 changes: 0 additions & 55 deletions autologin_linux/lib/src/autologin_linux.dart

This file was deleted.

69 changes: 69 additions & 0 deletions autologin_linux/test/autologin_linux_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8dc9dd7

Please sign in to comment.