Example #1
0
class TestStorageSession(unittest.TestCase):
    data = (
        "https://python-reference.readthedocs.io/en/latest/docs/statements/assert.html"
    )

    def setUp(self):
        self.storage = Storage(backend=Storage.STORAGE_BACKEND_SESSION)
        self.storage.del_data()
        self.storage.set_data(self.data)

    def tearDown(self):
        self.storage.del_data()

    def test_get_data_session(self):
        rdata = self.storage.get_data()
        self.assertEqual(self.data, rdata)

    def test_del_data_session(self):
        with captured_output() as (del_out, del_err):
            self.storage.del_data

        self.assertIn(del_out.getvalue().strip(), "unset")

        try:
            rdata = os.environ[Storage.SESSION_STORAGE_VARIABLE]
            self.assertIsNot(self.data, rdata)
        except KeyError:
            self.assertIsNone(None)
Example #2
0
class TestStorageKeyring(unittest.TestCase):
    data = (
        "https://python-reference.readthedocs.io/en/latest/docs/statements/assert.html"
    )

    def setUp(self):
        """Surpress RessourceWarnings.

        Link:
            https://github.com/jaraco/keyring/issues/380&https://github.com/jaraco/keyring/pull/381
        """
        with warnings.catch_warnings():
            warnings.filterwarnings(action="ignore",
                                    message="unclosed",
                                    category=ResourceWarning)

            self.storage = Storage(backend=Storage.STORAGE_BACKEND_KEYRING)
            self.storage.del_data()
            self.storage.set_data(self.data)

    def tearDown(self):
        self.storage.del_data()

    def test_get_data_keyring(self):
        rdata = self.storage.get_data()
        self.assertEqual(self.data, rdata)

    def test_del_data_keyring(self):
        self.storage.del_data()
        self.assertIsNone(self.storage.get_data())
Example #3
0
    def action_read(args):

        encoded_file = os.path.expanduser(args.read)

        if not os.path.isfile(encoded_file):
            print("{0} file not found.".format(encoded_file))
            sys.exit(1)

        # check permissions 0600
        st = os.stat(encoded_file)
        if oct(st.st_mode) > OSEnvController.FILE_PERMISSION:
            print("File {0} has the wrong permissions. \"0600\" are required.".
                  format(encoded_file))
            sys.exit(1)

        # read from file
        userkey = None
        try:
            userkey = getpass.getpass(
                prompt="Input master key and press [ENTER]: ")
            if userkey is not None and len(userkey) != 44:
                print("Wrong key length {length}, encryption process aborted!".
                      format(length=len(userkey)))
                sys.exit(1)
        except Exception as error:
            print("ERROR key", error)
            return None

        crypto = FileEncryption()
        json_data = crypto.decrypt_configfile(input_file=encoded_file,
                                              key=userkey)

        # save to keyring
        storage = Storage()
        storage.set_data(json_data.decode("utf-8"))