예제 #1
0
def test_delete_loops_through_all_resources():
    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/some_path").AndReturn(True)
    fs_mock.remove("test_web_root/some_path")
    fs_mock.rmdir("test_web_root")

    fs_mock.join('test_web_root', 'some_path_2').AndReturn("test_web_root/some_path_2")
    fs_mock.dirname('test_web_root/some_path_2').AndReturn("test_web_root")
    fs_mock.exists("test_web_root/some_path_2").AndReturn(True)
    fs_mock.remove("test_web_root/some_path_2")
    fs_mock.rmdir("test_web_root")

    settings = CustomSettings(WEB_ROOT="test_web_root")

    mox.ReplayAll()

    instance = StaticGenerator("some_path", "some_path_2", 
                               http_request=http_request,
                               model_base=model_base,
                               manager=manager,
                               model=model,
                               queryset=queryset,
                               settings=settings,
                               fs=fs_mock)

    instance.delete()

    mox.VerifyAll()
예제 #2
0
def test_delete_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/some_path").AndReturn(True)
    fs_mock.remove("test_web_root/some_path")

    fs_mock.rmdir("test_web_root")

    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.delete_from_path("some_path")

    mox.VerifyAll()
def test_delete_loops_through_all_resources():
    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/some_path").AndReturn(True)
    fs_mock.remove("test_web_root/some_path")
    fs_mock.rmdir("test_web_root")

    fs_mock.join('test_web_root', 'some_path_2/index.html').AndReturn("test_web_root/some_path_2")
    fs_mock.dirname('test_web_root/some_path_2').AndReturn("test_web_root")
    fs_mock.exists("test_web_root/some_path_2").AndReturn(True)
    fs_mock.remove("test_web_root/some_path_2")
    fs_mock.rmdir("test_web_root")

    settings = CustomSettings(WEB_ROOT="test_web_root")

    mox.ReplayAll()

    instance = StaticGenerator("some_path", "some_path_2", 
                               http_request=http_request,
                               model_base=model_base,
                               manager=manager,
                               model=model,
                               queryset=queryset,
                               settings=settings,
                               fs=fs_mock)

    instance.delete()

    mox.VerifyAll()
    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_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())
예제 #6
0
    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)
예제 #7
0
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()
예제 #8
0
def test_get_filename_from_path_when_path_ends_with_slash():
    mox = Mox()
    http_request, model_base, manager, model, queryset = get_mocks(mox)
    settings = CustomSettings(WEB_ROOT="test_web_root")
    
    fs_mock = mox.CreateMockAnything()
    fs_mock.join("test_web_root", "foo/bar/index.html").AndReturn("test_web_root/foo/bar/index.html")
    fs_mock.dirname("test_web_root/foo/bar/index.html").AndReturn("test_web_root/foo/bar")
    
    path_mock = '/foo/bar/'

    mox.ReplayAll()
    
    instance = StaticGenerator(http_request=http_request,
                               model_base=model_base,
                               manager=manager,
                               model=model,
                               queryset=queryset,
                               settings=settings,
                               fs=fs_mock)

    result = instance.get_filename_from_path(path_mock)
    
    assert result ==  ('test_web_root/foo/bar/index.html', 'test_web_root/foo/bar')
    mox.VerifyAll()
예제 #9
0
def test_delete_ignores_folder_delete_when_unable_to_delete_folder():
    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/some_path").AndReturn(True)
    fs_mock.remove("test_web_root/some_path")

    fs_mock.rmdir("test_web_root").AndRaise(OSError())

    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.delete_from_path("some_path")

    assert True, "Should work even when raising OSError"
예제 #10
0
def test_delete_raises_when_unable_to_delete_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/some_path").AndReturn(True)
    fs_mock.remove("test_web_root/some_path").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.delete_from_path("some_path")
    except StaticGeneratorException, e:
        assert str(e) == 'Could not delete file: test_web_root/some_path'
        mox.VerifyAll()
        return
