Esempio n. 1
0
    def setUp(self):
        self.test_dir = tempfile.mkdtemp()
        self.path = os.path.join(self.test_dir, "conf1.json")
        self.config = ConfigFile(self.path)

        self.site = DummySite(self.config)
        self.engine = DummyEngine(self.site)

        self._initial_keyring = keyring.get_keyring()
        self.keyring = TestKeyring()
        keyring.set_keyring(self.keyring)

        self.credentials_manager = AWSCredentialsManager(self.engine)
Esempio n. 2
0
class CredentialsManagerTestCase(unittest.TestCase):
    aws_access_key = "123"
    aws_secret_key = "456"

    def setUp(self):
        self.test_dir = tempfile.mkdtemp()
        self.path = os.path.join(self.test_dir, "conf1.json")
        self.config = ConfigFile(self.path)

        self.site = DummySite(self.config)
        self.engine = DummyEngine(self.site)

        self._initial_keyring = keyring.get_keyring()
        self.keyring = TestKeyring()
        keyring.set_keyring(self.keyring)

        self.credentials_manager = AWSCredentialsManager(self.engine)

    def tearDown(self):
        shutil.rmtree(self.test_dir)
        keyring.set_keyring(self._initial_keyring)

    def test_read_config(self):
        """
        Test that credentials can be retrieved from the config
        """
        self.config.set("aws-access-key", self.aws_access_key)
        self.keyring.set_password("aws", self.aws_access_key,
                                  self.aws_secret_key)

        credentials = self.credentials_manager.get_credentials()
        self.assertEqual(2, len(credentials))

        username, password = credentials
        self.assertEqual(self.aws_access_key, username)
        self.assertEqual(self.aws_secret_key, password)

    def test_write_config(self):
        """
        Test that credentials are persisted to a config file
        """
        self.credentials_manager.username = self.aws_access_key
        self.credentials_manager.password = self.aws_secret_key
        self.credentials_manager.save_credentials()

        self.config.load()

        self.assertEqual(self.aws_access_key,
                         self.config.get("aws-access-key"))
        self.assertEqual(self.aws_secret_key,
                         self.keyring.get_password("aws", "123"))
Esempio n. 3
0
    def setUp(self):
        self.test_dir = tempfile.mkdtemp()
        self.path = os.path.join(self.test_dir, "test")
        os.mkdir(self.path)

        self.path1 = os.path.join(self.path, "conf1.json")
        self.path2 = os.path.join(self.path, "conf2.json")
        self.conf1 = ConfigFile(self.path1)
        self.conf2 = ConfigFile(self.path2)

        self.conf1.set("a", 1)
        self.conf1.write()
        self.conf2.set("b", 2)
        self.conf2.write()
Esempio n. 4
0
    def setUp(self):
        self.test_dir = tempfile.mkdtemp()
        self.path = os.path.join(self.test_dir, "test")
        os.mkdir(self.path)

        self.path1 = os.path.join(self.path, "conf1.json")
        self.path2 = os.path.join(self.path, "conf2.json")
        self.conf1 = ConfigFile(self.path1)
        self.conf2 = ConfigFile(self.path2)

        self.conf1.set("a", 1)
        self.conf1.write()
        self.conf2.set("b", 2)
        self.conf2.write()
Esempio n. 5
0
class CredentialsManagerTestCase(unittest.TestCase):
    aws_access_key = "123"
    aws_secret_key = "456"

    def setUp(self):
        self.test_dir = tempfile.mkdtemp()
        self.path = os.path.join(self.test_dir, "conf1.json")
        self.config = ConfigFile(self.path)

        self.site = DummySite(self.config)
        self.engine = DummyEngine(self.site)

        self._initial_keyring = keyring.get_keyring()
        self.keyring = TestKeyring()
        keyring.set_keyring(self.keyring)

        self.credentials_manager = AWSCredentialsManager(self.engine)

    def tearDown(self):
        shutil.rmtree(self.test_dir)
        keyring.set_keyring(self._initial_keyring)

    def test_read_config(self):
        """
        Test that credentials can be retrieved from the config
        """
        self.config.set("aws-access-key", self.aws_access_key)
        self.keyring.set_password("aws", self.aws_access_key, self.aws_secret_key)

        credentials = self.credentials_manager.get_credentials()
        self.assertEqual(2, len(credentials))

        username, password = credentials
        self.assertEqual(self.aws_access_key, username)
        self.assertEqual(self.aws_secret_key, password)

    def test_write_config(self):
        """
        Test that credentials are persisted to a config file
        """
        self.credentials_manager.username = self.aws_access_key
        self.credentials_manager.password = self.aws_secret_key
        self.credentials_manager.save_credentials()

        self.config.load()

        self.assertEqual(self.aws_access_key, self.config.get("aws-access-key"))
        self.assertEqual(self.aws_secret_key, self.keyring.get_password("aws", "123"))
    def setUp(self):
        self.clear_django_settings()
        self.test_dir = tempfile.mkdtemp()
        self.path = os.path.join(self.test_dir, "test")
        os.mkdir(self.path)
        for dir in ['pages', 'static', 'templates', 'plugins']:
            os.mkdir(os.path.join(self.path, dir))

        conf_path = os.path.join(self.path, "config.json")
        self.conf_file = ConfigFile(conf_path)
        self.conf_file.set("collections", {})
        self.conf_file.write()

        for dir in ['blog', 'articles', 'docs']:
            now = datetime.now()
            path = os.path.join(self.path, 'pages', dir)
            os.mkdir(path)
            for i in range(1, 4):
                date = now + timedelta(hours=i)
                with open(os.path.join(path, 'page-{0}.html'.format(i)), 'w') as fp:
                    fp.write('title: {0} {1}\n'.format(dir, i))
                    fp.write('created: {0:%Y-%m-%dT%H:%M}\n'.format(date))
                    fp.write('\n')
                    fp.write('# Hello World\n')

            if dir == 'docs':
                with open(os.path.join(path, 'toc'), 'w') as fp:
                    fp.write('docs/page-2.html\n')
                    fp.write('docs/page-3.html\n')
                    fp.write('docs/page-1.html\n')

        self.site = Site(path=self.path, config_paths=conf_path)
