From 42ce5f0759fb91cfbcdf7167a0b3f357b47feed5 Mon Sep 17 00:00:00 2001 From: Bochun Zhang Date: Fri, 5 Aug 2016 17:09:44 -0700 Subject: [PATCH] Add reset command The reset command removes the local credential cache file. Example usage: $ oauth2l reset --- oauth2l/__init__.py | 9 +++++++++ oauth2l/oauth2l_test.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/oauth2l/__init__.py b/oauth2l/__init__.py index 808cbde1..e6500078 100644 --- a/oauth2l/__init__.py +++ b/oauth2l/__init__.py @@ -342,6 +342,10 @@ def _Info(args): else: print(_CompactJson(tokeninfo)) +def _Reset(args): + credentials_filename = _GetCredentialsFilename(args.credentials_filename) + if os.path.exists(credentials_filename): + os.remove(credentials_filename) def _Test(args): """Test an access token. Exits with 0 if valid, 1 otherwise.""" @@ -389,6 +393,11 @@ def _GetParser(): nargs='*', help='Scope to header. May be provided multiple times.') + # reset + reset = subparsers.add_parser('reset', help=_Reset.__doc__, + parents=[shared_flags]) + reset.set_defaults(func=_Reset) + # info info = subparsers.add_parser('info', help=_Info.__doc__, parents=[shared_flags]) diff --git a/oauth2l/oauth2l_test.py b/oauth2l/oauth2l_test.py index 2d4efed0..c4915eae 100644 --- a/oauth2l/oauth2l_test.py +++ b/oauth2l/oauth2l_test.py @@ -335,6 +335,39 @@ def testMissingStatus(self): self.assertEqual(1, mock_http.call_count) self.assertEqual(1, mock_h.request.call_count) + def testReset(self): + orig_os_remove = os.remove + orig_os_path_exist = os.path.exists + os.remove = mock_remove = mock.MagicMock() + os.path.exists = mock_exists = mock.MagicMock() + mock_exists.return_value = True + output = _GetCommandOutput('reset', []) + mock_remove.assert_called_once_with(os.path.expanduser('~/.oauth2l.token')) + os.remove = orig_os_remove + os.path.exists = orig_os_path_exist + + def testResetFileNotExist(self): + orig_os_remove = os.remove + orig_os_path_exist = os.path.exists + os.remove = mock_remove = mock.MagicMock() + os.path.exists = mock_exists = mock.MagicMock() + mock_exists.return_value = False + output = _GetCommandOutput('reset', []) + mock_remove.assert_not_called() + os.remove = orig_os_remove + os.path.exists = orig_os_path_exist + + def testResetWithFilename(self): + orig_os_remove = os.remove + orig_os_path_exist = os.path.exists + os.remove = mock_remove = mock.MagicMock() + os.path.exists = mock_exists = mock.MagicMock() + mock_exists.return_value = True + output = _GetCommandOutput('reset', ['--credentials_filename', '~/my_oauth_token']) + mock_remove.assert_called_once_with(os.path.expanduser('~/my_oauth_token')) + os.remove = orig_os_remove + os.path.exists = orig_os_path_exist + class TestServiceAccounts(unittest.TestCase): def setUp(self):