Spaces:
Sleeping

File size: 15,498 Bytes
2ce5ba7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
894a316
 
2ce5ba7
 
 
 
 
 
 
 
 
 
894a316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ce5ba7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
"""Test suite for .env.example — validación de variables, ejemplos y rutas paso a paso.

Este test verifica que .env.example:
1. Contiene todas las variables de entorno necesarias documentadas.
2. No expone secretos reales (solo placeholders/dummies seguros).
3. Incluye instrucciones paso a paso y ejemplos de formato para cada plataforma
   (Supabase, Copernicus, Groq, Hugging Face).
"""

from __future__ import annotations

import re
from pathlib import Path

import pytest

# Ruta relativa al repositorio (el test se ejecuta desde la raíz)
ENV_EXAMPLE_PATH = Path(".env.example")

# ── Expected variables and their expected placeholder format ───────────────────

ENV_EXPECTED: dict[str, str] = {
    # General
    "APP_ENV": "development | production",
    "LOG_LEVEL": "info | debug | warning | error",
    # UI / Backend
    "SHINY_HOST": "127.0.0.1 | localhost",
    "SHINY_PORT": "8001 | 8000",
    "API_HOST": "127.0.0.1 | localhost",
    "API_PORT": "8000",
    "API_BASE_URL": "http://127.0.0.1:8000 | https://<tu-app>.hf.space",
    "ALLOWED_ORIGINS": "http://127.0.0.1:8001,http://localhost:8001,https://<tu-app>.shinyapps.io",
    "CONFIDENCE_THRESHOLD": "0.0.0-1.0 (float)",
    "MAX_UPLOAD_MB": "50",
    # Hugging Face Spaces
    "HF_SPACE_ID": "<usuario>/<space>, p. ej. alexprieto/agrovision",
    "HF_TOKEN": "hf_xxxxxxxx (token write)",
    # Deployment alternatives
    "RENDER_API_KEY": "",
    "RENDER_SERVICE_ID": "",
    "SHINYAPPS_ACCOUNT": "",
    "SHINYAPPS_TOKEN": "",
    "SHINYAPPS_SECRET": "",
    # Model counting
    "COUNTING_ENABLED": "true | false",
    "MODEL_BACKEND": "mock | onnx",
    "MODEL_PATH": "/app/models/agrovision-plantcount-v2.0.0.onnx",
    "MODEL_VERSION": "2.0.0",
    "MODEL_ARCHITECTURE": "yolo26n | rfdetr_nano",
    "HF_MODEL_REPO": "<org>/agrovision-plantcount",
    # Telemetry
    "EVENTS_PERSIST": "true | false",
    "RATE_LIMIT_PER_MIN": "120 | 0",
    # Supabase
    "SUPABASE_URL": "https://<proyecto>.supabase.co",
    "SUPABASE_ANON_KEY": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9. ... (jwt placeholder)",
    "SUPABASE_SERVICE_ROLE_KEY": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9. ... (jwt placeholder)",
    "SUPABASE_STORAGE_BUCKET": "drone-images",
    "DATABASE_URL": "postgresql+asyncpg://postgres.<proyecto>:<pwd>@<host>:5432/postgres",
    # Groq
    "DEV_GROQ_API_KEY": "gsk_... (clave Groq)",
    # Copernicus
    "DEV_COPERNICUS_CLIENT_ID": "ID OAuth en dataspace.copernicus.eu",
    "DEV_COPERNICUS_CLIENT_SECRET": "Secret OAuth en dataspace.copernicus.eu",
}


# ── Helper: extract inline examples from .env.example comments ─────────────────