def test_get_filename_from_path_when_path_ends_with_slash():
    mox = Mox()
    http_request, model_base, manager, model, queryset = get_mocks(mox)
    settings = CustomSettings(WEB_ROOT="test_web_root")
    
    fs_mock = mox.CreateMockAnything()
    fs_mock.join("test_web_root", "foo/bar/index.html").AndReturn("test_web_root/foo/bar/index.html")
    fs_mock.dirname("test_web_root/foo/bar/index.html").AndReturn("test_web_root/foo/bar")
    
    path_mock = '/foo/bar/'

    mox.ReplayAll()
    
    instance = StaticGenerator(http_request=http_request,
                               model_base=model_base,
                               manager=manager,
                               model=model,
                               queryset=queryset,
                               settings=settings,
                               fs=fs_mock)

    result = instance.get_filename_from_path(path_mock)
    
    assert result ==  ('test_web_root/foo/bar/index.html', 'test_web_root/foo/bar')
    mox.VerifyAll()
예제 #12
0
def test_delete_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/some_path").AndReturn(True)
    fs_mock.remove("test_web_root/some_path")

    fs_mock.rmdir("test_web_root")

    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.delete_from_path("some_path")

    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/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_raises_when_unable_to_create_folder():
    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/index.html")
    fs_mock.dirname("test_web_root/some_path/index.html").AndReturn("test_web_root")
    fs_mock.exists("test_web_root").AndReturn(False)

    fs_mock.makedirs("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 directory: test_web_root'
        mox.VerifyAll()
        return
def test_delete_raises_when_unable_to_delete_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/some_path").AndReturn(True)
    fs_mock.remove("test_web_root/some_path").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.delete_from_path("some_path")
    except StaticGeneratorException, e:
        assert str(e) == 'Could not delete file: test_web_root/some_path'
        mox.VerifyAll()
        return
예제 #16
0
def test_publish_raises_when_unable_to_create_folder():
    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(False)

    fs_mock.makedirs("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 directory: test_web_root'
        mox.VerifyAll()
        return
예제 #17
0
    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_delete_ignores_folder_delete_when_unable_to_delete_folder():
    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/some_path").AndReturn(True)
    fs_mock.remove("test_web_root/some_path")

    fs_mock.rmdir("test_web_root").AndRaise(OSError())

    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.delete_from_path("some_path")

    assert True, "Should work even when raising OSError"
예제 #19
0
    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 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'))
예제 #21
0
 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
예제 #22
0
    def test_get_content_from_path(self):
        response_mock = Mock(content='foo', status_code=200)
        instance = StaticGenerator()
        with patch.object(staticgenerator, 'DummyHandler') as DummyHandler:
            DummyHandler().return_value = response_mock

            result = instance.get_content_from_path('/some_path')

        self.assertEqual('foo', result)
    def test_get_content_from_path(self):
        response_mock = Mock(content='foo', status_code=200)
        instance = StaticGenerator()
        with patch.object(staticgenerator, 'DummyHandler') as DummyHandler:
            DummyHandler().return_value = response_mock

            result = instance.get_content_from_path('/some_path')

        self.assertEqual('foo', result)
    def test_delete_from_path_deletes_current_file(self):
        instance = StaticGenerator()
        remove = Mock()
        with nested(patch('os.path.exists', Mock(return_value=True)),
                    patch('os.remove', remove)):

            instance.delete_from_path('/some_path')

        remove.assert_called_once_with('test_web_root/fresh/some_path')
예제 #25
0
def test_publish_loops_through_all_resources():
    mox = Mox()
    http_request, model_base, manager, model, queryset = get_mocks(mox)

    fs_mock = mox.CreateMockAnything()
    f = mox.CreateMockAnything()
    fs_mock.join(
        'test_web_root',
        'some_path_1/index.html').AndReturn('test_web_root/some_path_1')
    fs_mock.dirname('test_web_root/some_path_1').AndReturn('test_web_root')
    fs_mock.exists("test_web_root").AndReturn(True)
    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_1')

    fs_mock.join(
        'test_web_root',
        'some_path_2/index.html').AndReturn('test_web_root/some_path_2')
    fs_mock.dirname('test_web_root/some_path_2').AndReturn('test_web_root')
    fs_mock.exists("test_web_root").AndReturn(True)
    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_2')

    settings = CustomSettings(WEB_ROOT="test_web_root")

    mox.ReplayAll()

    try:
        get_content_from_path = StaticGenerator.get_content_from_path
        StaticGenerator.get_content_from_path = lambda self, path: "some_content"
        instance = StaticGenerator("some_path_1",
                                   "some_path_2",
                                   http_request=http_request,
                                   model_base=model_base,
                                   manager=manager,
                                   model=model,
                                   queryset=queryset,
                                   settings=settings,
                                   fs=fs_mock)

        instance.publish()

        mox.VerifyAll()
    finally:
        StaticGenerator.get_content_from_path = get_content_from_path
    def test_delete_loops_through_all_resources(self):
        instance = StaticGenerator('/some_path', '/some_path_2')
        remove = Mock()
        with nested(patch('os.path.exists', Mock(return_value=True)),
                    patch('os.remove', remove)):

            instance.delete()

        remove.assert_has_calls([call('test_web_root/fresh/some_path'),
                                 call('test_web_root/fresh/some_path_2')])
예제 #27
0
    def test_delete_from_path_does_not_delete_stale_file(self):
        instance = StaticGenerator()
        remove = Mock()
        with nested(patch('os.path.exists', Mock(return_value=True)),
                    patch('os.remove', remove)):

            instance.delete_from_path('/some_path')

        self.assertNotIn(call('test_web_root/stale/some_path'),
                         remove.call_args_list)
예제 #28
0
    def test_delete_ignores_folder_delete_when_unable_to_delete_folder(self):
        instance = StaticGenerator()
        rmdir = Mock(side_effect=OSError)
        with nested(patch('os.path.exists', Mock(return_value=True)),
                    patch('os.remove'),
                    patch('os.rmdir', rmdir)):

            instance.delete_from_path('/some_path')

        rmdir.assert_called_once_with('test_web_root/fresh')
    def test_delete_from_path_does_not_delete_stale_file(self):
        instance = StaticGenerator()
        remove = Mock()
        with nested(patch('os.path.exists', Mock(return_value=True)),
                    patch('os.remove', remove)):

            instance.delete_from_path('/some_path')

        self.assertNotIn(call('test_web_root/stale/some_path'),
                         remove.call_args_list)
예제 #30
0
    def test_delete_from_path_deletes_current_file(self):
        instance = StaticGenerator()
        remove = Mock()
        with nested(patch('os.path.exists', Mock(return_value=True)),
                    patch('os.remove', remove)):

            instance.delete_from_path('/some_path')

        remove.assert_has_calls([call('test_web_root/fresh/some_path'),
                                 call('test_web_root/fresh/some_path.gz')])
    def test_delete_ignores_folder_delete_when_unable_to_delete_folder(self):
        instance = StaticGenerator()
        rmdir = Mock(side_effect=OSError)
        with nested(patch('os.path.exists', Mock(return_value=True)),
                    patch('os.remove'),
                    patch('os.rmdir', rmdir)):

            instance.delete_from_path('/some_path')

        rmdir.assert_called_once_with('test_web_root/fresh')
예제 #32
0
    def test_get_lang_aware_filename_from_path_when_path_ends_with_slash(self):
        for lang in ('uk', 'ru'):
            # activate first non-default language
            if lang != settings.LANGUAGE_CODE:
                activate('ru')

        instance = StaticGenerator()

        result = instance.get_filename_from_path('/foo/bar/', '')

        self.assertEqual('test_web_root/foo/bar/{}.index.html%3F'.format(lang),
                         result)
예제 #33
0
    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)
예제 #34
0
    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)
