コード例 #1
0
ファイル: test_token_networks.py プロジェクト: sekmet/raiden
def test_register_token_without_balance(
    api_server_test_instance,
    token_amount,
    raiden_network: List[RaidenService],
    contract_manager,
    retry_timeout,
):
    app0 = raiden_network[0]
    contract_proxy, _ = app0.rpc_client.deploy_single_contract(
        contract_name=CONTRACT_HUMAN_STANDARD_TOKEN,
        contract=contract_manager.get_contract(CONTRACT_HUMAN_STANDARD_TOKEN),
        constructor_parameters=(token_amount, 2, "raiden", "Rd2"),
    )
    new_token_address = Address(to_canonical_address(contract_proxy.address))

    # Wait until Raiden can start using the token contract.
    # Here, the block at which the contract was deployed should be confirmed by Raiden.
    # Therefore, until that block is received.
    wait_for_block(
        raiden=app0,
        block_number=BlockNumber(app0.get_block_number() +
                                 DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS + 1),
        retry_timeout=retry_timeout,
    )

    # Burn all the eth and then make sure we get the appropriate API error
    burn_eth(app0.rpc_client)
    poor_request = grequests.put(
        api_url_for(
            api_server_test_instance,
            "registertokenresource",
            token_address=to_checksum_address(new_token_address),
        ))
    poor_response = poor_request.send().response
    assert_response_with_error(poor_response, HTTPStatus.PAYMENT_REQUIRED)
コード例 #2
0
def test_api_testnet_token_mint(api_server_test_instance: APIServer,
                                token_addresses):
    user_address = factories.make_checksum_address()
    token_address = token_addresses[0]
    url = api_url_for(api_server_test_instance,
                      "tokensmintresource",
                      token_address=token_address)
    request = grequests.post(url, json=dict(to=user_address, value=1))
    response = request.send().response
    assert_response_with_code(response, HTTPStatus.OK)

    # mint method defaults to mintFor
    request = grequests.post(url, json=dict(to=user_address, value=10))
    response = request.send().response
    assert_response_with_code(response, HTTPStatus.OK)

    # invalid due to negative value
    request = grequests.post(url, json=dict(to=user_address, value=-1))
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.BAD_REQUEST)

    # invalid due to invalid address
    request = grequests.post(url, json=dict(to=user_address[:-2], value=10))
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.BAD_REQUEST)

    # trying to mint with no ETH
    burn_eth(api_server_test_instance.rest_api.raiden_api.raiden.rpc_client)
    request = grequests.post(url, json=dict(to=user_address, value=1))
    response = request.send().response
    assert_response_with_code(response, HTTPStatus.PAYMENT_REQUIRED)
コード例 #3
0
def test_api_channel_set_reveal_timeout(
    api_server_test_instance: APIServer,
    raiden_network: List[RaidenService],
    token_addresses,
    settle_timeout,
):
    app0, app1 = raiden_network
    token_address = token_addresses[0]
    partner_address = app1.address

    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json=dict(reveal_timeout=0),
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.CONFLICT)

    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json=dict(reveal_timeout=settle_timeout + 1),
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.CONFLICT)

    reveal_timeout = int(settle_timeout / 2)
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json=dict(reveal_timeout=reveal_timeout),
    )
    response = request.send().response
    assert_response_with_code(response, HTTPStatus.OK)

    token_network_address = views.get_token_network_address_by_token_address(
        views.state_from_raiden(app0), app0.default_registry.address,
        token_address)
    assert token_network_address
    channel_state = views.get_channelstate_by_token_network_and_partner(
        chain_state=views.state_from_raiden(app0),
        token_network_address=token_network_address,
        partner_address=app1.address,
    )
    assert channel_state

    assert channel_state.reveal_timeout == reveal_timeout