def _extract_examples(content: str) -> dict[str, str]:
    """Parse comments in .env.example that follow the pattern:
       # VARIABLE=value            # <descripción>

    Also handles plain VAR=value lines without comments.

    Returns a dict mapping variable name -> example/description text.
    """
    examples: dict[str, str] = {}
    for line in content.splitlines():
        # Match lines like: VAR=value         # description
        m = re.match(r"^([A-Z_]+)=(.+?)\s+#\s+(.+)$", line)
        if m:
            var_name = m.group(1)
            example_val = m.group(2).strip()
            description = m.group(3).strip()
            examples[var_name] = f"{example_val} — {description}"
            continue
        # Also match plain VAR=value lines (no comment)
        m2 = re.match(r"^([A-Z_]+)=(.+?)$", line)
        if m2:
            var_name = m2.group(1)
            example_val = m2.group(2).strip()
            # Only add if not already added by the commented version
            if var_name not in examples:
                examples[var_name] = example_val
    return examples


# ── Helper: detect real-looking secrets ───────────────────────────────────────

_SECRETS_PATTERNS = [
    r"(?i) eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9",  # JWT placeholder (acceptable as dummy)
    r"^[a-zA-Z0-9]{8,}$",  # generic long alphanumeric (could be real, flag it)
    r"gsk_",  # Groq key prefix
    r"sk-",  # OpenAI-style key prefix
]


def _has_real_secret(value: str) -> bool:
    """Return True if the value looks like a real secret (not a dummy placeholder)."""
    # Strip common dummy markers
    v = value.strip()
    # Acceptable dummy patterns: starts with "placeholder_", ends with "...", is empty
    if v.startswith("placeholder_") or v.endswith("..."):
        return False
    if not v or v in {"", "<todo>", "<pending>"}:
        return False
    # Check against known secret patterns
    for pat in _SECRETS_PATTERNS:
        if re.search(pat, v):
            # But allow if it's clearly a placeholder (contains "placeholder" or "...")
            if "placeholder" in v.lower() or v.endswith("..."):
                return False
            return True
    # If it's a long alphanumeric without a known prefix, flag it
    if re.match(r"^[a-zA-Z0-9]{20,}$", v):
        return True
    return False


# ── Tests ─────────────────────────────────────────────────────────────────────

class TestEnvExampleHasAllVariables:
    """Verifica que .env.example contenga todas las variables esperadas."""

    @pytest.fixture(autouse=True)
    def _require_env_example(self):
        assert ENV_EXAMPLE_PATH.exists(), f"Falta .env.example en {ENV_EXAMPLE_PATH}"
        with open(ENV_EXAMPLE_PATH, encoding="utf-8") as f:
            self.content = f.read()
        with open(ENV_EXAMPLE_PATH, encoding="utf-8") as f:
            self.examples = _extract_examples(self.content)

    def test_all_expected_variables_present(self):
        """Cada variable definida en ENV_EXPECTED debe aparecer en .env.example."""
        for var in ENV_EXPECTED:
            assert var in self.content, f"Variable faltante en .env.example: {var}"

    def test_no_real_secrets_exposed(self):
        """Ninguna variable debe contener secretos reales (solo placeholders/dummies)."""
        for line in self.content.splitlines():
            m = re.match(r"^([A-Z_]+)=(.+?)\s*$", line)
            if m:
                var_name = m.group(1)
                val = m.group(2).strip()
                # Skip comment-only lines and empty values
                if not val or val.startswith("#"):
                    continue
                # Check if the value looks like a real secret
                if _has_real_secret(val):
                    pytest.fail(
                        f"Posible secreto real detectado en .env.example para '{var_name}': {val}. "
                        "Usa solo placeholders/dummies seguros."
                    )


