2026-08-31 22:29:13 +02:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
|
|
from . import piper_engine
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TtsSynthesizeTests(TestCase):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.user = User.objects.create_user(username='alice', password='pw12345678')
|
|
|
|
|
|
|
|
|
|
def test_requires_auth(self):
|
|
|
|
|
resp = self.client.post('/tts/synthesize/', {'text': 'Hallo'}, content_type='application/json')
|
|
|
|
|
self.assertEqual(resp.status_code, 401)
|
|
|
|
|
|
|
|
|
|
def test_rejects_empty_text(self):
|
|
|
|
|
self.client.force_login(self.user)
|
|
|
|
|
resp = self.client.post('/tts/synthesize/', {'text': ' '}, content_type='application/json')
|
|
|
|
|
self.assertEqual(resp.status_code, 400)
|
|
|
|
|
|
|
|
|
|
def test_rejects_text_over_limit(self):
|
|
|
|
|
self.client.force_login(self.user)
|
|
|
|
|
resp = self.client.post(
|
|
|
|
|
'/tts/synthesize/', {'text': 'a' * 501}, content_type='application/json')
|
|
|
|
|
self.assertEqual(resp.status_code, 400)
|
|
|
|
|
|
|
|
|
|
def test_rejects_invalid_json(self):
|
|
|
|
|
self.client.force_login(self.user)
|
|
|
|
|
resp = self.client.post('/tts/synthesize/', 'not json', content_type='application/json')
|
|
|
|
|
self.assertEqual(resp.status_code, 400)
|
|
|
|
|
|
|
|
|
|
@patch.object(piper_engine, 'synthesize_wav', return_value=b'RIFF....WAVEfmt fake')
|
2026-09-01 07:04:27 +02:00
|
|
|
def test_synthesizes_audio_default_lang(self, mock_synth):
|
2026-08-31 22:29:13 +02:00
|
|
|
self.client.force_login(self.user)
|
|
|
|
|
resp = self.client.post(
|
|
|
|
|
'/tts/synthesize/', {'text': 'Hallo Welt.'}, content_type='application/json')
|
|
|
|
|
self.assertEqual(resp.status_code, 200)
|
|
|
|
|
self.assertEqual(resp['Content-Type'], 'audio/wav')
|
|
|
|
|
self.assertEqual(b''.join(resp.streaming_content), b'RIFF....WAVEfmt fake')
|
2026-09-01 07:04:27 +02:00
|
|
|
mock_synth.assert_called_once_with('Hallo Welt.', 'de')
|
|
|
|
|
|
|
|
|
|
@patch.object(piper_engine, 'synthesize_wav', return_value=b'RIFF....WAVEfmt fake')
|
|
|
|
|
def test_synthesizes_audio_explicit_lang(self, mock_synth):
|
|
|
|
|
self.client.force_login(self.user)
|
|
|
|
|
resp = self.client.post(
|
|
|
|
|
'/tts/synthesize/', {'text': 'Hello world.', 'lang': 'en'}, content_type='application/json')
|
|
|
|
|
self.assertEqual(resp.status_code, 200)
|
|
|
|
|
mock_synth.assert_called_once_with('Hello world.', 'en')
|
|
|
|
|
|
|
|
|
|
def test_rejects_unsupported_lang(self):
|
|
|
|
|
self.client.force_login(self.user)
|
|
|
|
|
resp = self.client.post(
|
|
|
|
|
'/tts/synthesize/', {'text': 'Hallo', 'lang': 'fr'}, content_type='application/json')
|
|
|
|
|
self.assertEqual(resp.status_code, 400)
|