Esempio n. 7
0
    def setUp(self):
        self.test_dir = tempfile.mkdtemp()
        self.path = os.path.join(self.test_dir, "conf1.json")
        self.config = ConfigFile(self.path)

        self.site = DummySite(self.config)
        self.engine = DummyEngine(self.site)

        self._initial_keyring = keyring.get_keyring()
        self.keyring = TestKeyring()
        keyring.set_keyring(self.keyring)

        self.credentials_manager = AWSCredentialsManager(self.engine)
Esempio n. 8
0
    def __init__(self, paths):
        """
        Load all the config files passed.
        Make sure not to load the same one twice
        """
        self.configs = []

        loaded_paths = set()
        for path in paths:
            if path not in loaded_paths:
                self.configs.append(ConfigFile(path))
                loaded_paths.add(path)

        self.configs.append(ConfigFallback())

        logger.debug("Loaded configs: %s",
                     ', '.join([config.path for config in self.configs]))
Esempio n. 9
0
class TestConfigRouter(unittest.TestCase):
    """
    Test that the config router manages multiple files correctly.
    """

    def setUp(self):
        self.test_dir = tempfile.mkdtemp()
        self.path = os.path.join(self.test_dir, "test")
        os.mkdir(self.path)

        self.path1 = os.path.join(self.path, "conf1.json")
        self.path2 = os.path.join(self.path, "conf2.json")
        self.conf1 = ConfigFile(self.path1)
        self.conf2 = ConfigFile(self.path2)

        self.conf1.set("a", 1)
        self.conf1.write()
        self.conf2.set("b", 2)
        self.conf2.write()

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

    def test_read(self):
        """
        Check that the config router reads correctly from the filesystem
        """
        router = ConfigRouter([self.path1, self.path2])

        self.assertEqual(router.get("a"), 1)
        self.assertEqual(router.get("b"), 2)
        self.assertEqual(router.get("c"), None)

    def test_read_write(self):
        """
        Check that our config is readable after writing it
        """
        router = ConfigRouter([self.path1, self.path2])

        router.set("a", 3)
        router.set("b", 4)

        self.assertEqual(3, router.get("a"))
        self.assertEqual(4, router.get("b"))

    def test_write(self):
        """
        Check that the config router writes correctly to the filesystem
        """
        router = ConfigRouter([self.path1, self.path2])
        router.set("a", 3)
        router.set("b", 4)
        router.write()

        self.conf1.load()
        self.conf2.load()

        self.assertEqual(self.conf1.get("a"), 3)
        self.assertEqual(self.conf1.get("b"), None)
        self.assertEqual(self.conf2.get("b"), 4)
        self.assertEqual(self.conf2.get("a"), None)

    def test_collision(self):
        """
        Check that we get the right key when there is a collision
        """
        self.conf1.set("b", 3)
        self.conf2.set("a", 4)
        self.conf1.write()
        self.conf2.write()

        router = ConfigRouter([self.path1, self.path2])

        self.assertEqual(router.get("a"), 1)
        self.assertEqual(router.get("b"), 3)

    def test_duplicate(self):
        """
        Check that the config router handles duplicate files properly.
        """
        router = ConfigRouter([self.path1, self.path1])
        router.set("a", 3)
        router.write()

        self.conf1.load()
        self.assertEqual(self.conf1.get("a"), 3)

    def test_nested(self):
        """
        Test that we support nested config for context
        """
        self.conf1.set("context", {"k1": "v1"})
        self.conf2.set("context", {"k2": "v2"})
        self.conf1.write()
        self.conf2.write()

        router = ConfigRouter([self.path1, self.path2])
        context = router.get("context", default={}, nested=True)

        self.assertEqual(context.get("k1"), "v1")
        self.assertEqual(context.get("k2"), "v2")

    def test_dirty(self):
        """
        Test that we don't re-write files that we haven't changed
        """

        self.conf1.set("a", "b")
        self.conf1.write()

        with open(self.path1, "w") as f:
            f.write("canary")

        self.conf1.write()

        with open(self.path1) as f:
            self.assertEqual("canary", f.read())