コード例 #4
0
def test_payload_with_address_not_eip55(api_server_test_instance: APIServer):
    """ Provided addresses must be EIP55 encoded. """
    invalid_address = "0xf696209d2ca35e6c88e5b99b7cda3abf316bed69"
    channel_data_obj = {
        "partner_address": invalid_address,
        "token_address": "0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8",
        "settle_timeout": "90",
    }
    request = grequests.put(api_url_for(api_server_test_instance,
                                        "channelsresource"),
                            json=channel_data_obj)
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.BAD_REQUEST)
コード例 #5
0
def test_payload_with_address_invalid_length(
        api_server_test_instance: APIServer):
    """ Encoded addresses must have the right length. """
    invalid_address = "0x61c808d82a3ac53231750dadc13c777b59310b"  # one char short
    channel_data_obj = {
        "partner_address": invalid_address,
        "token_address": "0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8",
        "settle_timeout": "10",
    }
    request = grequests.put(api_url_for(api_server_test_instance,
                                        "channelsresource"),
                            json=channel_data_obj)
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.BAD_REQUEST)
コード例 #6
0
def test_payload_with_address_invalid_chars(
        api_server_test_instance: APIServer):
    """ Addresses cannot have invalid characters in it. """
    invalid_address = "0x61c808d82a3ac53231750dadc13c777b59310bdg"  # g at the end is invalid
    channel_data_obj = {
        "partner_address": invalid_address,
        "token_address": "0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8",
        "settle_timeout": "10",
    }
    request = grequests.put(api_url_for(api_server_test_instance,
                                        "channelsresource"),
                            json=channel_data_obj)
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.BAD_REQUEST)
コード例 #7
0
def test_api_channel_open_channel_invalid_input(
        api_server_test_instance: APIServer, token_addresses, reveal_timeout):
    partner_address = "0x61C808D82A3Ac53231750daDc13c777b59310bD9"
    token_address = token_addresses[0]
    settle_timeout = TEST_SETTLE_TIMEOUT_MIN - 1
    channel_data_obj = {
        "partner_address": partner_address,
        "token_address": to_checksum_address(token_address),
        "settle_timeout": str(settle_timeout),
        "reveal_timeout": str(reveal_timeout),
    }
    request = grequests.put(api_url_for(api_server_test_instance,
                                        "channelsresource"),
                            json=channel_data_obj)
    response = request.send().response
    assert_response_with_error(response, status_code=HTTPStatus.CONFLICT)

    channel_data_obj["settle_timeout"] = str(TEST_SETTLE_TIMEOUT_MAX + 1)
    request = grequests.put(api_url_for(api_server_test_instance,
                                        "channelsresource"),
                            json=channel_data_obj)
    response = request.send().response
    assert_response_with_error(response, status_code=HTTPStatus.CONFLICT)

    channel_data_obj["settle_timeout"] = str(TEST_SETTLE_TIMEOUT_MAX - 1)
    channel_data_obj["token_address"] = to_checksum_address(
        factories.make_address())
    request = grequests.put(api_url_for(api_server_test_instance,
                                        "channelsresource"),
                            json=channel_data_obj)
    response = request.send().response
    assert_response_with_error(response, status_code=HTTPStatus.CONFLICT)
コード例 #8
0
def test_api_channel_withdraw(api_server_test_instance: APIServer,
                              raiden_network: List[RaidenService],
                              token_addresses):
    _, app1 = raiden_network
    token_address = token_addresses[0]
    partner_address = app1.address

    # Withdraw a 0 amount
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json=dict(total_withdraw="0"),
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.CONFLICT)

    # Withdraw an amount larger than balance
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json=dict(total_withdraw="1500"),
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.CONFLICT)

    # Withdraw a valid amount
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json=dict(total_withdraw="750"),
    )
    response = request.send().response
    assert_response_with_code(response, HTTPStatus.OK)

    # Withdraw same amount as before which would sum up to more than the balance
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json=dict(total_withdraw="750"),
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.CONFLICT)
コード例 #9
0
def test_payload_with_invalid_addresses(api_server_test_instance: APIServer):
    """ Addresses require leading 0x in the payload. """
    invalid_address = "61c808d82a3ac53231750dadc13c777b59310bd9"
    channel_data_obj = {
        "partner_address": invalid_address,
        "token_address": "0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8",
        "settle_timeout": "10",
    }
    request = grequests.put(api_url_for(api_server_test_instance,
                                        "channelsresource"),
                            json=channel_data_obj)
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.BAD_REQUEST)

    url_without_prefix = (
        "http://localhost:{port}/api/v1/channels/ea674fdde714fd979de3edf0f56aa9716b898ec8"
    ).format(port=api_server_test_instance.config.port)

    request = grequests.patch(
        url_without_prefix, json=dict(state=ChannelState.STATE_SETTLED.value))
    response = request.send().response

    assert_response_with_code(response, HTTPStatus.NOT_FOUND)
