51 lines
1.4 KiB
Python
51 lines
1.4 KiB
Python
import re
|
|
|
|
import pytest
|
|
import httpx
|
|
|
|
from ntr_fetcher.soundcloud import SoundCloudClient
|
|
|
|
|
|
FAKE_HTML = """
|
|
<html><head><script>
|
|
window.__sc_hydration = [
|
|
{"hydratable": "user", "data": {}},
|
|
{"hydratable": "apiClient", "data": {"id": "test_client_id_abc123", "isExpiring": false}}
|
|
];
|
|
</script></head></html>
|
|
"""
|
|
|
|
FAKE_HTML_EXPIRING = """
|
|
<html><head><script>
|
|
window.__sc_hydration = [
|
|
{"hydratable": "apiClient", "data": {"id": "expiring_id_xyz", "isExpiring": true}}
|
|
];
|
|
</script></head></html>
|
|
"""
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_extract_client_id(httpx_mock):
|
|
httpx_mock.add_response(url="https://soundcloud.com", text=FAKE_HTML)
|
|
client = SoundCloudClient()
|
|
client_id = await client._extract_client_id()
|
|
assert client_id == "test_client_id_abc123"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_extract_client_id_caches(httpx_mock):
|
|
httpx_mock.add_response(url="https://soundcloud.com", text=FAKE_HTML)
|
|
client = SoundCloudClient()
|
|
id1 = await client._extract_client_id()
|
|
id2 = await client._extract_client_id()
|
|
assert id1 == id2
|
|
assert len(httpx_mock.get_requests()) == 1
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_extract_client_id_bad_html(httpx_mock):
|
|
httpx_mock.add_response(url="https://soundcloud.com", text="<html>no hydration here</html>")
|
|
client = SoundCloudClient()
|
|
with pytest.raises(ValueError, match="client_id"):
|
|
await client._extract_client_id()
|