Skip to content

Commit

Permalink
Relax validation and add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathangreen committed Sep 13, 2023
1 parent c4bda82 commit ff68e56
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
8 changes: 4 additions & 4 deletions core/service/storage/configuration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Optional

import boto3
from pydantic import HttpUrl, parse_obj_as, validator
from pydantic import AnyHttpUrl, parse_obj_as, validator

from core.service.configuration import ServiceConfiguration

Expand All @@ -14,10 +14,10 @@ class StorageConfiguration(ServiceConfiguration):
public_access_bucket: Optional[str] = None
analytics_bucket: Optional[str] = None

endpoint_url: Optional[HttpUrl] = None
endpoint_url: Optional[AnyHttpUrl] = None

url_template: HttpUrl = parse_obj_as(
HttpUrl, "https://{bucket}.s3.{region}.amazonaws.com/{key}"
url_template: AnyHttpUrl = parse_obj_as(
AnyHttpUrl, "https://{bucket}.s3.{region}.amazonaws.com/{key}"
)

@validator("region")
Expand Down
44 changes: 44 additions & 0 deletions tests/core/service/storage/test_configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import pytest

from core.config import CannotLoadConfiguration
from core.service.storage.configuration import StorageConfiguration


def test_region_validation_fail():
with pytest.raises(CannotLoadConfiguration) as exc_info:
StorageConfiguration(region="foo bar baz")

assert "PALACE_STORAGE_REGION: Invalid region: foo bar baz." in str(exc_info.value)


def test_region_validation_success():
configuration = StorageConfiguration(region="us-west-2")
assert configuration.region == "us-west-2"


@pytest.mark.parametrize(
"url",
[
"http://localhost:9000",
"https://real.endpoint.com",
"http://192.168.0.1",
],
)
def test_endpoint_url_validation_success(url: str):
configuration = StorageConfiguration(endpoint_url=url)
assert configuration.endpoint_url == url


@pytest.mark.parametrize(
"url, error",
[
("ftp://localhost:9000", "URL scheme not permitted"),
("foo bar baz", "invalid or missing URL scheme"),
],
)
def test_endpoint_url_validation_fail(url: str, error: str):
with pytest.raises(CannotLoadConfiguration) as exc_info:
StorageConfiguration(endpoint_url=url)

assert "PALACE_STORAGE_ENDPOINT_URL" in str(exc_info.value)
assert error in str(exc_info.value)

0 comments on commit ff68e56

Please sign in to comment.