class TestEnvExampleExamplesAndSteps:
    """Verifica que .env.example tenga ejemplos de formato y rutas paso a paso."""

    @pytest.fixture(autouse=True)
    def _require_env_example(self):
        assert ENV_EXAMPLE_PATH.exists()
        with open(ENV_EXAMPLE_PATH, encoding="utf-8") as f:
            self.content = f.read()

    def test_supabase_connection_string_steps_present(self):
        """DOC: .env.example debe documentar cómo obtener DATABASE_URL en Supabase
        via Connect -> Connection String -> pooler -> postgresql+asyncpg://."""
        content_lower = self.content.lower()
        supabase_steps = [
            "connect",
            "connection string",
            "session pooler",
            "transaction pooler",
            "postgresql+asyncpg",
            "supabase",
        ]
        found = all(step in content_lower for step in supabase_steps)
        assert found, (
            "Faltan pasos documentados de Supabase (Connect -> Connection String -> "
            "pooler -> postgresql+asyncpg://) en .env.example. "
            "Añade los comentarios de instrucción paso a paso."
        )

    def test_supabase_step_zero_project_creation_present(self):
        """DOC: .env.example debe documentar el Paso 0 para crear un proyecto desde cero."""
        content_lower = self.content.lower()
        assert "new project" in content_lower, "Falta instrucción 'New project' en .env.example"
        assert "database password" in content_lower, (
            "Falta mención a guardar 'Database Password' en .env.example"
        )

    def test_supabase_url_steps_present(self):
        """DOC: .env.example debe explicar cómo llegar a SUPABASE_URL (Settings -> Data API)."""
        content_lower = self.content.lower()
        url_steps = ["project settings", "data api", "project url", "copy"]
        for step in url_steps:
            assert step in content_lower, f"Paso faltante para SUPABASE_URL: '{step}'"

    def test_supabase_anon_key_steps_present(self):
        """DOC: .env.example debe explicar cómo llegar a SUPABASE_ANON_KEY (anon public)."""
        content_lower = self.content.lower()
        anon_steps = ["project settings", "api keys", "anon", "public", "copy"]
        for step in anon_steps:
            assert step in content_lower, f"Paso faltante para SUPABASE_ANON_KEY: '{step}'"

    def test_supabase_service_role_key_steps_present(self):
        """DOC: .env.example debe explicar cómo llegar a SUPABASE_SERVICE_ROLE_KEY (Reveal)."""
        content_lower = self.content.lower()
        sr_steps = ["service_role", "secret", "reveal", "copy"]
        for step in sr_steps:
            assert step in content_lower, f"Paso faltante para SUPABASE_SERVICE_ROLE_KEY: '{step}'"

    def test_supabase_storage_bucket_steps_present(self):
        """DOC: .env.example debe documentar la creación del bucket drone-images en Storage."""
        content_lower = self.content.lower()
        storage_steps = ["storage", "new bucket", "drone-images"]
        for step in storage_steps:
            assert step in content_lower, f"Paso faltante para SUPABASE_STORAGE_BUCKET: '{step}'"

    def test_supabase_url_anon_key_service_role_present(self):
        """DOC: SUPABASE_URL, SUPABASE_ANON_KEY, SUPABASE_SERVICE_ROLE_KEY deben aparecer."""
        for var in ["SUPABASE_URL", "SUPABASE_ANON_KEY", "SUPABASE_SERVICE_ROLE_KEY"]:
            assert var in self.content, f"Variable faltante: {var}"

    def test_copernicus_steps_present(self):
        """DOC: .env.example debe documentar cómo obtener credenciales de Copernicus CDSE."""
        content_lower = self.content.lower()
        copernicus_steps = [
            "copernicus",
            "dataspace",
            "client id",
            "client secret",
        ]
        found = all(step in content_lower for step in copernicus_steps)
        assert found, (
            "Faltan pasos documentados de Copernicus CDSE en .env.example. "
            "Añade los comentarios de instrucción paso a paso."
        )

    def test_groq_api_key_steps_present(self):
        """DOC: .env.example debe documentar cómo obtener la API key de Groq."""
        content_lower = self.content.lower()
        groq_steps = ["groq", "console.groq.com/keys", "create key"]
        found = all(step in content_lower for step in groq_steps)
        assert found, (
            "Faltan pasos documentados de Groq en .env.example. "
            "Añade los comentarios de instrucción paso a paso."
        )

    def test_hf_space_id_token_steps_present(self):
        """DOC: .env.example debe documentar HF_SPACE_ID y HF_TOKEN."""
        content_lower = self.content.lower()
        hf_steps = ["huggingface", "hf_space_id", "hf_token", "settings/tokens"]
        found = all(step in content_lower for step in hf_steps)
        assert found, (
            "Faltan pasos documentados de Hugging Face en .env.example. "
            "Añade los comentarios de instrucción paso a paso."
        )


