Example #1
0
def test_check_fails(requests_mock_datadir):
    """Checking deposit can fail for some reason"""
    unknown_api_url = "/1/private/test/10/check"
    client = PrivateApiDepositClient(config=CLIENT_TEST_CONFIG)

    with pytest.raises(ValueError, match="Problem when checking deposit"):
        client.check(unknown_api_url)
Example #2
0
def test_metadata_get_ko(requests_mock_datadir):
    """Reading metadata can fail for some reasons"""
    unknown_api_url = "/1/private/unknown/deposit-id/metadata/"
    client = PrivateApiDepositClient(config=CLIENT_TEST_CONFIG)

    with pytest.raises(ValueError, match="Problem when retrieving metadata"):
        client.metadata_get(unknown_api_url)
Example #3
0
def test_check(requests_mock_datadir):
    """When check ok, this should return the deposit's status"""
    api_url = "/1/private/test/1/check"
    client = PrivateApiDepositClient(config=CLIENT_TEST_CONFIG)

    r = client.check(api_url)
    assert r == "something"
Example #4
0
def test_archive_get_ko(tmp_path, datadir, requests_mock_datadir):
    """Reading archive can fail for some reasons"""
    unknown_api_url = "/1/private/unknown/deposit-id/raw/"
    client = PrivateApiDepositClient(config=CLIENT_TEST_CONFIG)

    with pytest.raises(ValueError, match="Problem when retrieving deposit"):
        client.archive_get(unknown_api_url, "some/path")
Example #5
0
def test_client_config(deposit_config_path):
    for client in [
            # config passed as constructor parameter
            PrivateApiDepositClient(config=CLIENT_TEST_CONFIG),
            # config loaded from environment
            PrivateApiDepositClient(),
    ]:
        assert client.base_url == CLIENT_TEST_CONFIG["url"]
        assert client.auth is None
Example #6
0
def test_metadata_get(datadir, requests_mock_datadir):
    """Reading archive should write data in temporary directory"""
    api_url = "/1/private/test/1/metadata"
    client = PrivateApiDepositClient(config=CLIENT_TEST_CONFIG)
    actual_metadata = client.metadata_get(api_url)

    assert isinstance(actual_metadata, str) is False
    expected_content = read_served_path(datadir,
                                        client.base_url,
                                        api_url,
                                        convert_fn=json.loads)
    assert actual_metadata == expected_content
Example #7
0
def test_status_update_with_no_release_id(mocker):
    """Reading metadata can fail for some reasons"""
    mocked_put = mocker.patch.object(Session, "request")

    deposit_client = PrivateApiDepositClient(config=CLIENT_TEST_CONFIG)
    deposit_client.status_update("/update/status/fail",
                                 DEPOSIT_STATUS_LOAD_FAILURE)

    mocked_put.assert_called_once_with(
        "put",
        "https://nowhere.org/update/status/fail",
        json={
            "status": DEPOSIT_STATUS_LOAD_FAILURE,
        },
    )
Example #8
0
def test_archive_get(tmp_path, datadir, requests_mock_datadir):
    """Retrieving archive data through private api should stream data"""
    api_url = "/1/private/test/1/raw/"
    client = PrivateApiDepositClient(CLIENT_TEST_CONFIG)

    expected_content = read_served_path(datadir, client.base_url, api_url)

    archive_path = os.path.join(tmp_path, "test.archive")
    archive_path = client.archive_get(api_url, archive_path)

    assert os.path.exists(archive_path) is True

    with open(archive_path, "rb") as f:
        actual_content = f.read()

    assert actual_content == expected_content
    assert client.base_url == CLIENT_TEST_CONFIG["url"]
    assert client.auth is None
Example #9
0
def test_status_update(mocker):
    """Update status"""
    mocked_put = mocker.patch.object(Session, "request")

    deposit_client = PrivateApiDepositClient(config=CLIENT_TEST_CONFIG)
    deposit_client.status_update(
        "/update/status",
        DEPOSIT_STATUS_LOAD_SUCCESS,
        release_id="some-release-id",
        status_detail="foo bar",
    )

    mocked_put.assert_called_once_with(
        "put",
        "https://nowhere.org/update/status",
        json={
            "status": DEPOSIT_STATUS_LOAD_SUCCESS,
            "status_detail": "foo bar",
            "release_id": "some-release-id",
        },
    )
Example #10
0
def test_archive_get_auth(tmp_path, datadir, requests_mock_datadir):
    """Retrieving archive data through private api should stream data"""
    api_url = "/1/private/test/1/raw/"
    config = CLIENT_TEST_CONFIG.copy()
    config["auth"] = {  # add authentication setup
        "username": "******",
        "password": "******",
    }
    client = PrivateApiDepositClient(config)

    expected_content = read_served_path(datadir, client.base_url, api_url)

    archive_path = os.path.join(tmp_path, "test.archive")
    archive_path = client.archive_get(api_url, archive_path)

    assert os.path.exists(archive_path) is True

    with open(archive_path, "rb") as f:
        actual_content = f.read()

    assert actual_content == expected_content
    assert client.base_url == CLIENT_TEST_CONFIG["url"]
    assert client.auth == ("user", "pass")
Example #11
0
class DepositChecker:
    """Deposit checker implementation.

    Trigger deposit's checks through the private api.

    """
    def __init__(self):
        self.config: Dict[str, Any] = config.load_from_envvar()
        self.client = PrivateApiDepositClient(config=self.config["deposit"])

    def check(self, collection: str, deposit_id: str) -> Dict[str, str]:
        status = None
        deposit_check_url = f"/{collection}/{deposit_id}/check/"
        logger.debug("deposit-check-url: %s", deposit_check_url)
        try:
            r = self.client.check(deposit_check_url)
            logger.debug("Check result: %s", r)
            status = "eventful" if r == "verified" else "failed"
        except Exception:
            logger.exception("Failure during check on '%s'", deposit_check_url)
            sentry_sdk.capture_exception()
            status = "failed"
        logger.debug("Check status: %s", status)
        return {"status": status}
Example #12
0
 def __init__(self):
     self.config: Dict[str, Any] = config.load_from_envvar()
     self.client = PrivateApiDepositClient(config=self.config["deposit"])