예제 #1
0
 def setUp(self):
     super(TestDiffSearchEngine, self).setUp()
     src = os.path.join(settings.ROOT, get_file('search.xml'))
     if not storage.exists(src):
         with storage.open(src, 'w') as f:
             f.write(open(src).read())
     self.helper = DiffHelper(make_file(1, src, filename='search.xml'),
                              make_file(2, src, filename='search.xml'))
예제 #2
0
 def setUp(self):
     super(TestDiffSearchEngine, self).setUp()
     src = os.path.join(settings.ROOT, get_file('search.xml'))
     if not storage.exists(src):
         with storage.open(src, 'w') as f:
             f.write(open(src).read())
     self.helper = DiffHelper(make_file(1, src, filename='search.xml'),
                              make_file(2, src, filename='search.xml'))
예제 #3
0
    def wrapper(request, one_id, two_id, *args, **kw):
        one = get_object_or_404(File, pk=one_id)
        two = get_object_or_404(File, pk=two_id)
        for obj in [one, two]:
            result = allowed(request, obj)
            if result is not True:
                return result
        try:
            obj = DiffHelper(one, two)
        except ObjectDoesNotExist:
            raise http.Http404

        response = func(request, obj, *args, **kw)
        if obj.left.selected:
            response['ETag'] = '"%s"' % obj.left.selected.get('sha256')
            response['Last-Modified'] = http_date(obj.left.selected
                                                          .get('modified'))
        return response
예제 #4
0
class TestDiffSearchEngine(TestCase):
    def setUp(self):
        super(TestDiffSearchEngine, self).setUp()
        src = os.path.join(settings.ROOT, get_file('search.xml'))
        if not storage.exists(src):
            with storage.open(src, 'w') as f:
                f.write(open(src).read())
        self.helper = DiffHelper(make_file(1, src, filename='search.xml'),
                                 make_file(2, src, filename='search.xml'))

    def tearDown(self):
        self.helper.cleanup()
        super(TestDiffSearchEngine, self).tearDown()

    @patch('olympia.files.file_viewer.FileViewer.is_search_engine')
    def test_diff_search(self, is_search_engine):
        is_search_engine.return_value = True
        self.helper.extract()
        shutil.copyfile(os.path.join(self.helper.left.dest, 'search.xml'),
                        os.path.join(self.helper.right.dest, 's-20010101.xml'))
        assert self.helper.select('search.xml')
        assert len(self.helper.get_deleted_files()) == 0
예제 #5
0
class TestDiffSearchEngine(TestCase):

    def setUp(self):
        super(TestDiffSearchEngine, self).setUp()
        src = os.path.join(settings.ROOT, get_file('search.xml'))
        if not storage.exists(src):
            with storage.open(src, 'w') as f:
                f.write(open(src).read())
        self.helper = DiffHelper(make_file(1, src, filename='search.xml'),
                                 make_file(2, src, filename='search.xml'))

    def tearDown(self):
        self.helper.cleanup()
        super(TestDiffSearchEngine, self).tearDown()

    @patch(
        'olympia.files.file_viewer.FileViewer.is_search_engine')
    def test_diff_search(self, is_search_engine):
        is_search_engine.return_value = True
        self.helper.extract()
        shutil.copyfile(os.path.join(self.helper.left.dest, 'search.xml'),
                        os.path.join(self.helper.right.dest, 's-20010101.xml'))
        assert self.helper.select('search.xml')
        assert len(self.helper.get_deleted_files()) == 0
예제 #6
0
 def setUp(self):
     super(TestDiffViewer, self).setUp()
     self.file_viewer = DiffHelper(self.files[0], self.files[1])
