test_lyrics.py 2.2 KB
Newer Older
1 2 3 4 5 6
import json
from django.urls import reverse

from funkwhale_api.music import models
from funkwhale_api.musicbrainz import api
from funkwhale_api.music import serializers
7
from funkwhale_api.music import tasks
8 9 10
from funkwhale_api.music import lyrics as lyrics_utils


11 12
def test_works_import_lyrics_if_any(
        lyricswiki_content, mocker, factories):
13 14
    mocker.patch(
        'funkwhale_api.music.lyrics._get_html',
15
        return_value=lyricswiki_content)
16 17 18
    lyrics = factories['music.Lyrics'](
        url='http://lyrics.wikia.com/System_Of_A_Down:Chop_Suey!')

19 20
    tasks.fetch_content(lyrics_id=lyrics.pk)
    lyrics.refresh_from_db()
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    self.assertIn(
        'Grab a brush and put on a little makeup',
        lyrics.content,
    )


def test_clean_content():
    c = """<div class="lyricbox">Hello<br /><script>alert('hello');</script>Is it me you're looking for?<br /></div>"""
    d = lyrics_utils.extract_content(c)
    d = lyrics_utils.clean_content(d)

    expected = """Hello
Is it me you're looking for?
"""
    assert d == expected


def test_markdown_rendering(factories):
    content = """Hello
Is it me you're looking for?"""

    l = factories['music.Lyrics'](content=content)

    expected = "<p>Hello<br />\nIs it me you're looking for?</p>"
    assert expected == l.content_rendered


48 49 50 51 52 53 54
def test_works_import_lyrics_if_any(
        lyricswiki_content,
        works,
        tracks,
        mocker,
        factories,
        logged_in_client):
55 56
    mocker.patch(
        'funkwhale_api.musicbrainz.api.works.get',
57
        return_value=works['get']['chop_suey'])
58 59
    mocker.patch(
        'funkwhale_api.musicbrainz.api.recordings.get',
60
        return_value=tracks['get']['chop_suey'])
61 62
    mocker.patch(
        'funkwhale_api.music.lyrics._get_html',
63
        return_value=lyricswiki_content)
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    track = factories['music.Track'](
        work=None,
        mbid='07ca77cf-f513-4e9c-b190-d7e24bbad448')

    url = reverse('api:v1:tracks-lyrics', kwargs={'pk': track.pk})
    response = logged_in_client.get(url)

    assert response.status_code == 200

    track.refresh_from_db()
    lyrics = models.Lyrics.objects.latest('id')
    work = models.Work.objects.latest('id')

    assert track.work == work
    assert lyrics.work == work