test_azure_0_credentials.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/usr/bin/env python
  2. from azure_storage.methods import extract_account_name, extract_account_key, extract_connection_string, \
  3. delete_keyring_credentials, set_account_name, set_connection_string
  4. from azure_storage.azure_credentials import cli, delete_credentials, store_credentials
  5. from unittest.mock import patch
  6. import argparse
  7. import pytest
  8. import os
  9. connect_str = \
  10. 'DefaultEndpointsProtocol=https;AccountName=testcredentials;AccountKey=xxx;EndpointSuffix=core.windows.net'
  11. passphrase = '12234'
  12. account_name = 'testcredentials'
  13. azure_account = str()
  14. @patch('getpass.getpass')
  15. def test_set_credentials_invalid(getpass):
  16. getpass.return_value = connect_str
  17. with pytest.raises(SystemExit):
  18. set_connection_string(passphrase=passphrase,
  19. account_name='credentials')
  20. @patch('getpass.getpass')
  21. def test_set_credentials_malformed_string(getpass):
  22. getpass.return_value = 'bad_string'
  23. with pytest.raises(SystemExit):
  24. set_connection_string(passphrase=passphrase,
  25. account_name=account_name)
  26. def test_set_account_name():
  27. assert set_account_name(passphrase=passphrase,
  28. account_name=account_name) == account_name
  29. def test_extract_account_name():
  30. assert extract_account_name(passphrase=passphrase) == account_name
  31. def test_extract_account_key_invalid_str():
  32. con_str = 'bad_key'
  33. with pytest.raises(SystemExit):
  34. extract_account_key(connect_str=con_str)
  35. @patch('getpass.getpass')
  36. def test_set_credentials(getpass):
  37. getpass.return_value = connect_str
  38. assert set_connection_string(passphrase=passphrase,
  39. account_name=account_name) == connect_str
  40. def test_extract_credentials():
  41. assert extract_connection_string(passphrase=passphrase,
  42. account_name=account_name) \
  43. .startswith('DefaultEndpointsProtocol')
  44. @patch('getpass.getpass')
  45. def test_extract_credentials_new_phrase(getpass):
  46. getpass.return_value = connect_str
  47. assert extract_connection_string(passphrase='fake',
  48. account_name=account_name) \
  49. .startswith('DefaultEndpointsProtocol')
  50. def test_delete_credentials():
  51. phrase = 'fake'
  52. assert delete_keyring_credentials(passphrase=phrase,
  53. account_name=account_name) == account_name
  54. @patch('getpass.getpass')
  55. def test_extract_credentials_invalid(getpass):
  56. getpass.return_value = connect_str
  57. phrase = 'fake'
  58. account = 'bogus'
  59. with pytest.raises(SystemExit):
  60. set_connection_string(passphrase=phrase,
  61. account_name=account)
  62. def test_delete_account_name():
  63. assert delete_keyring_credentials(passphrase=passphrase,
  64. account_name=passphrase) == passphrase
  65. def test_delete_account_name_missing():
  66. with pytest.raises(SystemExit):
  67. delete_keyring_credentials(passphrase=passphrase,
  68. account_name=passphrase)
  69. def test_delete_connection_string():
  70. assert delete_keyring_credentials(passphrase=passphrase,
  71. account_name=account_name) == account_name
  72. def test_delete_connection_string_missing():
  73. with pytest.raises(SystemExit):
  74. delete_keyring_credentials(passphrase=passphrase,
  75. account_name=account_name)
  76. @patch('argparse.ArgumentParser.parse_args')
  77. @patch('getpass.getpass')
  78. def test_credentials_store_integration(getpass, mock_args):
  79. getpass.return_value = connect_str
  80. mock_args.return_value = argparse.Namespace(passphrase=passphrase,
  81. account_name=account_name)
  82. arguments = cli()
  83. store_credentials(args=arguments)
  84. def test_extract_account_name_integration():
  85. assert extract_account_name(passphrase=passphrase) == account_name
  86. def test_extract_account_name_new(monkeypatch):
  87. new_account = 'account'
  88. monkeypatch.setattr('builtins.input', lambda _: new_account)
  89. assert extract_account_name(passphrase='fake') == new_account
  90. def test_delete_account_name_new():
  91. phrase = 'fake'
  92. assert delete_keyring_credentials(passphrase=phrase,
  93. account_name=phrase) == phrase
  94. def test_extract_credentials_integration():
  95. assert extract_connection_string(passphrase=passphrase,
  96. account_name=account_name) \
  97. .startswith('DefaultEndpointsProtocol')
  98. @patch('argparse.ArgumentParser.parse_args')
  99. @patch('getpass.getpass')
  100. def test_credentials_delete_integration(getpass, mock_args):
  101. getpass.return_value = connect_str
  102. mock_args.return_value = argparse.Namespace(passphrase=passphrase,
  103. account_name=account_name)
  104. arguments = cli()
  105. delete_credentials(args=arguments)
  106. def test_delete_account_name_integration():
  107. with pytest.raises(SystemExit):
  108. delete_keyring_credentials(passphrase=passphrase,
  109. account_name=passphrase)
  110. def test_delete_connection_string_integration():
  111. with pytest.raises(SystemExit):
  112. delete_keyring_credentials(passphrase=passphrase,
  113. account_name=account_name)
  114. @patch('getpass.getpass')
  115. def test_set_account_from_env_var(getpass):
  116. global azure_account
  117. azure_account = os.environ.get('AZURE_STORAGE_ACCOUNT')
  118. if azure_account:
  119. getpass.return_value = azure_account
  120. assert set_account_name(passphrase='AzureStorage',
  121. account_name=azure_account) == azure_account
  122. @patch('getpass.getpass')
  123. def test_set_connect_str_from_env_var(getpass):
  124. connection_string = os.environ.get('AZURE_CONNECTION_STRING')
  125. if connection_string:
  126. getpass.return_value = connection_string
  127. assert set_connection_string(passphrase='AzureStorage',
  128. account_name=azure_account) == connection_string