Example #1
0
def test_geturl():
    """Verify the short-cut to the URL."""
    uri = (builder.URIBuilder().add_scheme("https").add_credentials(
        "sigmavirus24", "not-my-re@l-password").add_host(
            "github.com").add_path("sigmavirus24/rfc3986").geturl())
    expected = ("https://*****:*****@github.com/"
                "sigmavirus24/rfc3986")
    assert expected == uri
Example #2
0
def test_finalize():
    """Verify the whole thing."""
    uri = (builder.URIBuilder().add_scheme("https").add_credentials(
        "sigmavirus24",
        "not-my-re@l-password").add_host("github.com").add_path(
            "sigmavirus24/rfc3986").finalize().unsplit())
    expected = ("https://*****:*****@github.com/"
                "sigmavirus24/rfc3986")
    assert expected == uri
Example #3
0
def test_from_uri_string():
    uribuilder = builder.URIBuilder().from_uri("https://bar.foo:4321/boom")
    assert uribuilder.scheme == "https"
    assert uribuilder.userinfo is None
    assert uribuilder.host == "bar.foo"
    assert uribuilder.port == "4321"
    assert uribuilder.path == "/boom"
    assert uribuilder.query is None
    assert uribuilder.fragment is None
Example #4
0
def test_finalize():
    """Verify the whole thing."""
    uri = builder.URIBuilder().add_scheme('https').add_credentials(
        'sigmavirus24',
        'not-my-re@l-password').add_host('github.com').add_path(
            'sigmavirus24/rfc3986').finalize().unsplit()
    expected = ('https://*****:*****@github.com/'
                'sigmavirus24/rfc3986')
    assert expected == uri
Example #5
0
def test_builder_default():
    """Verify the default values."""
    uribuilder = builder.URIBuilder()
    assert uribuilder.scheme is None
    assert uribuilder.userinfo is None
    assert uribuilder.host is None
    assert uribuilder.port is None
    assert uribuilder.path is None
    assert uribuilder.query is None
    assert uribuilder.fragment is None
Example #6
0
def test_from_uri_reference():
    uri = uri_reference("http://foo.bar:1234/baz")
    uribuilder = builder.URIBuilder().from_uri(uri)
    assert uribuilder.scheme == "http"
    assert uribuilder.userinfo is None
    assert uribuilder.host == "foo.bar"
    assert uribuilder.port == "1234"
    assert uribuilder.path == "/baz"
    assert uribuilder.query is None
    assert uribuilder.fragment is None
def to_url(record):
    if 'disabled' in record:
        if not isinstance(record['disabled'], (bool)):
            raise ValueError('"disabled" field must be boolean type')

        if record['disabled']:
            return

    if not validators.domain(record['domain_name']):
        raise ValueError('domain_name is in invalid format')

    uri_builder = builder.URIBuilder().add_host(record['domain_name'])

    if 'username' in record:
        if not record['username'].isalnum():
            raise ValueError('username must be alphanumeric string')
        if 'password' in record:
            if not record['password'].isalnum():
                raise ValueError('password must be alphanumeric string')
            uri_builder = uri_builder.add_credentials(record['username'],
                                                      record['password'])
        else:
            uri_builder = uri_builder.add_credentials(record['username'])

    if 'scheme' in record:
        if record['scheme'] not in ['http', 'https']:
            raise ValueError('scheme is invalid, only http and https allowed')
        uri_builder = uri_builder.add_scheme(record['scheme'])

    if 'port' in record:
        uri_builder = uri_builder.add_port(record['port'])

    if 'path' in record:
        uri_builder = uri_builder.add_path(record['path'])
        if not validators.url(uri_builder.finalize().unsplit()):
            raise ValueError('path is invalid format')

    if 'query' in record:
        uri_builder = uri_builder.add_query_from(record['query'])
        if not validators.url(uri_builder.finalize().unsplit()):
            raise ValueError('query is invalid format')

    if 'fragment' in record:
        uri_builder = uri_builder.add_fragment(record['fragment'])
        if not validators.url(uri_builder.finalize().unsplit()):
            raise ValueError('fragment is invalid format')

    uri = uri_builder.finalize().unsplit()

    return uri
