|
2 | 2 | #
|
3 | 3 | # This file is part of Invenio.
|
4 | 4 | # Copyright (C) 2015-2018 CERN.
|
| 5 | +# Copyright (C) 2024 KTH Royal Institute of Technology. |
5 | 6 | #
|
6 | 7 | # Invenio is free software; you can redistribute it and/or modify it
|
7 | 8 | # under the terms of the MIT License; see LICENSE file for more details.
|
|
16 | 17 | import warnings
|
17 | 18 | from os.path import join
|
18 | 19 |
|
| 20 | +import pytest |
19 | 21 | from flask import Flask
|
20 | 22 | from mock import patch
|
21 | 23 | from pkg_resources import EntryPoint
|
|
29 | 31 | create_config_loader,
|
30 | 32 | )
|
31 | 33 | from invenio_config.default import ALLOWED_HTML_ATTRS, ALLOWED_HTML_TAGS
|
| 34 | +from invenio_config.env import build_broker_url, build_db_uri, build_redis_url |
32 | 35 |
|
33 | 36 |
|
34 | 37 | class ConfigEP(EntryPoint):
|
@@ -231,3 +234,189 @@ class Config(object):
|
231 | 234 | assert app.config["ENV"] == "env"
|
232 | 235 | finally:
|
233 | 236 | shutil.rmtree(tmppath)
|
| 237 | + |
| 238 | + |
| 239 | +def set_env_vars(monkeypatch, env_vars): |
| 240 | + """Helper function to set environment variables.""" |
| 241 | + for key in env_vars: |
| 242 | + monkeypatch.delenv(key, raising=False) |
| 243 | + for key, value in env_vars.items(): |
| 244 | + monkeypatch.setenv(key, value) |
| 245 | + |
| 246 | + |
| 247 | +@pytest.mark.parametrize( |
| 248 | + "env_vars, expected_uri", |
| 249 | + [ |
| 250 | + ( |
| 251 | + { |
| 252 | + "INVENIO_DB_USER": "testuser", |
| 253 | + "INVENIO_DB_PASSWORD": "testpassword", |
| 254 | + "INVENIO_DB_HOST": "testhost", |
| 255 | + "INVENIO_DB_PORT": "5432", |
| 256 | + "INVENIO_DB_NAME": "testdb", |
| 257 | + "INVENIO_DB_PROTOCOL": "postgresql+psycopg2", |
| 258 | + }, |
| 259 | + "postgresql+psycopg2://testuser:testpassword@testhost:5432/testdb", |
| 260 | + ), |
| 261 | + ( |
| 262 | + { |
| 263 | + "INVENIO_SQLALCHEMY_DATABASE_URI": "postgresql+psycopg2://testuser:testpassword@testhost:5432/testdb" |
| 264 | + }, |
| 265 | + "postgresql+psycopg2://testuser:testpassword@testhost:5432/testdb", |
| 266 | + ), |
| 267 | + ( |
| 268 | + { |
| 269 | + "SQLALCHEMY_DATABASE_URI": "postgresql+psycopg2://testuser:testpassword@testhost:5432/testdb" |
| 270 | + }, |
| 271 | + "postgresql+psycopg2://testuser:testpassword@testhost:5432/testdb", |
| 272 | + ), |
| 273 | + ( |
| 274 | + {}, |
| 275 | + "postgresql+psycopg2://invenio-app-rdm:invenio-app-rdm@localhost/invenio-app-rdm", |
| 276 | + ), |
| 277 | + ], |
| 278 | +) |
| 279 | +def test_build_db_uri(monkeypatch, env_vars, expected_uri): |
| 280 | + """Test building database URI.""" |
| 281 | + set_env_vars(monkeypatch, env_vars) |
| 282 | + assert build_db_uri() == expected_uri |
| 283 | + |
| 284 | + |
| 285 | +@pytest.mark.parametrize( |
| 286 | + "env_vars, expected_url", |
| 287 | + [ |
| 288 | + ( |
| 289 | + { |
| 290 | + "INVENIO_AMQP_BROKER_USER": "testuser", |
| 291 | + "INVENIO_AMQP_BROKER_PASSWORD": "testpassword", |
| 292 | + "INVENIO_AMQP_BROKER_HOST": "testhost", |
| 293 | + "INVENIO_AMQP_BROKER_PORT": "5672", |
| 294 | + "INVENIO_AMQP_BROKER_PROTOCOL": "amqp", |
| 295 | + "INVENIO_AMQP_BROKER_VHOST": "/testvhost", |
| 296 | + }, |
| 297 | + "amqp://testuser:testpassword@testhost:5672/testvhost", |
| 298 | + ), |
| 299 | + ( |
| 300 | + { |
| 301 | + "INVENIO_AMQP_BROKER_USER": "testuser", |
| 302 | + "INVENIO_AMQP_BROKER_PASSWORD": "testpassword", |
| 303 | + "INVENIO_AMQP_BROKER_HOST": "testhost", |
| 304 | + "INVENIO_AMQP_BROKER_PORT": "5672", |
| 305 | + "INVENIO_AMQP_BROKER_PROTOCOL": "amqp", |
| 306 | + "INVENIO_AMQP_BROKER_VHOST": "testvhost", |
| 307 | + }, |
| 308 | + "amqp://testuser:testpassword@testhost:5672/testvhost", |
| 309 | + ), |
| 310 | + ( |
| 311 | + { |
| 312 | + "INVENIO_AMQP_BROKER_USER": "testuser", |
| 313 | + "INVENIO_AMQP_BROKER_PASSWORD": "testpassword", |
| 314 | + "INVENIO_AMQP_BROKER_HOST": "testhost", |
| 315 | + "INVENIO_AMQP_BROKER_PORT": "5672", |
| 316 | + "INVENIO_AMQP_BROKER_PROTOCOL": "amqp", |
| 317 | + "INVENIO_AMQP_BROKER_VHOST": "", |
| 318 | + }, |
| 319 | + "amqp://testuser:testpassword@testhost:5672/", |
| 320 | + ), |
| 321 | + ( |
| 322 | + { |
| 323 | + "INVENIO_AMQP_BROKER_USER": "testuser", |
| 324 | + "INVENIO_AMQP_BROKER_PASSWORD": "testpassword", |
| 325 | + "INVENIO_AMQP_BROKER_HOST": "testhost", |
| 326 | + "INVENIO_AMQP_BROKER_PORT": "5672", |
| 327 | + "INVENIO_AMQP_BROKER_PROTOCOL": "amqp", |
| 328 | + }, |
| 329 | + "amqp://testuser:testpassword@testhost:5672/", |
| 330 | + ), |
| 331 | + ], |
| 332 | +) |
| 333 | +def test_build_broker_url_with_vhost(monkeypatch, env_vars, expected_url): |
| 334 | + """Test building broker URL with vhost.""" |
| 335 | + set_env_vars(monkeypatch, env_vars) |
| 336 | + assert build_broker_url() == expected_url |
| 337 | + |
| 338 | + |
| 339 | +@pytest.mark.parametrize( |
| 340 | + "env_vars, expected_url", |
| 341 | + [ |
| 342 | + ( |
| 343 | + { |
| 344 | + "INVENIO_AMQP_BROKER_USER": "testuser", |
| 345 | + "INVENIO_AMQP_BROKER_PASSWORD": "testpassword", |
| 346 | + "INVENIO_AMQP_BROKER_HOST": "testhost", |
| 347 | + "INVENIO_AMQP_BROKER_PORT": "5672", |
| 348 | + "INVENIO_AMQP_BROKER_PROTOCOL": "amqp", |
| 349 | + "INVENIO_AMQP_BROKER_VHOST": "testvhost", |
| 350 | + }, |
| 351 | + "amqp://testuser:testpassword@testhost:5672/testvhost", |
| 352 | + ), |
| 353 | + ( |
| 354 | + { |
| 355 | + "INVENIO_AMQP_BROKER_USER": "testuser", |
| 356 | + "INVENIO_AMQP_BROKER_PASSWORD": "testpassword", |
| 357 | + "INVENIO_AMQP_BROKER_HOST": "testhost", |
| 358 | + "INVENIO_AMQP_BROKER_PORT": "5672", |
| 359 | + "INVENIO_AMQP_BROKER_PROTOCOL": "amqp", |
| 360 | + "INVENIO_AMQP_BROKER_VHOST": "", |
| 361 | + }, |
| 362 | + "amqp://testuser:testpassword@testhost:5672/", |
| 363 | + ), |
| 364 | + ( |
| 365 | + {"INVENIO_BROKER_URL": "amqp://guest:guest@localhost:5672/"}, |
| 366 | + "amqp://guest:guest@localhost:5672/", |
| 367 | + ), |
| 368 | + ( |
| 369 | + {}, |
| 370 | + "amqp://guest:guest@localhost:5672/", |
| 371 | + ), |
| 372 | + ], |
| 373 | +) |
| 374 | +def test_build_broker_url_with_vhost(monkeypatch, env_vars, expected_url): |
| 375 | + """Test building broker URL with vhost.""" |
| 376 | + set_env_vars(monkeypatch, env_vars) |
| 377 | + assert build_broker_url() == expected_url |
| 378 | + |
| 379 | + |
| 380 | +@pytest.mark.parametrize( |
| 381 | + "env_vars, db, expected_url", |
| 382 | + [ |
| 383 | + ( |
| 384 | + { |
| 385 | + "INVENIO_KV_CACHE_HOST": "testhost", |
| 386 | + "INVENIO_KV_CACHE_PORT": "6379", |
| 387 | + "INVENIO_KV_CACHE_PASSWORD": "testpassword", |
| 388 | + "INVENIO_KV_CACHE_PROTOCOL": "redis", |
| 389 | + }, |
| 390 | + 2, |
| 391 | + "redis://:testpassword@testhost:6379/2", |
| 392 | + ), |
| 393 | + ( |
| 394 | + { |
| 395 | + "INVENIO_KV_CACHE_HOST": "testhost", |
| 396 | + "INVENIO_KV_CACHE_PORT": "6379", |
| 397 | + "INVENIO_KV_CACHE_PROTOCOL": "redis", |
| 398 | + }, |
| 399 | + 1, |
| 400 | + "redis://testhost:6379/1", |
| 401 | + ), |
| 402 | + ( |
| 403 | + {"BROKER_URL": "redis://localhost:6379/0"}, |
| 404 | + None, |
| 405 | + "redis://localhost:6379/0", |
| 406 | + ), |
| 407 | + ( |
| 408 | + {"INVENIO_KV_CACHE_URL": "redis://localhost:6379/3"}, |
| 409 | + 3, |
| 410 | + "redis://localhost:6379/3", |
| 411 | + ), |
| 412 | + ( |
| 413 | + {}, |
| 414 | + 4, |
| 415 | + "redis://localhost:6379/4", |
| 416 | + ), |
| 417 | + ], |
| 418 | +) |
| 419 | +def test_build_redis_url(monkeypatch, env_vars, db, expected_url): |
| 420 | + """Test building Redis URL.""" |
| 421 | + set_env_vars(monkeypatch, env_vars) |
| 422 | + assert build_redis_url(db=db) == expected_url |
0 commit comments