|
| 1 | +from datetime import datetime |
| 2 | +from typing import Optional, Any |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from pyctuator.auth import Auth, BasicAuth |
| 7 | +from pyctuator.impl.spring_boot_admin_registration import BootAdminRegistrationHandler |
| 8 | +from tests.conftest import RegistrationTrackerFixture |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.usefixtures("boot_admin_server") |
| 12 | +def test_registration_no_auth(registration_tracker: RegistrationTrackerFixture) -> None: |
| 13 | + registration_handler = get_registration_handler("http://localhost:8001/register", None) |
| 14 | + |
| 15 | + try: |
| 16 | + registration_handler.start() |
| 17 | + assert registration_tracker.count == 1 |
| 18 | + |
| 19 | + finally: |
| 20 | + registration_handler.stop() |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.usefixtures("boot_admin_server") |
| 24 | +def test_registration_basic_auth_no_creds(registration_tracker: RegistrationTrackerFixture, caplog: Any) -> None: |
| 25 | + registration_handler = get_registration_handler("http://localhost:8001/register-with-basic-auth", None) |
| 26 | + |
| 27 | + try: |
| 28 | + registration_handler.start() |
| 29 | + assert registration_tracker.count == 0 |
| 30 | + |
| 31 | + error_message = "Failed registering with boot-admin, got %s - %s" |
| 32 | + assert error_message in [record.msg for record in caplog.records] |
| 33 | + |
| 34 | + error_args = (401, b'{"detail":"Not authenticated"}') |
| 35 | + assert error_args in [record.args for record in caplog.records if record.msg == error_message] |
| 36 | + |
| 37 | + finally: |
| 38 | + registration_handler.stop() |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.usefixtures("boot_admin_server") |
| 42 | +def test_registration_basic_auth_bad_creds(registration_tracker: RegistrationTrackerFixture, caplog: Any) -> None: |
| 43 | + registration_handler = get_registration_handler( |
| 44 | + "http://localhost:8001/register-with-basic-auth", |
| 45 | + BasicAuth("kuki", "puki") |
| 46 | + ) |
| 47 | + |
| 48 | + try: |
| 49 | + registration_handler.start() |
| 50 | + assert registration_tracker.count == 0 |
| 51 | + |
| 52 | + error_message = "Failed registering with boot-admin, got %s - %s" |
| 53 | + assert error_message in [record.msg for record in caplog.records] |
| 54 | + |
| 55 | + error_args = (401, b'{"detail":"Moo haha"}') |
| 56 | + assert error_args in [record.args for record in caplog.records if record.msg == error_message] |
| 57 | + |
| 58 | + finally: |
| 59 | + registration_handler.stop() |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.usefixtures("boot_admin_server") |
| 63 | +def test_registration_basic_auth(registration_tracker: RegistrationTrackerFixture) -> None: |
| 64 | + registration_handler = get_registration_handler( |
| 65 | + "http://localhost:8001/register-with-basic-auth", |
| 66 | + BasicAuth("moo", "haha") |
| 67 | + ) |
| 68 | + |
| 69 | + try: |
| 70 | + registration_handler.start() |
| 71 | + assert registration_tracker.count == 1 |
| 72 | + |
| 73 | + finally: |
| 74 | + registration_handler.stop() |
| 75 | + |
| 76 | + |
| 77 | +def get_registration_handler(registration_url: str, registration_auth: Optional[Auth]) -> BootAdminRegistrationHandler: |
| 78 | + return BootAdminRegistrationHandler( |
| 79 | + registration_url=registration_url, |
| 80 | + registration_auth=registration_auth, |
| 81 | + application_name="noauth", |
| 82 | + pyctuator_base_url="http://whatever/pyctuator", |
| 83 | + start_time=datetime.now(), |
| 84 | + service_url="http://whatever/service", |
| 85 | + registration_interval_sec=100 |
| 86 | + ) |
0 commit comments