def test_get_entries_cached(self):
        file = self.addon.current_version.current_file
        serializer = FileEntriesSerializer(instance=file)

        # start serialization
        data = serializer.data
        commit = serializer._get_commit(file)

        assert serializer._entries == data['entries']

        key = 'reviewers:fileentriesserializer:entries:{}'.format(commit.hex)
        assert cache.get(key) == data['entries']
    def get_serializer(self, obj, **extra_context):
        api_version = api_settings.DEFAULT_VERSION
        request = APIRequestFactory().get('/api/%s/' % api_version)
        request.versioning_scheme = api_settings.DEFAULT_VERSIONING_CLASS()
        request.version = api_version
        extra_context.setdefault('request', request)

        return FileEntriesSerializer(instance=obj, context=extra_context)
    def test_is_binary(self):
        serializer = FileEntriesSerializer()

        files = [
            'foo.rdf', 'foo.xml', 'foo.js', 'foo.py' 'foo.html', 'foo.txt',
            'foo.dtd', 'foo.xul', 'foo.sh', 'foo.properties', 'foo.json',
            'foo.src', 'CHANGELOG']

        for fname in files:
            mime, encoding = mimetypes.guess_type(fname)
            assert not serializer.is_binary(fname, mime, b'')

        for fname in ['foo.png', 'foo.gif', 'foo.exe', 'foo.swf']:
            mime, encoding = mimetypes.guess_type(fname)
            assert serializer.is_binary(fname, mime, b'')

        for contents in [b'#!/usr/bin/python', b'#python', b'\0x2']:
            mime, encoding = mimetypes.guess_type(fname)
            assert not serializer.is_binary('random_junk', mime, contents)
def test_file_entries_serializer_category_type(
        entry, filename, expected_category, expected_mimetype):
    serializer = FileEntriesSerializer()

    entry.name = filename

    root = os.path.join(
        settings.ROOT,
        'src/olympia/files/fixtures/files/file_viewer_filetypes/')

    if entry.type == 'tree':
        mime, category = serializer.get_entry_mime_type(entry, None)
    else:
        with open(os.path.join(root, filename), 'rb') as fobj:
            mime, category = serializer.get_entry_mime_type(
                entry, force_bytes(fobj.read()))

    assert mime == expected_mimetype
    assert category == expected_category
def test_file_entries_serializer_category_type(
        entry, filename, expected_category, expected_mimetype):
    serializer = FileEntriesSerializer()

    entry.name = filename

    root = os.path.join(
        settings.ROOT,
        'src/olympia/files/fixtures/files/file_viewer_filetypes/')

    if entry.type == 'tree':
        mime, category = serializer.get_entry_mime_type(entry, None)
    else:
        with open(os.path.join(root, filename), 'rb') as fobj:
            mime, category = serializer.get_entry_mime_type(
                entry, force_bytes(fobj.read()))

    assert mime == expected_mimetype
    assert category == expected_category
 def serialize(self, obj, **extra_context):
     return FileEntriesSerializer(
         instance=obj, context=extra_context).data