Example #8
0
date = utils.formatdate(nowtimestamp)
host = "api.fiu.edu"
uri = "/v1/mac/"
username = "******"
method = "GET"
password = "******"
mac = "78:24:af:3a:27:ea"

rfc3986_uri = ""

# Array ( [0] => Fri, 01 Feb 2019 02:32:34 +0000 [1] => GET [2] => api.fiu.edu [3] => /v1/mac/78:24:af:3a:27:ea [4] => method=GET&uri=%2Fv1%2Fmac%2F78%3A24%3Aaf%3A3a%3A27%3Aea )
# 591fb57dcc7508ff516cbbe5f41f77392392e8ce

# Implementation of RFC 3986
rfc3986_uri = builder.URIBuilder().add_query_from([('method', 'GET'),
                                                   ('uri', uri + mac)
                                                   ]).finalize().unsplit()
print(rfc3986_uri[1:])  # FIXME remove (?) from string

# implode
arr = date + "\n" + method + "\n" + host + "\n" + uri + "\n" + rfc3986_uri[1:]
print(arr)

# Create the signed message from api key and string to sign
p = hmac.new(password.encode('utf-8'), arr.encode('utf-8'),
             hashlib.sha1).hexdigest()
print(p)

## HTTPS Request
'''
	url : https://api.fiu.edu/v1/mac/78:24:af:3a:27:ea
Example #9
0
def test_add_credentials_requires_username():
    """Verify one needs a username to add credentials."""
    with pytest.raises(ValueError):
        builder.URIBuilder().add_credentials(None, None)
Example #10
0
def test_add_credentials(username, password, userinfo):
    """Verify we normalize usernames and passwords."""
    uribuilder = builder.URIBuilder().add_credentials(username, password)
    assert uribuilder.userinfo == userinfo
Example #11
0
def test_add_scheme(scheme):
    """Verify schemes are normalized when added."""
    uribuilder = builder.URIBuilder().add_scheme(scheme)
    assert uribuilder.scheme == "https"
Example #12
0
def test_repr():
    """Verify our repr looks like our class."""
    uribuilder = builder.URIBuilder()
    assert repr(uribuilder).startswith("URIBuilder(scheme=None")
Example #13
0
def test_add_host(hostname, expected_hostname):
    """Verify we normalize hostnames in add_host."""
    uribuilder = builder.URIBuilder().add_host(hostname)
    assert uribuilder.host == expected_hostname
Example #14
0
def test_add_invalid_port(port):
    """Verify we raise a ValueError for invalid ports."""
    with pytest.raises(ValueError):
        builder.URIBuilder().add_port(port)
Example #15
0
def test_add_port(port, expected):
    """Verify we normalize our port."""
    uribuilder = builder.URIBuilder().add_port(port)
    assert uribuilder.port == expected
Example #16
0
def test_add_host(hostname):
    """Verify we normalize hostnames in add_host."""
    uribuilder = builder.URIBuilder().add_host(hostname)
    assert uribuilder.host == 'google.com'
Example #17
0
def test_extend_path(uri, extend_with, expected_path):
    """Verify the behaviour of extend_path."""
    uribuilder = (builder.URIBuilder().from_uri(
        uri_reference(uri)).extend_path(extend_with))
    assert uribuilder.path == expected_path
Example #18
0
def test_add_fragment():
    """Verify our handling of fragments."""
    uribuilder = builder.URIBuilder().add_fragment("section-2.5.1")
    assert uribuilder.fragment == "section-2.5.1"
Example #19
0
def test_add_query():
    """Verify we do not modify the provided query string."""
    uribuilder = builder.URIBuilder().add_query("username=@foo")
    assert uribuilder.query == "username=@foo"
Example #20
0
def test_add_query_from(query_items, expected):
    """Verify the behaviour of add_query_from."""
    uribuilder = builder.URIBuilder().add_query_from(query_items)
    assert uribuilder.query == expected
Example #21
0
def test_add_path(path):
    """Verify we normalize our path value."""
    uribuilder = builder.URIBuilder().add_path(path)
    assert uribuilder.path == "/sigmavirus24/rfc3986"
Example #22
0
def select_consensus_checkpoint_provider(network, log):
    # Prompt the user for consensus checkpoint provider (weak subjectivity checkpoint)

    infura_bn_domain = INFURA_BEACON_NODE_DOMAINS[network]

    initial_state_url = None

    while initial_state_url is None:

        result = button_dialog(title='Adding consensus checkpoint provider',
                               text=(HTML(f'''