예제 #35
0
    def test_publish_loops_through_all_resources(self):
        instance = StaticGenerator('/some_path_1', '/some_path_2')
        rename = Mock(wraps=os.rename)
        with nested(patch('os.rename', rename),
                    patch.object(instance, 'get_content_from_path',
                                 Mock(return_value='some_content'))):

            instance.publish()

        rename.assert_has_calls([
            call(ANY, 'test_web_root/fresh/some_path_1'),
            call(ANY, 'test_web_root/fresh/some_path_2')])
    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_loops_through_all_resources(self):
        instance = StaticGenerator('/some_path_1', '/some_path_2')
        rename = Mock(wraps=os.rename)
        with nested(patch('os.rename', rename),
                    patch.object(instance, 'get_content_from_path',
                                 Mock(return_value='some_content'))):

            instance.publish()

        rename.assert_has_calls([
            call(ANY, 'test_web_root/fresh/some_path_1'),
            call(ANY, 'test_web_root/fresh/some_path_2')])
예제 #39
0
    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)
예제 #40
0
    def test_not_found_raises_proper_exception(self):
        response_mock = Mock(content='foo', status_code=404)
        instance = StaticGenerator()
        with nested(patch('staticgenerator.DummyHandler'),
                    self.assertRaises(StaticGeneratorException)
                   ) as (handler_mock, cm):
            handler_mock.return_value = Mock(return_value=response_mock)

            instance.get_content_from_path('/some_path')

        self.assertEqual(('The requested page("/some_path") returned '
                          'http code 404. Static Generation failed.'),
                         str(cm.exception))
    def test_not_found_raises_proper_exception(self):
        response_mock = Mock(content='foo', status_code=404)
        instance = StaticGenerator()
        with nested(patch('staticgenerator.DummyHandler'),
                    self.assertRaises(StaticGeneratorException)
                   ) as (handler_mock, cm):
            handler_mock.return_value = Mock(return_value=response_mock)

            instance.get_content_from_path('/some_path')

        self.assertEqual(('The requested page("/some_path") returned '
                          'http code 404. Static Generation failed.'),
                         str(cm.exception))
    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)
