Ejemplo n.º 1
0
    def test_load_credentials(self):

        with mock.patch('os.path.exists') as m:
            m.return_value = True

            good_creds = {"username": rand_str(), "password": rand_str()}
            m_open = mock.mock_open(read_data=json.dumps(good_creds))
            with mock.patch("builtins.open", m_open):
                good_credentials_file = "credentials.json"
                result = pyega3.load_credential(good_credentials_file)
                m_open.assert_called_once_with(good_credentials_file)
                self.assertEqual(len(result), 3)
                self.assertEqual(result[0], good_creds["username"])
                self.assertEqual(result[1], good_creds["password"])

            password1 = rand_str()
            good_creds1 = {"username": rand_str()}
            m_open1 = mock.mock_open(read_data=json.dumps(good_creds1))
            with mock.patch("builtins.open", m_open1):
                with mock.patch("getpass.getpass") as m_get_pw:
                    m_get_pw.return_value = password1
                    good_credentials_file1 = "credentials1.json"
                    result1 = pyega3.load_credential(good_credentials_file1)
                    m_open1.assert_called_once_with(good_credentials_file1)
                    self.assertEqual(len(result1), 3)
                    self.assertEqual(result1[0], good_creds1["username"])
                    self.assertEqual(result1[1], password1)

            with mock.patch("builtins.open",
                            mock.mock_open(read_data="bad json")):
                with self.assertRaises(SystemExit):
                    bad_credentials_file = "bad_credentials.json"
                    result = pyega3.load_credential(bad_credentials_file)
Ejemplo n.º 2
0
 def __init__(self, credentialsPath):
     *credentials, self.key = ega.load_credential(
         os.path.expanduser(credentialsPath))
     self.credentials = credentials
     self.token = ega.get_token(credentials)
Ejemplo n.º 3
0
 def test_load_credential(self, getpw):
     user_input = ["U", "P"]
     getpw.return_value = user_input[1]
     with mock.patch('builtins.input', side_effect=user_input[0]):
         self.assertEqual(pyega3.load_credential("unknownfile.txt"),
                          (user_input[0], user_input[1], None))