Exemplo n.º 1
0
    def setUp(self):
        self.tmp = tempfile.mkdtemp()
        self.config = mock.MagicMock(
            accounts_dir=os.path.join(self.tmp, "accounts"))
        from letsencrypt.account import AccountFileStorage
        self.storage = AccountFileStorage(self.config)

        from letsencrypt.account import Account
        self.acc = Account(regr=messages.RegistrationResource(
            uri=None, new_authzr_uri=None, body=messages.Registration()),
                           key=KEY)
Exemplo n.º 2
0
    def setUp(self):
        self.tmp = tempfile.mkdtemp()
        self.config = mock.MagicMock(
            accounts_dir=os.path.join(self.tmp, "accounts"))
        from letsencrypt.account import AccountFileStorage
        self.storage = AccountFileStorage(self.config)

        from letsencrypt.account import Account
        self.acc = Account(
            regr=messages.RegistrationResource(
                uri=None, new_authzr_uri=None, body=messages.Registration()),
            key=KEY)
Exemplo n.º 3
0
class AccountFileStorageTest(unittest.TestCase):
    """Tests for letsencrypt.account.AccountFileStorage."""

    def setUp(self):
        self.tmp = tempfile.mkdtemp()
        self.config = mock.MagicMock(
            accounts_dir=os.path.join(self.tmp, "accounts"))
        from letsencrypt.account import AccountFileStorage
        self.storage = AccountFileStorage(self.config)

        from letsencrypt.account import Account
        self.acc = Account(
            regr=messages.RegistrationResource(
                uri=None, new_authzr_uri=None, body=messages.Registration()),
            key=KEY)

    def tearDown(self):
        shutil.rmtree(self.tmp)

    def test_init_creates_dir(self):
        self.assertTrue(os.path.isdir(self.config.accounts_dir))

    def test_save_and_restore(self):
        self.storage.save(self.acc)
        account_path = os.path.join(self.config.accounts_dir, self.acc.id)
        self.assertTrue(os.path.exists(account_path))
        for file_name in "regr.json", "meta.json", "private_key.json":
            self.assertTrue(os.path.exists(
                os.path.join(account_path, file_name)))
        self.assertEqual("0400", oct(os.stat(os.path.join(
            account_path, "private_key.json"))[stat.ST_MODE] & 0o777))

        # restore
        self.assertEqual(self.acc, self.storage.load(self.acc.id))

    def test_find_all(self):
        self.storage.save(self.acc)
        self.assertEqual([self.acc], self.storage.find_all())

    def test_find_all_none_empty_list(self):
        self.assertEqual([], self.storage.find_all())

    def test_find_all_accounts_dir_absent(self):
        os.rmdir(self.config.accounts_dir)
        self.assertEqual([], self.storage.find_all())

    def test_find_all_load_skips(self):
        self.storage.load = mock.MagicMock(
            side_effect=["x", errors.AccountStorageError, "z"])
        with mock.patch("letsencrypt.account.os.listdir") as mock_listdir:
            mock_listdir.return_value = ["x", "y", "z"]
            self.assertEqual(["x", "z"], self.storage.find_all())

    def test_load_non_existent_raises_error(self):
        self.assertRaises(errors.AccountNotFound, self.storage.load, "missing")

    def test_load_id_mismatch_raises_error(self):
        self.storage.save(self.acc)
        shutil.move(os.path.join(self.config.accounts_dir, self.acc.id),
                    os.path.join(self.config.accounts_dir, "x" + self.acc.id))
        self.assertRaises(errors.AccountStorageError, self.storage.load,
                          "x" + self.acc.id)

    def test_load_ioerror(self):
        self.storage.save(self.acc)
        mock_open = mock.mock_open()
        mock_open.side_effect = IOError
        with mock.patch("__builtin__.open", mock_open):
            self.assertRaises(
                errors.AccountStorageError, self.storage.load, self.acc.id)

    def test_save_ioerrors(self):
        mock_open = mock.mock_open()
        mock_open.side_effect = IOError  # TODO: [None, None, IOError]
        with mock.patch("__builtin__.open", mock_open):
            self.assertRaises(
                errors.AccountStorageError, self.storage.save, self.acc)
Exemplo n.º 4
0
class AccountFileStorageTest(unittest.TestCase):
    """Tests for letsencrypt.account.AccountFileStorage."""
    def setUp(self):
        self.tmp = tempfile.mkdtemp()
        self.config = mock.MagicMock(
            accounts_dir=os.path.join(self.tmp, "accounts"))
        from letsencrypt.account import AccountFileStorage
        self.storage = AccountFileStorage(self.config)

        from letsencrypt.account import Account
        self.acc = Account(regr=messages.RegistrationResource(
            uri=None, new_authzr_uri=None, body=messages.Registration()),
                           key=KEY)

    def tearDown(self):
        shutil.rmtree(self.tmp)

    def test_init_creates_dir(self):
        self.assertTrue(os.path.isdir(self.config.accounts_dir))

    def test_save_and_restore(self):
        self.storage.save(self.acc)
        account_path = os.path.join(self.config.accounts_dir, self.acc.id)
        self.assertTrue(os.path.exists(account_path))
        for file_name in "regr.json", "meta.json", "private_key.json":
            self.assertTrue(
                os.path.exists(os.path.join(account_path, file_name)))
        self.assertEqual(
            "0400",
            oct(
                os.stat(os.path.join(account_path, "private_key.json"))[
                    stat.ST_MODE] & 0o777))

        # restore
        self.assertEqual(self.acc, self.storage.load(self.acc.id))

    def test_find_all(self):
        self.storage.save(self.acc)
        self.assertEqual([self.acc], self.storage.find_all())

    def test_find_all_none_empty_list(self):
        self.assertEqual([], self.storage.find_all())

    def test_find_all_accounts_dir_absent(self):
        os.rmdir(self.config.accounts_dir)
        self.assertEqual([], self.storage.find_all())

    def test_find_all_load_skips(self):
        self.storage.load = mock.MagicMock(
            side_effect=["x", errors.AccountStorageError, "z"])
        with mock.patch("letsencrypt.account.os.listdir") as mock_listdir:
            mock_listdir.return_value = ["x", "y", "z"]
            self.assertEqual(["x", "z"], self.storage.find_all())

    def test_load_non_existent_raises_error(self):
        self.assertRaises(errors.AccountNotFound, self.storage.load, "missing")

    def test_load_id_mismatch_raises_error(self):
        self.storage.save(self.acc)
        shutil.move(os.path.join(self.config.accounts_dir, self.acc.id),
                    os.path.join(self.config.accounts_dir, "x" + self.acc.id))
        self.assertRaises(errors.AccountStorageError, self.storage.load,
                          "x" + self.acc.id)

    def test_load_ioerror(self):
        self.storage.save(self.acc)
        mock_open = mock.mock_open()
        mock_open.side_effect = IOError
        with mock.patch("__builtin__.open", mock_open):
            self.assertRaises(errors.AccountStorageError, self.storage.load,
                              self.acc.id)

    def test_save_ioerrors(self):
        mock_open = mock.mock_open()
        mock_open.side_effect = IOError  # TODO: [None, None, IOError]
        with mock.patch("__builtin__.open", mock_open):
            self.assertRaises(errors.AccountStorageError, self.storage.save,
                              self.acc)