def setUp(self): self.private_config_file = PrivateConfigFile( 'private_config.yml', envs=['production']) self.ordbok = Ordbok( config_files=['config.yml', 'local_config.yml', self.private_config_file] )
def setUp(self): self.private_config_file = PrivateConfigFile('private_config.yml', envs=['production']) self.ordbok = Ordbok(config_files=[ 'config.yml', 'local_config.yml', self.private_config_file ])
class OrdbokPrivateConfigFileTestCase(unittest.TestCase): def setUp(self): self.private_config_file = PrivateConfigFile('private_config.yml', envs=['production']) self.ordbok = Ordbok(config_files=[ 'config.yml', 'local_config.yml', self.private_config_file ]) @unittest.skipIf(os.environ.get('SKIP_ENCRYPT_TEST'), 'as env var to skip lengthy test') @fudge.patch('ordbok.config_private.open_wrapper') def test_ordbok_private_config(self, fudged_open): fudged_config_files_with_private = deepcopy(fudged_config_files) fudged_config_files_with_private.update({ u'private_config.yml': u""" OAUTH_KEY: 'super_secret_key' OAUTH_SECRET: 'even_secreter_secret' """, }) # set directly instead of completely rewriting 'config.yml' self.ordbok['PRIVATE_KEY_ORDBOK'] = 'foobarbaz' self.private_config_file.init_config(self.ordbok) encrypted_content = self.private_config_file._encrypt_content( fudged_config_files_with_private[u'private_config.yml']) fudged_config_files_with_private.update({ u'private_config.yml.private': encrypted_content, }) fudged_open.is_callable().calls( fake_file_factory(fudged_config_files_with_private)) self.ordbok['ENVIRONMENT'] = 'production' self.ordbok.load() self.assertEquals(self.ordbok['OAUTH_KEY'], 'super_secret_key') self.assertEquals(self.ordbok['OAUTH_SECRET'], 'even_secreter_secret') @fudge.patch('ordbok.config_private.open_wrapper') @fudge.patch('os.path.exists') def test_ordbok_private_config_no_encrypted_file(self, fudged_open, fudged_path_exists): fudged_config_files_with_private = deepcopy(fudged_config_files) fudged_config_files_with_private.update({ u'private_config.yml': u""" OAUTH_KEY: 'super_secret_key' OAUTH_SECRET: 'even_secreter_secret' """, }) # set directly instead of completely rewriting 'config.yml' self.ordbok['PRIVATE_KEY_ORDBOK'] = 'foobarbaz' self.private_config_file.init_config(self.ordbok) fudged_open.is_callable().calls( fake_file_factory(fudged_config_files_with_private)) fudged_path_exists.is_callable().calls( fake_path_exists_factory(fudged_config_files_with_private)) self.ordbok['ENVIRONMENT'] = 'production' with self.assertRaises(OrdbokMissingEncryptedPrivateConfigFile): self.ordbok.load() @fudge.patch('ordbok.config_private.open_wrapper') @fudge.patch('os.path.exists') def test_ordbok_private_config_no_file(self, fudged_open, fudged_path_exists): # set directly instead of completely rewriting 'config.yml' self.ordbok['PRIVATE_KEY_ORDBOK'] = 'foobarbaz' self.private_config_file.init_config(self.ordbok) fudged_open.is_callable().calls(fake_file_factory(fudged_config_files)) fudged_path_exists.is_callable().calls( fake_path_exists_factory(fudged_config_files)) self.ordbok['ENVIRONMENT'] = 'production' with self.assertRaises(OrdbokMissingPrivateConfigFile): self.ordbok.load() @fudge.patch('ordbok.config_private.open_wrapper') @fudge.patch('os.path.exists') def test_ordbok_private_config_load_decrypted(self, fudged_open, fudged_path_exists): fudged_config_files_with_private = deepcopy(fudged_config_files) file_content = u""" OAUTH_KEY: 'super_secret_key' OAUTH_SECRET: 'even_secreter_secret' """ fudged_config_files_with_private.update( {u'private_config.yml': file_content}) # set directly instead of completely rewriting 'config.yml' self.ordbok['PRIVATE_KEY_ORDBOK'] = 'foobarbaz' self.private_config_file.init_config(self.ordbok) fudged_open.is_callable().calls( fake_file_factory(fudged_config_files_with_private)) fudged_path_exists.is_callable().calls( fake_path_exists_factory(fudged_config_files_with_private)) self.ordbok['ENVIRONMENT'] = 'production' decrypted_file = self.private_config_file._load_decrypted_file() self.assertEqual(file_content, decrypted_file) @fudge.patch('ordbok.config_private.open_wrapper') @fudge.patch('os.path.exists') def test_ordbok_private_config_load_decrypted_no_file( self, fudged_open, fudged_path_exists): # set directly instead of completely rewriting 'config.yml' self.ordbok['PRIVATE_KEY_ORDBOK'] = 'foobarbaz' self.private_config_file.init_config(self.ordbok) fudged_open.is_callable().calls(fake_file_factory(fudged_config_files)) fudged_path_exists.is_callable().calls( fake_path_exists_factory(fudged_config_files)) self.ordbok['ENVIRONMENT'] = 'production' with self.assertRaises(OrdbokMissingPrivateConfigFile): self.private_config_file._load_decrypted_file() @fudge.patch('ordbok.config_private.open_wrapper') @mock.patch.object(PrivateConfigFile, '_load_yaml') def test_ordbok_private_config_envs(self, fudged_open, mock_load_yaml): fudged_open.is_callable().calls(fake_file_factory(fudged_config_files)) mock_load_yaml.return_value = '' self.ordbok.load() self.assertFalse(mock_load_yaml.called) @fudge.patch('ordbok.config_private.open_wrapper') def test_ordbok_private_config_no_private_key(self, fudged_open): self.ordbok['ENVIRONMENT'] = 'production' fudged_config_files.update({ u'private_config.yml.private': 'foobarbaz', }) fudged_open.is_callable().calls(fake_file_factory(fudged_config_files)) with self.assertRaises(OrdbokMissingPrivateKeyException): self.ordbok.load() @fudge.patch('ordbok.config_private.open_wrapper') @mock.patch.object(PrivateConfigFile, '_load_yaml') def test_ordbok_private_config_no_envs(self, fudged_open, mock_load_yaml): fudged_open.is_callable().calls(fake_file_factory(fudged_config_files)) mock_load_yaml.return_value = '' self.ordbok['ENVIRONMENT'] = 'production' self.ordbok.load() self.assertTrue(mock_load_yaml.called) def test_ordbok_private_key(self): self.ordbok['PRIVATE_KEY_ORDBOK'] = 'foobarbaz' self.assertEqual(self.ordbok.private_file_key, 'foobarbaz')
class OrdbokPrivateConfigFileTestCase(unittest.TestCase): def setUp(self): self.private_config_file = PrivateConfigFile( 'private_config.yml', envs=['production']) self.ordbok = Ordbok( config_files=['config.yml', 'local_config.yml', self.private_config_file] ) @unittest.skipIf(os.environ.get('SKIP_ENCRYPT_TEST'), 'as env var to skip lengthy test') @fudge.patch('ordbok.config_private.open_wrapper') def test_ordbok_private_config(self, fudged_open): fudged_config_files_with_private = deepcopy(fudged_config_files) fudged_config_files_with_private.update({ u'private_config.yml': u""" OAUTH_KEY: 'super_secret_key' OAUTH_SECRET: 'even_secreter_secret' """, }) # set directly instead of completely rewriting 'config.yml' self.ordbok['PRIVATE_KEY_ORDBOK'] = 'foobarbaz' self.private_config_file.init_config(self.ordbok) encrypted_content = self.private_config_file._encrypt_content( fudged_config_files_with_private[u'private_config.yml']) fudged_config_files_with_private.update({ u'private_config.yml.private': encrypted_content, }) fudged_open.is_callable().calls( fake_file_factory(fudged_config_files_with_private)) self.ordbok['ENVIRONMENT'] = 'production' self.ordbok.load() self.assertEquals(self.ordbok['OAUTH_KEY'], 'super_secret_key') self.assertEquals(self.ordbok['OAUTH_SECRET'], 'even_secreter_secret') @fudge.patch('ordbok.config_private.open_wrapper') @fudge.patch('os.path.exists') def test_ordbok_private_config_no_encrypted_file( self, fudged_open, fudged_path_exists): fudged_config_files_with_private = deepcopy(fudged_config_files) fudged_config_files_with_private.update({ u'private_config.yml': u""" OAUTH_KEY: 'super_secret_key' OAUTH_SECRET: 'even_secreter_secret' """, }) # set directly instead of completely rewriting 'config.yml' self.ordbok['PRIVATE_KEY_ORDBOK'] = 'foobarbaz' self.private_config_file.init_config(self.ordbok) fudged_open.is_callable().calls( fake_file_factory(fudged_config_files_with_private)) fudged_path_exists.is_callable().calls( fake_path_exists_factory(fudged_config_files_with_private)) self.ordbok['ENVIRONMENT'] = 'production' with self.assertRaises(OrdbokMissingEncryptedPrivateConfigFile): self.ordbok.load() @fudge.patch('ordbok.config_private.open_wrapper') @fudge.patch('os.path.exists') def test_ordbok_private_config_no_file( self, fudged_open, fudged_path_exists): # set directly instead of completely rewriting 'config.yml' self.ordbok['PRIVATE_KEY_ORDBOK'] = 'foobarbaz' self.private_config_file.init_config(self.ordbok) fudged_open.is_callable().calls( fake_file_factory(fudged_config_files)) fudged_path_exists.is_callable().calls( fake_path_exists_factory(fudged_config_files)) self.ordbok['ENVIRONMENT'] = 'production' with self.assertRaises(OrdbokMissingPrivateConfigFile): self.ordbok.load() @fudge.patch('ordbok.config_private.open_wrapper') @fudge.patch('os.path.exists') def test_ordbok_private_config_load_decrypted( self, fudged_open, fudged_path_exists): fudged_config_files_with_private = deepcopy(fudged_config_files) file_content = u""" OAUTH_KEY: 'super_secret_key' OAUTH_SECRET: 'even_secreter_secret' """ fudged_config_files_with_private.update({ u'private_config.yml': file_content }) # set directly instead of completely rewriting 'config.yml' self.ordbok['PRIVATE_KEY_ORDBOK'] = 'foobarbaz' self.private_config_file.init_config(self.ordbok) fudged_open.is_callable().calls( fake_file_factory(fudged_config_files_with_private)) fudged_path_exists.is_callable().calls( fake_path_exists_factory(fudged_config_files_with_private)) self.ordbok['ENVIRONMENT'] = 'production' decrypted_file = self.private_config_file._load_decrypted_file() self.assertEqual(file_content, decrypted_file) @fudge.patch('ordbok.config_private.open_wrapper') @fudge.patch('os.path.exists') def test_ordbok_private_config_load_decrypted_no_file( self, fudged_open, fudged_path_exists): # set directly instead of completely rewriting 'config.yml' self.ordbok['PRIVATE_KEY_ORDBOK'] = 'foobarbaz' self.private_config_file.init_config(self.ordbok) fudged_open.is_callable().calls( fake_file_factory(fudged_config_files)) fudged_path_exists.is_callable().calls( fake_path_exists_factory(fudged_config_files)) self.ordbok['ENVIRONMENT'] = 'production' with self.assertRaises(OrdbokMissingPrivateConfigFile): self.private_config_file._load_decrypted_file() @fudge.patch('ordbok.config_private.open_wrapper') @mock.patch.object(PrivateConfigFile, '_load_yaml') def test_ordbok_private_config_envs( self, fudged_open, mock_load_yaml): fudged_open.is_callable().calls( fake_file_factory(fudged_config_files)) mock_load_yaml.return_value = '' self.ordbok.load() self.assertFalse(mock_load_yaml.called) @fudge.patch('ordbok.config_private.open_wrapper') def test_ordbok_private_config_no_private_key(self, fudged_open): self.ordbok['ENVIRONMENT'] = 'production' fudged_config_files.update({ u'private_config.yml.private': 'foobarbaz', }) fudged_open.is_callable().calls(fake_file_factory(fudged_config_files)) with self.assertRaises(OrdbokMissingPrivateKeyException): self.ordbok.load() @fudge.patch('ordbok.config_private.open_wrapper') @mock.patch.object(PrivateConfigFile, '_load_yaml') def test_ordbok_private_config_no_envs( self, fudged_open, mock_load_yaml): fudged_open.is_callable().calls( fake_file_factory(fudged_config_files)) mock_load_yaml.return_value = '' self.ordbok['ENVIRONMENT'] = 'production' self.ordbok.load() self.assertTrue(mock_load_yaml.called) def test_ordbok_private_key(self): self.ordbok['PRIVATE_KEY_ORDBOK'] = 'foobarbaz' self.assertEqual(self.ordbok.private_file_key, 'foobarbaz')