Esempio n. 10
0
class TestConfigRouter(unittest.TestCase):
    """
    Test that the config router manages multiple files correctly.
    """
    def setUp(self):
        self.test_dir = tempfile.mkdtemp()
        self.path = os.path.join(self.test_dir, "test")
        os.mkdir(self.path)

        self.path1 = os.path.join(self.path, "conf1.json")
        self.path2 = os.path.join(self.path, "conf2.json")
        self.conf1 = ConfigFile(self.path1)
        self.conf2 = ConfigFile(self.path2)

        self.conf1.set("a", 1)
        self.conf1.write()
        self.conf2.set("b", 2)
        self.conf2.write()

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

    def test_read(self):
        """
        Check that the config router reads correctly from the filesystem
        """
        router = ConfigRouter([self.path1, self.path2])

        self.assertEqual(router.get("a"), 1)
        self.assertEqual(router.get("b"), 2)
        self.assertEqual(router.get("c"), None)

    def test_read_write(self):
        """
        Check that our config is readable after writing it
        """
        router = ConfigRouter([self.path1, self.path2])

        router.set("a", 3)
        router.set("b", 4)

        self.assertEqual(3, router.get("a"))
        self.assertEqual(4, router.get("b"))

    def test_write(self):
        """
        Check that the config router writes correctly to the filesystem
        """
        router = ConfigRouter([self.path1, self.path2])
        router.set("a", 3)
        router.set("b", 4)
        router.write()

        self.conf1.load()
        self.conf2.load()

        self.assertEqual(self.conf1.get("a"), 3)
        self.assertEqual(self.conf1.get("b"), None)
        self.assertEqual(self.conf2.get("b"), 4)
        self.assertEqual(self.conf2.get("a"), None)

    def test_collision(self):
        """
        Check that we get the right key when there is a collision
        """
        self.conf1.set("b", 3)
        self.conf2.set("a", 4)
        self.conf1.write()
        self.conf2.write()

        router = ConfigRouter([self.path1, self.path2])

        self.assertEqual(router.get("a"), 1)
        self.assertEqual(router.get("b"), 3)

    def test_duplicate(self):
        """
        Check that the config router handles duplicate files properly.
        """
        router = ConfigRouter([self.path1, self.path1])
        router.set("a", 3)
        router.write()

        self.conf1.load()
        self.assertEqual(self.conf1.get("a"), 3)

    def test_nested(self):
        """
        Test that we support nested config for context
        """
        self.conf1.set("context", {"k1": "v1"})
        self.conf2.set("context", {"k2": "v2"})
        self.conf1.write()
        self.conf2.write()

        router = ConfigRouter([self.path1, self.path2])
        context = router.get("context", default={}, nested=True)

        self.assertEqual(context.get("k1"), "v1")
        self.assertEqual(context.get("k2"), "v2")

    def test_dirty(self):
        """
        Test that we don't re-write files that we haven't changed
        """

        self.conf1.set("a", "b")
        self.conf1.write()

        with open(self.path1, "w") as f:
            f.write("canary")

        self.conf1.write()

        with open(self.path1) as f:
            self.assertEqual("canary", f.read())

    def test_missing_file(self):
        """
        Test that we don't throw on a missing file, and that the configuration
        remains in a consistent state.
        """
        wrong_path = os.path.join(self.path, "does_not_exist.json")

        self.conf1.set("context", {"k1": "v1"})
        self.conf1.write()

        router = ConfigRouter([wrong_path, self.path1])

        self.assertEqual(router.get("context").get("k1"), "v1")

    def test_broken_file(self):
        """
        Test that we don't throw on a broken file, and that the configuration
        remains in a consistent state.
        """

        with open(self.path1, "w") as f:
            f.write("{broken}")

        self.conf2.set("context", {"k1": "v1"})
        self.conf2.write()

        router = ConfigRouter([self.path1, self.path2])

        self.assertEqual(router.get("context").get("k1"), "v1")