コード例 #10
0
def test_token_events_errors_for_unregistered_token(api_server_test_instance):
    request = grequests.get(
        api_url_for(
            api_server_test_instance,
            "tokenchanneleventsresourceblockchain",
            token_address="0x61C808D82A3Ac53231750daDc13c777b59310bD9",
            from_block="5",
            to_block="20",
        ))
    response = request.send().response
    assert_response_with_error(response, status_code=HTTPStatus.NOT_FOUND)

    request = grequests.get(
        api_url_for(
            api_server_test_instance,
            "channelblockchaineventsresource",
            token_address="0x61C808D82A3Ac53231750daDc13c777b59310bD9",
            partner_address="0x61C808D82A3Ac53231750daDc13c777b59313bD9",
            from_block="5",
            to_block="20",
        ))
    response = request.send().response
    assert_response_with_error(response, status_code=HTTPStatus.NOT_FOUND)
コード例 #11
0
ファイル: test_payments.py プロジェクト: weilbith/raiden
def test_api_payments_with_invalid_input(api_server_test_instance: APIServer,
                                         raiden_network, token_addresses):
    _, app1 = raiden_network
    amount = 100
    token_address = token_addresses[0]
    target_address = app1.raiden.address
    settle_timeout = 39

    # Invalid identifier being 0 or negative
    request = grequests.post(
        api_url_for(
            api_server_test_instance,
            "token_target_paymentresource",
            token_address=to_checksum_address(token_address),
            target_address=to_checksum_address(target_address),
        ),
        json={
            "amount": str(amount),
            "identifier": "0",
            "lock_timeout": str(settle_timeout)
        },
    )
    response = request.send().response
    assert_response_with_error(response, status_code=HTTPStatus.CONFLICT)

    request = grequests.post(
        api_url_for(
            api_server_test_instance,
            "token_target_paymentresource",
            token_address=to_checksum_address(token_address),
            target_address=to_checksum_address(target_address),
        ),
        json={
            "amount": str(amount),
            "identifier": "-1",
            "lock_timeout": str(settle_timeout)
        },
    )
    response = request.send().response
    assert_response_with_error(response, status_code=HTTPStatus.CONFLICT)

    request = grequests.post(
        api_url_for(
            api_server_test_instance,
            "token_target_paymentresource",
            token_address=to_checksum_address(token_address),
            target_address=to_checksum_address(target_address),
        ),
        json={
            "amount": str(amount),
            "identifier": str(UINT64_MAX + 1),
            "lock_timeout": str(settle_timeout),
        },
    )
    response = request.send().response
    assert_response_with_error(response, status_code=HTTPStatus.CONFLICT)