예제 #7
0
class TestDiffViewer(FilesBase, TestCase):
    fixtures = ['base/addon_3615', 'base/users']

    def setUp(self):
        super(TestDiffViewer, self).setUp()
        self.file_viewer = DiffHelper(self.files[0], self.files[1])

    def poll_url(self):
        return reverse('files.compare.poll',
                       args=[self.files[0].pk, self.files[1].pk])

    def add_file(self, file_obj, name, contents):
        dest = os.path.join(file_obj.dest, name)
        with open(dest, 'w') as f:
            f.write(contents)

    def file_url(self, file=None):
        args = [self.files[0].pk, self.files[1].pk]
        if file:
            args.extend(['file', file])
        return reverse('files.compare', args=args)

    def check_urls(self, status):
        for url in [self.poll_url(), self.file_url()]:
            assert self.client.get(url).status_code == status

    def test_tree_no_file(self):
        self.file_viewer.extract()
        res = self.client.get(self.file_url('doesnotexist.js'))
        assert res.status_code == 404

    def test_content_file(self):
        self.file_viewer.extract()
        res = self.client.get(self.file_url(not_binary))
        doc = pq(res.content)
        assert len(doc('#content')) == 0
        assert len(doc('#diff[data-left][data-right]')) == 1

    def test_binary_serve_links(self):
        self.file_viewer.extract()
        res = self.client.get(self.file_url(binary))
        doc = pq(res.content)
        node = doc('#content-wrapper a')
        assert len(node) == 2
        assert node[0].text.startswith('Download ar.dic')

    def test_view_both_present(self):
        self.file_viewer.extract()
        res = self.client.get(self.file_url(not_binary))
        doc = pq(res.content)
        assert len(doc('#content')) == 0
        assert len(doc('#diff[data-left][data-right]')) == 1
        assert len(doc('#content-wrapper p')) == 2

    def test_view_one_missing(self):
        self.file_viewer.extract()
        os.remove(os.path.join(self.file_viewer.right.dest, 'install.js'))
        res = self.client.get(self.file_url(not_binary))
        doc = pq(res.content)
        assert len(doc('#content')) == 0
        assert len(doc('#diff[data-left][data-right]')) == 1
        assert len(doc('#content-wrapper p')) == 1

    def test_view_left_binary(self):
        self.file_viewer.extract()
        filename = os.path.join(self.file_viewer.left.dest, 'install.js')
        open(filename, 'w').write('MZ')
        res = self.client.get(self.file_url(not_binary))
        assert b'This file is not viewable online' in res.content

    def test_view_right_binary(self):
        self.file_viewer.extract()
        filename = os.path.join(self.file_viewer.right.dest, 'install.js')
        open(filename, 'w').write('MZ')
        assert not self.file_viewer.is_diffable()
        res = self.client.get(self.file_url(not_binary))
        assert b'This file is not viewable online' in res.content

    def test_different_tree(self):
        self.file_viewer.extract()
        os.remove(os.path.join(self.file_viewer.left.dest, not_binary))
        res = self.client.get(self.file_url(not_binary))
        doc = pq(res.content)
        assert doc('h4:last').text() == 'Deleted files:'
        assert len(doc('ul.root')) == 2

    def test_file_chooser_selection(self):
        res = self.client.get(self.file_url())
        doc = pq(res.content)

        assert doc('#id_left option[selected]').attr('value') == (str(
            self.files[0].id))
        assert doc('#id_right option[selected]').attr('value') == (str(
            self.files[1].id))

    def test_file_chooser_selection_same_hash(self):
        """
        In cases where multiple files are coalesced, the file selector may not
        have an actual entry for certain files. Instead, the entry with the
        identical hash should be selected.
        """
        res = self.client.get(
            reverse('files.compare',
                    args=(self.files[0].id, self.files[2].id)))
        doc = pq(res.content)

        assert doc('#id_left option[selected]').attr('value') == (str(
            self.files[0].id))
        assert doc('#id_right option[selected]').attr('value') == (str(
            self.files[1].id))

    def test_files_list_uses_correct_links(self):
        res = self.client.get(
            reverse('files.compare',
                    args=(self.files[0].id, self.files[2].id)))
        doc = pq(res.content)

        install_js_link = doc(
            '#files-tree li a.file[data-short="install.js"]')[0].get('href')

        expected = reverse('files.compare',
                           args=(self.files[0].id, self.files[2].id, 'file',
                                 'install.js'))

        assert install_js_link == expected