예제 #43
0
    def test_delete_raises_when_unable_to_delete_file(self):
        instance = StaticGenerator()
        with nested(patch('os.path.exists'),
                    patch('os.remove'),
                    self.assertRaises(StaticGeneratorException)
                   ) as (exists, remove, cm):
            exists.return_value = True
            remove.side_effect = ValueError

            instance.delete_from_path('/some_path')

        self.assertEqual('Could not delete file', str(cm.exception))
        self.assertEqual('test_web_root/fresh/some_path',
                         cm.exception.filename)
예제 #44
0
    def test_request_exception_raises_proper_exception(self):
        instance = StaticGenerator()
        with nested(patch('staticgenerator.DummyHandler'),
                    self.assertRaises(StaticGeneratorException)
                   ) as (handler_mock, cm):
            handler_mock.return_value = Mock(
                side_effect=ValueError('exception'))

            instance.get_content_from_path('/some_path')

        self.assertEqual('The requested page("/some_path") raised an '
                         'exception. Static Generation failed. '
                         'Error: exception',
                         str(cm.exception))
    def test_request_exception_raises_proper_exception(self):
        instance = StaticGenerator()
        with nested(patch('staticgenerator.DummyHandler'),
                    self.assertRaises(StaticGeneratorException)
                   ) as (handler_mock, cm):
            handler_mock.return_value = Mock(
                side_effect=ValueError('exception'))

            instance.get_content_from_path('/some_path')

        self.assertEqual('The requested page("/some_path") raised an '
                         'exception. Static Generation failed. '
                         'Error: exception',
                         str(cm.exception))
    def test_delete_raises_when_unable_to_delete_file(self):
        instance = StaticGenerator()
        with nested(patch('os.path.exists'),
                    patch('os.remove'),
                    self.assertRaises(StaticGeneratorException)
                   ) as (exists, remove, cm):
            exists.return_value = True
            remove.side_effect = ValueError

            instance.delete_from_path('/some_path')

        self.assertEqual('Could not delete file', str(cm.exception))
        self.assertEqual('test_web_root/fresh/some_path',
                         cm.exception.filename)
예제 #47
0
 def test_delete_loops_through_all_resources(self):
     instance = StaticGenerator('/path_one', '/path/two')
     remove = Mock()
     with nested(patch('os.path.exists', Mock(return_value=True)),
                 patch('os.remove', remove)):
         instance.delete()
     # default language cache path contains no language code
     remove.assert_has_calls([call('test_web_root/fresh/path_one'),
                              call('test_web_root/fresh/path_one.gz'),
                              call('test_web_root/fresh/fr.path_one'),
                              call('test_web_root/fresh/fr.path_one.gz'),
                              call('test_web_root/fresh/path/two'),
                              call('test_web_root/fresh/path/two.gz'),
                              call('test_web_root/fresh/path/fr.two'),
                              call('test_web_root/fresh/path/fr.two.gz')])
