Exemplo n.º 1
0
def test_message_unpin():
    '''
    Testing message_unpin function 
    '''
    clear()

    now = datetime.now()
    timestamp = datetime.timestamp(now)

    fake_token = jwt.encode({
        'u_id': 3,
        'time': timestamp
    },
                            SECRET,
                            algorithm='HS256')

    #creating users to create channels
    user1 = auth_register("*****@*****.**", "user1pass", "user1", "last1",
                          None)
    user2 = auth_register("*****@*****.**", "user2pass", "user2", "last2",
                          None)
    token1 = user1['token']
    token2 = user2['token']

    #creating channels
    ch_id1 = channels_create(token1, "FirstChannel", True)['channel_id']

    #creating channel messages
    m_id1 = message_send(token1, ch_id1, 'hello1')['message_id']
    m_id2 = message_send(token1, ch_id1, 'hello2')['message_id']

    with pytest.raises(InputError):
        #invalid message_id
        message_unpin(token2, 4)

    #user pinning a message
    message_pin(token1, m_id1)

    #user unpinning a pinned message
    message_unpin(token1, m_id1)

    with pytest.raises(InputError):
        #message is already unpinned
        message_unpin(token1, m_id1)

    message_pin(token1, m_id1)
    with pytest.raises(AccessError):
        #unauthorised user that is not an owner or part of the channel
        message_unpin(fake_token, m_id1)

    message_pin(token1, m_id2)
    message = find_message(ch_id1, m_id2)
    assert message['is_pinned']

    message_unpin(token1, m_id2)
    message = find_message(ch_id1, m_id2)
    assert not message['is_pinned']

    clear()
Exemplo n.º 2
0
def test_unreact():
    '''
    Test a valid case of message_unreact to a message someone
    else had sent
    '''
    workspace_reset()
    user1 = reg_user1()
    user2 = reg_user2()
    channel1 = create_ch1(user1)
    invite_to_ch1(user1, user2, channel1)
    msg1 = send_msg1(user1, channel1)

    # react to the message
    react_to_msg(1, msg1, user2)

    payload = {
        'token': user2['token'],
        'message_id': msg1['message_id'],
        'react_id': 1
    }
    message.unreact(payload)

    message1_reacts = find_message(msg1['message_id'])['reacts']
    for i in message1_reacts:
        if i['react_id'] == 1:
            assert user2['u_id'] not in i['u_ids']
Exemplo n.º 3
0
def pin(payload):  # pylint: disable=R1711
    'testing functionability for message pin'
    user = get_user_from('token', payload['token'])
    message = find_message(payload['message_id'])
    channel = get_channel(message['channel_id'])

    if message['is_pinned'] is True:
        raise InputError(description='Message is already pinned')

    if not test_in_channel(user['u_id'], channel):
        raise AccessError(
            description='You do not have permission to pin this message')

    if not check_owner(user, channel):
        raise InputError(description='You do not have permission')

    message['is_pinned'] = True

    return
Exemplo n.º 4
0
def remove(payload):  # pylint: disable=R1711
    '''
    Function to remove a message from a channel
    '''
    user = get_user_from('token', payload['token'])
    messages = get_messages_store()

    message = find_message(payload['message_id'])

    channel = get_channel(message['channel_id'])

    if message['u_id'] != user['u_id']:
        if not check_owner(user, channel):
            raise AccessError(description='You do not have permission')

    messages.remove(message)
    channel['messages'].remove(message)

    return
Exemplo n.º 5
0
def edit(payload):
    '''
    Function to remove a message from a channel
    '''
    message_store = get_messages_store()
    user = get_user_from('token', payload['token'])
    message = find_message(payload['message_id'])

    channel = get_channel(message['channel_id'])

    if message['u_id'] != user['u_id']:
        if not check_owner(user, channel):
            raise AccessError(description='You do not have permission')

    if len(payload['message']) == 0:  # pylint: disable=C1801, R1705
        channel['messages'].remove(message)
        message_store.remove(message)
        return
    else:
        message['message'] = payload['message']
        return