예제 #8
0
 def setUp(self):
     super(TestDiffHelper, self).setUp()
     src = os.path.join(settings.ROOT, get_file('dictionary-test.xpi'))
     self.helper = DiffHelper(make_file(1, src), make_file(2, src))
예제 #9
0
class TestDiffHelper(TestCase):
    def setUp(self):
        super(TestDiffHelper, self).setUp()
        src = os.path.join(settings.ROOT, get_file('dictionary-test.xpi'))
        self.helper = DiffHelper(make_file(1, src), make_file(2, src))

    def tearDown(self):
        self.helper.cleanup()
        super(TestDiffHelper, self).tearDown()

    def clear_cache(self):
        cache.delete(self.helper.left._cache_key())
        cache.delete(self.helper.right._cache_key())

    def test_files_not_extracted(self):
        assert not self.helper.is_extracted()

    def test_files_extracted(self):
        self.helper.extract()
        assert self.helper.is_extracted()

    def test_get_files(self):
        assert self.helper.left.get_files() == (self.helper.get_files())

    def test_diffable(self):
        self.helper.extract()
        self.helper.select('install.js')
        assert self.helper.is_diffable()

    def test_diffable_one_missing(self):
        self.helper.extract()
        os.remove(os.path.join(self.helper.right.dest, 'install.js'))
        self.helper.select('install.js')
        assert self.helper.is_diffable()

    def test_diffable_allow_empty(self):
        self.helper.extract()
        self.assertRaises(AssertionError, self.helper.right.read_file)
        assert self.helper.right.read_file(allow_empty=True) == ''

    def test_diffable_both_missing(self):
        self.helper.extract()
        self.helper.select('foo.js')
        assert not self.helper.is_diffable()

    def test_diffable_deleted_files(self):
        self.helper.extract()
        os.remove(os.path.join(self.helper.left.dest, 'install.js'))
        assert 'install.js' in self.helper.get_deleted_files()

    def test_diffable_one_binary_same(self):
        self.helper.extract()
        self.helper.select('install.js')
        self.helper.left.selected['binary'] = True
        assert self.helper.is_binary()

    def test_diffable_one_binary_diff(self):
        self.helper.extract()
        self.change(self.helper.left.dest, 'asd')
        self.helper.select('install.js')
        self.helper.left.selected['binary'] = True
        assert self.helper.is_binary()

    def test_diffable_two_binary_diff(self):
        self.helper.extract()
        self.change(self.helper.left.dest, 'asd')
        self.change(self.helper.right.dest, 'asd123')
        self.clear_cache()
        self.helper.select('install.js')
        self.helper.left.selected['binary'] = True
        self.helper.right.selected['binary'] = True
        assert self.helper.is_binary()

    def test_diffable_one_directory(self):
        self.helper.extract()
        self.helper.select('install.js')
        self.helper.left.selected['directory'] = True
        assert not self.helper.is_diffable()
        assert self.helper.left.selected['msg'].startswith('This file')

    def test_diffable_parent(self):
        self.helper.extract()
        self.change(self.helper.left.dest,
                    'asd',
                    filename='__MACOSX/._dictionaries')
        self.clear_cache()
        files = self.helper.get_files()
        assert files['__MACOSX/._dictionaries']['diff']
        assert files['__MACOSX']['diff']

    def change(self, file, text, filename='install.js'):
        path = os.path.join(file, filename)
        with open(path, 'rb') as f:
            data = f.read()
        data += text.encode('utf-8')
        with open(path, 'wb') as f2:
            f2.write(data)
예제 #10
0
 def setUp(self):
     super(TestDiffViewer, self).setUp()
     self.file_viewer = DiffHelper(self.files[0], self.files[1])