class TestEnvExampleFormatExamples:
    """Verifica que cada variable tenga un ejemplo de formato realista (dummy seguro)."""

    @pytest.fixture(autouse=True)
    def _require_env_example(self):
        assert ENV_EXAMPLE_PATH.exists()
        with open(ENV_EXAMPLE_PATH, encoding="utf-8") as f:
            self.content = f.read()
        self.examples = _extract_examples(self.content)

    # Supabase examples
    def test_supabase_url_example_safe(self):
        """SUPABASE_URL debe tener un ejemplo con formato realista pero seguro."""
        ex = self.examples.get("SUPABASE_URL", "")
        assert "supabase.co" in ex.lower() or "https://" in ex, (
            f"SUPABASE_URL example missing realistic format: {ex}"
        )
        assert not _has_real_secret(ex), f"SUPABASE_URL example parece secreto real: {ex}"

    def test_supabase_anon_key_example_safe(self):
        """SUPABASE_ANON_KEY debe tener un ejemplo JWT placeholder (no secreto real)."""
        ex = self.examples.get("SUPABASE_ANON_KEY", "")
        assert "eyJhbGci" in ex or "placeholder" in ex.lower(), (
            f"SUPABASE_ANON_KEY example missing JWT format: {ex}"
        )
        assert not _has_real_secret(ex), f"SUPABASE_ANON_KEY example parece secreto real: {ex}"

    def test_database_url_example_safe(self):
        """DATABASE_URL debe tener un ejemplo con prefijo postgresql+asyncpg:// y host seguro."""
        ex = self.examples.get("DATABASE_URL", "")
        assert "postgresql+asyncpg://" in ex, (
            f"DATABASE_URL example missing postgresql+asyncpg:// prefix: {ex}"
        )
        assert not _has_real_secret(ex), f"DATABASE_URL example parece secreto real: {ex}"

    def test_groq_key_example_safe(self):
        """DEV_GROQ_API_KEY debe tener ejemplo gsk_... (formato Groq)."""
        ex = self.examples.get("DEV_GROQ_API_KEY", "")
        assert "gsk_" in ex.lower() or "placeholder" in ex.lower(), (
            f"DEV_GROQ_API_KEY example missing gsk_ format: {ex}"
        )
        assert not _has_real_secret(ex), f"DEV_GROQ_API_KEY example parece secreto real: {ex}"

    def test_copernicus_client_id_example_safe(self):
        """DEV_COPERNICUS_CLIENT_ID debe tener ejemplo de ID OAuth placeholder."""
        ex = self.examples.get("DEV_COPERNICUS_CLIENT_ID", "")
        assert "placeholder" in ex.lower() or "id" in ex.lower()
        assert "id" in ex.lower() or "placeholder" in ex.lower()
        assert not _has_real_secret(ex)

    def test_copernicus_client_secret_example_safe(self):
        """DEV_COPERNICUS_CLIENT_SECRET debe tener ejemplo de secreto placeholder."""
        ex = self.examples.get("DEV_COPERNICUS_CLIENT_SECRET", "")
        assert "placeholder" in ex.lower() or "secret" in ex.lower()
        assert not _has_real_secret(ex)

    def test_hf_space_id_example_safe(self):
        """HF_SPACE_ID debe tener ejemplo <usuario>/<space>."""
        ex = self.examples.get("HF_SPACE_ID", "")
        assert "/" in ex and (" " in ex or "user" in ex.lower() or "space" in ex.lower()), (
            f"HF_SPACE_ID example missing <usuario>/<space> format: {ex}"
        )
        assert not _has_real_secret(ex), f"HF_SPACE_ID ejemplo parece secreto real: {ex}"

    def test_hf_token_example_safe(self):
        """HF_TOKEN debe tener ejemplo hf_... placeholder."""
        ex = self.examples.get("HF_TOKEN", "")
        assert "hf_" in ex.lower() or "placeholder" in ex.lower(), (
            f"HF_TOKEN example missing hf_ format: {ex}"
        )
        assert not _has_real_secret(ex), f"HF_TOKEN ejemplo parece secreto real: {ex}"