Skip to content

Commit

Permalink
Add BASE_URL env var (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsklikas committed Jun 7, 2023
1 parent 3e15811 commit 1c7d6c8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def _login_ui_layer(self) -> Layer:
"HYDRA_ADMIN_URL": self._get_hydra_endpoint_info(),
"KRATOS_PUBLIC_URL": self._get_kratos_endpoint_info(),
"PORT": APPLICATION_PORT,
"BASE_URL": self._domain_url,
},
}
},
Expand Down
11 changes: 6 additions & 5 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
# See LICENSE file for licensing details.

"""Unit test configuration."""
from unittest.mock import MagicMock

import ops.testing
import pytest
from pytest_mock import MockerFixture

from charm import IdentityPlatformLoginUiOperatorCharm

ops.testing.SIMULATE_CAN_CONNECT = True


@pytest.fixture()
def harness(mocked_kubernetes_service_patcher):
def harness(mocked_kubernetes_service_patcher: MagicMock) -> ops.testing.Harness:
"""Initialize harness with Charm."""
harness = ops.testing.Harness(IdentityPlatformLoginUiOperatorCharm)
harness.set_model_name("testing")
Expand All @@ -20,7 +21,7 @@ def harness(mocked_kubernetes_service_patcher):


@pytest.fixture()
def mocked_kubernetes_service_patcher(mocker):
def mocked_kubernetes_service_patcher(mocker: MockerFixture) -> MagicMock:
mocked_service_patcher = mocker.patch("charm.KubernetesServicePatch")
mocked_service_patcher.return_value = lambda x, y: None
yield mocked_service_patcher
return mocked_service_patcher
63 changes: 52 additions & 11 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@

"""Test functions for unit testing Identity Platform Login UI Operator."""
import json
from typing import Tuple

from ops.model import ActiveStatus, WaitingStatus
from ops.testing import Harness
from pytest_mock import MockerFixture

CONTAINER_NAME = "login-ui"
TEST_PORT = "8080"


def setup_ingress_relation(harness) -> int:
def setup_ingress_relation(harness: Harness) -> Tuple[int, str]:
"""Set up ingress relation."""
harness.set_leader(True)
relation_id = harness.add_relation("ingress", "traefik")
Expand All @@ -26,7 +29,7 @@ def setup_ingress_relation(harness) -> int:
return relation_id, url


def setup_kratos_relation(harness) -> int:
def setup_kratos_relation(harness: Harness) -> int:
relation_id = harness.add_relation("kratos-endpoint-info", "kratos")
harness.add_relation_unit(relation_id, "kratos/0")
harness.update_relation_data(
Expand All @@ -40,7 +43,7 @@ def setup_kratos_relation(harness) -> int:
return relation_id


def setup_hydra_relation(harness) -> int:
def setup_hydra_relation(harness: Harness) -> int:
relation_id = harness.add_relation("endpoint-info", "hydra")
harness.add_relation_unit(relation_id, "hydra/0")
harness.update_relation_data(
Expand All @@ -54,7 +57,7 @@ def setup_hydra_relation(harness) -> int:
return relation_id


def test_not_leader(harness) -> None:
def test_not_leader(harness: Harness) -> None:
"""Test with unit not being leader."""
harness.set_leader(False)

Expand All @@ -68,7 +71,7 @@ def test_not_leader(harness) -> None:
) in harness._get_backend_calls()


def test_install_can_connect(harness) -> None:
def test_install_can_connect(harness: Harness) -> None:
"""Test installation with connection."""
harness.set_leader(True)
harness.set_can_connect(CONTAINER_NAME, True)
Expand All @@ -77,7 +80,7 @@ def test_install_can_connect(harness) -> None:
assert harness.charm.unit.status == ActiveStatus()


def test_install_can_not_connect(harness) -> None:
def test_install_can_not_connect(harness: Harness) -> None:
"""Test installation with connection."""
harness.set_leader(True)
harness.set_can_connect(CONTAINER_NAME, False)
Expand All @@ -86,7 +89,7 @@ def test_install_can_not_connect(harness) -> None:
assert harness.charm.unit.status == WaitingStatus("Waiting to connect to Login_UI container")


def test_layer_updated_without_any_endpoint_info(harness) -> None:
def test_layer_updated_without_any_endpoint_info(harness: Harness) -> None:
"""Test Pebble Layer after updates."""
harness.set_leader(True)
harness.set_can_connect(CONTAINER_NAME, True)
Expand All @@ -105,6 +108,7 @@ def test_layer_updated_without_any_endpoint_info(harness) -> None:
"HYDRA_ADMIN_URL": "",
"KRATOS_PUBLIC_URL": "",
"PORT": TEST_PORT,
"BASE_URL": None,
},
}
},
Expand All @@ -113,7 +117,7 @@ def test_layer_updated_without_any_endpoint_info(harness) -> None:
assert harness.charm._login_ui_layer.to_dict() == expected_layer


