def setUp(self): self.tmp = tempfile.mkdtemp() self.config = mock.MagicMock( accounts_dir=os.path.join(self.tmp, "accounts")) from certbot.account import AccountFileStorage self.storage = AccountFileStorage(self.config) from certbot.account import Account self.acc = Account(regr=messages.RegistrationResource( uri=None, body=messages.Registration()), key=KEY)
def setUp(self): super(AccountFileStorageTest, self).setUp() from certbot.account import AccountFileStorage self.storage = AccountFileStorage(self.config) from certbot.account import Account new_authzr_uri = "hi" self.acc = Account(regr=messages.RegistrationResource( uri=None, body=messages.Registration(), new_authzr_uri=new_authzr_uri), key=KEY) self.mock_client = mock.MagicMock() self.mock_client.directory.new_authz = new_authzr_uri
def setUp(self): self.tmp = tempfile.mkdtemp() self.config = mock.MagicMock( accounts_dir=os.path.join(self.tmp, "accounts")) from certbot.account import AccountFileStorage self.storage = AccountFileStorage(self.config) from certbot.account import Account new_authzr_uri = "hi" self.acc = Account( regr=messages.RegistrationResource( uri=None, body=messages.Registration(), new_authzr_uri=new_authzr_uri), key=KEY) self.mock_client = mock.MagicMock() self.mock_client.directory.new_authz = new_authzr_uri
def setUp(self): self.tmp = tempfile.mkdtemp() self.config = mock.MagicMock( accounts_dir=os.path.join(self.tmp, "accounts")) from certbot.account import AccountFileStorage self.storage = AccountFileStorage(self.config) from certbot.account import Account self.acc = Account( regr=messages.RegistrationResource( uri=None, new_authzr_uri=None, body=messages.Registration()), key=KEY)
def setUp(self): super(AccountFileStorageTest, self).setUp() from certbot.account import AccountFileStorage self.storage = AccountFileStorage(self.config) from certbot.account import Account new_authzr_uri = "hi" self.acc = Account( regr=messages.RegistrationResource( uri=None, body=messages.Registration(), new_authzr_uri=new_authzr_uri), key=KEY) self.mock_client = mock.MagicMock() self.mock_client.directory.new_authz = new_authzr_uri
class AccountFileStorageTest(test_util.ConfigTestCase): """Tests for certbot.account.AccountFileStorage.""" #pylint: disable=too-many-public-methods def setUp(self): super(AccountFileStorageTest, self).setUp() from certbot.account import AccountFileStorage self.storage = AccountFileStorage(self.config) from certbot.account import Account new_authzr_uri = "hi" self.acc = Account(regr=messages.RegistrationResource( uri=None, body=messages.Registration(), new_authzr_uri=new_authzr_uri), key=KEY) self.mock_client = mock.MagicMock() self.mock_client.directory.new_authz = new_authzr_uri 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, self.mock_client) 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.assertTrue( oct( os.stat(os.path.join(account_path, "private_key.json"))[ stat.ST_MODE] & 0o777) in ("0400", "0o400")) # restore loaded = self.storage.load(self.acc.id) self.assertEqual(self.acc, loaded) def test_save_and_restore_old_version(self): """Saved regr should include a new_authzr_uri for older Certbots""" self.storage.save(self.acc, self.mock_client) path = os.path.join(self.config.accounts_dir, self.acc.id, "regr.json") with open(path, "r") as f: regr = json.load(f) self.assertTrue("new_authzr_uri" in regr) def test_save_regr(self): self.storage.save_regr(self.acc, self.mock_client) account_path = os.path.join(self.config.accounts_dir, self.acc.id) self.assertTrue(os.path.exists(account_path)) self.assertTrue(os.path.exists(os.path.join(account_path, "regr.json"))) for file_name in "meta.json", "private_key.json": self.assertFalse( os.path.exists(os.path.join(account_path, file_name))) def test_find_all(self): self.storage.save(self.acc, self.mock_client) 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): # pylint: disable=protected-access self.storage._load_for_server_path = mock.MagicMock( side_effect=["x", errors.AccountStorageError, "z"]) with mock.patch("certbot.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, self.mock_client) 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 _set_server(self, server): self.config.server = server from certbot.account import AccountFileStorage self.storage = AccountFileStorage(self.config) def test_find_all_neither_exists(self): self._set_server( 'https://acme-staging-v02.api.letsencrypt.org/directory') self.assertEqual([], self.storage.find_all()) self.assertEqual([], self.storage.find_all()) self.assertFalse(os.path.islink(self.config.accounts_dir)) def test_find_all_find_before_save(self): self._set_server( 'https://acme-staging-v02.api.letsencrypt.org/directory') self.assertEqual([], self.storage.find_all()) self.storage.save(self.acc, self.mock_client) self.assertEqual([self.acc], self.storage.find_all()) self.assertEqual([self.acc], self.storage.find_all()) self.assertFalse(os.path.islink(self.config.accounts_dir)) # we shouldn't have created a v1 account prev_server_path = 'https://acme-staging.api.letsencrypt.org/directory' self.assertFalse( os.path.isdir( self.config.accounts_dir_for_server_path(prev_server_path))) def test_find_all_save_before_find(self): self._set_server( 'https://acme-staging-v02.api.letsencrypt.org/directory') self.storage.save(self.acc, self.mock_client) self.assertEqual([self.acc], self.storage.find_all()) self.assertEqual([self.acc], self.storage.find_all()) self.assertFalse(os.path.islink(self.config.accounts_dir)) self.assertTrue(os.path.isdir(self.config.accounts_dir)) prev_server_path = 'https://acme-staging.api.letsencrypt.org/directory' self.assertFalse( os.path.isdir( self.config.accounts_dir_for_server_path(prev_server_path))) def test_find_all_server_downgrade(self): # don't use v2 accounts with a v1 url self._set_server( 'https://acme-staging-v02.api.letsencrypt.org/directory') self.assertEqual([], self.storage.find_all()) self.storage.save(self.acc, self.mock_client) self.assertEqual([self.acc], self.storage.find_all()) self._set_server('https://acme-staging.api.letsencrypt.org/directory') self.assertEqual([], self.storage.find_all()) def test_upgrade_version(self): self._set_server('https://acme-staging.api.letsencrypt.org/directory') self.storage.save(self.acc, self.mock_client) self._set_server( 'https://acme-staging-v02.api.letsencrypt.org/directory') self.assertEqual([self.acc], self.storage.find_all()) @mock.patch('os.rmdir') def test_corrupted_account(self, mock_rmdir): # pylint: disable=protected-access self._set_server('https://acme-staging.api.letsencrypt.org/directory') self.storage.save(self.acc, self.mock_client) mock_rmdir.side_effect = OSError self.storage._load_for_server_path = mock.MagicMock( side_effect=errors.AccountStorageError) self._set_server( 'https://acme-staging-v02.api.letsencrypt.org/directory') self.assertEqual([], self.storage.find_all()) def test_load_ioerror(self): self.storage.save(self.acc, self.mock_client) mock_open = mock.mock_open() mock_open.side_effect = IOError with mock.patch("six.moves.builtins.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("six.moves.builtins.open", mock_open): self.assertRaises(errors.AccountStorageError, self.storage.save, self.acc, self.mock_client) def test_delete(self): self.storage.save(self.acc, self.mock_client) self.storage.delete(self.acc.id) self.assertRaises(errors.AccountNotFound, self.storage.load, self.acc.id) def test_delete_no_account(self): self.assertRaises(errors.AccountNotFound, self.storage.delete, self.acc.id)
def _set_server(self, server): self.config.server = server from certbot.account import AccountFileStorage self.storage = AccountFileStorage(self.config)
class AccountFileStorageTest(test_util.ConfigTestCase): """Tests for certbot.account.AccountFileStorage.""" def setUp(self): super(AccountFileStorageTest, self).setUp() from certbot.account import AccountFileStorage self.storage = AccountFileStorage(self.config) from certbot.account import Account new_authzr_uri = "hi" self.acc = Account(regr=messages.RegistrationResource( uri=None, body=messages.Registration(), new_authzr_uri=new_authzr_uri), key=KEY) self.mock_client = mock.MagicMock() self.mock_client.directory.new_authz = new_authzr_uri 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, self.mock_client) 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.assertTrue( oct( os.stat(os.path.join(account_path, "private_key.json"))[ stat.ST_MODE] & 0o777) in ("0400", "0o400")) # restore loaded = self.storage.load(self.acc.id) self.assertEqual(self.acc, loaded) def test_save_and_restore_old_version(self): """Saved regr should include a new_authzr_uri for older Certbots""" self.storage.save(self.acc, self.mock_client) path = os.path.join(self.config.accounts_dir, self.acc.id, "regr.json") with open(path, "r") as f: regr = json.load(f) self.assertTrue("new_authzr_uri" in regr) def test_save_regr(self): self.storage.save_regr(self.acc, self.mock_client) account_path = os.path.join(self.config.accounts_dir, self.acc.id) self.assertTrue(os.path.exists(account_path)) self.assertTrue(os.path.exists(os.path.join(account_path, "regr.json"))) for file_name in "meta.json", "private_key.json": self.assertFalse( os.path.exists(os.path.join(account_path, file_name))) def test_find_all(self): self.storage.save(self.acc, self.mock_client) 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("certbot.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, self.mock_client) 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, self.mock_client) mock_open = mock.mock_open() mock_open.side_effect = IOError with mock.patch("six.moves.builtins.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("six.moves.builtins.open", mock_open): self.assertRaises(errors.AccountStorageError, self.storage.save, self.acc, self.mock_client) def test_delete(self): self.storage.save(self.acc, self.mock_client) self.storage.delete(self.acc.id) self.assertRaises(errors.AccountNotFound, self.storage.load, self.acc.id) def test_delete_no_account(self): self.assertRaises(errors.AccountNotFound, self.storage.delete, self.acc.id)
class AccountFileStorageTest(unittest.TestCase): """Tests for certbot.account.AccountFileStorage.""" def setUp(self): self.tmp = tempfile.mkdtemp() self.config = mock.MagicMock( accounts_dir=os.path.join(self.tmp, "accounts")) from certbot.account import AccountFileStorage self.storage = AccountFileStorage(self.config) from certbot.account import Account self.acc = Account(regr=messages.RegistrationResource( 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.assertTrue( oct( os.stat(os.path.join(account_path, "private_key.json"))[ stat.ST_MODE] & 0o777) in ("0400", "0o400")) # restore self.assertEqual(self.acc, self.storage.load(self.acc.id)) def test_save_regr(self): self.storage.save_regr(self.acc) account_path = os.path.join(self.config.accounts_dir, self.acc.id) self.assertTrue(os.path.exists(account_path)) self.assertTrue(os.path.exists(os.path.join(account_path, "regr.json"))) for file_name in "meta.json", "private_key.json": self.assertFalse( os.path.exists(os.path.join(account_path, file_name))) 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("certbot.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("six.moves.builtins.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("six.moves.builtins.open", mock_open): self.assertRaises(errors.AccountStorageError, self.storage.save, self.acc) def test_delete(self): self.storage.save(self.acc) self.storage.delete(self.acc.id) self.assertRaises(errors.AccountNotFound, self.storage.load, self.acc.id) def test_delete_no_account(self): self.assertRaises(errors.AccountNotFound, self.storage.delete, self.acc.id)
class AccountFileStorageTest(unittest.TestCase): """Tests for certbot.account.AccountFileStorage.""" def setUp(self): self.tmp = tempfile.mkdtemp() self.config = mock.MagicMock( accounts_dir=os.path.join(self.tmp, "accounts")) from certbot.account import AccountFileStorage self.storage = AccountFileStorage(self.config) from certbot.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.assertTrue(oct(os.stat(os.path.join( account_path, "private_key.json"))[stat.ST_MODE] & 0o777) in ("0400", "0o400")) # restore self.assertEqual(self.acc, self.storage.load(self.acc.id)) def test_save_regr(self): self.storage.save_regr(self.acc) account_path = os.path.join(self.config.accounts_dir, self.acc.id) self.assertTrue(os.path.exists(account_path)) self.assertTrue(os.path.exists(os.path.join( account_path, "regr.json"))) for file_name in "meta.json", "private_key.json": self.assertFalse(os.path.exists( os.path.join(account_path, file_name))) 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("certbot.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("six.moves.builtins.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("six.moves.builtins.open", mock_open): self.assertRaises( errors.AccountStorageError, self.storage.save, self.acc)
class AccountFileStorageTest(TempDirTestCase): """Tests for certbot.account.AccountFileStorage.""" def setUp(self): super(AccountFileStorageTest, self).setUp() self.config = mock.MagicMock( accounts_dir=os.path.join(self.tempdir, "accounts")) from certbot.account import AccountFileStorage self.storage = AccountFileStorage(self.config) from certbot.account import Account new_authzr_uri = "hi" self.acc = Account( regr=messages.RegistrationResource( uri=None, body=messages.Registration(), new_authzr_uri=new_authzr_uri), key=KEY) self.mock_client = mock.MagicMock() self.mock_client.directory.new_authz = new_authzr_uri 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, self.mock_client) 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.assertTrue(oct(os.stat(os.path.join( account_path, "private_key.json"))[stat.ST_MODE] & 0o777) in ("0400", "0o400")) # restore loaded = self.storage.load(self.acc.id) self.assertEqual(self.acc, loaded) def test_save_and_restore_old_version(self): """Saved regr should include a new_authzr_uri for older Certbots""" self.storage.save(self.acc, self.mock_client) path = os.path.join(self.config.accounts_dir, self.acc.id, "regr.json") with open(path, "r") as f: regr = json.load(f) self.assertTrue("new_authzr_uri" in regr) def test_save_regr(self): self.storage.save_regr(self.acc, self.mock_client) account_path = os.path.join(self.config.accounts_dir, self.acc.id) self.assertTrue(os.path.exists(account_path)) self.assertTrue(os.path.exists(os.path.join( account_path, "regr.json"))) for file_name in "meta.json", "private_key.json": self.assertFalse(os.path.exists( os.path.join(account_path, file_name))) def test_find_all(self): self.storage.save(self.acc, self.mock_client) 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("certbot.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, self.mock_client) 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, self.mock_client) mock_open = mock.mock_open() mock_open.side_effect = IOError with mock.patch("six.moves.builtins.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("six.moves.builtins.open", mock_open): self.assertRaises( errors.AccountStorageError, self.storage.save, self.acc, self.mock_client) def test_delete(self): self.storage.save(self.acc, self.mock_client) self.storage.delete(self.acc.id) self.assertRaises(errors.AccountNotFound, self.storage.load, self.acc.id) def test_delete_no_account(self): self.assertRaises(errors.AccountNotFound, self.storage.delete, self.acc.id)
class AccountFileStorageTest(test_util.ConfigTestCase): """Tests for certbot.account.AccountFileStorage.""" #pylint: disable=too-many-public-methods def setUp(self): super(AccountFileStorageTest, self).setUp() from certbot.account import AccountFileStorage self.storage = AccountFileStorage(self.config) from certbot.account import Account new_authzr_uri = "hi" self.acc = Account(regr=messages.RegistrationResource( uri=None, body=messages.Registration(), new_authzr_uri=new_authzr_uri), key=KEY) self.mock_client = mock.MagicMock() self.mock_client.directory.new_authz = new_authzr_uri def test_init_creates_dir(self): self.assertTrue( os.path.isdir( misc.underscores_for_unsupported_characters_in_path( self.config.accounts_dir))) def test_save_and_restore(self): self.storage.save(self.acc, self.mock_client) 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.assertTrue( filesystem.check_mode( os.path.join(account_path, "private_key.json"), 0o400)) # restore loaded = self.storage.load(self.acc.id) self.assertEqual(self.acc, loaded) def test_save_and_restore_old_version(self): """Saved regr should include a new_authzr_uri for older Certbots""" self.storage.save(self.acc, self.mock_client) path = os.path.join(self.config.accounts_dir, self.acc.id, "regr.json") with open(path, "r") as f: regr = json.load(f) self.assertTrue("new_authzr_uri" in regr) def test_save_regr(self): self.storage.save_regr(self.acc, self.mock_client) account_path = os.path.join(self.config.accounts_dir, self.acc.id) self.assertTrue(os.path.exists(account_path)) self.assertTrue(os.path.exists(os.path.join(account_path, "regr.json"))) for file_name in "meta.json", "private_key.json": self.assertFalse( os.path.exists(os.path.join(account_path, file_name))) def test_find_all(self): self.storage.save(self.acc, self.mock_client) 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): # pylint: disable=protected-access self.storage._load_for_server_path = mock.MagicMock( side_effect=["x", errors.AccountStorageError, "z"]) with mock.patch("certbot.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, self.mock_client) 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 _set_server(self, server): self.config.server = server from certbot.account import AccountFileStorage self.storage = AccountFileStorage(self.config) def test_find_all_neither_exists(self): self._set_server( 'https://acme-staging-v02.api.letsencrypt.org/directory') self.assertEqual([], self.storage.find_all()) self.assertEqual([], self.storage.find_all()) self.assertFalse(os.path.islink(self.config.accounts_dir)) def test_find_all_find_before_save(self): self._set_server( 'https://acme-staging-v02.api.letsencrypt.org/directory') self.assertEqual([], self.storage.find_all()) self.storage.save(self.acc, self.mock_client) self.assertEqual([self.acc], self.storage.find_all()) self.assertEqual([self.acc], self.storage.find_all()) self.assertFalse(os.path.islink(self.config.accounts_dir)) # we shouldn't have created a v1 account prev_server_path = 'https://acme-staging.api.letsencrypt.org/directory' self.assertFalse( os.path.isdir( self.config.accounts_dir_for_server_path(prev_server_path))) def test_find_all_save_before_find(self): self._set_server( 'https://acme-staging-v02.api.letsencrypt.org/directory') self.storage.save(self.acc, self.mock_client) self.assertEqual([self.acc], self.storage.find_all()) self.assertEqual([self.acc], self.storage.find_all()) self.assertFalse(os.path.islink(self.config.accounts_dir)) self.assertTrue(os.path.isdir(self.config.accounts_dir)) prev_server_path = 'https://acme-staging.api.letsencrypt.org/directory' self.assertFalse( os.path.isdir( self.config.accounts_dir_for_server_path(prev_server_path))) def test_find_all_server_downgrade(self): # don't use v2 accounts with a v1 url self._set_server( 'https://acme-staging-v02.api.letsencrypt.org/directory') self.assertEqual([], self.storage.find_all()) self.storage.save(self.acc, self.mock_client) self.assertEqual([self.acc], self.storage.find_all()) self._set_server('https://acme-staging.api.letsencrypt.org/directory') self.assertEqual([], self.storage.find_all()) def test_upgrade_version_staging(self): self._set_server('https://acme-staging.api.letsencrypt.org/directory') self.storage.save(self.acc, self.mock_client) self._set_server( 'https://acme-staging-v02.api.letsencrypt.org/directory') self.assertEqual([self.acc], self.storage.find_all()) def test_upgrade_version_production(self): self._set_server('https://acme-v01.api.letsencrypt.org/directory') self.storage.save(self.acc, self.mock_client) self._set_server('https://acme-v02.api.letsencrypt.org/directory') self.assertEqual([self.acc], self.storage.find_all()) @mock.patch('certbot.compat.os.rmdir') def test_corrupted_account(self, mock_rmdir): # pylint: disable=protected-access self._set_server('https://acme-staging.api.letsencrypt.org/directory') self.storage.save(self.acc, self.mock_client) mock_rmdir.side_effect = OSError self.storage._load_for_server_path = mock.MagicMock( side_effect=errors.AccountStorageError) self._set_server( 'https://acme-staging-v02.api.letsencrypt.org/directory') self.assertEqual([], self.storage.find_all()) def test_upgrade_load(self): self._set_server('https://acme-staging.api.letsencrypt.org/directory') self.storage.save(self.acc, self.mock_client) prev_account = self.storage.load(self.acc.id) self._set_server( 'https://acme-staging-v02.api.letsencrypt.org/directory') account = self.storage.load(self.acc.id) self.assertEqual(prev_account, account) def test_upgrade_load_single_account(self): self._set_server('https://acme-staging.api.letsencrypt.org/directory') self.storage.save(self.acc, self.mock_client) prev_account = self.storage.load(self.acc.id) self._set_server_and_stop_symlink( 'https://acme-staging-v02.api.letsencrypt.org/directory') account = self.storage.load(self.acc.id) self.assertEqual(prev_account, account) def test_load_ioerror(self): self.storage.save(self.acc, self.mock_client) mock_open = mock.mock_open() mock_open.side_effect = IOError with mock.patch("six.moves.builtins.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("six.moves.builtins.open", mock_open): self.assertRaises(errors.AccountStorageError, self.storage.save, self.acc, self.mock_client) def test_delete(self): self.storage.save(self.acc, self.mock_client) self.storage.delete(self.acc.id) self.assertRaises(errors.AccountNotFound, self.storage.load, self.acc.id) def test_delete_no_account(self): self.assertRaises(errors.AccountNotFound, self.storage.delete, self.acc.id) def _assert_symlinked_account_removed(self): # create v1 account self._set_server('https://acme-staging.api.letsencrypt.org/directory') self.storage.save(self.acc, self.mock_client) # ensure v2 isn't already linked to it with mock.patch('certbot.constants.LE_REUSE_SERVERS', {}): self._set_server( 'https://acme-staging-v02.api.letsencrypt.org/directory') self.assertRaises(errors.AccountNotFound, self.storage.load, self.acc.id) def _test_delete_folders(self, server_url): # create symlinked servers self._set_server('https://acme-staging.api.letsencrypt.org/directory') self.storage.save(self.acc, self.mock_client) self._set_server( 'https://acme-staging-v02.api.letsencrypt.org/directory') self.storage.load(self.acc.id) # delete starting at given server_url self._set_server(server_url) self.storage.delete(self.acc.id) # make sure we're gone from both urls self._set_server('https://acme-staging.api.letsencrypt.org/directory') self.assertRaises(errors.AccountNotFound, self.storage.load, self.acc.id) self._set_server( 'https://acme-staging-v02.api.letsencrypt.org/directory') self.assertRaises(errors.AccountNotFound, self.storage.load, self.acc.id) def test_delete_folders_up(self): self._test_delete_folders( 'https://acme-staging.api.letsencrypt.org/directory') self._assert_symlinked_account_removed() def test_delete_folders_down(self): self._test_delete_folders( 'https://acme-staging-v02.api.letsencrypt.org/directory') self._assert_symlinked_account_removed() def _set_server_and_stop_symlink(self, server_path): self._set_server(server_path) with open(os.path.join(self.config.accounts_dir, 'foo'), 'w') as f: f.write('bar') def test_delete_shared_account_up(self): self._set_server_and_stop_symlink( 'https://acme-staging-v02.api.letsencrypt.org/directory') self._test_delete_folders( 'https://acme-staging.api.letsencrypt.org/directory') def test_delete_shared_account_down(self): self._set_server_and_stop_symlink( 'https://acme-staging-v02.api.letsencrypt.org/directory') self._test_delete_folders( 'https://acme-staging-v02.api.letsencrypt.org/directory')
class AccountFileStorageTest(test_util.ConfigTestCase): """Tests for certbot.account.AccountFileStorage.""" #pylint: disable=too-many-public-methods def setUp(self): super(AccountFileStorageTest, self).setUp() from certbot.account import AccountFileStorage self.storage = AccountFileStorage(self.config) from certbot.account import Account new_authzr_uri = "hi" self.acc = Account( regr=messages.RegistrationResource( uri=None, body=messages.Registration(), new_authzr_uri=new_authzr_uri), key=KEY) self.mock_client = mock.MagicMock() self.mock_client.directory.new_authz = new_authzr_uri 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, self.mock_client) 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.assertTrue(oct(os.stat(os.path.join( account_path, "private_key.json"))[stat.ST_MODE] & 0o777) in ("0400", "0o400")) # restore loaded = self.storage.load(self.acc.id) self.assertEqual(self.acc, loaded) def test_save_and_restore_old_version(self): """Saved regr should include a new_authzr_uri for older Certbots""" self.storage.save(self.acc, self.mock_client) path = os.path.join(self.config.accounts_dir, self.acc.id, "regr.json") with open(path, "r") as f: regr = json.load(f) self.assertTrue("new_authzr_uri" in regr) def test_save_regr(self): self.storage.save_regr(self.acc, self.mock_client) account_path = os.path.join(self.config.accounts_dir, self.acc.id) self.assertTrue(os.path.exists(account_path)) self.assertTrue(os.path.exists(os.path.join( account_path, "regr.json"))) for file_name in "meta.json", "private_key.json": self.assertFalse(os.path.exists( os.path.join(account_path, file_name))) def test_find_all(self): self.storage.save(self.acc, self.mock_client) 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): # pylint: disable=protected-access self.storage._load_for_server_path = mock.MagicMock( side_effect=["x", errors.AccountStorageError, "z"]) with mock.patch("certbot.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, self.mock_client) 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 _set_server(self, server): self.config.server = server from certbot.account import AccountFileStorage self.storage = AccountFileStorage(self.config) def test_find_all_neither_exists(self): self._set_server('https://acme-staging-v02.api.letsencrypt.org/directory') self.assertEqual([], self.storage.find_all()) self.assertEqual([], self.storage.find_all()) self.assertFalse(os.path.islink(self.config.accounts_dir)) def test_find_all_find_before_save(self): self._set_server('https://acme-staging-v02.api.letsencrypt.org/directory') self.assertEqual([], self.storage.find_all()) self.storage.save(self.acc, self.mock_client) self.assertEqual([self.acc], self.storage.find_all()) self.assertEqual([self.acc], self.storage.find_all()) self.assertFalse(os.path.islink(self.config.accounts_dir)) # we shouldn't have created a v1 account prev_server_path = 'https://acme-staging.api.letsencrypt.org/directory' self.assertFalse(os.path.isdir(self.config.accounts_dir_for_server_path(prev_server_path))) def test_find_all_save_before_find(self): self._set_server('https://acme-staging-v02.api.letsencrypt.org/directory') self.storage.save(self.acc, self.mock_client) self.assertEqual([self.acc], self.storage.find_all()) self.assertEqual([self.acc], self.storage.find_all()) self.assertFalse(os.path.islink(self.config.accounts_dir)) self.assertTrue(os.path.isdir(self.config.accounts_dir)) prev_server_path = 'https://acme-staging.api.letsencrypt.org/directory' self.assertFalse(os.path.isdir(self.config.accounts_dir_for_server_path(prev_server_path))) def test_find_all_server_downgrade(self): # don't use v2 accounts with a v1 url self._set_server('https://acme-staging-v02.api.letsencrypt.org/directory') self.assertEqual([], self.storage.find_all()) self.storage.save(self.acc, self.mock_client) self.assertEqual([self.acc], self.storage.find_all()) self._set_server('https://acme-staging.api.letsencrypt.org/directory') self.assertEqual([], self.storage.find_all()) def test_upgrade_version_staging(self): self._set_server('https://acme-staging.api.letsencrypt.org/directory') self.storage.save(self.acc, self.mock_client) self._set_server('https://acme-staging-v02.api.letsencrypt.org/directory') self.assertEqual([self.acc], self.storage.find_all()) def test_upgrade_version_production(self): self._set_server('https://acme-v01.api.letsencrypt.org/directory') self.storage.save(self.acc, self.mock_client) self._set_server('https://acme-v02.api.letsencrypt.org/directory') self.assertEqual([self.acc], self.storage.find_all()) @mock.patch('os.rmdir') def test_corrupted_account(self, mock_rmdir): # pylint: disable=protected-access self._set_server('https://acme-staging.api.letsencrypt.org/directory') self.storage.save(self.acc, self.mock_client) mock_rmdir.side_effect = OSError self.storage._load_for_server_path = mock.MagicMock( side_effect=errors.AccountStorageError) self._set_server('https://acme-staging-v02.api.letsencrypt.org/directory') self.assertEqual([], self.storage.find_all()) def test_upgrade_load(self): self._set_server('https://acme-staging.api.letsencrypt.org/directory') self.storage.save(self.acc, self.mock_client) prev_account = self.storage.load(self.acc.id) self._set_server('https://acme-staging-v02.api.letsencrypt.org/directory') account = self.storage.load(self.acc.id) self.assertEqual(prev_account, account) def test_upgrade_load_single_account(self): self._set_server('https://acme-staging.api.letsencrypt.org/directory') self.storage.save(self.acc, self.mock_client) prev_account = self.storage.load(self.acc.id) self._set_server_and_stop_symlink('https://acme-staging-v02.api.letsencrypt.org/directory') account = self.storage.load(self.acc.id) self.assertEqual(prev_account, account) def test_load_ioerror(self): self.storage.save(self.acc, self.mock_client) mock_open = mock.mock_open() mock_open.side_effect = IOError with mock.patch("six.moves.builtins.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("six.moves.builtins.open", mock_open): self.assertRaises( errors.AccountStorageError, self.storage.save, self.acc, self.mock_client) def test_delete(self): self.storage.save(self.acc, self.mock_client) self.storage.delete(self.acc.id) self.assertRaises(errors.AccountNotFound, self.storage.load, self.acc.id) def test_delete_no_account(self): self.assertRaises(errors.AccountNotFound, self.storage.delete, self.acc.id) def _assert_symlinked_account_removed(self): # create v1 account self._set_server('https://acme-staging.api.letsencrypt.org/directory') self.storage.save(self.acc, self.mock_client) # ensure v2 isn't already linked to it with mock.patch('certbot.constants.LE_REUSE_SERVERS', {}): self._set_server('https://acme-staging-v02.api.letsencrypt.org/directory') self.assertRaises(errors.AccountNotFound, self.storage.load, self.acc.id) def _test_delete_folders(self, server_url): # create symlinked servers self._set_server('https://acme-staging.api.letsencrypt.org/directory') self.storage.save(self.acc, self.mock_client) self._set_server('https://acme-staging-v02.api.letsencrypt.org/directory') self.storage.load(self.acc.id) # delete starting at given server_url self._set_server(server_url) self.storage.delete(self.acc.id) # make sure we're gone from both urls self._set_server('https://acme-staging.api.letsencrypt.org/directory') self.assertRaises(errors.AccountNotFound, self.storage.load, self.acc.id) self._set_server('https://acme-staging-v02.api.letsencrypt.org/directory') self.assertRaises(errors.AccountNotFound, self.storage.load, self.acc.id) def test_delete_folders_up(self): self._test_delete_folders('https://acme-staging.api.letsencrypt.org/directory') self._assert_symlinked_account_removed() def test_delete_folders_down(self): self._test_delete_folders('https://acme-staging-v02.api.letsencrypt.org/directory') self._assert_symlinked_account_removed() def _set_server_and_stop_symlink(self, server_path): self._set_server(server_path) with open(os.path.join(self.config.accounts_dir, 'foo'), 'w') as f: f.write('bar') def test_delete_shared_account_up(self): self._set_server_and_stop_symlink('https://acme-staging-v02.api.letsencrypt.org/directory') self._test_delete_folders('https://acme-staging.api.letsencrypt.org/directory') def test_delete_shared_account_down(self): self._set_server_and_stop_symlink('https://acme-staging-v02.api.letsencrypt.org/directory') self._test_delete_folders('https://acme-staging-v02.api.letsencrypt.org/directory')