Skip to content
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

feat(shorebird_cli): add logout command #25

Merged
merged 1 commit into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/shorebird_cli/lib/src/command_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class ShorebirdCliCommandRunner extends CompletionCommandRunner<int> {
codePushApiClientBuilder ?? ShorebirdCodePushApiClient.new;

addCommand(LoginCommand(auth: authentication, logger: _logger));
addCommand(LogoutCommand(auth: authentication, logger: _logger));
addCommand(
PublishCommand(
auth: authentication,
Expand Down
1 change: 1 addition & 0 deletions packages/shorebird_cli/lib/src/commands/commands.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export 'login_command.dart';
export 'logout_command.dart';
export 'publish_command.dart';
export 'update_command.dart';
39 changes: 39 additions & 0 deletions packages/shorebird_cli/lib/src/commands/logout_command.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'package:args/command_runner.dart';
import 'package:mason_logger/mason_logger.dart';
import 'package:shorebird_cli/src/auth/auth.dart';

/// {@template logout_command}
///
/// `shorebird logout`
/// Logout of the current Shorebird user.
/// {@endtemplate}
class LogoutCommand extends Command<int> {
/// {@macro logout_command}
LogoutCommand({required Auth auth, required Logger logger})
: _auth = auth,
_logger = logger;

@override
String get description => 'Logout of the current Shorebird user';

@override
String get name => 'logout';

final Auth _auth;
final Logger _logger;

@override
Future<int> run() async {
final session = _auth.currentSession;
if (session == null) {
_logger.info('You are already logged out.');
return ExitCode.success.code;
}

final logoutProgress = _logger.progress('Logging out of shorebird.dev');
_auth.logout();
logoutProgress.complete();

return ExitCode.success.code;
}
}
52 changes: 52 additions & 0 deletions packages/shorebird_cli/test/src/commands/logout_command_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'package:mason_logger/mason_logger.dart';
import 'package:mocktail/mocktail.dart';
import 'package:shorebird_cli/src/auth/auth.dart';
import 'package:shorebird_cli/src/auth/session.dart';
import 'package:shorebird_cli/src/commands/logout_command.dart';
import 'package:test/test.dart';

class _MockLogger extends Mock implements Logger {}

class _MockAuth extends Mock implements Auth {}

class _MockProgress extends Mock implements Progress {}

void main() {
group('LogoutCommand', () {
late Logger logger;
late Auth auth;
late LogoutCommand logoutCommand;

setUp(() {
logger = _MockLogger();
auth = _MockAuth();
logoutCommand = LogoutCommand(auth: auth, logger: logger);

when(() => logger.progress(any())).thenReturn(_MockProgress());
});

test('exits with code 0 when already logged out', () async {
final result = await logoutCommand.run();
expect(result, equals(ExitCode.success.code));

verify(
() => logger.info('You are already logged out.'),
).called(1);
});

test('exits with code 0 when logged out successfully', () async {
const session = Session(apiKey: 'test-api-key', projectId: 'example');
when(() => auth.currentSession).thenReturn(session);

final progress = _MockProgress();
when(() => progress.complete(any())).thenAnswer((invocation) {});
when(() => logger.progress(any())).thenReturn(progress);

final result = await logoutCommand.run();
expect(result, equals(ExitCode.success.code));

verify(() => logger.progress('Logging out of shorebird.dev')).called(1);
verify(() => auth.logout()).called(1);
});
});
}