|
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.utils import build_broker_url, build_db_uri, build_redis_url |
32 | 35 |
|
33 | 36 |
|
34 | 37 | class ConfigEP(EntryPoint):
|
@@ -231,3 +234,123 @@ class Config(object):
|
231 | 234 | assert app.config["ENV"] == "env"
|
232 | 235 | finally:
|
233 | 236 | shutil.rmtree(tmppath)
|
| 237 | + |
| 238 | + |
| 239 | +@pytest.mark.parametrize( |
| 240 | + "env_vars, expected_uri", |
| 241 | + [ |
| 242 | + ( |
| 243 | + { |
| 244 | + "DB_USER": "testuser", |
| 245 | + "DB_PASSWORD": "testpassword", |
| 246 | + "DB_HOST": "testhost", |
| 247 | + "DB_PORT": "5432", |
| 248 | + "DB_NAME": "testdb", |
| 249 | + }, |
| 250 | + "postgresql+psycopg2://testuser:testpassword@testhost:5432/testdb", |
| 251 | + ), |
| 252 | + ( |
| 253 | + {"SQLALCHEMY_DATABASE_URI": "sqlite:///test.db"}, |
| 254 | + "sqlite:///test.db", |
| 255 | + ), |
| 256 | + ( |
| 257 | + {}, |
| 258 | + "postgresql+psycopg2://invenio-app-rdm:invenio-app-rdm@localhost/invenio-app-rdm", |
| 259 | + ), |
| 260 | + ], |
| 261 | +) |
| 262 | +def test_build_db_uri(monkeypatch, env_vars, expected_uri): |
| 263 | + """Test building database URI.""" |
| 264 | + for key in [ |
| 265 | + "DB_USER", |
| 266 | + "DB_PASSWORD", |
| 267 | + "DB_HOST", |
| 268 | + "DB_PORT", |
| 269 | + "DB_NAME", |
| 270 | + "SQLALCHEMY_DATABASE_URI", |
| 271 | + ]: |
| 272 | + monkeypatch.delenv(key, raising=False) |
| 273 | + for key, value in env_vars.items(): |
| 274 | + monkeypatch.setenv(key, value) |
| 275 | + |
| 276 | + assert build_db_uri() == expected_uri |
| 277 | + |
| 278 | + |
| 279 | +@pytest.mark.parametrize( |
| 280 | + "env_vars, expected_url", |
| 281 | + [ |
| 282 | + ( |
| 283 | + { |
| 284 | + "BROKER_USER": "testuser", |
| 285 | + "BROKER_PASSWORD": "testpassword", |
| 286 | + "BROKER_HOST": "testhost", |
| 287 | + "BROKER_PORT": "5672", |
| 288 | + }, |
| 289 | + "amqp://testuser:testpassword@testhost:5672/", |
| 290 | + ), |
| 291 | + ( |
| 292 | + {"BROKER_URL": "amqp://guest:guest@localhost:5672/"}, |
| 293 | + "amqp://guest:guest@localhost:5672/", |
| 294 | + ), |
| 295 | + ( |
| 296 | + {}, |
| 297 | + "amqp://guest:guest@localhost:5672/", |
| 298 | + ), |
| 299 | + ], |
| 300 | +) |
| 301 | +def test_build_broker_url(monkeypatch, env_vars, expected_url): |
| 302 | + """Test building broker URL.""" |
| 303 | + for key in [ |
| 304 | + "BROKER_USER", |
| 305 | + "BROKER_PASSWORD", |
| 306 | + "BROKER_HOST", |
| 307 | + "BROKER_PORT", |
| 308 | + "BROKER_URL", |
| 309 | + ]: |
| 310 | + monkeypatch.delenv(key, raising=False) |
| 311 | + for key, value in env_vars.items(): |
| 312 | + monkeypatch.setenv(key, value) |
| 313 | + |
| 314 | + assert build_broker_url() == expected_url |
| 315 | + |
| 316 | + |
| 317 | +@pytest.mark.parametrize( |
| 318 | + "env_vars, db, expected_url", |
| 319 | + [ |
| 320 | + ( |
| 321 | + { |
| 322 | + "REDIS_HOST": "testhost", |
| 323 | + "REDIS_PORT": "6379", |
| 324 | + "REDIS_PASSWORD": "testpassword", |
| 325 | + }, |
| 326 | + 2, |
| 327 | + "redis://:testpassword@testhost:6379/2", |
| 328 | + ), |
| 329 | + ( |
| 330 | + { |
| 331 | + "REDIS_HOST": "testhost", |
| 332 | + "REDIS_PORT": "6379", |
| 333 | + }, |
| 334 | + 1, |
| 335 | + "redis://testhost:6379/1", |
| 336 | + ), |
| 337 | + ( |
| 338 | + {"BROKER_URL": "redis://localhost:6379/0"}, |
| 339 | + None, |
| 340 | + "redis://localhost:6379/0", |
| 341 | + ), |
| 342 | + ( |
| 343 | + {}, |
| 344 | + 4, |
| 345 | + "redis://localhost:6379/4", |
| 346 | + ), |
| 347 | + ], |
| 348 | +) |
| 349 | +def test_build_redis_url(monkeypatch, env_vars, db, expected_url): |
| 350 | + """Test building Redis URL.""" |
| 351 | + for key in ["REDIS_HOST", "REDIS_PORT", "REDIS_PASSWORD", "BROKER_URL"]: |
| 352 | + monkeypatch.delenv(key, raising=False) |
| 353 | + for key, value in env_vars.items(): |
| 354 | + monkeypatch.setenv(key, value) |
| 355 | + |
| 356 | + assert build_redis_url(db=db) == expected_url |
0 commit comments