Esempio n. 1
0
def test_parse_and_convert_modified_meeting():
    original_dict = db.get_all_meetings()[0]
    parsed_object = Meeting.parse(original_dict)  # Type: Meeting
    parsed_object.started = not parsed_object.started
    original_dict["started"] = not original_dict["started"]
    parsed_dict = parsed_object.to_json_dict()
    assert original_dict == parsed_dict
Esempio n. 2
0
def test_get_role_none():
    meeting = Meeting.parse(db.get_meeting(mid))
    all_staff_ids = set([staff["_id"] for staff in db.get_all_staff()])
    non_participants = all_staff_ids.difference(set(meeting.staff))
    staff = Staff.parse(db.get_staff(non_participants.pop()))
    roles = get_role(staff, meeting)
    assert (len(roles) == 0)
def test_meeting_contract_deployment():
    mch = MeetingContractHelper(c)
    meeting = Meeting.parse(db.get_all_meetings()[0])
    contract_addr = mch.new_meeting_contract(meeting)

    mdt_meeting = w3.eth.contract(
        address=contract_addr,
        abi=mch.contract_abi,
    )

    assert mdt_meeting.functions.getMeetingId().call() == meeting.id
def test_meeting_contract_set_hash():
    mch = MeetingContractHelper(c)
    meeting = Meeting.parse(db.get_all_meetings()[0])
    contract_addr = mch.new_meeting_contract(meeting)
    meeting.contract_id = contract_addr
    mch.set_event_hash(meeting, "start_event", "end_event")

    mdt_meeting = w3.eth.contract(
        address=contract_addr,
        abi=mch.contract_abi,
    )

    assert mdt_meeting.functions.getEvents().call() == [
        "start_event", "end_event"
    ]
