def test_connect(test_client):
    socketio_test_client = socketio.test_client(app,
                                                flask_test_client=test_client)
    assert socketio_test_client.is_connected()

    socketio_test_client.disconnect()
    assert not socketio_test_client.is_connected()
def test_emit_events_two(test_client, json_header):
    socketio_test_client = socketio.test_client(app,
                                                flask_test_client=test_client)
    send_to_connected_clients({"id": "test"})
    received = socketio_test_client.get_received()
    assert received[0]["name"] == "findProof"
    assert received[0]["args"][0]["id"] == "test"
def test_proof_incorrect_hash(test_client, json_header, requests_mock):
    socketio_test_client = socketio.test_client(app,
                                                flask_test_client=test_client)
    transaction = {
        "id": "test3",
        "to": "test1",
        "from": "test2",
        "timestamp": 123,
        "amount": 10,
        "signature": "test4",
    }
    send_to_connected_clients(transaction)

    received = socketio_test_client.get_received()
    assert received[0]["name"] == "findProof"

    hash_ = sha256((str(5) + "test2" + "test2" + str(10) + "test3" +
                    "test4").encode("utf-8")).hexdigest()
    socketio_test_client.emit("proof", {
        "id": "test3",
        "proof": hash_,
        "nonce": 5,
        "minerId": "test"
    })

    received = socketio_test_client.get_received()
    assert received == []
def test_proof_correct_payload(test_client, json_header, requests_mock_mining):
    socketio_test_client = socketio.test_client(app,
                                                flask_test_client=test_client)
    transaction = {
        "from": "test2",
        "to": "test1",
        "amount": VALID_AMOUNT,
        "timestamp": VALID_TIMESTAMP,
        "id": VALID_ID,
        "signature": VALID_SIGNATURE,
    }
    send_to_connected_clients(transaction)

    received = socketio_test_client.get_received()
    assert received[0]["name"] == "findProof"

    hash_ = sha256(
        (str(VALID_NONCE) + str(VALID_AMOUNT) + str(VALID_TIMESTAMP) +
         VALID_ID + VALID_SIGNATURE).encode("utf-8")).hexdigest()

    assert hash_ == VALID_PROOF
    socketio_test_client.emit(
        "proof",
        {
            "id": VALID_ID,
            "proof": hash_,
            "nonce": VALID_NONCE,
            "minerId": "test"
        },
    )

    received = socketio_test_client.get_received()
    assert received[0]["name"] == "stopProof"
    assert received[1]["name"] == "reward"
def test_emit_events(test_client):
    socketio_test_client = socketio.test_client(app,
                                                flask_test_client=test_client)
    socketio_test_client.emit("echo", {"a": "b"})
    received = socketio_test_client.get_received()

    assert received[0]["name"] == "response"
    assert received[0]["args"][0]["a"] == "b"
def test_wrong_payload(test_client, json_header):
    socketio_test_client = socketio.test_client(app,
                                                flask_test_client=test_client)
    try:
        send_to_connected_clients({"not_id": "test"})
        assert False
    except Exception:
        received = socketio_test_client.get_received()
        assert received == []
def test_proof_incorrect_payload(test_client, json_header):
    socketio_test_client = socketio.test_client(app,
                                                flask_test_client=test_client)
    socketio_test_client.emit("proof", {"id": "something"})
    received = socketio_test_client.get_received()
    assert received == []