class CollectionsPluginTest(unittest.TestCase):

    def setUp(self):
        self.clear_django_settings()
        self.test_dir = tempfile.mkdtemp()
        self.path = os.path.join(self.test_dir, "test")
        os.mkdir(self.path)
        for dir in ['pages', 'static', 'templates', 'plugins']:
            os.mkdir(os.path.join(self.path, dir))

        conf_path = os.path.join(self.path, "config.json")
        self.conf_file = ConfigFile(conf_path)
        self.conf_file.set("collections", {})
        self.conf_file.write()

        for dir in ['blog', 'articles', 'docs']:
            now = datetime.now()
            path = os.path.join(self.path, 'pages', dir)
            os.mkdir(path)
            for i in range(1, 4):
                date = now + timedelta(hours=i)
                with open(os.path.join(path, 'page-{0}.html'.format(i)), 'w') as fp:
                    fp.write('title: {0} {1}\n'.format(dir, i))
                    fp.write('created: {0:%Y-%m-%dT%H:%M}\n'.format(date))
                    fp.write('\n')
                    fp.write('# Hello World\n')

            if dir == 'docs':
                with open(os.path.join(path, 'toc'), 'w') as fp:
                    fp.write('docs/page-2.html\n')
                    fp.write('docs/page-3.html\n')
                    fp.write('docs/page-1.html\n')

        self.site = Site(path=self.path, config_paths=conf_path)

    def tearDown(self):
        shutil.rmtree(self.test_dir)
        self.clear_django_settings()

    def clear_django_settings(self):
        django.conf.settings._wrapped = django.conf.empty

    def test_init(self):
        coll = Collection('Articles', 'a/', 'article.html')
        self.assertEquals('<Articles: a/ (0 pages)>', coll.__repr__())
        self.assertEquals('Articles', coll.title)
        self.assertEquals('a/', coll.path)
        self.assertEquals('article.html', coll.template)
        self.assertEquals([], coll.pages)

    def test_page_filter(self):
        pages = [
            Page(self.site, 'articles/page-1.html'),
            Page(self.site, 'blog/page-1.html'),
        ]
        for path in ['articles/', 'blog/']:
            coll = Collection('Collection', path, 'template.html', pages=pages)
            self.assertEquals(1, len(coll.pages))
            self.assertEquals(1, len(coll))
            for page in coll.pages:
                self.assertTrue(page['path'].startswith(path))

    def test_contains_page(self):
        pages = [
            Page(self.site, 'blog/page-1.html'),
            Page(self.site, 'blog/page-2.html'),
            Page(self.site, 'blog/page-3.html'),
        ]
        other_page = Page(self.site, 'blog/page-4.html')
        coll = Collection('Collection', 'blog/', 'template.html', pages=pages)
        coll.sort('date', reverse=True)

        self.assertEquals(['blog/page-3.html', 'blog/page-2.html', 'blog/page-1.html'],
                          [p['path'] for p in coll.pages])

        self.assertTrue(coll.contains_page(pages[0]))
        self.assertFalse(coll.contains_page(other_page))

        page_ctx = coll.page_context(pages[0])
        self.assertEquals('blog/page-1.html', page_ctx['path'])

        with self.assertRaises(ValueError) as cm:
            page_ctx = coll.page_context(other_page)
        self.assertEquals("'blog/page-4.html' is not in list", str(cm.exception))

    def test_navigation(self):
        pages = [
            Page(self.site, 'blog/page-1.html'),
            Page(self.site, 'blog/page-2.html'),
            Page(self.site, 'blog/page-3.html'),
        ]
        coll = Collection('Collection', 'blog/', 'template.html', pages=pages)
        coll.sort('date', reverse=False)
        coll.create_navigation()

        self.assertEquals('blog/page-2.html', coll[0]['prev_post']['path'])
        self.assertEquals('blog/page-3.html', coll[1]['prev_post']['path'])

        self.assertEquals('blog/page-2.html', coll[2]['next_post']['path'])
        self.assertEquals('blog/page-1.html', coll[1]['next_post']['path'])

    def test_sortbytoc(self):
        pages = [
            Page(self.site, 'docs/page-1.html'),
            Page(self.site, 'docs/page-2.html'),
            Page(self.site, 'docs/page-3.html')
        ]
        coll = Collection('Collection', 'docs/', 'docs.html', pages=pages)
        coll.sort(toc=os.path.join(self.path, 'pages', 'docs', 'toc'))

        self.assertEquals(['docs/page-2.html', 'docs/page-3.html', 'docs/page-1.html'],
                          [p['path'] for p in coll.pages])


    def test_preBuild(self):
        # TODO: test preBuild method
        pass

    def test_preBuildPage(self):
        # TODO: test preBuildPage method
        pass