diff --git a/packages/shorebird_cli/lib/src/commands/login_command.dart b/packages/shorebird_cli/lib/src/commands/login_command.dart index 0917d0ee..fa479156 100644 --- a/packages/shorebird_cli/lib/src/commands/login_command.dart +++ b/packages/shorebird_cli/lib/src/commands/login_command.dart @@ -27,6 +27,15 @@ class LoginCommand extends ShorebirdCommand { @override Future run() async { + if (auth.isAuthenticated) { + logger + ..info('You are already logged in as <${auth.email}>.') + ..info( + 'Run ${lightCyan.wrap('shorebird logout')} to log out and try again.', + ); + return ExitCode.success.code; + } + final api.AuthProvider provider; if (results.wasParsed('provider')) { provider = api.AuthProvider.values.byName(results['provider'] as String); @@ -40,13 +49,6 @@ class LoginCommand extends ShorebirdCommand { try { await auth.login(provider, prompt: prompt); - } on UserAlreadyLoggedInException catch (error) { - logger - ..info('You are already logged in as <${error.email}>.') - ..info( - 'Run ${lightCyan.wrap('shorebird logout')} to log out and try again.', - ); - return ExitCode.success.code; } on UserNotFoundException catch (error) { final consoleUri = Uri.https('console.shorebird.dev'); logger diff --git a/packages/shorebird_cli/test/src/commands/login_command_test.dart b/packages/shorebird_cli/test/src/commands/login_command_test.dart index bc308a57..c817eeff 100644 --- a/packages/shorebird_cli/test/src/commands/login_command_test.dart +++ b/packages/shorebird_cli/test/src/commands/login_command_test.dart @@ -48,6 +48,7 @@ void main() { when(() => results.wasParsed('provider')).thenReturn(false); when(() => results['provider']).thenReturn(null); + when(() => auth.isAuthenticated).thenReturn(false); when(() => auth.client).thenReturn(httpClient); when(() => auth.credentialsFilePath).thenReturn( p.join(applicationConfigHome.path, 'credentials.json'), @@ -120,25 +121,27 @@ void main() { }); }); - test('exits with code 0 when already logged in', () async { - when( - () => auth.login( - any(), - prompt: any(named: 'prompt'), - ), - ).thenThrow(UserAlreadyLoggedInException(email: email)); + group('when user is already logged in', () { + setUp(() { + when(() => auth.isAuthenticated).thenReturn(true); + when(() => auth.email).thenReturn(email); + }); - final result = await runWithOverrides(command.run); - expect(result, equals(ExitCode.success.code)); + test('prints message and exits with code 0 when already logged in', + () async { + final result = await runWithOverrides(command.run); - verify( - () => logger.info('You are already logged in as <$email>.'), - ).called(1); - verify( - () => logger.info( - "Run ${lightCyan.wrap('shorebird logout')} to log out and try again.", - ), - ).called(1); + expect(result, equals(ExitCode.success.code)); + verify( + () => logger.info('You are already logged in as <$email>.'), + ).called(1); + verify( + () => logger.info( + "Run ${lightCyan.wrap('shorebird logout')} to log out and try again.", + ), + ).called(1); + verifyNever(() => auth.login(any(), prompt: any(named: 'prompt'))); + }); }); test('exits with code 70 if no user is found', () async {