| 1 |
import re |
| 2 |
import uuid |
| 3 |
from contextlib import nullcontext as does_not_raise |
| 4 |
|
| 5 |
import pytest |
| 6 |
import requests |
| 7 |
|
| 8 |
from beets.library import Album |
| 9 |
from beets.test.helper import PluginMixin, TestHelper |
| 10 |
from beets.ui import UserError |
| 11 |
from beetsplug import mbcollection |
| 12 |
|
| 13 |
|
| 14 |
class TestMbCollectionPlugin(PluginMixin, TestHelper): |
| 15 |
"""Tests for the MusicBrainzCollectionPlugin class methods.""" |
| 16 |
|
| 17 |
plugin = "mbcollection" |
| 18 |
|
| 19 |
COLLECTION_ID = str(uuid.uuid4()) |
| 20 |
|
| 21 |
@pytest.fixture(autouse=True) |
| 22 |
def setup_config(self): |
| 23 |
self.config["musicbrainz"]["user"] = "testuser" |
| 24 |
self.config["musicbrainz"]["pass"] = "testpass" |
| 25 |
self.config["mbcollection"]["collection"] = self.COLLECTION_ID |
| 26 |
|
| 27 |
@pytest.mark.parametrize( |
| 28 |
"user_collections,expectation", |
| 29 |
[ |
| 30 |
( |
| 31 |
[], |
| 32 |
pytest.raises( |
| 33 |
UserError, match=r"no collections exist for user" |
| 34 |
), |
| 35 |
), |
| 36 |
( |
| 37 |
[{"id": "c1", "entity_type": "event"}], |
| 38 |
pytest.raises(UserError, match=r"No release collection found."), |
| 39 |
), |
| 40 |
( |
| 41 |
[{"id": "c1", "entity_type": "release"}], |
| 42 |
pytest.raises(UserError, match=r"invalid collection ID"), |
| 43 |
), |
| 44 |
( |
| 45 |
[{"id": COLLECTION_ID, "entity_type": "release"}], |
| 46 |
does_not_raise(), |
| 47 |
), |
| 48 |
], |
| 49 |
ids=["no collections", "no release collections", "invalid ID", "valid"], |
| 50 |
) |
| 51 |
def test_get_collection_validation( |
| 52 |
self, requests_mock, user_collections, expectation |
| 53 |
): |
| 54 |
requests_mock.get( |
| 55 |
"/ws/2/collection", json={"collections": user_collections} |
| 56 |
) |
| 57 |
|
| 58 |
with expectation: |
| 59 |
mbcollection.MusicBrainzCollectionPlugin().collection |
| 60 |
|
| 61 |
def test_mbupdate(self, requests_mock, monkeypatch): |
| 62 |
"""Verify mbupdate sync of a MusicBrainz collection with the library. |
| 63 |
|
| 64 |
This test ensures that the command: |
| 65 |
- fetches collection releases using paginated requests, |
| 66 |
- submits releases that exist locally but are missing from the remote |
| 67 |
collection |
| 68 |
- and removes releases from the remote collection that are not in the |
| 69 |
local library. Small chunk sizes are forced to exercise pagination and |
| 70 |
batching logic. |
| 71 |
""" |
| 72 |
for mb_albumid in [ |
| 73 |
# already present in remote collection |
| 74 |
"in_collection1", |
| 75 |
"in_collection2", |
| 76 |
# two new albums not in remote collection |
| 77 |
"00000000-0000-0000-0000-000000000001", |
| 78 |
"00000000-0000-0000-0000-000000000002", |
| 79 |
]: |
| 80 |
self.lib.add(Album(mb_albumid=mb_albumid)) |
| 81 |
|
| 82 |
# The relevant collection |
| 83 |
requests_mock.get( |
| 84 |
"/ws/2/collection", |
| 85 |
json={ |
| 86 |
"collections": [ |
| 87 |
{ |
| 88 |
"id": self.COLLECTION_ID, |
| 89 |
"entity_type": "release", |
| 90 |
"release_count": 3, |
| 91 |
} |
| 92 |
] |
| 93 |
}, |
| 94 |
) |
| 95 |
|
| 96 |
collection_releases = f"/ws/2/collection/{self.COLLECTION_ID}/releases" |
| 97 |
# Force small fetch chunk to require multiple paged requests. |
| 98 |
monkeypatch.setattr( |
| 99 |
"beetsplug.mbcollection.MBCollection.FETCH_CHUNK_SIZE", 2 |
| 100 |
) |
| 101 |
# 3 releases are fetched in two pages. |
| 102 |
requests_mock.get( |
| 103 |
re.compile(rf".*{collection_releases}\b.*&offset=0.*"), |
| 104 |
json={ |
| 105 |
"releases": [{"id": "in_collection1"}, {"id": "not_in_library"}] |
| 106 |
}, |
| 107 |
) |
| 108 |
requests_mock.get( |
| 109 |
re.compile(rf".*{collection_releases}\b.*&offset=2.*"), |
| 110 |
json={"releases": [{"id": "in_collection2"}]}, |
| 111 |
) |
| 112 |
|
| 113 |
# Force small submission chunk |
| 114 |
monkeypatch.setattr( |
| 115 |
"beetsplug.mbcollection.MBCollection.SUBMISSION_CHUNK_SIZE", 1 |
| 116 |
) |
| 117 |
# so that releases are added using two requests |
| 118 |
requests_mock.put( |
| 119 |
re.compile( |
| 120 |
rf".*{collection_releases}/00000000-0000-0000-0000-000000000001" |
| 121 |
) |
| 122 |
) |
| 123 |
requests_mock.put( |
| 124 |
re.compile( |
| 125 |
rf".*{collection_releases}/00000000-0000-0000-0000-000000000002" |
| 126 |
) |
| 127 |
) |
| 128 |
# and finally, one release is removed |
| 129 |
requests_mock.delete( |
| 130 |
re.compile(rf".*{collection_releases}/not_in_library") |
| 131 |
) |
| 132 |
|
| 133 |
self.run_command("mbupdate", "--remove") |
| 134 |
|
| 135 |
assert requests_mock.call_count == 6 |
| 136 |
|
| 137 |
def test_mbupdate_logs_unauthorized_errors(self, requests_mock, caplog): |
| 138 |
response = requests.Response() |
| 139 |
response.status_code = 401 |
| 140 |
requests_mock.get( |
| 141 |
"/ws/2/collection", |
| 142 |
exc=requests.exceptions.HTTPError(response=response), |
| 143 |
) |
| 144 |
|
| 145 |
with caplog.at_level("ERROR", logger="beets.mbcollection"): |
| 146 |
self.run_command("mbupdate") |
| 147 |
|
| 148 |
expected_message = ( |
| 149 |
"Failed to update MusicBrainz collection: HTTP Error: 401" |
| 150 |
" Unauthorized. Check your musicbrainz.user and musicbrainz.pass" |
| 151 |
" configuration" |
| 152 |
) |
| 153 |
assert expected_message in caplog.text |