2026-09-17 21:07:33 +00:00
|
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
|
|
2026-09-16 19:47:35 +00:00
|
|
|
import pytest
|
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
|
|
|
|
|
from app.core.auth_dependency import require_session
|
|
|
|
|
from app.db import get_db
|
|
|
|
|
from app.main import app
|
|
|
|
|
from app.models.channel import Channel
|
2026-09-17 21:07:33 +00:00
|
|
|
from app.models.video import Video
|
2026-09-16 19:47:35 +00:00
|
|
|
from app.services import sync
|
|
|
|
|
from app.services.youtube_client import YouTubeInsufficientScope
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def client(db_session):
|
|
|
|
|
def _get_db_override():
|
|
|
|
|
yield db_session
|
|
|
|
|
|
|
|
|
|
app.dependency_overrides[get_db] = _get_db_override
|
|
|
|
|
app.dependency_overrides[require_session] = lambda: None
|
|
|
|
|
# Deliberately not using `with TestClient(app)`: that runs the app's
|
|
|
|
|
# lifespan, which would try to reach the real MeTube instance and DB.
|
|
|
|
|
yield TestClient(app)
|
|
|
|
|
del app.dependency_overrides[get_db]
|
|
|
|
|
del app.dependency_overrides[require_session]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _seed_channel(db_session, youtube_subscription_id="sub1"):
|
|
|
|
|
channel = Channel(
|
|
|
|
|
youtube_channel_id="chanA",
|
|
|
|
|
youtube_subscription_id=youtube_subscription_id,
|
|
|
|
|
title="Channel A",
|
|
|
|
|
subscribed=True,
|
|
|
|
|
)
|
|
|
|
|
db_session.add(channel)
|
|
|
|
|
db_session.commit()
|
|
|
|
|
db_session.refresh(channel)
|
|
|
|
|
return channel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_unsubscribe_success(client, db_session, monkeypatch):
|
|
|
|
|
channel = _seed_channel(db_session)
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(sync.google_oauth, "get_credentials", lambda db: object())
|
|
|
|
|
calls = {}
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
sync.youtube_client,
|
|
|
|
|
"unsubscribe",
|
|
|
|
|
lambda creds, sub_id: calls.setdefault("sub_id", sub_id),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
resp = client.post(f"/api/channels/{channel.id}/unsubscribe")
|
|
|
|
|
|
|
|
|
|
assert resp.status_code == 200
|
|
|
|
|
assert resp.json()["subscribed"] is False
|
|
|
|
|
assert calls["sub_id"] == "sub1"
|
|
|
|
|
|
|
|
|
|
db_session.refresh(channel)
|
|
|
|
|
assert channel.subscribed is False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_unsubscribe_without_subscription_id_returns_400(client, db_session):
|
|
|
|
|
channel = _seed_channel(db_session, youtube_subscription_id=None)
|
|
|
|
|
|
|
|
|
|
resp = client.post(f"/api/channels/{channel.id}/unsubscribe")
|
|
|
|
|
|
|
|
|
|
assert resp.status_code == 400
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_unsubscribe_insufficient_scope_returns_403(client, db_session, monkeypatch):
|
|
|
|
|
channel = _seed_channel(db_session)
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(sync.google_oauth, "get_credentials", lambda db: object())
|
|
|
|
|
|
|
|
|
|
def raise_insufficient(creds, sub_id):
|
|
|
|
|
raise YouTubeInsufficientScope("Request had insufficient authentication scopes.")
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(sync.youtube_client, "unsubscribe", raise_insufficient)
|
|
|
|
|
|
|
|
|
|
resp = client.post(f"/api/channels/{channel.id}/unsubscribe")
|
|
|
|
|
|
|
|
|
|
assert resp.status_code == 403
|
|
|
|
|
|
|
|
|
|
db_session.refresh(channel)
|
|
|
|
|
assert channel.subscribed is True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_unsubscribe_channel_not_found(client):
|
|
|
|
|
resp = client.post("/api/channels/9999/unsubscribe")
|
|
|
|
|
assert resp.status_code == 404
|
2026-09-17 21:07:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _seed_video(db_session, channel, video_id, published_at):
|
|
|
|
|
video = Video(
|
|
|
|
|
youtube_video_id=video_id,
|
|
|
|
|
channel_id=channel.id,
|
|
|
|
|
title=f"Video {video_id}",
|
|
|
|
|
published_at=published_at,
|
|
|
|
|
youtube_url=f"https://www.youtube.com/watch?v={video_id}",
|
|
|
|
|
)
|
|
|
|
|
db_session.add(video)
|
|
|
|
|
db_session.commit()
|
|
|
|
|
return video
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_channel_responses_include_subscriber_and_new_videos_counts(client, db_session):
|
|
|
|
|
channel = _seed_channel(db_session)
|
|
|
|
|
channel.subscriber_count = 1200000
|
|
|
|
|
db_session.commit()
|
|
|
|
|
|
|
|
|
|
now = datetime.now(timezone.utc)
|
|
|
|
|
_seed_video(db_session, channel, "vidRecent", now - timedelta(days=1))
|
|
|
|
|
_seed_video(db_session, channel, "vidOld", now - timedelta(days=30))
|
|
|
|
|
|
|
|
|
|
resp = client.get("/api/channels")
|
|
|
|
|
assert resp.status_code == 200
|
|
|
|
|
payload = resp.json()
|
|
|
|
|
assert len(payload) == 1
|
|
|
|
|
assert payload[0]["subscriber_count"] == 1200000
|
|
|
|
|
# Only the video from 1 day ago is inside the 7-day window.
|
|
|
|
|
assert payload[0]["new_videos_count"] == 1
|
|
|
|
|
|
|
|
|
|
resp_single = client.get(f"/api/channels/{channel.id}")
|
|
|
|
|
assert resp_single.status_code == 200
|
|
|
|
|
single = resp_single.json()
|
|
|
|
|
assert single["subscriber_count"] == 1200000
|
|
|
|
|
assert single["new_videos_count"] == 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_channel_new_videos_count_zero_without_recent_videos(client, db_session):
|
|
|
|
|
channel = _seed_channel(db_session)
|
|
|
|
|
|
|
|
|
|
now = datetime.now(timezone.utc)
|
|
|
|
|
_seed_video(db_session, channel, "vidOld", now - timedelta(days=30))
|
|
|
|
|
|
|
|
|
|
resp = client.get("/api/channels")
|
|
|
|
|
assert resp.status_code == 200
|
|
|
|
|
payload = resp.json()
|
|
|
|
|
assert len(payload) == 1
|
|
|
|
|
assert payload[0]["subscriber_count"] is None
|
|
|
|
|
assert payload[0]["new_videos_count"] == 0
|