예제 #11
0
class TestDiffViewer(FilesBase, TestCase):
    fixtures = ['base/addon_3615', 'base/users']

    def setUp(self):
        super(TestDiffViewer, self).setUp()
        self.file_viewer = DiffHelper(self.files[0], self.files[1])

    def poll_url(self):
        return reverse('files.compare.poll', args=[self.files[0].pk,
                                                   self.files[1].pk])

    def add_file(self, file_obj, name, contents):
        dest = os.path.join(file_obj.dest, name)
        open(dest, 'w').write(contents)

    def file_url(self, file=None):
        args = [self.files[0].pk, self.files[1].pk]
        if file:
            args.extend(['file', file])
        return reverse('files.compare', args=args)

    def check_urls(self, status):
        for url in [self.poll_url(), self.file_url()]:
            assert self.client.get(url).status_code == status

    def test_tree_no_file(self):
        self.file_viewer.extract()
        res = self.client.get(self.file_url('doesnotexist.js'))
        assert res.status_code == 404

    def test_content_file(self):
        self.file_viewer.extract()
        res = self.client.get(self.file_url(not_binary))
        doc = pq(res.content)
        assert len(doc('#content')) == 0
        assert len(doc('#diff[data-left][data-right]')) == 1

    def test_binary_serve_links(self):
        self.file_viewer.extract()
        res = self.client.get(self.file_url(binary))
        doc = pq(res.content)
        node = doc('#content-wrapper a')
        assert len(node) == 2
        assert node[0].text.startswith('Download ar.dic')

    def test_view_both_present(self):
        self.file_viewer.extract()
        res = self.client.get(self.file_url(not_binary))
        doc = pq(res.content)
        assert len(doc('#content')) == 0
        assert len(doc('#diff[data-left][data-right]')) == 1
        assert len(doc('#content-wrapper p')) == 2

    def test_view_one_missing(self):
        self.file_viewer.extract()
        os.remove(os.path.join(self.file_viewer.right.dest, 'install.js'))
        res = self.client.get(self.file_url(not_binary))
        doc = pq(res.content)
        assert len(doc('#content')) == 0
        assert len(doc('#diff[data-left][data-right]')) == 1
        assert len(doc('#content-wrapper p')) == 1

    def test_view_left_binary(self):
        self.file_viewer.extract()
        filename = os.path.join(self.file_viewer.left.dest, 'install.js')
        open(filename, 'w').write('MZ')
        res = self.client.get(self.file_url(not_binary))
        assert 'This file is not viewable online' in res.content

    def test_view_right_binary(self):
        self.file_viewer.extract()
        filename = os.path.join(self.file_viewer.right.dest, 'install.js')
        open(filename, 'w').write('MZ')
        assert not self.file_viewer.is_diffable()
        res = self.client.get(self.file_url(not_binary))
        assert 'This file is not viewable online' in res.content

    def test_different_tree(self):
        self.file_viewer.extract()
        os.remove(os.path.join(self.file_viewer.left.dest, not_binary))
        res = self.client.get(self.file_url(not_binary))
        doc = pq(res.content)
        assert doc('h4:last').text() == 'Deleted files:'
        assert len(doc('ul.root')) == 2

    def test_file_chooser_selection(self):
        res = self.client.get(self.file_url())
        doc = pq(res.content)

        assert doc('#id_left option[selected]').attr('value') == (
            str(self.files[0].id))
        assert doc('#id_right option[selected]').attr('value') == (
            str(self.files[1].id))

    def test_file_chooser_selection_same_hash(self):
        """
        In cases where multiple files are coalesced, the file selector may not
        have an actual entry for certain files. Instead, the entry with the
        identical hash should be selected.
        """
        res = self.client.get(reverse('files.compare',
                                      args=(self.files[0].id,
                                            self.files[2].id)))
        doc = pq(res.content)

        assert doc('#id_left option[selected]').attr('value') == (
            str(self.files[0].id))
        assert doc('#id_right option[selected]').attr('value') == (
            str(self.files[1].id))

    def test_files_list_uses_correct_links(self):
        res = self.client.get(reverse('files.compare',
                                      args=(self.files[0].id,
                                            self.files[2].id)))
        doc = pq(res.content)

        install_js_link = doc(
            '#files-tree li a.file[data-short="install.js"]'
        )[0].get('href')

        expected = reverse(
            'files.compare',
            args=(self.files[0].id, self.files[2].id, 'file', 'install.js'))

        assert install_js_link == expected
