-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7335fb9
commit 5c0d25a
Showing
7 changed files
with
158 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import requests | ||
|
||
from ethpm.backends.base import ( | ||
BaseURIBackend, | ||
) | ||
from ethpm.constants import ( | ||
GITHUB_PREFIX, | ||
) | ||
from ethpm.utils.uri import ( | ||
is_valid_github_uri, | ||
validate_github_uri_contents, | ||
) | ||
|
||
|
||
class GithubOverHTTPSBackend(BaseURIBackend): | ||
""" | ||
Base class for all URIs pointing to a content-addressed Github URI. | ||
""" | ||
def can_handle_uri(self, uri: str) -> bool: | ||
return is_valid_github_uri(uri) | ||
|
||
def fetch_uri_contents(self, uri: str) -> bytes: | ||
http_uri, validation_hash = uri.split('#') | ||
response = requests.get(http_uri) | ||
response.raise_for_status() | ||
validate_github_uri_contents(response.content, validation_hash) | ||
return response.content | ||
|
||
@property | ||
def base_uri(self) -> str: | ||
return GITHUB_PREFIX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import pytest | ||
import requests_mock | ||
|
||
from ethpm.backends.http import ( | ||
GithubOverHTTPSBackend, | ||
) | ||
from ethpm.exceptions import ( | ||
ValidationError, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'uri', | ||
( | ||
'http://raw.githubusercontent.com/ethpm/ethpm-spec/481739f6138907db88602558711e9d3c1301c269/examples/owned/contracts/Owned.sol#0xbfdea1fa5f33c30fee8443c5ffa1020027f8813e0007bb6f82aaa2843a7fdd60', # noqa: E501 | ||
'http://raw.githubusercontent.com/ethpm/ethpm-spec/481739f6138907db88602558711e9d3c1301c269/examples/owned/contracts/Owned.sol#bfdea1fa5f33c30fee8443c5ffa1020027f8813e0007bb6f82aaa2843a7fdd60', # noqa: E501 | ||
'https://raw.githubusercontent.com/ethpm/ethpm-spec/481739f6138907db88602558711e9d3c1301c269/examples/owned/contracts/Owned.sol#bfdea1fa5f33c30fee8443c5ffa1020027f8813e0007bb6f82aaa2843a7fdd60', # noqa: E501 | ||
'https://raw.githubusercontent.com/ethpm/ethpm-spec/481739f6138907db88602558711e9d3c1301c269/examples/owned/contracts/Owned.sol#0xbfdea1fa5f33c30fee8443c5ffa1020027f8813e0007bb6f82aaa2843a7fdd60', # noqa: E501 | ||
) | ||
) | ||
def test_github_over_https_backend(uri, owned_contract): | ||
base_uri = 'https://raw.githubusercontent.com/' | ||
backend = GithubOverHTTPSBackend() | ||
assert backend.base_uri == base_uri | ||
assert backend.can_handle_uri(uri) is True | ||
with requests_mock.Mocker() as m: | ||
m.get(requests_mock.ANY, text=owned_contract) | ||
response = backend.fetch_uri_contents(uri) | ||
assert response.startswith(b'pragma') | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'uri', | ||
( | ||
'http://raw.githubusercontent.com/ethpm/ethpm-spec/481739f6138907db88602558711e9d3c1301c269/examples/owned/contracts/Owned.sol#bfdea1fa5f33c30fee8443c5ffa1020027f8813e0007bb6f82aaa2843a7fdd61', # noqa: E501 | ||
'https://raw.githubusercontent.com/ethpm/ethpm-spec/481739f6138907db88602558711e9d3c1301c269/examples/owned/contracts/Owned.sol#0xbfdea1fa5f33c30fee8443c5ffa1020027f8813e0007bb6f82aaa2843a7fdd61', # noqa: E501 | ||
) | ||
) | ||
def test_fetch_uri_contents_raises_exception_with_invalid_content_hash(uri, owned_contract): | ||
backend = GithubOverHTTPSBackend() | ||
with pytest.raises(ValidationError): | ||
with requests_mock.Mocker() as m: | ||
m.get(requests_mock.ANY, text=owned_contract) | ||
backend.fetch_uri_contents(uri) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters