def test_msg_send_later_invalid_membership(initial_data, initial_msgs): ''' access error user1 calls msg_send_later to channel 2 ''' curr_time = int(time.time()) with pytest.raises(AccessError): message_send_later(users[0]['token'], 2, 'late msg', curr_time + 1)
def test_msg_send_later_invalid_token(initial_msgs, initial_data): ''' access error pass an invalid token to msg_send_later ''' curr_time = int(time.time()) with pytest.raises(AccessError): message_send_later('invalid_token', 1, 'late msg', curr_time + 1)
def test_msg_send_later_invalid_msglength(initial_data, initial_msgs): ''' input error user 1 calls msg_send_later to channel 1 with a too long msg ''' curr_time = int(time.time()) with pytest.raises(InputError): message_send_later(users[0]['token'], 1, 'm' * 1001, curr_time + 1)
def test_msg_send_later_invalid_time(initial_data, initial_msgs): ''' input error user 1 calls msg_send_later to channel 1 with a time in the past ''' curr_time = int(time.time()) with pytest.raises(InputError): message_send_later(users[0]['token'], 1, 'late msg', curr_time - 1)
def test_msg_send_later_invalid_channel(initial_data, initial_msgs): ''' input error user 1 calls msg_send_later to an non-existing channel (0) ''' curr_time = int(time.time()) with pytest.raises(InputError): message_send_later(users[0]['token'], 0, 'late msg', curr_time + 1)
def send_later(): """ This is a flask wrapper for the message_send_later function Parameters: No parameters Returns: (dictionary): A dictionary containing the message_id of the message that was sent. """ data = request.get_json() token = data['token'] channel_id = int(data['channel_id']) message = data['message'] time = (data['time_sent']) if len(message) >= 1000: raise InputError(description="Message is too long") if not check_if_channel_exists(channel_id): raise InputError(description="Channel does not exist") if int(time) < int(datetime.datetime.now().replace(tzinfo=timezone.utc).timestamp()): raise InputError(description="Invalid time") if not check_if_user_in_channel_member(token, channel_id): raise AccessError(description="User not member of channel") message_id = message_send_later(token, channel_id, message, time) return dumps(message_id)
def send_message_late(): data = request.get_json() token = data['token'] channel_id = int(data['channel_id']) message = data['message'] time_sent = int(data['time_sent']) return dumps(message_send_later(token, channel_id, message, time_sent))
def test_msg_send_later_standard_2(initial_data, initial_msgs): ''' no error user 1 calls msg_send_later in 0 second and send another msg immediately ''' curr_time = int(time.time()) resp = message_send_later(users[0]['token'], 1, 'late msg', curr_time) message_send(users[0]['token'], 1, 'imme msg') assert len(channels[0]['messages']) == 4 assert resp['message_id'] == 10003
def test_message_send_later_invalid_InputError(): with pytest.raises(InputError): message_send_later( hayden_dict['token'], chan_id['channel_id'], "Hayens message", datetime.datetime(2020, 3, 28, 2, 9, 46, 184346).replace(tzinfo=timezone.utc).timestamp())
def test_message_send_later_invalid_AccessError(): with pytest.raises(AccessError): message_send_later( rob_dict['token'], chan_id['channel_id'], "Robs message", datetime.datetime(2020, 3, 28, 2, 9, 46, 184346).replace(tzinfo=timezone.utc).timestamp())
'is_pinned': False } def test_message_send_invalid_InputError(): with pytest.raises(InputError): message_send(hayden_dict['token'], chan_id['channel_id'], "H" * 1001) def test_message_send_invalid_AccessError(): with pytest.raises(AccessError): message_send(rob_dict['token'], chan_id['channel_id'], "Robs message") message_id1 = message_send_later( hayden_dict['token'], chan_id['channel_id'], "Haydens Message later", (datetime.datetime.now() + datetime.timedelta(0, 1)).replace(tzinfo=timezone.utc).timestamp()) def test_message_send_later(): assert get_messages_store()['Messages'][1] == { 'channel_id': chan_id["channel_id"], 'message_id': message_id1['message_id'], 'user_id': hayden_dict['u_id'], 'message': 'Haydens Message later', 'reacts': [], 'time_created': get_messages_store()['Messages'][1]['time_created'], 'is_pinned': False }