Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added url validation for starting with http, https #20

Merged
merged 13 commits into from
Feb 22, 2022
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Changelog
=========

Version 1.9
===========

* Validate that the schema of IQM server URL is http or https. `#20 <https://github.com/iqm-finland/iqm-client/pull/20>`_

WingCode marked this conversation as resolved.
Show resolved Hide resolved
Version 1.8
===========

Expand Down
7 changes: 7 additions & 0 deletions src/iqm_client/iqm_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@
SECONDS_BETWEEN_CALLS = 1


class ClientConfigurationError(RuntimeError):
"""Wrong configuration provided.
"""


class CircuitExecutionError(RuntimeError):
"""Something went wrong on the server.
"""
Expand Down Expand Up @@ -245,6 +250,8 @@ def __init__(
username: Optional[str] = None,
api_key: Optional[str] = None
):
if not url.startswith(('http:', 'https:')):
raise ClientConfigurationError(f'The URL schema has to be http or https. Incorrect schema in URL: {url}')
self._base_url = url
self._settings = settings
self._credentials = _get_credentials(username, api_key)
Expand Down
11 changes: 9 additions & 2 deletions tests/test_iqm_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
from mockito import mock, when
from requests import HTTPError

from iqm_client.iqm_client import (Circuit, IQMClient, RunStatus,
SingleQubitMapping)
from iqm_client.iqm_client import (Circuit, ClientConfigurationError,
IQMClient, RunStatus, SingleQubitMapping)
from tests.conftest import existing_run, missing_run


Expand Down Expand Up @@ -134,3 +134,10 @@ def test_user_warning_is_emitted_when_warnings_in_response(base_url, settings_di
.thenReturn(mock({'status_code': 200, 'text': json.dumps({'status': 'ready', 'warnings': [msg]})})):
with pytest.warns(UserWarning, match=msg):
client.get_run(existing_run)


def test_base_url_is_invalid(settings_dict):
invalid_base_url = 'https//example.com'
with pytest.raises(ClientConfigurationError) as exc:
IQMClient(invalid_base_url, settings_dict)
assert f'The URL schema has to be http or https. Incorrect schema in URL: {invalid_base_url}' == str(exc.value)