Exemplo n.º 6
0
def react(payload):
    '''
    Function to add a react to a given message
    '''
    global REACT_IDS  # pylint: disable=W0603
    user = get_user_from('token', payload['token'])

    message = find_message(int(payload['message_id']))

    channel = get_channel(message['channel_id'])

    if int(payload['react_id']) not in REACT_IDS:
        raise InputError(description='Unable to react with react_id ' +
                         str(payload['react_id']))

    if not test_in_channel(user['u_id'], channel):
        raise InputError(description='Unable to react as you are ' +
                         'not a part of that channel')

    for i in message['reacts']:
        if i['react_id'] == payload['react_id']:
            # this react is already present in the message
            # just add another u_id
            if user['u_id'] in i['u_ids']:
                # if the user has reacted, unreact them
                unreact(payload)
            i['u_ids'].append(user['u_id'])
            return

    # no previous react wih react_id
    new_react = {
        'react_id': payload['react_id'],
        'u_ids': [],
        'is_user_reacted': False
    }
    new_react['u_ids'].append(user['u_id'])
    message['reacts'].append(new_react)

    return
Exemplo n.º 7
0
def test_react1():
    '''
    Test a valid use of react on your own message
    '''
    workspace_reset()
    user1 = reg_user1()
    channel1 = create_ch1(user1)
    msg1 = send_msg1(user1, channel1)

    payload = {
        'token': user1['token'],
        'message_id': msg1['message_id'],
        'react_id': 1
    }

    # find the reacts in the message with react_id 1
    # Assert user1 has reacted
    message.react(payload)
    message1_reacts = find_message(msg1['message_id'])['reacts']
    for i in message1_reacts:
        if i['react_id'] == 1:
            assert user1['u_id'] in i['u_ids']
Exemplo n.º 8
0
def unreact(payload):
    '''
    Function to remove a react from a message
    '''
    global REACT_IDS  # pylint: disable=W0603
    user = get_user_from('token', payload['token'])

    message = find_message(payload['message_id'])

    channel = get_channel(message['channel_id'])

    if int(payload['react_id']) not in REACT_IDS:
        raise InputError(description='Unable to react with react_id ' +
                         str(payload['react_id']))

    if not test_in_channel(user['u_id'], channel):
        raise InputError(description='Unable to react as you are ' +
                         'not a part of that channel')

    for i in message['reacts']:
        if i['react_id'] == payload['react_id']:
            # this react is already present in the message
            # just remove u_id
            if user['u_id'] not in i['u_ids']:
                raise InputError(description='Attempting to uncreact ' +
                                 'a message you have not reacted to')
            i['u_ids'].remove(user['u_id'])
            if len(i['u_ids']) == 0:  # pylint: disable=C1801
                # no one else has reacted, so remove react
                message['reacts'].remove(i)
                print(message['reacts'])
            return

    # unable to find react of react_id in messages
    raise InputError(description='Message with ID message_id ' +
                     'does not contain an active react with with ID react_id')
Exemplo n.º 9
0
def message_unpin(token, message_id):
    '''
    Given a message within a channel, remove its mark as unpinned
    '''
    u_id = get_u_id(token)

    channel_id = find_channel(message_id)
    message = find_message(channel_id, message_id)

    m_id = message_exists(message_id)
    auth_user = user_in_channel(u_id, channel_id)

    # Input errors for invalid / already unpinned messages
    if not m_id:
        raise InputError("Invalid message ID")
    if not message['is_pinned']:
        raise InputError("Message is already unpinned")
    # Access error for non-owners / unauthorised users
    if not auth_user:
        raise AccessError("User is not a member of the channel")

    message['is_pinned'] = False

    return {}
Exemplo n.º 10
0
def message_pin(token, message_id):
    '''
    Given a message within a channel, mark it as "pinned" to be given special display treatment by the frontend
    '''
    u_id = get_u_id(token)

    channel_id = find_channel(message_id)
    message = find_message(channel_id, message_id)

    m_id = message_exists(message_id)
    auth_user = user_in_channel(u_id, channel_id)

    # Input errors for invalid / already pinned messages
    if not m_id:
        raise InputError("Invalid message ID")
    if message['is_pinned']:
        raise InputError("Message is already pinned")
    # Access error for non-owners / unauthorised users
    if not auth_user:
        raise AccessError("User is not a member of the channel")

    message['is_pinned'] = True

    return {}
Exemplo n.º 11
0
def test_react2():
    '''
    Test a valid use of react where someone else reacts to someones
    elses message
    '''
    workspace_reset()

    user1 = reg_user1()
    user2 = reg_user2()
    channel1 = create_ch1(user1)
    msg1 = send_msg1(user1, channel1)

    invite_to_ch1(user1, user2, channel1)

    payload = {
        'token': user2['token'],
        'message_id': msg1['message_id'],
        'react_id': 1
    }
    message.react(payload)
    message1_reacts = find_message(msg1['message_id'])['reacts']
    for i in message1_reacts:
        if i['react_id'] == 1:
            assert user2['u_id'] in i['u_ids']