예제 #1
0
    def test_read_encrypted_file(self):
        encrypted_file = path.join(self.tmpdir.name, 'test.beancount.asc')
        self.encrypt_as_file(INPUT, encrypted_file)

        with test_utils.environ('GNUPGHOME', self.ringdir):
            plaintext = encryption.read_encrypted_file(encrypted_file)
            self.assertEqual(INPUT, plaintext)
예제 #2
0
    def test_include_encrypted(self):
        with test_utils.tempdir() as tmpdir:
            test_utils.create_temporary_files(
                tmpdir, {
                    'apples.beancount':
                    """
                  include "oranges.beancount.asc"
                  2014-01-01 open Assets:Apples
                """,
                    'oranges.beancount':
                    """
                  2014-01-02 open Assets:Oranges
                """
                })

            # Encrypt the oranges file and remove the unencrypted file.
            with open(path.join(tmpdir, 'oranges.beancount')) as infile:
                self.encrypt_as_file(
                    infile.read(), path.join(tmpdir, 'oranges.beancount.asc'))
            os.remove(path.join(tmpdir, 'oranges.beancount'))

            # Load the top-level file which includes the encrypted file.
            with test_utils.environ('GNUPGHOME', self.ringdir):
                entries, errors, options_map = loader.load_file(
                    path.join(tmpdir, 'apples.beancount'))

        self.assertFalse(errors)
        self.assertEqual(2, len(entries))
        self.assertRegex(entries[0].meta['filename'], 'apples.beancount')
        self.assertRegex(entries[1].meta['filename'], 'oranges.+count.asc')
예제 #3
0
 def test_load_cache_override_filename_pattern_by_env_var(self):
     with test_utils.environ('BEANCOUNT_LOAD_CACHE_FILENAME',
                             '__{filename}__'):
         loader.initialize(use_cache=True)
         with test_utils.tempdir() as tmp:
             test_utils.create_temporary_files(
                 tmp, {
                     'apples.beancount':
                     """
                   2014-01-01 open Assets:Apples
                 """
                 })
             filename = path.join(tmp, 'apples.beancount')
             entries, errors, options_map = loader.load_file(filename)
             self.assertEqual({'__apples.beancount__', 'apples.beancount'},
                              set(os.listdir(tmp)))
예제 #4
0
    def test_read_encrypted_file(self):
        # Import secret and public keys.
        self._run_gpg('--import', stdin=TEST_PUBLIC_KEY.encode('ascii'))
        self._run_gpg('--import', stdin=TEST_SECRET_KEY.encode('ascii'))

        # Encrypt the Beancount plaintext file with it.
        out, err = self._run_gpg('--recipient',
                                 'beancount-test',
                                 '--encrypt',
                                 '--output=-',
                                 stdin=INPUT.encode('utf8'))
        encrypted_file = path.join(self.tmpdir, 'test.beancount.asc')
        with open(encrypted_file, 'w') as encfile:
            encfile.write(out)

        with test_utils.environ('GNUPGHOME', self.ringdir):
            plaintext = encryption.read_encrypted_file(encrypted_file)
            self.assertEqual(INPUT, plaintext)
예제 #5
0
 def test_environ_contextmanager(self):
     with test_utils.environ('PATH', '/unlikely-to-be-your-path'):
         self.assertEqual('/unlikely-to-be-your-path', os.getenv('PATH'))
     self.assertNotEqual('/unlikely-to-be-your-path', os.getenv('PATH'))