Having a consensus checkpoint provider is highly recommended for your
beacon node. It makes it possible to get a fully synced beacon in just a
few minutes compared to having to wait hours or days.

An easy way to get a provider is to create a free account on Infura. Your
Infura account can also be used later on in the wizard to provide an eth1
fallback node.

https://infura.io/

If you have access to a custom beacon node, you can enter your own URL to
that beacon node with the custom option. That beacon node should be on the
<b>{network.capitalize()}</b> Ethereum network.

Do you want add a consensus checkpoint provider?
''')),
                               buttons=[('Infura', 1), ('Custom', 2),
                                        ('Skip', 3), ('Quit', False)]).run()

        if not result:
            return result

        if result == 3:
            return ''
        elif result == 1:
            valid_url = False
            infura_credentials = None
            input_canceled = False

            while not valid_url:
                not_valid_msg = ''
                initial_state_url = None
                if infura_credentials is not None:
                    not_valid_msg = ('''

<style bg="red" fg="black">Your last input were <b>not valid credentials</b>. Please make sure to enter
valid credentials from Infura.</style>''')

                infura_credentials = input_dialog(
                    title='Consensus checkpoint provider using Infura',
                    text=(HTML(f'''
Please enter your Infura ETH 2 project's credentials from:

https://infura.io/

Once you are logged into your Infura account, click on <b>DASHBOARD</b> button in
the top right corner. Click on the <b>ETH 2</b> button in the left column menu.
Click on <b>CREATE NEW PROJECT</b> button. Give it a name. You should see your
project settings, a <b>PROJECT ID</b> and a <b>PROJECT SECRET</b> in the <b>KEYS</b> section.
Enter those values separated by a colon in this format:

&lt;PROJECT ID&gt;:&lt;PROJECT SECRET&gt;

* Press the tab key to switch between the controls below{not_valid_msg}
'''))).run()

                if not infura_credentials:
                    input_canceled = True
                    break

                if ':' not in infura_credentials:
                    continue

                credentials = infura_credentials.split(':')
                username = credentials[0]
                password = credentials[1]

                initial_state_url = urlbuilder.URIBuilder().add_scheme(
                    'https').add_host(infura_bn_domain).add_credentials(
                        username, password).finalize().unsplit()

                valid_url = beacon_node_url_validator(network,
                                                      initial_state_url, log)

            if input_canceled:
                # User clicked the cancel button
                continue

        elif result == 2:
            valid_url = False
            entered_url = None
            input_canceled = False

            while not valid_url:
                not_valid_msg = ''
                initial_state_url = None
                if entered_url is not None:
                    not_valid_msg = ('''

<style bg="red" fg="black">Your last URL was <b>not valid beacon node</b>. Please make sure to enter a URL for
a valid beacon node.</style>''')

                entered_url = input_dialog(
                    title='Consensus checkpoint provider using custom URL',
                    text=(HTML(f'''
Please enter your beacon node URL:

It usually starts with 'https://' and it should point to the root of a
running beacon node on the <b>{network.capitalize()}</b> Ethereum network that supports the
Ethereum Beacon Node API. It should implement these endpoints:

- {BN_DEPOSIT_CONTRACT_URL}
- {BN_FINALIZED_STATE_URL}

* Press the tab key to switch between the controls below{not_valid_msg}
'''))).run()

                if not entered_url:
                    input_canceled = True
                    break

                initial_state_url = entered_url

                valid_url = beacon_node_url_validator(network,
                                                      initial_state_url, log)

            if input_canceled:
                # User clicked the cancel button
                continue

    return initial_state_url
Example #23
0
def test_extend_query_with(uri, extend_with, expected_query):
    """Verify the behaviour of extend_query_with."""
    uribuilder = (builder.URIBuilder().from_uri(
        uri_reference(uri)).extend_query_with(extend_with))
    assert parse_qs(uribuilder.query) == expected_query