karaoke-gen 0.101.0__py3-none-any.whl → 0.105.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- backend/Dockerfile.base +1 -0
- backend/api/routes/admin.py +226 -3
- backend/api/routes/audio_search.py +4 -32
- backend/api/routes/file_upload.py +18 -83
- backend/api/routes/jobs.py +2 -2
- backend/api/routes/push.py +238 -0
- backend/api/routes/rate_limits.py +428 -0
- backend/api/routes/users.py +79 -19
- backend/config.py +25 -1
- backend/exceptions.py +66 -0
- backend/main.py +26 -1
- backend/models/job.py +4 -0
- backend/models/user.py +20 -2
- backend/services/email_validation_service.py +646 -0
- backend/services/firestore_service.py +21 -0
- backend/services/gce_encoding/main.py +22 -8
- backend/services/job_defaults_service.py +113 -0
- backend/services/job_manager.py +109 -13
- backend/services/push_notification_service.py +409 -0
- backend/services/rate_limit_service.py +641 -0
- backend/services/stripe_service.py +2 -2
- backend/tests/conftest.py +8 -1
- backend/tests/test_admin_delete_outputs.py +352 -0
- backend/tests/test_audio_search.py +12 -8
- backend/tests/test_email_validation_service.py +298 -0
- backend/tests/test_file_upload.py +8 -6
- backend/tests/test_gce_encoding_worker.py +229 -0
- backend/tests/test_impersonation.py +18 -3
- backend/tests/test_made_for_you.py +6 -4
- backend/tests/test_push_notification_service.py +460 -0
- backend/tests/test_push_routes.py +357 -0
- backend/tests/test_rate_limit_service.py +396 -0
- backend/tests/test_rate_limits_api.py +392 -0
- backend/tests/test_stripe_service.py +205 -0
- backend/workers/video_worker_orchestrator.py +42 -0
- karaoke_gen/instrumental_review/static/index.html +35 -9
- {karaoke_gen-0.101.0.dist-info → karaoke_gen-0.105.4.dist-info}/METADATA +2 -1
- {karaoke_gen-0.101.0.dist-info → karaoke_gen-0.105.4.dist-info}/RECORD +41 -26
- {karaoke_gen-0.101.0.dist-info → karaoke_gen-0.105.4.dist-info}/WHEEL +0 -0
- {karaoke_gen-0.101.0.dist-info → karaoke_gen-0.105.4.dist-info}/entry_points.txt +0 -0
- {karaoke_gen-0.101.0.dist-info → karaoke_gen-0.105.4.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Unit tests for EmailValidationService.
|
|
3
|
+
|
|
4
|
+
Tests email normalization, disposable domain detection, and blocklist management.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import pytest
|
|
8
|
+
from unittest.mock import Mock, MagicMock, patch
|
|
9
|
+
|
|
10
|
+
# Mock Google Cloud before imports
|
|
11
|
+
import sys
|
|
12
|
+
sys.modules['google.cloud.firestore'] = MagicMock()
|
|
13
|
+
sys.modules['google.cloud.storage'] = MagicMock()
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class TestEmailNormalization:
|
|
17
|
+
"""Test email normalization logic."""
|
|
18
|
+
|
|
19
|
+
@pytest.fixture
|
|
20
|
+
def mock_db(self):
|
|
21
|
+
"""Create a mock Firestore client."""
|
|
22
|
+
mock = MagicMock()
|
|
23
|
+
# Default: empty blocklist config
|
|
24
|
+
mock_doc = Mock()
|
|
25
|
+
mock_doc.exists = False
|
|
26
|
+
mock.collection.return_value.document.return_value.get.return_value = mock_doc
|
|
27
|
+
return mock
|
|
28
|
+
|
|
29
|
+
@pytest.fixture
|
|
30
|
+
def email_service(self, mock_db):
|
|
31
|
+
"""Create EmailValidationService instance with mocks."""
|
|
32
|
+
from backend.services.email_validation_service import EmailValidationService
|
|
33
|
+
service = EmailValidationService(db=mock_db)
|
|
34
|
+
return service
|
|
35
|
+
|
|
36
|
+
# =========================================================================
|
|
37
|
+
# Gmail Normalization Tests
|
|
38
|
+
# =========================================================================
|
|
39
|
+
|
|
40
|
+
def test_normalize_gmail_removes_dots(self, email_service):
|
|
41
|
+
"""Test that dots are removed from Gmail local part."""
|
|
42
|
+
result = email_service.normalize_email("j.o.h.n@gmail.com")
|
|
43
|
+
assert result == "john@gmail.com"
|
|
44
|
+
|
|
45
|
+
def test_normalize_gmail_removes_plus_suffix(self, email_service):
|
|
46
|
+
"""Test that +tag is removed from Gmail local part."""
|
|
47
|
+
result = email_service.normalize_email("john+spam@gmail.com")
|
|
48
|
+
assert result == "john@gmail.com"
|
|
49
|
+
|
|
50
|
+
def test_normalize_gmail_removes_dots_and_plus(self, email_service):
|
|
51
|
+
"""Test both dots and +tag are removed from Gmail."""
|
|
52
|
+
result = email_service.normalize_email("j.o.h.n+newsletter@gmail.com")
|
|
53
|
+
assert result == "john@gmail.com"
|
|
54
|
+
|
|
55
|
+
def test_normalize_googlemail_treated_like_gmail(self, email_service):
|
|
56
|
+
"""Test that googlemail.com is normalized like gmail.com."""
|
|
57
|
+
result = email_service.normalize_email("j.o.h.n+test@googlemail.com")
|
|
58
|
+
assert result == "john@googlemail.com"
|
|
59
|
+
|
|
60
|
+
def test_normalize_gmail_to_lowercase(self, email_service):
|
|
61
|
+
"""Test Gmail addresses are lowercased."""
|
|
62
|
+
result = email_service.normalize_email("JOHN.DOE+Test@GMAIL.COM")
|
|
63
|
+
assert result == "johndoe@gmail.com"
|
|
64
|
+
|
|
65
|
+
# =========================================================================
|
|
66
|
+
# Non-Gmail Normalization Tests
|
|
67
|
+
# =========================================================================
|
|
68
|
+
|
|
69
|
+
def test_normalize_non_gmail_preserves_dots(self, email_service):
|
|
70
|
+
"""Test dots are preserved for non-Gmail domains."""
|
|
71
|
+
result = email_service.normalize_email("j.o.h.n@example.com")
|
|
72
|
+
assert result == "j.o.h.n@example.com"
|
|
73
|
+
|
|
74
|
+
def test_normalize_non_gmail_preserves_plus(self, email_service):
|
|
75
|
+
"""Test +tag is preserved for non-Gmail domains."""
|
|
76
|
+
result = email_service.normalize_email("john+tag@company.com")
|
|
77
|
+
assert result == "john+tag@company.com"
|
|
78
|
+
|
|
79
|
+
def test_normalize_non_gmail_to_lowercase(self, email_service):
|
|
80
|
+
"""Test non-Gmail addresses are lowercased."""
|
|
81
|
+
result = email_service.normalize_email("John.Doe@Example.Com")
|
|
82
|
+
assert result == "john.doe@example.com"
|
|
83
|
+
|
|
84
|
+
def test_normalize_strips_whitespace(self, email_service):
|
|
85
|
+
"""Test whitespace is stripped."""
|
|
86
|
+
result = email_service.normalize_email(" john@example.com ")
|
|
87
|
+
assert result == "john@example.com"
|
|
88
|
+
|
|
89
|
+
# =========================================================================
|
|
90
|
+
# Edge Cases
|
|
91
|
+
# =========================================================================
|
|
92
|
+
|
|
93
|
+
def test_normalize_empty_string(self, email_service):
|
|
94
|
+
"""Test empty string returns empty."""
|
|
95
|
+
result = email_service.normalize_email("")
|
|
96
|
+
assert result == ""
|
|
97
|
+
|
|
98
|
+
def test_normalize_none_returns_empty(self, email_service):
|
|
99
|
+
"""Test None returns empty string."""
|
|
100
|
+
result = email_service.normalize_email(None)
|
|
101
|
+
assert result == ""
|
|
102
|
+
|
|
103
|
+
def test_normalize_invalid_no_at_sign(self, email_service):
|
|
104
|
+
"""Test email without @ is returned as-is (lowercase)."""
|
|
105
|
+
result = email_service.normalize_email("notanemail")
|
|
106
|
+
assert result == "notanemail"
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
class TestDisposableDomainDetection:
|
|
110
|
+
"""Test disposable domain detection."""
|
|
111
|
+
|
|
112
|
+
@pytest.fixture
|
|
113
|
+
def mock_db(self):
|
|
114
|
+
"""Create a mock Firestore client."""
|
|
115
|
+
mock = MagicMock()
|
|
116
|
+
mock_doc = Mock()
|
|
117
|
+
mock_doc.exists = False
|
|
118
|
+
mock.collection.return_value.document.return_value.get.return_value = mock_doc
|
|
119
|
+
return mock
|
|
120
|
+
|
|
121
|
+
@pytest.fixture
|
|
122
|
+
def email_service(self, mock_db):
|
|
123
|
+
"""Create EmailValidationService instance with mocks."""
|
|
124
|
+
from backend.services.email_validation_service import EmailValidationService
|
|
125
|
+
# Clear any cached blocklist
|
|
126
|
+
EmailValidationService._blocklist_cache = None
|
|
127
|
+
EmailValidationService._blocklist_cache_time = None
|
|
128
|
+
service = EmailValidationService(db=mock_db)
|
|
129
|
+
return service
|
|
130
|
+
|
|
131
|
+
def test_detect_known_disposable_domain(self, email_service):
|
|
132
|
+
"""Test known disposable domains are detected."""
|
|
133
|
+
# These are in DEFAULT_DISPOSABLE_DOMAINS
|
|
134
|
+
assert email_service.is_disposable_domain("user@tempmail.com") is True
|
|
135
|
+
assert email_service.is_disposable_domain("user@mailinator.com") is True
|
|
136
|
+
assert email_service.is_disposable_domain("user@guerrillamail.com") is True
|
|
137
|
+
|
|
138
|
+
def test_detect_legitimate_domain(self, email_service):
|
|
139
|
+
"""Test legitimate domains are not flagged."""
|
|
140
|
+
assert email_service.is_disposable_domain("user@gmail.com") is False
|
|
141
|
+
assert email_service.is_disposable_domain("user@yahoo.com") is False
|
|
142
|
+
assert email_service.is_disposable_domain("user@company.com") is False
|
|
143
|
+
|
|
144
|
+
def test_detect_case_insensitive(self, email_service):
|
|
145
|
+
"""Test domain detection is case-insensitive."""
|
|
146
|
+
assert email_service.is_disposable_domain("user@TEMPMAIL.COM") is True
|
|
147
|
+
assert email_service.is_disposable_domain("user@TempMail.Com") is True
|
|
148
|
+
|
|
149
|
+
def test_detect_invalid_email_returns_false(self, email_service):
|
|
150
|
+
"""Test invalid email returns False."""
|
|
151
|
+
assert email_service.is_disposable_domain("notanemail") is False
|
|
152
|
+
assert email_service.is_disposable_domain("") is False
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
class TestBlocklistManagement:
|
|
156
|
+
"""Test blocklist management functionality."""
|
|
157
|
+
|
|
158
|
+
@pytest.fixture
|
|
159
|
+
def mock_db(self):
|
|
160
|
+
"""Create a mock Firestore client with blocklist."""
|
|
161
|
+
mock = MagicMock()
|
|
162
|
+
mock_doc = Mock()
|
|
163
|
+
mock_doc.exists = True
|
|
164
|
+
mock_doc.to_dict.return_value = {
|
|
165
|
+
"disposable_domains": ["custom-temp.com"],
|
|
166
|
+
"blocked_emails": ["spammer@example.com"],
|
|
167
|
+
"blocked_ips": ["192.168.1.100"],
|
|
168
|
+
}
|
|
169
|
+
mock.collection.return_value.document.return_value.get.return_value = mock_doc
|
|
170
|
+
return mock
|
|
171
|
+
|
|
172
|
+
@pytest.fixture
|
|
173
|
+
def email_service(self, mock_db):
|
|
174
|
+
"""Create EmailValidationService instance with mocks."""
|
|
175
|
+
from backend.services.email_validation_service import EmailValidationService
|
|
176
|
+
# Clear any cached blocklist
|
|
177
|
+
EmailValidationService._blocklist_cache = None
|
|
178
|
+
EmailValidationService._blocklist_cache_time = None
|
|
179
|
+
service = EmailValidationService(db=mock_db)
|
|
180
|
+
return service
|
|
181
|
+
|
|
182
|
+
def test_is_email_blocked(self, email_service):
|
|
183
|
+
"""Test blocked email detection."""
|
|
184
|
+
assert email_service.is_email_blocked("spammer@example.com") is True
|
|
185
|
+
assert email_service.is_email_blocked("legitimate@example.com") is False
|
|
186
|
+
|
|
187
|
+
def test_is_email_blocked_case_insensitive(self, email_service):
|
|
188
|
+
"""Test blocked email detection is case-insensitive."""
|
|
189
|
+
assert email_service.is_email_blocked("SPAMMER@example.com") is True
|
|
190
|
+
assert email_service.is_email_blocked("Spammer@Example.Com") is True
|
|
191
|
+
|
|
192
|
+
def test_is_ip_blocked(self, email_service):
|
|
193
|
+
"""Test blocked IP detection."""
|
|
194
|
+
assert email_service.is_ip_blocked("192.168.1.100") is True
|
|
195
|
+
assert email_service.is_ip_blocked("10.0.0.1") is False
|
|
196
|
+
|
|
197
|
+
def test_custom_disposable_domain_added(self, email_service):
|
|
198
|
+
"""Test custom disposable domains from Firestore are included."""
|
|
199
|
+
assert email_service.is_disposable_domain("user@custom-temp.com") is True
|
|
200
|
+
|
|
201
|
+
def test_blocklist_caching(self, email_service, mock_db):
|
|
202
|
+
"""Test blocklist is cached."""
|
|
203
|
+
# First call
|
|
204
|
+
email_service.is_disposable_domain("user@test.com")
|
|
205
|
+
# Second call should use cache
|
|
206
|
+
email_service.is_disposable_domain("user@test2.com")
|
|
207
|
+
|
|
208
|
+
# Should only call Firestore once due to caching
|
|
209
|
+
assert mock_db.collection.return_value.document.return_value.get.call_count == 1
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
class TestBetaEnrollmentValidation:
|
|
213
|
+
"""Test beta enrollment validation."""
|
|
214
|
+
|
|
215
|
+
@pytest.fixture
|
|
216
|
+
def mock_db(self):
|
|
217
|
+
"""Create a mock Firestore client."""
|
|
218
|
+
mock = MagicMock()
|
|
219
|
+
mock_doc = Mock()
|
|
220
|
+
mock_doc.exists = True
|
|
221
|
+
mock_doc.to_dict.return_value = {
|
|
222
|
+
"disposable_domains": [],
|
|
223
|
+
"blocked_emails": ["blocked@example.com"],
|
|
224
|
+
"blocked_ips": [],
|
|
225
|
+
}
|
|
226
|
+
mock.collection.return_value.document.return_value.get.return_value = mock_doc
|
|
227
|
+
return mock
|
|
228
|
+
|
|
229
|
+
@pytest.fixture
|
|
230
|
+
def email_service(self, mock_db):
|
|
231
|
+
"""Create EmailValidationService instance with mocks."""
|
|
232
|
+
from backend.services.email_validation_service import EmailValidationService
|
|
233
|
+
EmailValidationService._blocklist_cache = None
|
|
234
|
+
EmailValidationService._blocklist_cache_time = None
|
|
235
|
+
service = EmailValidationService(db=mock_db)
|
|
236
|
+
return service
|
|
237
|
+
|
|
238
|
+
def test_validate_legitimate_email(self, email_service):
|
|
239
|
+
"""Test legitimate email passes validation."""
|
|
240
|
+
is_valid, error = email_service.validate_email_for_beta("user@gmail.com")
|
|
241
|
+
assert is_valid is True
|
|
242
|
+
assert error == ""
|
|
243
|
+
|
|
244
|
+
def test_validate_disposable_email_rejected(self, email_service):
|
|
245
|
+
"""Test disposable email is rejected."""
|
|
246
|
+
is_valid, error = email_service.validate_email_for_beta("user@tempmail.com")
|
|
247
|
+
assert is_valid is False
|
|
248
|
+
assert "Disposable email" in error
|
|
249
|
+
|
|
250
|
+
def test_validate_blocked_email_rejected(self, email_service):
|
|
251
|
+
"""Test blocked email is rejected."""
|
|
252
|
+
is_valid, error = email_service.validate_email_for_beta("blocked@example.com")
|
|
253
|
+
assert is_valid is False
|
|
254
|
+
assert "not allowed" in error
|
|
255
|
+
|
|
256
|
+
def test_validate_invalid_format_rejected(self, email_service):
|
|
257
|
+
"""Test invalid email format is rejected."""
|
|
258
|
+
is_valid, error = email_service.validate_email_for_beta("notanemail")
|
|
259
|
+
assert is_valid is False
|
|
260
|
+
assert "Invalid email format" in error
|
|
261
|
+
|
|
262
|
+
def test_validate_empty_email_rejected(self, email_service):
|
|
263
|
+
"""Test empty email is rejected."""
|
|
264
|
+
is_valid, error = email_service.validate_email_for_beta("")
|
|
265
|
+
assert is_valid is False
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
class TestIPHashing:
|
|
269
|
+
"""Test IP address hashing."""
|
|
270
|
+
|
|
271
|
+
@pytest.fixture
|
|
272
|
+
def email_service(self):
|
|
273
|
+
"""Create EmailValidationService instance with mocks."""
|
|
274
|
+
mock_db = MagicMock()
|
|
275
|
+
mock_doc = Mock()
|
|
276
|
+
mock_doc.exists = False
|
|
277
|
+
mock_db.collection.return_value.document.return_value.get.return_value = mock_doc
|
|
278
|
+
|
|
279
|
+
from backend.services.email_validation_service import EmailValidationService
|
|
280
|
+
return EmailValidationService(db=mock_db)
|
|
281
|
+
|
|
282
|
+
def test_hash_ip_consistent(self, email_service):
|
|
283
|
+
"""Test IP hashing produces consistent results."""
|
|
284
|
+
hash1 = email_service.hash_ip("192.168.1.1")
|
|
285
|
+
hash2 = email_service.hash_ip("192.168.1.1")
|
|
286
|
+
assert hash1 == hash2
|
|
287
|
+
|
|
288
|
+
def test_hash_ip_different_for_different_ips(self, email_service):
|
|
289
|
+
"""Test different IPs produce different hashes."""
|
|
290
|
+
hash1 = email_service.hash_ip("192.168.1.1")
|
|
291
|
+
hash2 = email_service.hash_ip("192.168.1.2")
|
|
292
|
+
assert hash1 != hash2
|
|
293
|
+
|
|
294
|
+
def test_hash_ip_is_sha256(self, email_service):
|
|
295
|
+
"""Test IP hash is SHA-256 (64 hex chars)."""
|
|
296
|
+
hash_result = email_service.hash_ip("192.168.1.1")
|
|
297
|
+
assert len(hash_result) == 64
|
|
298
|
+
assert all(c in "0123456789abcdef" for c in hash_result)
|
|
@@ -1681,17 +1681,19 @@ class TestUploadEndpointThemeSupport:
|
|
|
1681
1681
|
)
|
|
1682
1682
|
|
|
1683
1683
|
def test_upload_endpoint_uses_resolve_cdg_txt_defaults(self):
|
|
1684
|
-
"""Verify the upload endpoint uses
|
|
1684
|
+
"""Verify the upload endpoint uses resolve_cdg_txt_defaults for theme-based defaults."""
|
|
1685
|
+
import inspect
|
|
1685
1686
|
from backend.api.routes import file_upload as file_upload_module
|
|
1686
1687
|
|
|
1687
|
-
|
|
1688
|
-
source_code = f.read()
|
|
1688
|
+
source_code = inspect.getsource(file_upload_module)
|
|
1689
1689
|
|
|
1690
|
-
|
|
1690
|
+
# Check for the centralized resolve_cdg_txt_defaults function (imported from job_defaults_service)
|
|
1691
|
+
has_resolve_call = 'resolve_cdg_txt_defaults(' in source_code
|
|
1691
1692
|
|
|
1692
1693
|
assert has_resolve_call, (
|
|
1693
|
-
"file_upload.py does not call
|
|
1694
|
-
"
|
|
1694
|
+
"file_upload.py does not call resolve_cdg_txt_defaults(). "
|
|
1695
|
+
"Theme-driven CDG/TXT defaults (controlled by DEFAULT_ENABLE_CDG/DEFAULT_ENABLE_TXT "
|
|
1696
|
+
"settings) must be applied via the centralized job_defaults_service."
|
|
1695
1697
|
)
|
|
1696
1698
|
|
|
1697
1699
|
def test_upload_endpoint_has_optional_cdg_txt_params(self):
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Tests for GCE Encoding Worker (gce_encoding/main.py).
|
|
3
|
+
|
|
4
|
+
These tests verify the file-finding logic in the GCE encoding worker,
|
|
5
|
+
particularly ensuring that instrumental selection is respected.
|
|
6
|
+
|
|
7
|
+
NOTE: This test file is self-contained and doesn't import the full app
|
|
8
|
+
to avoid dependency issues in CI/CD environments.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import pytest
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
# Copy the find_file function here to test it in isolation
|
|
16
|
+
# This avoids importing the full gce_encoding module which has GCS dependencies
|
|
17
|
+
def find_file(work_dir: Path, *patterns):
|
|
18
|
+
'''Find a file matching any of the given glob patterns.'''
|
|
19
|
+
for pattern in patterns:
|
|
20
|
+
matches = list(work_dir.glob(f"**/{pattern}"))
|
|
21
|
+
if matches:
|
|
22
|
+
return matches[0]
|
|
23
|
+
return None
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class TestFindFile:
|
|
27
|
+
"""Test the find_file function used to locate input files."""
|
|
28
|
+
|
|
29
|
+
def test_find_file_single_match(self, tmp_path):
|
|
30
|
+
"""Test finding a file with a single match."""
|
|
31
|
+
# Create test file
|
|
32
|
+
test_file = tmp_path / "test_instrumental.flac"
|
|
33
|
+
test_file.touch()
|
|
34
|
+
|
|
35
|
+
result = find_file(tmp_path, "*instrumental*.flac")
|
|
36
|
+
assert result is not None
|
|
37
|
+
assert result.name == "test_instrumental.flac"
|
|
38
|
+
|
|
39
|
+
def test_find_file_no_match(self, tmp_path):
|
|
40
|
+
"""Test finding a file with no matches returns None."""
|
|
41
|
+
result = find_file(tmp_path, "*nonexistent*.flac")
|
|
42
|
+
assert result is None
|
|
43
|
+
|
|
44
|
+
def test_find_file_priority_order(self, tmp_path):
|
|
45
|
+
"""Test that find_file returns first matching pattern."""
|
|
46
|
+
# Create multiple files that match different patterns
|
|
47
|
+
first = tmp_path / "first_pattern.flac"
|
|
48
|
+
second = tmp_path / "second_pattern.flac"
|
|
49
|
+
first.touch()
|
|
50
|
+
second.touch()
|
|
51
|
+
|
|
52
|
+
# First pattern should win
|
|
53
|
+
result = find_file(tmp_path, "*first*.flac", "*second*.flac")
|
|
54
|
+
assert result.name == "first_pattern.flac"
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class TestInstrumentalSelection:
|
|
58
|
+
"""Test that instrumental selection is properly respected."""
|
|
59
|
+
|
|
60
|
+
def test_clean_instrumental_prioritized_when_selected(self, tmp_path):
|
|
61
|
+
"""Test that clean instrumental is found when 'clean' is selected.
|
|
62
|
+
|
|
63
|
+
When both instrumentals exist and 'clean' is selected,
|
|
64
|
+
the clean version should be found.
|
|
65
|
+
"""
|
|
66
|
+
# Create both instrumental files (as they exist in GCS)
|
|
67
|
+
clean = tmp_path / "Artist - Title (Instrumental Clean).flac"
|
|
68
|
+
backing = tmp_path / "Artist - Title (Instrumental Backing).flac"
|
|
69
|
+
clean.touch()
|
|
70
|
+
backing.touch()
|
|
71
|
+
|
|
72
|
+
# Simulate finding instrumental with 'clean' selection
|
|
73
|
+
# (mimics the logic in run_encoding when instrumental_selection == 'clean')
|
|
74
|
+
result = find_file(
|
|
75
|
+
tmp_path,
|
|
76
|
+
"*instrumental_clean*.flac", "*Instrumental Clean*.flac",
|
|
77
|
+
"*instrumental*.flac", "*Instrumental*.flac",
|
|
78
|
+
"*instrumental*.wav"
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
assert result is not None
|
|
82
|
+
assert "Clean" in result.name
|
|
83
|
+
|
|
84
|
+
def test_backing_instrumental_prioritized_when_selected(self, tmp_path):
|
|
85
|
+
"""Test that backing instrumental is found when 'with_backing' is selected.
|
|
86
|
+
|
|
87
|
+
When both instrumentals exist and 'with_backing' is selected,
|
|
88
|
+
the backing version should be found.
|
|
89
|
+
|
|
90
|
+
This test would have caught the bug where the GCE worker always
|
|
91
|
+
used the clean instrumental regardless of user selection.
|
|
92
|
+
"""
|
|
93
|
+
# Create both instrumental files (as they exist in GCS)
|
|
94
|
+
clean = tmp_path / "Artist - Title (Instrumental Clean).flac"
|
|
95
|
+
backing = tmp_path / "Artist - Title (Instrumental Backing).flac"
|
|
96
|
+
clean.touch()
|
|
97
|
+
backing.touch()
|
|
98
|
+
|
|
99
|
+
# Simulate finding instrumental with 'with_backing' selection
|
|
100
|
+
# (mimics the logic in run_encoding when instrumental_selection == 'with_backing')
|
|
101
|
+
result = find_file(
|
|
102
|
+
tmp_path,
|
|
103
|
+
"*instrumental_with_backing*.flac", "*Instrumental Backing*.flac",
|
|
104
|
+
"*with_backing*.flac", "*Backing*.flac",
|
|
105
|
+
"*instrumental*.flac", "*Instrumental*.flac",
|
|
106
|
+
"*instrumental*.wav"
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
assert result is not None
|
|
110
|
+
assert "Backing" in result.name
|
|
111
|
+
|
|
112
|
+
def test_backing_instrumental_with_stem_naming(self, tmp_path):
|
|
113
|
+
"""Test finding backing instrumental with GCS stem naming convention.
|
|
114
|
+
|
|
115
|
+
In GCS, stems are named like 'stems/instrumental_with_backing.flac'
|
|
116
|
+
"""
|
|
117
|
+
stems_dir = tmp_path / "stems"
|
|
118
|
+
stems_dir.mkdir()
|
|
119
|
+
|
|
120
|
+
clean = stems_dir / "instrumental_clean.flac"
|
|
121
|
+
backing = stems_dir / "instrumental_with_backing.flac"
|
|
122
|
+
clean.touch()
|
|
123
|
+
backing.touch()
|
|
124
|
+
|
|
125
|
+
# Simulate finding instrumental with 'with_backing' selection
|
|
126
|
+
result = find_file(
|
|
127
|
+
tmp_path,
|
|
128
|
+
"*instrumental_with_backing*.flac", "*Instrumental Backing*.flac",
|
|
129
|
+
"*with_backing*.flac", "*Backing*.flac",
|
|
130
|
+
"*instrumental*.flac", "*Instrumental*.flac",
|
|
131
|
+
"*instrumental*.wav"
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
assert result is not None
|
|
135
|
+
assert "with_backing" in result.name
|
|
136
|
+
|
|
137
|
+
def test_fallback_to_generic_when_specific_not_found(self, tmp_path):
|
|
138
|
+
"""Test fallback to generic pattern when specific not found."""
|
|
139
|
+
# Only create a generically named instrumental
|
|
140
|
+
generic = tmp_path / "instrumental.flac"
|
|
141
|
+
generic.touch()
|
|
142
|
+
|
|
143
|
+
# Both clean and backing selections should fall back
|
|
144
|
+
for selection in ["clean", "with_backing"]:
|
|
145
|
+
if selection == "with_backing":
|
|
146
|
+
patterns = (
|
|
147
|
+
"*instrumental_with_backing*.flac", "*Instrumental Backing*.flac",
|
|
148
|
+
"*with_backing*.flac", "*Backing*.flac",
|
|
149
|
+
"*instrumental*.flac", "*Instrumental*.flac",
|
|
150
|
+
"*instrumental*.wav"
|
|
151
|
+
)
|
|
152
|
+
else:
|
|
153
|
+
patterns = (
|
|
154
|
+
"*instrumental_clean*.flac", "*Instrumental Clean*.flac",
|
|
155
|
+
"*instrumental*.flac", "*Instrumental*.flac",
|
|
156
|
+
"*instrumental*.wav"
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
result = find_file(tmp_path, *patterns)
|
|
160
|
+
assert result is not None
|
|
161
|
+
assert result.name == "instrumental.flac"
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
class TestInstrumentalSelectionRegression:
|
|
165
|
+
"""Regression tests for instrumental selection bug.
|
|
166
|
+
|
|
167
|
+
Bug: User selected 'with_backing' instrumental in the UI but the final
|
|
168
|
+
video used the clean instrumental instead.
|
|
169
|
+
|
|
170
|
+
Root cause: The GCE worker's find_file() always prioritized
|
|
171
|
+
'*instrumental_clean*' patterns before generic patterns, ignoring
|
|
172
|
+
the user's selection.
|
|
173
|
+
"""
|
|
174
|
+
|
|
175
|
+
def test_bug_scenario_both_instrumentals_present(self, tmp_path):
|
|
176
|
+
"""Recreate the exact bug scenario where both instrumentals exist.
|
|
177
|
+
|
|
178
|
+
This is the key regression test. When both instrumentals are downloaded
|
|
179
|
+
from GCS (which happens with the GCE worker), the correct one must
|
|
180
|
+
be selected based on the user's choice.
|
|
181
|
+
"""
|
|
182
|
+
# Setup: Both instrumentals in stems/ (as downloaded from GCS)
|
|
183
|
+
stems = tmp_path / "stems"
|
|
184
|
+
stems.mkdir()
|
|
185
|
+
|
|
186
|
+
(stems / "instrumental_clean.flac").touch()
|
|
187
|
+
(stems / "instrumental_with_backing.flac").touch()
|
|
188
|
+
|
|
189
|
+
# Also have properly named versions in the root (from _setup_working_directory)
|
|
190
|
+
(tmp_path / "Artist - Song (Instrumental Clean).flac").touch()
|
|
191
|
+
(tmp_path / "Artist - Song (Instrumental Backing).flac").touch()
|
|
192
|
+
|
|
193
|
+
# When user selected 'with_backing', the backing version MUST be found
|
|
194
|
+
backing_patterns = (
|
|
195
|
+
"*instrumental_with_backing*.flac", "*Instrumental Backing*.flac",
|
|
196
|
+
"*with_backing*.flac", "*Backing*.flac",
|
|
197
|
+
"*instrumental*.flac", "*Instrumental*.flac",
|
|
198
|
+
"*instrumental*.wav"
|
|
199
|
+
)
|
|
200
|
+
result = find_file(tmp_path, *backing_patterns)
|
|
201
|
+
|
|
202
|
+
assert result is not None
|
|
203
|
+
# Must find a backing-related file, not the clean one
|
|
204
|
+
name = result.name.lower()
|
|
205
|
+
assert "backing" in name or "with_backing" in name, \
|
|
206
|
+
f"Expected backing instrumental but found: {result.name}"
|
|
207
|
+
|
|
208
|
+
def test_clean_selection_still_works(self, tmp_path):
|
|
209
|
+
"""Verify clean selection still works correctly after the fix."""
|
|
210
|
+
stems = tmp_path / "stems"
|
|
211
|
+
stems.mkdir()
|
|
212
|
+
|
|
213
|
+
(stems / "instrumental_clean.flac").touch()
|
|
214
|
+
(stems / "instrumental_with_backing.flac").touch()
|
|
215
|
+
|
|
216
|
+
(tmp_path / "Artist - Song (Instrumental Clean).flac").touch()
|
|
217
|
+
(tmp_path / "Artist - Song (Instrumental Backing).flac").touch()
|
|
218
|
+
|
|
219
|
+
# When user selected 'clean', the clean version must be found
|
|
220
|
+
clean_patterns = (
|
|
221
|
+
"*instrumental_clean*.flac", "*Instrumental Clean*.flac",
|
|
222
|
+
"*instrumental*.flac", "*Instrumental*.flac",
|
|
223
|
+
"*instrumental*.wav"
|
|
224
|
+
)
|
|
225
|
+
result = find_file(tmp_path, *clean_patterns)
|
|
226
|
+
|
|
227
|
+
assert result is not None
|
|
228
|
+
name = result.name.lower()
|
|
229
|
+
assert "clean" in name, f"Expected clean instrumental but found: {result.name}"
|
|
@@ -11,6 +11,7 @@ from fastapi import FastAPI
|
|
|
11
11
|
from backend.api.routes.admin import router
|
|
12
12
|
from backend.api.dependencies import require_admin
|
|
13
13
|
from backend.services.user_service import get_user_service
|
|
14
|
+
from backend.services.auth_service import AuthResult, UserType
|
|
14
15
|
from backend.models.user import User, Session
|
|
15
16
|
|
|
16
17
|
|
|
@@ -20,15 +21,29 @@ app.include_router(router, prefix="/api")
|
|
|
20
21
|
|
|
21
22
|
|
|
22
23
|
def get_mock_admin():
|
|
23
|
-
"""Override for require_admin dependency - returns admin
|
|
24
|
-
return (
|
|
24
|
+
"""Override for require_admin dependency - returns admin AuthResult."""
|
|
25
|
+
return AuthResult(
|
|
26
|
+
is_valid=True,
|
|
27
|
+
user_type=UserType.ADMIN,
|
|
28
|
+
remaining_uses=-1,
|
|
29
|
+
message="Admin session valid",
|
|
30
|
+
user_email="admin@nomadkaraoke.com",
|
|
31
|
+
is_admin=True,
|
|
32
|
+
)
|
|
25
33
|
|
|
26
34
|
|
|
27
35
|
def get_mock_regular_user():
|
|
28
36
|
"""Override for require_admin dependency - returns regular user (should fail)."""
|
|
29
37
|
# This simulates what happens when a non-admin tries to access
|
|
30
38
|
# In reality, require_admin raises 403, but we test the logic
|
|
31
|
-
return (
|
|
39
|
+
return AuthResult(
|
|
40
|
+
is_valid=True,
|
|
41
|
+
user_type=UserType.LIMITED,
|
|
42
|
+
remaining_uses=5,
|
|
43
|
+
message="User session valid",
|
|
44
|
+
user_email="user@example.com",
|
|
45
|
+
is_admin=False,
|
|
46
|
+
)
|
|
32
47
|
|
|
33
48
|
|
|
34
49
|
@pytest.fixture
|
|
@@ -1503,6 +1503,8 @@ class TestMadeForYouDistributionSettings:
|
|
|
1503
1503
|
settings.default_brand_prefix = "NOMAD"
|
|
1504
1504
|
settings.default_discord_webhook_url = None
|
|
1505
1505
|
settings.default_youtube_description = None
|
|
1506
|
+
settings.default_enable_cdg = True
|
|
1507
|
+
settings.default_enable_txt = True
|
|
1506
1508
|
return settings
|
|
1507
1509
|
|
|
1508
1510
|
@pytest.mark.asyncio
|
|
@@ -1522,7 +1524,7 @@ class TestMadeForYouDistributionSettings:
|
|
|
1522
1524
|
patch('backend.services.audio_search_service.get_audio_search_service') as mock_get_audio, \
|
|
1523
1525
|
patch('backend.services.storage_service.StorageService'), \
|
|
1524
1526
|
patch('backend.api.routes.users.get_theme_service', return_value=mock_theme_service), \
|
|
1525
|
-
patch('backend.
|
|
1527
|
+
patch('backend.services.job_defaults_service.get_settings', return_value=mock_settings):
|
|
1526
1528
|
|
|
1527
1529
|
from backend.services.audio_search_service import NoResultsError
|
|
1528
1530
|
mock_audio_service = MagicMock()
|
|
@@ -1558,7 +1560,7 @@ class TestMadeForYouDistributionSettings:
|
|
|
1558
1560
|
patch('backend.services.audio_search_service.get_audio_search_service') as mock_get_audio, \
|
|
1559
1561
|
patch('backend.services.storage_service.StorageService'), \
|
|
1560
1562
|
patch('backend.api.routes.users.get_theme_service', return_value=mock_theme_service), \
|
|
1561
|
-
patch('backend.
|
|
1563
|
+
patch('backend.services.job_defaults_service.get_settings', return_value=mock_settings):
|
|
1562
1564
|
|
|
1563
1565
|
from backend.services.audio_search_service import NoResultsError
|
|
1564
1566
|
mock_audio_service = MagicMock()
|
|
@@ -1594,7 +1596,7 @@ class TestMadeForYouDistributionSettings:
|
|
|
1594
1596
|
patch('backend.services.audio_search_service.get_audio_search_service') as mock_get_audio, \
|
|
1595
1597
|
patch('backend.services.storage_service.StorageService'), \
|
|
1596
1598
|
patch('backend.api.routes.users.get_theme_service', return_value=mock_theme_service), \
|
|
1597
|
-
patch('backend.
|
|
1599
|
+
patch('backend.services.job_defaults_service.get_settings', return_value=mock_settings):
|
|
1598
1600
|
|
|
1599
1601
|
from backend.services.audio_search_service import NoResultsError
|
|
1600
1602
|
mock_audio_service = MagicMock()
|
|
@@ -1628,7 +1630,7 @@ class TestMadeForYouDistributionSettings:
|
|
|
1628
1630
|
patch('backend.services.audio_search_service.get_audio_search_service') as mock_get_audio, \
|
|
1629
1631
|
patch('backend.services.storage_service.StorageService'), \
|
|
1630
1632
|
patch('backend.api.routes.users.get_theme_service', return_value=mock_theme_service), \
|
|
1631
|
-
patch('backend.
|
|
1633
|
+
patch('backend.services.job_defaults_service.get_settings', return_value=mock_settings):
|
|
1632
1634
|
|
|
1633
1635
|
from backend.services.audio_search_service import NoResultsError
|
|
1634
1636
|
mock_audio_service = MagicMock()
|