From 45aeec443d7aed3bd97a138f2a6f1f324d46b0cc Mon Sep 17 00:00:00 2001 From: "Antoine R. Dumont (@ardumont)" <antoine.romain.dumont@gmail.com> Date: Wed, 17 Apr 2019 11:45:05 +0200 Subject: [PATCH] utils: Enforce necessary setup for deposit and associated client Related T1648 --- swh/deposit/tests/test_utils.py | 20 ++++++++++++++++++++ swh/deposit/utils.py | 14 ++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/swh/deposit/tests/test_utils.py b/swh/deposit/tests/test_utils.py index e3495685..27e06047 100644 --- a/swh/deposit/tests/test_utils.py +++ b/swh/deposit/tests/test_utils.py @@ -4,6 +4,7 @@ # See top-level LICENSE file for more information import unittest +import pytest from unittest.mock import patch @@ -12,7 +13,9 @@ from swh.deposit.models import Deposit, DepositClient def test_origin_url_from(): + """With correctly setup-ed deposit, all is fine + """ for provider_url, external_id in ( ('http://somewhere.org', 'uuid'), ('http://overthejungle.org', 'diuu'), @@ -28,6 +31,23 @@ def test_origin_url_from(): provider_url.rstrip('/'), external_id) +def test_origin_url_from_ko(): + """Badly configured deposit should raise + + """ + for provider_url, external_id in ( + (None, 'uuid'), + ('http://overthejungle.org', None), + ): + deposit = Deposit( + client=DepositClient(provider_url=provider_url), + external_id=None + ) + + with pytest.raises(AssertionError): + utils.origin_url_from(deposit) + + class UtilsTestCase(unittest.TestCase): """Utils library diff --git a/swh/deposit/utils.py b/swh/deposit/utils.py index 4818fc74..beb31ef6 100644 --- a/swh/deposit/utils.py +++ b/swh/deposit/utils.py @@ -11,17 +11,27 @@ from swh.model.identifiers import normalize_timestamp def origin_url_from(deposit): - """Given a deposit instance, return the associated origin url + """Given a deposit instance, return the associated origin url. + + This expects a deposit and the associated client to be correctly + configured. Args: deposit (Deposit): The deposit from which derives the origin url + Raises: + AssertionError if: + - the client's provider_url field is not configured. + - the deposit's external_id field is not configured. + Returns The associated origin url """ - base_url = deposit.client.provider_url external_id = deposit.external_id + assert external_id is not None + base_url = deposit.client.provider_url + assert base_url is not None return '%s/%s' % (base_url.rstrip('/'), external_id) -- GitLab