def test_broadcast_move( fx_server: WSGIServer, fx_session: scoped_session, fx_other_server: WSGIServer, fx_other_session: Session, fx_user: User, fx_novice_status: typing.Mapping[str, str], ): now = datetime.datetime.utcnow() node = Node(url=fx_server.url, last_connected_at=now) node2 = Node(url=fx_other_server.url, last_connected_at=datetime.datetime.utcnow()) move = fx_user.create_novice(fx_novice_status) fx_session.add_all([node, node2, move]) fx_session.commit() assert not fx_other_session.query(Move).get(move.id) serialized = move.serialize( use_bencode=False, include_signature=True, include_id=True, ) multicast(serialized=serialized, broadcast=broadcast_move) assert fx_other_session.query(Move).get(move.id) assert node.last_connected_at > now
def test_sync_node_unaviable_on_branch_point( fx_user: User, fx_session: scoped_session, fx_server: WSGIServer, fx_other_session: Session, fx_novice_status: typing.Mapping[str, str], code: int): move = fx_user.create_novice(fx_novice_status) block = Block.create(fx_user, [move]) Block.sync(Node(url=fx_server.url), fx_other_session) assert fx_other_session.query(Block).count() == 1 serialized = block.serialize(use_bencode=False, include_suffix=True, include_moves=True, include_hash=True) serialized['id'] = block.id + 1 with Mocker() as m: m.register_uri( 'GET', f'{fx_server.url}/blocks/last', json={'block': serialized}, status_code=200, ) m.register_uri( 'GET', f'{fx_server.url}/blocks/1', status_code=code, ) assert not Block.sync(Node(url=fx_server.url), fx_other_session)
def test_move_broadcast(fx_session: scoped_session, fx_user: User, fx_novice_status: typing.Mapping[str, str]): move = fx_user.create_novice(fx_novice_status) fx_session.add(move) fx_session.commit() with unittest.mock.patch('nekoyume.tasks.multicast') as m: move_broadcast(move.id, 'http://localhost:5000', 'http://localhost:5001', session=fx_session) serialized = move.serialize( use_bencode=False, include_signature=True, include_id=True, ) assert m.called args = m.call_args[1] assert serialized == args['serialized'] assert isinstance(args['sent_node'], Node) assert args['sent_node'].url == 'http://localhost:5000' assert isinstance(args['my_node'], Node) assert args['my_node'].url == 'http://localhost:5001' broadcast = args['broadcast'] assert isinstance(broadcast, typing.Callable) assert broadcast.__name__ == 'broadcast_move'
def test_sync_node_unavailable_on_get_last_block( fx_user: User, fx_session: scoped_session, fx_other_session: Session, fx_server: WSGIServer, fx_novice_status: typing.Mapping[str, str], code: int): move = fx_user.create_novice(fx_novice_status) Block.create(fx_user, [move]) with Mocker() as m: m.get(url=f'{fx_server.url}/blocks/last', status_code=code) Block.sync(Node(url=fx_server.url), fx_other_session) assert fx_other_session.query(Block).count() == 0
def test_execute_raise_invalid_move_error( fx_user: User, fx_novice_status: typing.Mapping[str, str]): move = fx_user.create_novice(fx_novice_status) Block.create(fx_user, [move]) for move_type in Move.__subclasses__(): move = fx_user.move(move_type()) assert not move.block assert not move.block_id with raises(InvalidMoveError): move.execute()
def broadcast_move_failed(fx_session: scoped_session, fx_user: User, fx_novice_status: typing.Mapping[str, str], error): now = datetime.datetime.utcnow() move = fx_user.create_novice(fx_novice_status) node = Node(url='http://test.neko', last_connected_at=now) fx_session.add_all([node, move]) fx_session.commit() with Mocker() as m: serialized = move.serialize( use_bencode=False, include_signature=True, include_id=True, ) m.post('http://test.neko', exc=error) broadcast_move(serialized=serialized) assert node.last_connected_at == now
def test_broadcast_move_same_url(fx_session: scoped_session, fx_user: User, fx_novice_status: typing.Mapping[str, str]): url = 'http://test.neko' now = datetime.datetime.utcnow() node = Node(url=url, last_connected_at=now) move = fx_user.create_novice(fx_novice_status) fx_session.add_all([node, move]) fx_session.commit() with Mocker() as m: serialized = move.serialize( use_bencode=False, include_signature=True, include_id=True, ) broadcast_move(serialized=serialized, sent_node=node) assert not m.called assert node.last_connected_at == now
def test_prevent_hack_and_slash_when_dead( fx_test_client: FlaskClient, fx_session: Session, fx_user: User, fx_private_key: PrivateKey, fx_novice_status: typing.Dict[str, str], ): move = fx_user.create_novice(fx_novice_status) Block.create(fx_user, [move]) assert fx_user.avatar().dead is False while fx_user.avatar().hp > 0: move = fx_user.hack_and_slash() Block.create(fx_user, [move]) assert fx_user.avatar().dead is True response = fx_test_client.post('/session_moves', data={'name': 'hack_and_slash'}) assert response.status_code == 302
def test_broadcast_move_my_node(fx_session: scoped_session, fx_user: User, fx_novice_status: typing.Mapping[str, str]): url = 'http://test.neko' now = datetime.datetime.utcnow() node = Node(url=url, last_connected_at=now) move = fx_user.create_novice(fx_novice_status) fx_session.add_all([node, move]) fx_session.commit() with Mocker() as m: m.post('http://test.neko/moves', json={'result': 'success'}) expected = serialized = move.serialize( use_bencode=False, include_signature=True, include_id=True, ) broadcast_move(serialized=serialized, my_node=node) expected['sent_node'] = 'http://test.neko' assert node.last_connected_at > now # check request.json value assert m.request_history[0].json() == expected