"""Integration: the musician Flask service enforces the shared key on its
authenticated endpoints while leaving the open ones reachable.
"""
import conjurer_musician as m


def _client(key="test-secret"):
    m.API_KEY = key
    return m.app.test_client()


def test_clear_pr_pls_rejected_without_key():
    client = _client()
    assert client.get("/clear_pr_pls").status_code == 401


def test_clear_pr_pls_accepted_with_key():
    client = _client()
    resp = client.get(
        "/clear_pr_pls", headers={"X-Conjurer-Api-Key": "test-secret"}
    )
    assert resp.status_code == 200


def test_mp3_list_is_open():
    client = _client()
    resp = client.get("/mp3")
    assert resp.status_code == 200
    assert "music_file_list" in resp.get_json()


def test_open_when_key_unset():
    client = _client(key=None)
    assert client.get("/clear_pr_pls").status_code == 200
