See #170: now record downloads count on tracks/uploads

This commit is contained in:
Eliot Berriot 2020-01-20 12:13:02 +01:00
parent 3674d1235d
commit b22b9c83b0
No known key found for this signature in database
GPG key ID: 6B501DFD73514E14
12 changed files with 161 additions and 18 deletions

View file

@ -45,3 +45,49 @@ def test_guess_mimetype_dont_crash_with_s3(factories, mocker, settings):
f = factories["music.Upload"].build(audio_file__filename="test.mp3")
assert utils.guess_mimetype(f.audio_file) == "audio/mpeg"
def test_increment_downloads_count(factories, mocker, cache, anonymous_user, settings):
ident = {"type": "anonymous", "id": "noop"}
get_ident = mocker.patch(
"funkwhale_api.common.throttling.get_ident", return_value=ident
)
cache_set = mocker.spy(utils.cache, "set")
wsgi_request = mocker.Mock(META={})
upload = factories["music.Upload"]()
utils.increment_downloads_count(
upload=upload, user=anonymous_user, wsgi_request=wsgi_request
)
upload.refresh_from_db()
get_ident.assert_called_once_with(user=anonymous_user, request=wsgi_request)
assert upload.downloads_count == 1
assert upload.track.downloads_count == 1
cache_set.assert_called_once_with(
"downloads_count:upload-{}:{}-{}".format(upload.pk, ident["type"], ident["id"]),
1,
settings.MIN_DELAY_BETWEEN_DOWNLOADS_COUNT,
)
def test_increment_downloads_count_already_tracked(
factories, mocker, cache, anonymous_user
):
ident = {"type": "anonymous", "id": "noop"}
mocker.patch("funkwhale_api.common.throttling.get_ident", return_value=ident)
wsgi_request = mocker.Mock(META={})
upload = factories["music.Upload"]()
cache.set(
"downloads_count:upload-{}:{}-{}".format(upload.pk, ident["type"], ident["id"]),
1,
)
utils.increment_downloads_count(
upload=upload, user=anonymous_user, wsgi_request=wsgi_request
)
upload.refresh_from_db()
assert upload.downloads_count == 0
assert upload.track.downloads_count == 0