Let users pick their Gravatar as their profile image (SCR-1)
CI / validate-monitoring (push) Successful in 8s
CI / lint (push) Successful in 13s
CI / federation-interop (push) Successful in 1m42s
CI / test (push) Successful in 5m46s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / trigger-deploy (push) Skipped
CI / notify (push) Successful in 7s
CI / validate-monitoring (push) Successful in 8s
CI / lint (push) Successful in 13s
CI / federation-interop (push) Successful in 1m42s
CI / test (push) Successful in 5m46s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / trigger-deploy (push) Skipped
CI / notify (push) Successful in 7s
Opt-in per user and gated by AVATAR_GRAVATAR_ENABLED. The server fetches the image by the SHA-256 of the account email and caches it with the uploads, so browsers never contact Gravatar. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c55a6596d0
commit
9243fe4d3f
8 files changed
+328
-4
No files matched your search
@@ -1,6 +1,9 @@
|
||||
import hashlib
|
||||
import io
|
||||
import threading
|
||||
|
||||
import pytest
|
||||
from werkzeug.serving import make_server
|
||||
|
||||
from scrobbler.extensions import db
|
||||
from scrobbler.models import User
|
||||
@@ -115,3 +118,128 @@ def test_upload_rejects_bad_files(client, auth, app, storage, metric_delta, data
|
||||
def test_upload_requires_a_file(client, auth, storage):
|
||||
response = client.put(URL, headers=auth, data={}, content_type="multipart/form-data")
|
||||
assert response.status_code == 422
|
||||
|
||||
|
||||
# --- Gravatar ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _digest(email):
|
||||
return hashlib.sha256(email.encode()).hexdigest()
|
||||
|
||||
|
||||
GRAVATAR_JPEG = b"\xff\xd8\xff\xe0gravatar-jpeg"
|
||||
_GRAVATARS = {
|
||||
_digest("alice@example.com"): ("200 OK", GRAVATAR_JPEG),
|
||||
_digest("html@example.com"): ("200 OK", b"<html>not an image</html>"),
|
||||
_digest("big@example.com"): ("200 OK", b"\xff\xd8\xff" + b"x" * (1024 * 1024)),
|
||||
_digest("boom@example.com"): ("500 Internal Server Error", b"boom"),
|
||||
}
|
||||
SEEN = []
|
||||
|
||||
|
||||
def _fake_gravatar(environ, start_response):
|
||||
SEEN.append((environ["PATH_INFO"], environ["QUERY_STRING"]))
|
||||
status, body = _GRAVATARS.get(
|
||||
environ["PATH_INFO"].rsplit("/", 1)[-1], ("404 Not Found", b"not found")
|
||||
)
|
||||
start_response(status, [("Content-Length", str(len(body)))])
|
||||
return [body]
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def fake_gravatar():
|
||||
server = make_server("127.0.0.1", 0, _fake_gravatar, threaded=True)
|
||||
threading.Thread(target=server.serve_forever, daemon=True).start()
|
||||
yield f"http://127.0.0.1:{server.server_port}/avatar/"
|
||||
server.shutdown()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def gravatar(app, storage, fake_gravatar):
|
||||
keys = ("AVATAR_GRAVATAR_ENABLED", "GRAVATAR_URL")
|
||||
saved = {k: app.config[k] for k in keys}
|
||||
app.config.update(AVATAR_GRAVATAR_ENABLED=True, GRAVATAR_URL=fake_gravatar)
|
||||
SEEN.clear()
|
||||
yield
|
||||
app.config.update(saved)
|
||||
|
||||
|
||||
def use_gravatar(client, auth):
|
||||
return client.post(f"{URL}/gravatar", headers=auth)
|
||||
|
||||
|
||||
def set_email(user, email):
|
||||
user.email = email
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def test_gravatar_requires_auth(client):
|
||||
assert client.post(f"{URL}/gravatar").status_code == 401
|
||||
|
||||
|
||||
def test_gravatar_is_off_by_default(client, auth, user, storage, metric_delta):
|
||||
me = client.get("/api/v1/auth/me", headers=auth).get_json()
|
||||
assert me["gravatar_enabled"] is False
|
||||
response = use_gravatar(client, auth)
|
||||
assert (response.status_code, response.get_json()["error"]["code"]) == (
|
||||
404,
|
||||
"gravatar_disabled",
|
||||
)
|
||||
|
||||
|
||||
def test_gravatar_is_cached_and_served_like_an_upload(client, auth, user, gravatar, metric_delta):
|
||||
ok = metric_delta("scrobbler_avatar_gravatar_fetches_total", result="ok")
|
||||
action = metric_delta("scrobbler_avatar_actions_total", action="gravatar")
|
||||
assert client.get("/api/v1/auth/me", headers=auth).get_json()["gravatar_enabled"] is True
|
||||
response = use_gravatar(client, auth)
|
||||
assert response.status_code == 200
|
||||
assert response.get_json()["avatar_source"] == "gravatar"
|
||||
image = client.get(URL, headers=auth)
|
||||
assert (image.data, image.mimetype) == (GRAVATAR_JPEG, "image/jpeg")
|
||||
# Asked for the SHA-256 of the email at a fixed size, with a 404 rather than a stock picture.
|
||||
assert [(f"/avatar/{_digest('alice@example.com')}", "s=256&d=404")] == SEEN
|
||||
assert (ok.delta, action.delta) == (1, 1)
|
||||
|
||||
|
||||
def test_gravatar_email_is_trimmed_and_lowercased(client, auth, user, gravatar):
|
||||
set_email(user, " Alice@Example.COM ")
|
||||
assert use_gravatar(client, auth).status_code == 200
|
||||
|
||||
|
||||
def test_gravatar_replaces_an_upload_and_back(client, auth, user, gravatar):
|
||||
put(client, auth, data=PNG, name="me.png")
|
||||
use_gravatar(client, auth)
|
||||
assert client.get(URL, headers=auth).data == GRAVATAR_JPEG
|
||||
put(client, auth)
|
||||
assert client.get(URL, headers=auth).data == JPEG
|
||||
assert client.get("/api/v1/auth/me", headers=auth).get_json()["avatar_source"] == "upload"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("email", "status", "code", "result"),
|
||||
[
|
||||
("ghost@example.com", 404, "no_gravatar", "none"),
|
||||
("boom@example.com", 502, "gravatar_unavailable", "error"),
|
||||
("html@example.com", 502, "gravatar_unavailable", "bad_type"),
|
||||
("big@example.com", 502, "gravatar_unavailable", "too_large"),
|
||||
],
|
||||
)
|
||||
def test_gravatar_failures_keep_the_current_image(
|
||||
client, auth, user, gravatar, metric_delta, email, status, code, result
|
||||
):
|
||||
put(client, auth)
|
||||
set_email(user, email)
|
||||
counted = metric_delta("scrobbler_avatar_gravatar_fetches_total", result=result)
|
||||
response = use_gravatar(client, auth)
|
||||
assert (response.status_code, response.get_json()["error"]["code"]) == (status, code)
|
||||
assert counted.delta == 1
|
||||
assert client.get(URL, headers=auth).data == JPEG
|
||||
|
||||
|
||||
def test_gravatar_unreachable_is_a_502(client, auth, user, gravatar, app):
|
||||
app.config["GRAVATAR_URL"] = "http://127.0.0.1:1/avatar/"
|
||||
response = use_gravatar(client, auth)
|
||||
assert (response.status_code, response.get_json()["error"]["code"]) == (
|
||||
502,
|
||||
"gravatar_unavailable",
|
||||
)
|
||||
Reference in new issue
Block a user