예제 #48
0
    def test_extract_resources_when_resource_is_a_str(self):
        resources_mock = "some_str"

        instance = StaticGenerator(resources_mock)

        self.assertEqual(1, len(instance.resources))
        self.assertEqual('some_str', instance.resources[0])
예제 #49
0
    def test_extract_resources_when_resource_is_a_model(self):
        resources_mock = Model(url='some_model_url')

        instance = StaticGenerator(resources_mock)

        self.assertEqual(1, len(instance.resources))
        self.assertEqual('some_model_url', instance.resources[0])
예제 #50
0
def test_extract_resources_when_resource_is_a_model():
    mox = Mox()
    http_request, model_base, manager, model, queryset = get_mocks(mox)
    
    class Model(object):
        def get_absolute_url(self):
            return 'some_model_url'
        
    resources_mock = Model()
    model = Model
    
    settings = CustomSettings(WEB_ROOT="some_web_root")

    mox.ReplayAll()

    instance = StaticGenerator(resources_mock,
                               http_request=http_request,
                               model_base=model_base,
                               manager=manager,
                               model=model,
                               queryset=queryset,
                               settings=settings)

    assert len(instance.resources) == 1 
    assert instance.resources[0] == 'some_model_url'
    mox.VerifyAll()
예제 #51
0
class StaticGeneratorMiddleware(object):
    """
    This requires settings.STATIC_GENERATOR_URLS tuple to match on URLs
    
    Example::
        
        STATIC_GENERATOR_URLS = (
            r'^/$',
            r'^/blog',
        )
        
    """
    urls = tuple([re.compile(url) for url in settings.STATIC_GENERATOR_URLS])
    gen = StaticGenerator()

    def process_response(self, request, response):

        if not getattr(settings, 'ENABLE_STATIC_GENERATOR', False):
            return response

        if response.status_code == 200:
            for url in self.urls:
                if url.match(request.path_info):
                    self.gen.publish_from_path(request.path_info,
                                               response.content)
                    break
        return response
def test_publish_loops_through_all_resources():
    mox = Mox()
    http_request, model_base, manager, model, queryset = get_mocks(mox)

    fs_mock = mox.CreateMockAnything()
    f = mox.CreateMockAnything()
    fs_mock.join('test_web_root', 'some_path_1/index.html').AndReturn('test_web_root/some_path_1')
    fs_mock.dirname('test_web_root/some_path_1').AndReturn('test_web_root')
    fs_mock.exists("test_web_root").AndReturn(True)
    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_1')

    fs_mock.join('test_web_root', 'some_path_2/index.html').AndReturn('test_web_root/some_path_2')
    fs_mock.dirname('test_web_root/some_path_2').AndReturn('test_web_root')
    fs_mock.exists("test_web_root").AndReturn(True)
    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_2')

    settings = CustomSettings(WEB_ROOT="test_web_root")

    mox.ReplayAll()

    try:
        get_content_from_path = StaticGenerator.get_content_from_path
        StaticGenerator.get_content_from_path = lambda self, path: "some_content"
        instance = StaticGenerator("some_path_1", "some_path_2",
                                   http_request=http_request,
                                   model_base=model_base,
                                   manager=manager,
                                   model=model,
                                   queryset=queryset,
                                   settings=settings,
                                   fs=fs_mock)

        instance.publish()

        mox.VerifyAll()
    finally:
        StaticGenerator.get_content_from_path = get_content_from_path
예제 #53
0
    def test_get_server_name_gets_name_from_site(self):
        with patch('django.contrib.sites.models'
                   '.Site.objects.get_current') as get_current:
            get_current().domain = 'custom_domain'

            instance = StaticGenerator()

        self.assertEqual('custom_domain', instance.server_name)