コード例 #12
0
ファイル: test_payments.py プロジェクト: weilbith/raiden
def test_api_payments_with_lock_timeout(api_server_test_instance: APIServer,
                                        raiden_network, token_addresses):
    _, app1 = raiden_network
    amount = 100
    identifier = 42
    token_address = token_addresses[0]
    target_address = app1.raiden.address
    number_of_nodes = 2
    reveal_timeout = number_of_nodes * 4 + DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS
    settle_timeout = 39

    # try lock_timeout = reveal_timeout - should not work
    request = grequests.post(
        api_url_for(
            api_server_test_instance,
            "token_target_paymentresource",
            token_address=to_checksum_address(token_address),
            target_address=to_checksum_address(target_address),
        ),
        json={
            "amount": str(amount),
            "identifier": str(identifier),
            "lock_timeout": str(reveal_timeout),
        },
    )
    response = request.send().response
    assert_response_with_error(response, status_code=HTTPStatus.CONFLICT)

    # try lock_timeout = reveal_timeout * 2  - should  work.
    request = grequests.post(
        api_url_for(
            api_server_test_instance,
            "token_target_paymentresource",
            token_address=to_checksum_address(token_address),
            target_address=to_checksum_address(target_address),
        ),
        json={
            "amount": str(amount),
            "identifier": str(identifier),
            "lock_timeout": str(2 * reveal_timeout),
        },
    )
    with watch_for_unlock_failures(*raiden_network):
        response = request.send().response
    assert_proper_response(response, status_code=HTTPStatus.OK)

    # try lock_timeout = settle_timeout - should work.
    request = grequests.post(
        api_url_for(
            api_server_test_instance,
            "token_target_paymentresource",
            token_address=to_checksum_address(token_address),
            target_address=to_checksum_address(target_address),
        ),
        json={
            "amount": str(amount),
            "identifier": str(identifier),
            "lock_timeout": str(settle_timeout),
        },
    )
    response = request.send().response
    assert_proper_response(response, status_code=HTTPStatus.OK)

    # try lock_timeout = settle_timeout+1 - should not work.
    request = grequests.post(
        api_url_for(
            api_server_test_instance,
            "token_target_paymentresource",
            token_address=to_checksum_address(token_address),
            target_address=to_checksum_address(target_address),
        ),
        json={
            "amount": amount,
            "identifier": identifier,
            "lock_timeout": settle_timeout + 1
        },
    )
    response = request.send().response
    assert_response_with_error(response, status_code=HTTPStatus.CONFLICT)
コード例 #13
0
ファイル: test_token_networks.py プロジェクト: sekmet/raiden
def test_register_token(
    api_server_test_instance,
    token_amount,
    raiden_network: List[RaidenService],
    contract_manager,
    retry_timeout,
):
    app0 = raiden_network[0]
    contract_proxy, _ = app0.rpc_client.deploy_single_contract(
        contract_name=CONTRACT_HUMAN_STANDARD_TOKEN,
        contract=contract_manager.get_contract(CONTRACT_HUMAN_STANDARD_TOKEN),
        constructor_parameters=(token_amount, 2, "raiden", "Rd1"),
    )
    new_token_address = Address(to_canonical_address(contract_proxy.address))
    contract_proxy, _ = app0.rpc_client.deploy_single_contract(
        contract_name=CONTRACT_HUMAN_STANDARD_TOKEN,
        contract=contract_manager.get_contract(CONTRACT_HUMAN_STANDARD_TOKEN),
        constructor_parameters=(token_amount, 2, "raiden", "Rd2"),
    )
    other_token_address = Address(to_canonical_address(contract_proxy.address))

    # Wait until Raiden can start using the token contract.
    # Here, the block at which the contract was deployed should be confirmed by Raiden.
    # Therefore, until that block is received.
    wait_for_block(
        raiden=app0,
        block_number=BlockNumber(app0.get_block_number() +
                                 DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS + 1),
        retry_timeout=retry_timeout,
    )
    wait_for_block(
        raiden=app0,
        block_number=BlockNumber(app0.get_block_number() +
                                 DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS + 1),
        retry_timeout=retry_timeout,
    )

    register_request = grequests.put(
        api_url_for(
            api_server_test_instance,
            "registertokenresource",
            token_address=to_checksum_address(new_token_address),
        ))
    register_response = register_request.send().response
    assert_proper_response(register_response, status_code=HTTPStatus.CREATED)
    response_json = get_json_response(register_response)
    assert "token_network_address" in response_json
    assert is_checksum_address(response_json["token_network_address"])

    # now try to reregister it and get the error
    conflict_request = grequests.put(
        api_url_for(
            api_server_test_instance,
            "registertokenresource",
            token_address=to_checksum_address(new_token_address),
        ))
    conflict_response = conflict_request.send().response
    assert_response_with_error(conflict_response, HTTPStatus.CONFLICT)

    # Test that adding a second token throws a forbidden error
    forbidden_request = grequests.put(
        api_url_for(
            api_server_test_instance,
            "registertokenresource",
            token_address=to_checksum_address(other_token_address),
        ))
    forbidden_response = forbidden_request.send().response
    assert_response_with_error(forbidden_response, HTTPStatus.FORBIDDEN)
    response_json = get_json_response(forbidden_response)
    assert "Number of token networks will exceed the maximum of" in response_json[
        "errors"]
