def test_page_html(self): config_file = os.path.join('configs', 'local.json') msite = multisite.Multisite(config_file) self.assertEqual( msite.page('local-site/other.html', None), Contents.HTML )
def test_clone(self): url = 'https://github.com/cmccandless/hundredpushups.git' site = multisite.Multisite() site.add_site(name='git-site', source=url, source_type='git') index_url = ('https://raw.githubusercontent.com/' 'cmccandless/hundredpushups/master/index.md') expected = requests.get(index_url).text self.assertFileContentsEqual('git-site/index.md', expected)
def test_bad_directory_path(self): config_file = os.path.join('configs', 'local.json') msite = multisite.Multisite(config_file=config_file) with self.assertRaises(OSError): msite.add_site( 'bad-dir', os.path.join('archives', 'zip-site.zip'), source_type='local' )
def test_not_found_default(self): old_cwd = os.getcwd() os.chdir(self.workspace) try: config_file = 'not_found.json' msite = multisite.Multisite(config_file) status, response = msite.get('/does-not-exist') self.assertEqual(status, 404) self.assertEqual(response, '404: Page not found') finally: os.chdir(old_cwd)
def test_homepage_not_found(self): old_cwd = os.getcwd() os.chdir(self.workspace) try: config_file = 'homepage-not-found.json' msite = multisite.Multisite(config_file) status, response = msite.homepage() self.assertEqual(status, 404) self.assertEqual(response, '404: Page not found') finally: os.chdir(old_cwd)
def add_git_site(opts): site = multisite.Multisite(opts.config) if opts.name is None: setattr(opts, 'name', opts.source_path.split('/')[-1]) site.add_site( opts.name, opts.source_path, source_type='git', auto_update=opts.auto_update, git_branch=opts.branch, git_remote=opts.remote )
def test_existing_config_file(self): config_file = os.path.join('configs', 'local.json') msite = multisite.Multisite(config_file=config_file) expected = { "local-site": { "name": "local-site", "source": "local-site/", "location": "local-site/", "source_type": "local", "auto_update": False } } self.assertDictEqual(msite.sites, expected)
def test_add_local_site(self): config_file = os.path.join(self.workspace, 'add_local_site.json') msite = multisite.Multisite(config_file=config_file) msite.add_site('local', 'local-site', source_type='local') expected = { "local": { "name": "local", "source": "local-site", "location": "local-site", "source_type": "local", "auto_update": False } } self.assertDictEqual(msite.sites, expected)
def test_add_archive_site(self): old_cwd = os.getcwd() os.chdir(self.workspace) try: config_file = 'add_archive_site.json' msite = multisite.Multisite(config_file=config_file) zip_file = os.path.join('..', 'archives', 'zip-site.zip') msite.add_site('zip-site', zip_file, source_type='archive') expected = { "zip-site": { "name": "zip-site", "source": zip_file, "location": os.path.join('.', "zip-site"), "source_type": "archive", "auto_update": False } } self.assertDictEqual(msite.sites, expected) finally: os.chdir(old_cwd)
def run(opts): site = multisite.Multisite(opts.config) class CustomHandler(http.server.BaseHTTPRequestHandler): def do_GET(self): status, response = site.get(self.path) self.send_response(status) self.send_header("Content-type", "text-html") self.end_headers() self.wfile.write(response.encode('utf-8')) try: server = http.server.HTTPServer( ('localhost', opts.port), CustomHandler ) print('Waiting for requests...') server.serve_forever() except KeyboardInterrupt: print('Shutting down server...') server.socket.close()
def test_subsite_page_not_found(self): config_file = os.path.join('configs', 'local.json') msite = multisite.Multisite(config_file) status, response = msite.get('/local-site/does-not-exist') self.assertEqual(status, 404) self.assertEqual(response, Contents.NOT_FOUND)
def test_get_subsite_homepage(self): config_file = os.path.join('configs', 'local.json') msite = multisite.Multisite(config_file) status, response = msite.get('/local-site/') self.assertEqual(status, 200) self.assertEqual(response, Contents.MD)
def test_get_subsite_page_flexible_extension(self): config_file = os.path.join('configs', 'local.json') msite = multisite.Multisite(config_file) status, response = msite.get('/local-site/other') self.assertEqual(status, 200) self.assertEqual(response, Contents.HTML)
def test_get_root_returns_homepage(self): config_file = os.path.join('configs', 'local.json') msite = multisite.Multisite(config_file) status, response = msite.get('/') self.assertEqual(status, 200) self.assertEqual(response, Contents.HOMEPAGE)
def test_page_flexible_extension_markdown(self): config_file = os.path.join('configs', 'local.json') msite = multisite.Multisite(config_file) self.assertEqual(msite.page('local-site/index'), Contents.MD)
def test_page_flexible_extension_html(self): config_file = os.path.join('configs', 'local.json') msite = multisite.Multisite(config_file) self.assertEqual(msite.page('local-site/other'), Contents.HTML)
def test_page_markdown(self): config_file = os.path.join('configs', 'local.json') msite = multisite.Multisite(config_file) self.assertEqual(msite.page('local-site/index.md', None), Contents.MD)
def add_local_site(opts): site = multisite.Multisite(opts.config) if opts.name is None: bname = os.path.basename(opts.source_path.rstrip(os.path.sep)) setattr(opts, 'name', bname) site.add_site(opts.name, opts.source_path, source_type='local')
def add_archive_site(opts): site = multisite.Multisite(opts.config) if opts.name is None: bname = os.path.basename(opts.source_path) setattr(opts, 'name', os.path.splitext(bname)[0]) site.add_site(opts.name, opts.source_path, source_type='archive')
def static(opts): site = multisite.Multisite(opts.config) site.make_static(opts.directory)