def test_layer_updated_with_kratos_endpoint_info(harness) -> None:
def test_layer_updated_with_kratos_endpoint_info(harness: Harness) -> None:
"""Test Pebble Layer when relation data is in place."""
harness.set_leader(True)
harness.set_can_connect(CONTAINER_NAME, True)
Expand All @@ -135,6 +139,7 @@ def test_layer_updated_with_kratos_endpoint_info(harness) -> None:
"public_endpoint"
],
"PORT": TEST_PORT,
"BASE_URL": None,
},
}
},
Expand All @@ -143,7 +148,7 @@ def test_layer_updated_with_kratos_endpoint_info(harness) -> None:
assert harness.charm._login_ui_layer.to_dict() == expected_layer


def test_layer_updated_with_hydra_endpoint_info(harness) -> None:
def test_layer_updated_with_hydra_endpoint_info(harness: Harness) -> None:
"""Test Pebble Layer when relation data is in place."""
harness.set_leader(True)
harness.set_can_connect(CONTAINER_NAME, True)
Expand All @@ -165,6 +170,7 @@ def test_layer_updated_with_hydra_endpoint_info(harness) -> None:
],
"KRATOS_PUBLIC_URL": "",
"PORT": TEST_PORT,
"BASE_URL": None,
},
}
},
Expand All @@ -173,7 +179,7 @@ def test_layer_updated_with_hydra_endpoint_info(harness) -> None:
assert harness.charm._login_ui_layer.to_dict() == expected_layer


def test_layer_updated_with_endpoint_info(harness) -> None:
def test_layer_updated_with_endpoint_info(harness: Harness) -> None:
"""Test Pebble Layer when relation data is in place."""
harness.set_leader(True)
harness.set_can_connect(CONTAINER_NAME, True)
Expand All @@ -198,6 +204,7 @@ def test_layer_updated_with_endpoint_info(harness) -> None:
"public_endpoint"
],
"PORT": TEST_PORT,
"BASE_URL": None,
},
}
},
Expand All @@ -206,7 +213,41 @@ def test_layer_updated_with_endpoint_info(harness) -> None:
assert harness.charm._login_ui_layer.to_dict() == expected_layer


def test_ui_endpoint_info(harness, mocker) -> None:
def test_layer_updated_with_ingress_ready(harness: Harness) -> None:
harness.set_leader(True)
harness.set_can_connect(CONTAINER_NAME, True)
harness.charm.on.login_ui_pebble_ready.emit(CONTAINER_NAME)
hydra_relation_id = setup_hydra_relation(harness)
kratos_relation_id = setup_kratos_relation(harness)
_, url = setup_ingress_relation(harness)

expected_layer = {
"summary": "login_ui layer",
"description": "pebble config layer for identity platform login ui",
"services": {
CONTAINER_NAME: {
"override": "replace",
"summary": "identity platform login ui",
"command": "identity_platform_login_ui",
"startup": "enabled",
"environment": {
"HYDRA_ADMIN_URL": harness.get_relation_data(hydra_relation_id, "hydra")[
"admin_endpoint"
],
"KRATOS_PUBLIC_URL": harness.get_relation_data(kratos_relation_id, "kratos")[
"public_endpoint"
],
"PORT": TEST_PORT,
"BASE_URL": url.replace("http", "https").replace(":80", ""),
},
}
},
}

assert harness.charm._login_ui_layer.to_dict() == expected_layer


def test_ui_endpoint_info(harness: Harness, mocker: MockerFixture) -> None:
mocked_service_patcher = mocker.patch(
"charm.LoginUIEndpointsProvider.send_endpoints_relation_data"
)
Expand Down

0 comments on commit 1c7d6c8

Please sign in to comment.