コード例 #14
0
def test_api_channel_open_and_deposit(api_server_test_instance: APIServer,
                                      token_addresses, reveal_timeout):

    first_partner_address = "0x61C808D82A3Ac53231750daDc13c777b59310bD9"
    token_address = token_addresses[0]
    token_address_hex = to_checksum_address(token_address)
    settle_timeout = 1650
    channel_data_obj = {
        "partner_address": first_partner_address,
        "token_address": token_address_hex,
        "settle_timeout": str(settle_timeout),
        "reveal_timeout": str(reveal_timeout),
    }
    # First let's try to create channel with the null address and see error is handled
    channel_data_obj["partner_address"] = NULL_ADDRESS_HEX
    request = grequests.put(api_url_for(api_server_test_instance,
                                        "channelsresource"),
                            json=channel_data_obj)
    response = request.send().response
    assert_response_with_error(response, status_code=HTTPStatus.BAD_REQUEST)
    # now let's really create a new channel
    channel_data_obj["partner_address"] = first_partner_address
    request = grequests.put(api_url_for(api_server_test_instance,
                                        "channelsresource"),
                            json=channel_data_obj)
    response = request.send().response

    assert_proper_response(response, HTTPStatus.CREATED)
    first_channel_id = 1
    json_response = get_json_response(response)
    expected_response = channel_data_obj.copy()
    expected_response.update({
        "balance": "0",
        "state": ChannelState.STATE_OPENED.value,
        "channel_identifier": "1",
        "total_deposit": "0",
    })
    assert check_dict_nested_attrs(json_response, expected_response)
    token_network_address = json_response["token_network_address"]

    # Now let's try to open the same channel again, because it is possible for
    # the participants to race on the channel creation, this is not considered
    # an error.
    request = grequests.put(api_url_for(api_server_test_instance,
                                        "channelsresource"),
                            json=channel_data_obj)
    response = request.send().response
    assert_proper_response(response, HTTPStatus.OK)
    json_response = get_json_response(response)
    assert check_dict_nested_attrs(json_response, expected_response)

    # now let's open a channel and make a deposit too
    second_partner_address = "0x29FA6cf0Cce24582a9B20DB94Be4B6E017896038"
    total_deposit = 100
    channel_data_obj = {
        "partner_address": second_partner_address,
        "token_address": to_checksum_address(token_address),
        "settle_timeout": str(settle_timeout),
        "reveal_timeout": str(reveal_timeout),
        "total_deposit": str(total_deposit),
    }
    request = grequests.put(api_url_for(api_server_test_instance,
                                        "channelsresource"),
                            json=channel_data_obj)
    response = request.send().response

    assert_proper_response(response, HTTPStatus.CREATED)
    second_channel_id = 2
    json_response = get_json_response(response)
    expected_response = channel_data_obj.copy()
    expected_response.update({
        "balance": str(total_deposit),
        "state": ChannelState.STATE_OPENED.value,
        "channel_identifier": str(second_channel_id),
        "token_network_address": token_network_address,
        "total_deposit": str(total_deposit),
    })
    assert check_dict_nested_attrs(json_response, expected_response)

    # assert depositing again with less than the initial deposit returns 409
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=second_partner_address,
        ),
        json={"total_deposit": "99"},
    )
    response = request.send().response
    assert_proper_response(response, HTTPStatus.CONFLICT)

    # assert depositing negative amount fails
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=first_partner_address,
        ),
        json={"total_deposit": "-1000"},
    )
    response = request.send().response
    assert_proper_response(response, HTTPStatus.CONFLICT)

    # let's deposit on the first channel
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=first_partner_address,
        ),
        json={"total_deposit": str(total_deposit)},
    )
    response = request.send().response
    assert_proper_response(response)
    json_response = get_json_response(response)
    expected_response = {
        "channel_identifier": str(first_channel_id),
        "partner_address": first_partner_address,
        "token_address": to_checksum_address(token_address),
        "settle_timeout": str(settle_timeout),
        "reveal_timeout": str(reveal_timeout),
        "state": ChannelState.STATE_OPENED.value,
        "balance": str(total_deposit),
        "total_deposit": str(total_deposit),
        "token_network_address": token_network_address,
    }
    assert check_dict_nested_attrs(json_response, expected_response)

    # let's try querying for the second channel
    request = grequests.get(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=second_partner_address,
        ))

    response = request.send().response
    assert_proper_response(response)
    json_response = get_json_response(response)
    expected_response = {
        "channel_identifier": str(second_channel_id),
        "partner_address": second_partner_address,
        "token_address": to_checksum_address(token_address),
        "settle_timeout": str(settle_timeout),
        "reveal_timeout": str(reveal_timeout),
        "state": ChannelState.STATE_OPENED.value,
        "balance": str(total_deposit),
        "total_deposit": str(total_deposit),
        "token_network_address": token_network_address,
    }
    assert check_dict_nested_attrs(json_response, expected_response)

    # finally let's burn all eth and try to open another channel
    burn_eth(api_server_test_instance.rest_api.raiden_api.raiden.rpc_client)
    channel_data_obj = {
        "partner_address": "0xf3AF96F89b3d7CdcBE0C083690A28185Feb0b3CE",
        "token_address": to_checksum_address(token_address),
        "settle_timeout": str(settle_timeout),
        "reveal_timeout": str(reveal_timeout),
    }
    request = grequests.put(api_url_for(api_server_test_instance,
                                        "channelsresource"),
                            json=channel_data_obj)
    response = request.send().response
    assert_proper_response(response, HTTPStatus.PAYMENT_REQUIRED)
    json_response = get_json_response(response)
    assert "The account balance is below the estimated amount" in json_response[
        "errors"]