Esempio n. 5
0
def room_message(event_string):
    """
    Main function that handles all events that enter through the web socket
    """
    # Parse json as dict
    try:
        event_json = json.loads(event_string)
    except ValueError as ve:
        log.error("Error Parsing room-message as JSON!")
        log.error(ve)
        traceback.print_tb(ve.__traceback__)
        return json.dumps(
            sign(
                get_error_ack(
                    "unknown",
                    "unknown",
                    content=AckErrorContent(
                        error_code=EventError.MALFORMED_EVENT,
                        details='Cant parse message as JSON'))).to_json_dict())

    # Validate JSON dict using schema
    ok, err_event = validate_schema(event_json)
    if not ok:
        log.error("Sending error msg")
        errormsg = json.dumps(sign(err_event).to_json_dict())
        log.error("======== error_msg: " + errormsg)
        return errormsg

    # Parse dict as event object
    try:
        event = Event.parse(event_json)
    except KeyError as ve:
        log.error("Error Parsing room-message!")
        log.error(ve)
        traceback.print_tb(ve.__traceback__)
        return json.dumps(
            sign(
                get_error_ack("unknown",
                              "unknown",
                              content=AckErrorContent(
                                  error_code=EventError.MALFORMED_EVENT,
                                  details='Cant parse message as Event'))).
            to_json_dict())

    # Check timestamp
    ok, err_event = validate_timestamp(event)
    if not ok:
        return json.dumps(sign(err_event).to_json_dict())

    # Get the staff
    try:
        staff = Staff.parse(db.get_staff(event.by))
    except KeyError as ke:
        log.error(ke)
        traceback.print_tb(ke.__traceback__)
        return json.dumps(
            sign(
                get_error_ack(event.id,
                              event.meeting_id,
                              content=AckErrorContent(
                                  error_code=EventError.STAFF_NOT_FOUND,
                                  details=''))).to_json_dict())
    except TypeError as te:
        log.error(te)
        traceback.print_tb(te.__traceback__)
        return json.dumps(
            sign(
                get_error_ack(event.id,
                              event.meeting_id,
                              content=AckErrorContent(
                                  error_code=EventError.STAFF_NOT_FOUND,
                                  details=''))).to_json_dict())

    # Get the meeting
    try:
        meeting = Meeting.parse(db.get_meeting(event.meeting_id))
    except KeyError as ke:
        log.error(ke)
        traceback.print_tb(ke.__traceback__)
        return json.dumps(
            sign(
                get_error_ack(event.id,
                              event.meeting_id,
                              content=AckErrorContent(
                                  error_code=EventError.MEETING_NOT_FOUND,
                                  details=''))).to_json_dict())

    except TypeError as te:
        log.error(te)
        traceback.print_tb(te.__traceback__)
        return json.dumps(
            sign(
                get_error_ack(event.id,
                              event.meeting_id,
                              content=AckErrorContent(
                                  error_code=EventError.MEETING_NOT_FOUND,
                                  details=''))).to_json_dict())

    # Check signature, before trusting anything it says

    if event.type in [MeetingEventType.JOIN, MeetingEventType.START
                      ]:  # We check contract for join and start
        ok, err_event = validate_signature(event,
                                           staff,
                                           meeting,
                                           check_contract=True)
        if not ok:
            return json.dumps(sign(err_event).to_json_dict())
    else:
        ok, err_event = validate_signature(event, staff, meeting)
        if not ok:
            return json.dumps(sign(err_event).to_json_dict())

    # Get the roles
    roles = get_role(staff, meeting)

    # A preliminary authority check to see if the user can make any statements about the meeting
    ok, err_event = validate_preliminary_authority(event, roles)
    if not ok:
        return json.dumps(sign(err_event).to_json_dict())

    # Get the event type
    event_type = MeetingEventType(event.type)
    ack_event = None
    ok = False
    end_meeting = False
    send_privately = False

    if event_type == MeetingEventType.START:
        ok, ack_event = start(event, staff, meeting, roles)

    if event_type == MeetingEventType.JOIN:
        ok, ack_event = join(event, staff, meeting, roles)

    if event_type == MeetingEventType.LEAVE:
        ok, ack_event = leave(event, staff, meeting)

    if event_type == MeetingEventType.POLL:
        ok, ack_event = poll(event, staff, meeting, roles)

    if event_type == MeetingEventType.VOTE:
        ok, ack_event = vote(event, staff, meeting, roles)

    if event_type == MeetingEventType.POLL_END:
        ok, ack_event = end_poll(event, staff, meeting, roles)

    if event_type == MeetingEventType.COMMENT or event_type == MeetingEventType.REPLY or event_type == MeetingEventType.DISAGREEMENT:
        ok, ack_event = comment_reply_disagreement(event, staff, meeting,
                                                   roles)

    if event_type == MeetingEventType.DISCUSSION:
        ok, ack_event = discussion(event, staff, meeting, roles)

    if event_type == MeetingEventType.PATIENT_DATA_CHANGE:
        ok, ack_event = patient_data_change(event, staff, meeting, roles)

    if event_type == MeetingEventType.END:
        ok, ack_event = end(event, staff, meeting, roles)
        if ok:
            end_meeting = True

    if not ok:  # If not ok we send the error ack event privately
        return json.dumps(sign(ack_event).to_json_dict())
    else:
        # If an event has been referenced
        check_and_remove_ref_event(meeting, event.ref_event)

        # Add ack and event to unreferenced events
        add_event_as_unref(meeting, event)
        add_event_as_unref(meeting, ack_event)

        # Sign the ack
        signed_ack_event = sign(ack_event)

        if not send_privately:  # Only Broadcast if the send_privately is set to False
            # Broadcast event
            send(json.dumps(event.to_json_dict()), broadcast_room=meeting.id)
            send(json.dumps(signed_ack_event.to_json_dict()),
                 broadcast_room=meeting.id)

        record(event, meeting)
        record(signed_ack_event, meeting)

        if end_meeting:
            end_meeting_session(meeting, event, signed_ack_event)

        # Send the ack event to the user privately as well
        return json.dumps(signed_ack_event.to_json_dict())
Esempio n. 6
0
def test_parse_and_convert_bad_staff():
    original_dict = db.get_all_staff()[0]
    print(original_dict)
    original_dict.pop("name")
    with pytest.raises(KeyError) as e:
        parsed_dict = Meeting.parse(original_dict)
Esempio n. 7
0
def test_parse_and_convert_meeting():
    original_dict = db.get_all_meetings()[0]
    parsed_object = Meeting.parse(original_dict)
    generated_dict = parsed_object.to_json_dict()
    assert original_dict == generated_dict
Esempio n. 8
0
def test_parse_and_convert_bad_event():
    original_dict = copy.deepcopy(general_event_dict)
    print(original_dict)
    original_dict.pop("meetingId")
    with pytest.raises(KeyError) as e:
        parsed_dict = Meeting.parse(original_dict)
Esempio n. 9
0
def test_get_role_participant():
    meeting = Meeting.parse(db.get_meeting(mid))
    staff = Staff.parse(db.get_staff(meeting.staff[1]))
    roles = get_role(staff, meeting)
    assert (Role.PARTICIPANT in roles)
Esempio n. 10
0
def test_get_role_host():
    meeting = Meeting.parse(db.get_meeting(mid))
    staff = Staff.parse(db.get_staff(meeting.host))
    roles = get_role(staff, meeting)
    assert (Role.HOST in roles) and (Role.PARTICIPANT in roles)