def test_publish_raises_when_unable_to_create_temp_file(): mox = Mox() http_request, model_base, manager, model, queryset = get_mocks(mox) fs_mock = mox.CreateMockAnything() fs_mock.join("test_web_root", "some_path/index.html").AndReturn("test_web_root/some_path") fs_mock.dirname("test_web_root/some_path").AndReturn("test_web_root") fs_mock.exists("test_web_root").AndReturn(True) fs_mock.tempfile(directory="test_web_root").AndRaise(ValueError()) settings = CustomSettings(WEB_ROOT="test_web_root") mox.ReplayAll() instance = StaticGenerator(http_request=http_request, model_base=model_base, manager=manager, model=model, queryset=queryset, settings=settings, fs=fs_mock) try: instance.publish_from_path("some_path", content="some_content") except StaticGeneratorException, e: assert str(e) == 'Could not create the file: test_web_root/some_path' mox.VerifyAll() return
def test_publish_from_path(): mox = Mox() http_request, model_base, manager, model, queryset = get_mocks(mox) fs_mock = mox.CreateMockAnything() fs_mock.join("test_web_root", "some_path/index.html").AndReturn("test_web_root/some_path") fs_mock.dirname("test_web_root/some_path").AndReturn("test_web_root") fs_mock.exists("test_web_root").AndReturn(True) f = mox.CreateMockAnything() filename = "some_temp_file" fs_mock.tempfile(directory="test_web_root").AndReturn([f, filename]) fs_mock.write(f, "some_content") fs_mock.close(f) fs_mock.chmod(filename, stat.S_IREAD | stat.S_IWRITE | stat.S_IWUSR | stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH) fs_mock.rename('some_temp_file', 'test_web_root/some_path') settings = CustomSettings(WEB_ROOT="test_web_root") mox.ReplayAll() instance = StaticGenerator(http_request=http_request, model_base=model_base, manager=manager, model=model, queryset=queryset, settings=settings, fs=fs_mock) instance.publish_from_path("some_path", content="some_content") mox.VerifyAll()
def test_publish_from_path(): mox = Mox() http_request, model_base, manager, model, queryset = get_mocks(mox) fs_mock = mox.CreateMockAnything() fs_mock.join("test_web_root", "some_path").AndReturn("test_web_root/some_path") fs_mock.dirname("test_web_root/some_path").AndReturn("test_web_root") fs_mock.exists("test_web_root").AndReturn(True) f = mox.CreateMockAnything() filename = "some_temp_file" fs_mock.tempfile(directory="test_web_root").AndReturn([f, filename]) fs_mock.write(f, "some_content") fs_mock.close(f) fs_mock.chmod(filename, stat.S_IREAD | stat.S_IWRITE | stat.S_IWUSR | stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH) fs_mock.rename('some_temp_file', 'test_web_root/some_path') settings = CustomSettings(WEB_ROOT="test_web_root") mox.ReplayAll() instance = StaticGenerator(http_request=http_request, model_base=model_base, manager=manager, model=model, queryset=queryset, settings=settings, fs=fs_mock) instance.publish_from_path("some_path", content="some_content") mox.VerifyAll()
def test_publish_from_path_creates_current_file(self): instance = StaticGenerator() instance.publish_from_path('/some_path', content='some_content') self.assertEqual('some_content', open('test_web_root/fresh/some_path').read())
def test_publish_from_path_hard_links_stale_file(self): instance = StaticGenerator() instance.publish_from_path('/some_path', content='some_content') self.assertEqual(os.stat('test_web_root/fresh/some_path').st_ino, os.stat('test_web_root/stale/some_path').st_ino)
def test_publish_raises_when_unable_to_create_temp_file(): mox = Mox() http_request, model_base, manager, model, queryset = get_mocks(mox) fs_mock = mox.CreateMockAnything() fs_mock.join("test_web_root", "some_path").AndReturn("test_web_root/some_path") fs_mock.dirname("test_web_root/some_path").AndReturn("test_web_root") fs_mock.exists("test_web_root").AndReturn(True) fs_mock.tempfile(directory="test_web_root").AndRaise(ValueError()) settings = CustomSettings(WEB_ROOT="test_web_root") mox.ReplayAll() instance = StaticGenerator(http_request=http_request, model_base=model_base, manager=manager, model=model, queryset=queryset, settings=settings, fs=fs_mock) try: instance.publish_from_path("some_path", content="some_content") except StaticGeneratorException, e: assert str(e) == 'Could not create the file: test_web_root/some_path' mox.VerifyAll() return
def test_publish_fails_silently_when_unable_to_rename_temp_file(self): instance = StaticGenerator() with patch('os.rename') as rename: rename.side_effect = ValueError('message') instance.publish_from_path('/some_path', content='some_content') self.assertFalse(os.path.exists('test_web_root/fresh/some_path'))
def process_response(self, request, response): if response.status_code == 200: for url in self.urls: if url.match(request.path): gen = StaticGenerator() gen.publish_from_path(request.path, response.content) break return response
def test_publish_raises_when_unable_to_create_temp_file(self): instance = StaticGenerator() with nested(patch('tempfile.mkstemp'), self.assertRaises(StaticGeneratorException) ) as (mkstemp, cm): mkstemp.side_effect = ValueError('message') instance.publish_from_path('/some_path', content='some_content') self.assertEqual('Could not write temporary fresh file', str(cm.exception)) self.assertEqual('test_web_root/fresh', cm.exception.fresh_directory)
def test_publish_raises_when_unable_to_hard_link_stale_file(self): instance = StaticGenerator() with nested(patch('os.link'), self.assertRaises(StaticGeneratorException) ) as (link, cm): link.side_effect = OSError(2, 'message') instance.publish_from_path('/some_path', content='some_content') self.assertEqual('Could not link file', str(cm.exception)) self.assertEqual('test_web_root/fresh/some_path', cm.exception.src) self.assertEqual('test_web_root/stale/some_path', cm.exception.dst)
def test_publish_raises_when_unable_to_create_current_folder(self): instance = StaticGenerator() with nested(patch('os.path.exists'), patch('os.makedirs'), self.assertRaises(StaticGeneratorException) ) as (exists, makedirs, cm): exists.return_value = False makedirs.side_effect = ValueError('message') instance.publish_from_path('/some_path', content='some_content') self.assertEqual('Could not create directory', str(cm.exception)) self.assertEqual('test_web_root/fresh', cm.exception.directory)
def test_publish_raises_when_unable_to_create_stale_folder(self): real_makedirs = os.makedirs def makedirs_mock(directory, *args): if directory == 'test_web_root/stale': raise ValueError() real_makedirs(directory, *args) instance = StaticGenerator() with nested(patch('os.path.exists'), patch('os.makedirs'), self.assertRaises(StaticGeneratorException) ) as (exists, makedirs, cm): exists.return_value = False makedirs.side_effect = makedirs_mock instance.publish_from_path('/some_path', content='some_content') self.assertEqual('Could not create directory', str(cm.exception)) self.assertEqual('test_web_root/stale', cm.exception.directory)
def test_publish_from_path_serves_stale_file_temporarily(self): instance = StaticGenerator() os.makedirs('test_web_root/stale') open('test_web_root/stale/some_path', 'w').write('stale content') def handler(_request): """A mock request handler At the time of the request, the current content should be hard linked to the stale version. """ current_content = open('test_web_root/fresh/some_path').read() return Mock(content=('this content replaces {0!r}' .format(current_content)), status_code=200) with patch.object(staticgenerator, 'DummyHandler') as DummyHandler: DummyHandler.return_value = handler instance.publish_from_path('/some_path') self.assertEqual("this content replaces 'stale content'", open('test_web_root/fresh/some_path').read())