예제 #12
0
 def setUp(self):
     super(TestDiffHelper, self).setUp()
     src = os.path.join(settings.ROOT, get_file('dictionary-test.xpi'))
     self.helper = DiffHelper(make_file(1, src), make_file(2, src))
예제 #13
0
class TestDiffHelper(TestCase):

    def setUp(self):
        super(TestDiffHelper, self).setUp()
        src = os.path.join(settings.ROOT, get_file('dictionary-test.xpi'))
        self.helper = DiffHelper(make_file(1, src), make_file(2, src))

    def tearDown(self):
        self.helper.cleanup()
        super(TestDiffHelper, self).tearDown()

    def test_files_not_extracted(self):
        assert not self.helper.is_extracted()

    def test_files_extracted(self):
        self.helper.extract()
        assert self.helper.is_extracted()

    def test_get_files(self):
        assert self.helper.left.get_files() == (
            self.helper.get_files())

    def test_diffable(self):
        self.helper.extract()
        self.helper.select('install.js')
        assert self.helper.is_diffable()

    def test_diffable_one_missing(self):
        self.helper.extract()
        os.remove(os.path.join(self.helper.right.dest, 'install.js'))
        self.helper.select('install.js')
        assert self.helper.is_diffable()

    def test_diffable_allow_empty(self):
        self.helper.extract()
        self.assertRaises(AssertionError, self.helper.right.read_file)
        assert self.helper.right.read_file(allow_empty=True) == ''

    def test_diffable_both_missing(self):
        self.helper.extract()
        self.helper.select('foo.js')
        assert not self.helper.is_diffable()

    def test_diffable_deleted_files(self):
        self.helper.extract()
        os.remove(os.path.join(self.helper.left.dest, 'install.js'))
        assert 'install.js' in self.helper.get_deleted_files()

    def test_diffable_one_binary_same(self):
        self.helper.extract()
        self.helper.select('install.js')
        self.helper.left.selected['binary'] = True
        assert self.helper.is_binary()

    def test_diffable_one_binary_diff(self):
        self.helper.extract()
        self.change(self.helper.left.dest, 'asd')
        cache.clear()
        self.helper.select('install.js')
        self.helper.left.selected['binary'] = True
        assert self.helper.is_binary()

    def test_diffable_two_binary_diff(self):
        self.helper.extract()
        self.change(self.helper.left.dest, 'asd')
        self.change(self.helper.right.dest, 'asd123')
        cache.clear()
        self.helper.select('install.js')
        self.helper.left.selected['binary'] = True
        self.helper.right.selected['binary'] = True
        assert self.helper.is_binary()

    def test_diffable_one_directory(self):
        self.helper.extract()
        self.helper.select('install.js')
        self.helper.left.selected['directory'] = True
        assert not self.helper.is_diffable()
        assert self.helper.left.selected['msg'].startswith('This file')

    def test_diffable_parent(self):
        self.helper.extract()
        self.change(self.helper.left.dest, 'asd',
                    filename='__MACOSX/._dictionaries')
        cache.clear()
        files = self.helper.get_files()
        assert files['__MACOSX/._dictionaries']['diff']
        assert files['__MACOSX']['diff']

    def change(self, file, text, filename='install.js'):
        path = os.path.join(file, filename)
        data = open(path, 'r').read()
        data += text
        open(path, 'w').write(data)