134 lines
4.1 KiB
Python
134 lines
4.1 KiB
Python
import pytest
|
|
from django.test import Client
|
|
from core.models import AppUser, Library, Channel, MediaSource, MediaItem, Airing
|
|
from django.utils import timezone
|
|
from datetime import timedelta
|
|
import uuid
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
return Client()
|
|
|
|
@pytest.fixture
|
|
def user(db):
|
|
return AppUser.objects.create_user(username="apiuser2", email="api2@example.com", password="password")
|
|
|
|
@pytest.fixture
|
|
def library(db, user):
|
|
return Library.objects.create(owner_user=user, name="Network Lib")
|
|
|
|
@pytest.fixture
|
|
def channel(db, user, library):
|
|
return Channel.objects.create(
|
|
owner_user=user,
|
|
library=library,
|
|
name="Test Channel",
|
|
slug="test-ch",
|
|
channel_number=1,
|
|
scheduling_mode="template_driven"
|
|
)
|
|
|
|
@pytest.mark.django_db
|
|
def test_list_channels(client, channel):
|
|
response = client.get("/api/channel/")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert len(data) == 1
|
|
assert data[0]["name"] == "Test Channel"
|
|
assert data[0]["slug"] == "test-ch"
|
|
|
|
@pytest.mark.django_db
|
|
def test_get_channel(client, channel):
|
|
response = client.get(f"/api/channel/{channel.id}")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["name"] == "Test Channel"
|
|
|
|
@pytest.mark.django_db
|
|
def test_get_channel_not_found(client):
|
|
response = client.get("/api/channel/999")
|
|
assert response.status_code == 404
|
|
|
|
@pytest.mark.django_db
|
|
def test_create_channel(client, user, library):
|
|
payload = {
|
|
"name": "New API Channel",
|
|
"slug": "new-api-ch",
|
|
"channel_number": 5,
|
|
"description": "Created via API",
|
|
"library_id": library.id,
|
|
"owner_user_id": user.id
|
|
}
|
|
response = client.post("/api/channel/", data=payload, content_type="application/json")
|
|
assert response.status_code == 201
|
|
data = response.json()
|
|
assert data["name"] == "New API Channel"
|
|
assert data["slug"] == "new-api-ch"
|
|
|
|
# Verify it hit the DB
|
|
assert Channel.objects.get(id=data["id"]).name == "New API Channel"
|
|
|
|
@pytest.fixture
|
|
def media_source(db, library):
|
|
return MediaSource.objects.create(
|
|
library=library,
|
|
name="Test Source",
|
|
source_type="local_directory",
|
|
uri="/mock/test"
|
|
)
|
|
|
|
@pytest.fixture
|
|
def media_item_youtube(db, media_source):
|
|
return MediaItem.objects.create(
|
|
media_source=media_source,
|
|
title="YT Test Video",
|
|
item_kind="video",
|
|
runtime_seconds=600,
|
|
file_path="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
|
|
)
|
|
|
|
@pytest.mark.django_db
|
|
def test_channel_now_playing_uncached_media_path(client, channel, media_item_youtube):
|
|
now = timezone.now()
|
|
Airing.objects.create(
|
|
channel=channel,
|
|
media_item=media_item_youtube,
|
|
starts_at=now - timedelta(minutes=5),
|
|
ends_at=now + timedelta(minutes=5),
|
|
slot_kind="program",
|
|
status="playing",
|
|
generation_batch_uuid=uuid.uuid4()
|
|
)
|
|
|
|
response = client.get(f"/api/channel/{channel.id}/now")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["media_item_title"] == "YT Test Video"
|
|
# Should use the raw file_path since there is no cached_file_path
|
|
assert data["media_item_path"] == "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
|
|
|
|
@pytest.mark.django_db
|
|
def test_channel_now_playing_cached_media_path(client, channel, media_item_youtube):
|
|
from django.conf import settings
|
|
import os
|
|
media_item_youtube.cached_file_path = os.path.join(settings.MEDIA_ROOT, "dQw4w9WgXcQ.mp4")
|
|
media_item_youtube.save()
|
|
|
|
now = timezone.now()
|
|
Airing.objects.create(
|
|
channel=channel,
|
|
media_item=media_item_youtube,
|
|
starts_at=now - timedelta(minutes=5),
|
|
ends_at=now + timedelta(minutes=5),
|
|
slot_kind="program",
|
|
status="playing",
|
|
generation_batch_uuid=uuid.uuid4()
|
|
)
|
|
|
|
response = client.get(f"/api/channel/{channel.id}/now")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["media_item_title"] == "YT Test Video"
|
|
# Should resolve the cached_file_path to a web-accessible MEDIA_URL
|
|
assert data["media_item_path"] == "/dQw4w9WgXcQ.mp4"
|