예제 #54
0
def test_not_found_raises_proper_exception():
    mox = Mox()

    http_request, model_base, manager, model, queryset = get_mocks(mox)
    settings = CustomSettings(WEB_ROOT="test_web_root")

    path_mock = 'some_path'

    request_mock = mox.CreateMockAnything()
    request_mock.META = mox.CreateMockAnything()
    request_mock.META.setdefault('SERVER_PORT', 80)
    request_mock.META.setdefault('SERVER_NAME', 'localhost')

    http_request.__call__().AndReturn(request_mock)

    response_mock = mox.CreateMockAnything()
    response_mock.content = 'foo'
    response_mock.status_code = 404

    handler_mock = mox.CreateMockAnything()
    handler_mock.__call__().AndReturn(handler_mock)
    handler_mock.__call__(request_mock).AndReturn(response_mock)

    mox.ReplayAll()

    try:
        dummy_handler = staticgenerator.DummyHandler
        staticgenerator.DummyHandler = handler_mock

        instance = StaticGenerator(http_request=http_request,
                                   model_base=model_base,
                                   manager=manager,
                                   model=model,
                                   queryset=queryset,
                                   settings=settings)

        result = instance.get_content_from_path(path_mock)
    except StaticGeneratorException, e:
        assert str(
            e
        ) == 'The requested page("some_path") returned http code 404. Static Generation failed.'
        mox.VerifyAll()
        return
    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_get_content_from_path():
    mox = Mox()
    http_request, model_base, manager, model, queryset = get_mocks(mox)
    settings = CustomSettings(WEB_ROOT="test_web_root")
    
    path_mock = 'some_path'
    
    request_mock = mox.CreateMockAnything()
    request_mock.META = mox.CreateMockAnything()
    request_mock.META.setdefault('SERVER_PORT', 80)
    request_mock.META.setdefault('SERVER_NAME', 'localhost')
    
    response_mock = mox.CreateMockAnything()
    response_mock.content = 'foo'
    response_mock.status_code = 200

    http_request.__call__().AndReturn(request_mock)
    
    handler_mock = mox.CreateMockAnything()
    handler_mock.__call__().AndReturn(handler_mock)
    handler_mock.__call__(request_mock).AndReturn(response_mock)

    mox.ReplayAll()
    
    try:
        dummy_handler = staticgenerator.DummyHandler
        staticgenerator.DummyHandler = handler_mock

        instance = StaticGenerator(http_request=http_request,
                                   model_base=model_base,
                                   manager=manager,
                                   model=model,
                                   queryset=queryset,
                                   settings=settings)
    
        result = instance.get_content_from_path(path_mock)
    finally:
        staticgenerator.DummyHandler = dummy_handler
    
    assert result == 'foo'
    mox.VerifyAll()
def test_not_found_raises_proper_exception():
    mox = Mox()

    http_request, model_base, manager, model, queryset = get_mocks(mox)
    settings = CustomSettings(WEB_ROOT="test_web_root")

    path_mock = 'some_path'

    request_mock = mox.CreateMockAnything()
    request_mock.META = mox.CreateMockAnything()
    request_mock.META.setdefault('SERVER_PORT', 80)
    request_mock.META.setdefault('SERVER_NAME', 'localhost')

    http_request.__call__().AndReturn(request_mock)

    response_mock = mox.CreateMockAnything()
    response_mock.content = 'foo'
    response_mock.status_code = 404

    handler_mock = mox.CreateMockAnything()
    handler_mock.__call__().AndReturn(handler_mock)
    handler_mock.__call__(request_mock).AndReturn(response_mock)

    mox.ReplayAll()

    try:
        dummy_handler = staticgenerator.DummyHandler
        staticgenerator.DummyHandler = handler_mock

        instance = StaticGenerator(http_request=http_request,
                                   model_base=model_base,
                                   manager=manager,
                                   model=model,
                                   queryset=queryset,
                                   settings=settings)

        result = instance.get_content_from_path(path_mock)
    except StaticGeneratorException, e:
        assert str(e) == 'The requested page("some_path") returned http code 404. Static Generation failed.'
        mox.VerifyAll()
        return
    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())