test_token.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import
  3. import json
  4. import sys
  5. # Import Pepper Libraries
  6. import pepper.cli
  7. # Import Testing Libraries
  8. from mock import patch, mock_open, MagicMock
  9. def test_token():
  10. sys.argv = ['pepper', '*', 'test.ping']
  11. client = pepper.cli.PepperCli()
  12. client.options.mktoken = True
  13. mock_data = (
  14. '{"perms": [".*", "@runner", "@wheel", "@jobs"], "start": 1529967752.516165, '
  15. '"token": "7130faa1e17f935d5f2702465cafdc73212d64d0", "expire": 1529968905.1131861, '
  16. '"user": "pepper", "eauth": "pam"}\n'
  17. )
  18. mock_api = MagicMock()
  19. mock_api.login = MagicMock(return_value=mock_data)
  20. with patch('pepper.cli.open', mock_open(read_data=mock_data)), \
  21. patch('pepper.cli.PepperCli.get_login_details', MagicMock(return_value=mock_data)), \
  22. patch('pepper.cli.PepperCli.parse_login', MagicMock(return_value={})), \
  23. patch('os.remove', MagicMock(return_value=None)), \
  24. patch('json.dump', MagicMock(side_effect=Exception('Test Error'))):
  25. ret1 = client.login(mock_api)
  26. with patch('os.path.isfile', MagicMock(return_value=False)):
  27. ret2 = client.login(mock_api)
  28. with patch('time.time', MagicMock(return_value=1529968044.133632)):
  29. ret3 = client.login(mock_api)
  30. assert json.loads(ret1) == json.loads(mock_data)
  31. assert json.loads(ret2) == json.loads(mock_data)
  32. assert ret3 == json.loads(mock_data)