コード例 #15
0
def test_api_channel_state_change_errors(api_server_test_instance: APIServer,
                                         token_addresses, reveal_timeout):
    partner_address = "0x61C808D82A3Ac53231750daDc13c777b59310bD9"
    token_address = token_addresses[0]
    settle_timeout = 1650
    channel_data_obj = {
        "partner_address": partner_address,
        "token_address": to_checksum_address(token_address),
        "settle_timeout": str(settle_timeout),
        "reveal_timeout": str(reveal_timeout),
    }
    request = grequests.put(api_url_for(api_server_test_instance,
                                        "channelsresource"),
                            json=channel_data_obj)
    response = request.send().response
    assert_proper_response(response, HTTPStatus.CREATED)

    # let's try to set a random state
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json=dict(state="inlimbo"),
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.BAD_REQUEST)

    # let's try to set both new state and total_deposit
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json=dict(state=ChannelState.STATE_CLOSED.value, total_deposit="200"),
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.CONFLICT)

    # let's try to set both new state and total_withdraw
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json=dict(state=ChannelState.STATE_CLOSED.value, total_withdraw="200"),
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.CONFLICT)

    # let's try to set both total deposit and total_withdraw
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json=dict(total_deposit="500", total_withdraw="200"),
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.CONFLICT)

    # let's try to set both new state and reveal_timeout
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json=dict(state=ChannelState.STATE_CLOSED.value, reveal_timeout="50"),
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.CONFLICT)

    # let's try to set both total_deposit and reveal_timeout
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json=dict(total_deposit="500", reveal_timeout="50"),
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.CONFLICT)

    # let's try to set both total_withdraw and reveal_timeout
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json=dict(total_withdraw="500", reveal_timeout="50"),
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.CONFLICT)

    # let's try to patch with no arguments
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ))
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.BAD_REQUEST)

    # ok now let's close and settle for real
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json=dict(state=ChannelState.STATE_CLOSED.value),
    )
    response = request.send().response
    assert_proper_response(response)

    # let's try to deposit to a settled channel
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json=dict(total_deposit="500"),
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.CONFLICT)