def test_message_sendlaterdm_errors(user_setup, dm_setup, channel_setup, message_setup): # invalid token with pytest.raises(AccessError): message_sendlater_v1(make_token(100000), 0, "hello", datetime.datetime.now().timestamp() + 1) # DM ID is not a valid DM with pytest.raises(InputError): message_sendlaterdm_v1(make_token(2), 20, "hello", datetime.datetime.now().timestamp() + 1) # Message is more than 1000 characters thousand_string = '' for i in range(1005): thousand_string += str(i) with pytest.raises(InputError): message_sendlaterdm_v1(make_token(2), 0, thousand_string, datetime.datetime.now().timestamp() + 1) # Time sent is a time in the past with pytest.raises(InputError): message_sendlaterdm_v1(make_token(2), 0, "hello", datetime.datetime.now().timestamp() - 5) # the authorised user is not a member of the DM they are trying to post to with pytest.raises(AccessError): message_sendlaterdm_v1(make_token(2), 1, "hello", datetime.datetime.now().timestamp() + 1)
def test_message_sendlater_errors(user_setup, dm_setup, channel_setup, message_setup): # invalid token with pytest.raises(AccessError): message_sendlater_v1(make_token(100000), 0, "hello", datetime.datetime.now().timestamp() + 1) # channel id is not a valid channel with pytest.raises(InputError): message_sendlater_v1(make_token(1), 10, "hello", datetime.datetime.now().timestamp() + 1) # message is more than 1000 characters thousand_string = '' for i in range(1005): thousand_string += str(i) with pytest.raises(InputError): message_sendlater_v1(make_token(2), 0, thousand_string, datetime.datetime.now().timestamp() + 1) # time sent is a time in the past with pytest.raises(InputError): message_sendlater_v1(make_token(2), 0, "hello", datetime.datetime.now().timestamp() - 5) # authorised user has not joined the channel they are trying to post to with pytest.raises(AccessError): message_sendlater_v1(make_token(1), 2, "hello", datetime.datetime.now().timestamp() + 1)
def test_time_past(reg_user, crt_channel, message_text): clear_v2() user_token = reg_user(0)['token'] channel_id = crt_channel(user_token)['channel_id'] message = message_text(0) dt = datetime.now(timezone.utc) - timedelta(hours=9, minutes=50) time_sent = dt.replace(tzinfo=timezone.utc).timestamp() with pytest.raises(InputError): message_sendlater_v1(user_token, channel_id, message, time_sent)
def test_long_message(reg_user, crt_channel, message_text): clear_v2() user_token = reg_user(0)['token'] channel_id = crt_channel(user_token)['channel_id'] message = 'a' * 1001 dt = datetime.now(timezone.utc) + timedelta(hours=0, minutes=0, seconds=1) time_sent = dt.replace(tzinfo=timezone.utc).timestamp() with pytest.raises(InputError): message_sendlater_v1(user_token, channel_id, message, time_sent)
def test_message_sendlater_invalid_channel_id(): # Test if InputError is raised when given invalid channel id clear_v1() user = auth_register_v2('*****@*****.**', 'password123', 'Terry', 'Wills') channels_create_v2(user['token'], 'TERRYS CHANNEL', True) time = datetime.now().replace(microsecond=0) + timedelta(0, 3) time_sent = time.timestamp() with pytest.raises(InputError): message_sendlater_v1(user['token'], -1, 'anyone here?', time_sent)
def test_message_sendlater_invalid_time_sent(): # InputError raised if time sent is set to time in the past clear_v1() user = auth_register_v2('*****@*****.**', 'pasword24323132', 'Johnny', 'Silverfinger') channel = channels_create_v2(user['token'], 'Insert Title Here', True) time = datetime.now().replace(microsecond=0) - timedelta(0, 3) time_sent = time.timestamp() with pytest.raises(InputError): message_sendlater_v1(user['token'], channel['channel_id'], 'yo guys', time_sent)
def test_invalid_token(reg_user, crt_channel, message_text): clear_v2() user_token = reg_user(0)['token'] channel_id = crt_channel(user_token)['channel_id'] message = message_text(0) dt = datetime.now(timezone.utc) + timedelta(hours=0, minutes=0, seconds=1) time_sent = dt.replace(tzinfo=timezone.utc).timestamp() with pytest.raises(AccessError): message_sendlater_v1("Invalid token", channel_id, message, time_sent)
def test_message_sendlater_invalid_token(): # Test if AccessError is raised when given invalid token clear_v1() user = auth_register_v2('*****@*****.**', 'password1234', 'Harry', 'Lurr') channel = channels_create_v2(user['token'], 'HARRYS CHANNEL', True) time = datetime.now().replace(microsecond=0) + timedelta(0, 3) time_sent = time.timestamp() with pytest.raises(AccessError): message_sendlater_v1(-2382732, channel['channel_id'], 'Poopoo', time_sent)
def test_message_sendlater_message_too_long(): # InputError raised if message given is more than 1000 characters clear_v1() user = auth_register_v2('*****@*****.**', 'pasword24323132', 'Tinker', 'Bell') channel = channels_create_v2(user['token'], 'Insert Title Here', True) time = datetime.now().replace(microsecond=0) + timedelta(0, 3) time_sent = time.timestamp() with pytest.raises(InputError): message_sendlater_v1(user['token'], channel['channel_id'], 'L' * 1001, time_sent)
def test_tag_sendlater(): clear_v1() user1 = auth_register_v2("*****@*****.**", "finalpassword", "Eric", "Zheng") user2 = auth_register_v2("*****@*****.**", "validpassword", "Josh", "Hatton") channel1 = channels_create_v2(user1['token'], 'Channel 1', True) channel_join_v2(user2['token'], channel1['channel_id']) msg1str = "@joshhatton sup" current_time = int(time.time()) message_sendlater_v1(user1['token'], channel1['channel_id'], msg1str, current_time + 2) assert len(notifications_get_v1(user2['token'])['notifications']) == 0 time.sleep(3) assert len(notifications_get_v1(user2['token'])['notifications']) == 1
def message_sendlater(): data = request.get_json() token = data["token"] channel_id = data["channel_id"] message = data["message"] time_sent = data["time_sent"] return dumps(message_sendlater_v1(token, channel_id, message, time_sent))
def test_message_sendlater_user_not_in_channel(): # Test if AccessError is raised when user tries to leave a group # that they are not a member of clear_v1() user_owner = auth_register_v2('*****@*****.**', 'password2321', 'Irwin', 'Dundee') user = auth_register_v2('*****@*****.**', 'password2321232', 'Roland', 'Tiddle') channel = channels_create_v2(user_owner['token'], 'Irwins Channel', True) time = datetime.now().replace(microsecond=0) + timedelta(0, 3) time_sent = time.timestamp() with pytest.raises(AccessError): message_sendlater_v1(user['token'], channel['channel_id'], 'can i join pls', time_sent)
def test_mixed_success(create_input): current_time = int(time.time()) message_send_v2(create_input[0][0]["token"], create_input[1][0]["channel_id"], "This is the first normal message") assert len( channel_messages_v2(create_input[0][0]["token"], create_input[1][0]["channel_id"], 0)["messages"]) == 1 msg = message_sendlater_v1(create_input[0][1]["token"], create_input[1][0]["channel_id"], "Hey there, I'm from the past!", current_time + 2) assert type(msg) == dict assert type(msg["message_id"]) == int message_send_v2(create_input[0][2]["token"], create_input[1][0]["channel_id"], "This is the second normal message") # Make sure delayed message has not been sent yet msgs = channel_messages_v2(create_input[0][1]["token"], create_input[1][0]["channel_id"], 0)["messages"] assert len(msgs) == 2 # Wait 2.5 seconds and check again; delayed message should be there time.sleep(2.5) msgs = channel_messages_v2(create_input[0][1]["token"], create_input[1][0]["channel_id"], 0)["messages"] assert len(msgs) == 3 assert msgs[0]["message"] == "Hey there, I'm from the past!" assert msgs[0]["time_created"] == current_time + 2 assert msgs[1]["message"] == "This is the second normal message" assert msgs[1]["time_created"] == current_time assert msgs[2]["message"] == "This is the first normal message" assert msgs[2]["time_created"] == current_time
def test_message_sendlater_sendlaterdm(user_setup, dm_setup, channel_setup): assert message_sendlater_v1(make_token(2), 0, "Hello", datetime.datetime.now().timestamp() + 1) == { "message_id": 0 } assert message_sendlater_v1(make_token(2), 0, "Hi", datetime.datetime.now().timestamp() + 1) == { "message_id": 1 } assert message_sendlaterdm_v1(make_token(2), 0, "Hello", datetime.datetime.now().timestamp() + 1) == { "message_id": 2 } assert message_sendlaterdm_v1(make_token(2), 0, "Hi", datetime.datetime.now().timestamp() + 1) == { "message_id": 3 }
def test_common(reg_user, crt_channel, message_text): clear_v2() token = reg_user(0)['token'] channel_id = crt_channel(token)['channel_id'] dt = datetime.now(timezone.utc) + timedelta(hours=0, minutes=0, seconds=1) time_sent = dt.replace(tzinfo=timezone.utc).timestamp() # message_id = message_sendlater_v1(token, channel_id, message_text(0), time_sent)['message_id'] # message = channel_messages_v2(user_token, channel_id, 0)['messages'][0] # assert message['message_id'] == message_id # assert message['message'] == message_text(0) assert message_sendlater_v1(token, channel_id, message_text(0), time_sent)['message_id'] == 0 prev_message_id = message_send_v2(token, channel_id, message_text(1))['message_id'] assert message_sendlater_v1(token, channel_id, message_text(2), time_sent)['message_id'] == prev_message_id + 1
def message_sendlater(): parameters = request.get_json() token = parameters['token'] channel_id = parameters['channel_id'] message = parameters['message'] time_sent = parameters['time_sent'] output = message_sendlater_v1(token, channel_id, message, time_sent) message_id = output['message_id'] standup_thread = threading.Thread(target=sendlater_handler, args=[message_id, time_sent]) standup_thread.start() return dumps(output)
def message_sendlater(): info = request.get_json() token = info["token"] channel_id = info["channel_id"] message = info["message"] time_sent = info["time_sent"] message_sendlater = message_sendlater_v1(token, channel_id, message, time_sent) return dumps(message_sendlater)
def test_message_sendlater_valid_case(): clear_v1() user = auth_register_v2('*****@*****.**', 'pasword24323132', 'Terrence', 'Andrews') channel = channels_create_v2(user['token'], 'Title', True) # 3 seconds time = datetime.now().replace(microsecond=0) + timedelta(0, 3) time_sent = time.timestamp() # 5 seconds time_later = datetime.now().replace(microsecond=0) + timedelta(0, 5) time_sent_later = time_later.timestamp() def test_sent_empty(message): messages = search_v2(user['token'], message) assert messages == {'messages': []} # Check if messages is empty 2 seconds after the first message_sendlater has been called timer = threading.Timer(2, test_sent_empty, args=('Hey Guys!')) timer.start() def test_sent_later_empty(): messages = search_v2(user['token'], '2 seconds later') assert messages == {'messages': []} # Check 2nd message has been sent yet timer = threading.Timer(2, test_sent_later_empty) timer.start() msg = message_sendlater_v1(user['token'], channel['channel_id'], "Hey Guys!", time_sent) msg_search = search_v2(user['token'], 'Hey Guys!') assert msg_search['messages'][0]['message_id'] == msg['message_id'] assert msg_search['messages'][0]['time_created'] == time_sent # Second message sent msg_later = message_sendlater_v1(user['token'], channel['channel_id'], "2 seconds later", time_sent_later) msg_search = search_v2(user['token'], '2 seconds later') assert msg_search['messages'][0]['message_id'] == msg_later['message_id'] assert msg_search['messages'][0]['time_created'] == time_sent_later
def test_success_no_delay(create_input): assert channel_messages_v2(create_input[0][0]["token"], create_input[1][0]["channel_id"], 0)["messages"] == [] current_time = int(time.time()) msg = message_sendlater_v1(create_input[0][0]["token"], create_input[1][0]["channel_id"], "Hey there!", current_time) assert type(msg) == dict assert type(msg["message_id"]) == int # Message should have been sent already msgs = channel_messages_v2(create_input[0][0]["token"], create_input[1][0]["channel_id"], 0)["messages"] assert len(msgs) == 1 assert msgs[0]["message"] == "Hey there!" assert msgs[0]["time_created"] == current_time
def test_simple_success(create_input): assert channel_messages_v2(create_input[0][0]["token"], create_input[1][0]["channel_id"], 0)["messages"] == [] current_time = int(time.time()) msg = message_sendlater_v1(create_input[0][0]["token"], create_input[1][0]["channel_id"], "Hey there!", current_time + 2) assert type(msg) == dict assert type(msg["message_id"]) == int # Make sure message has not been sent yet assert channel_messages_v2(create_input[0][0]["token"], create_input[1][0]["channel_id"], 0)["messages"] == [] # Wait 2.5 seconds and check again; message should be there time.sleep(2.5) msgs = channel_messages_v2(create_input[0][0]["token"], create_input[1][0]["channel_id"], 0)["messages"] assert len(msgs) == 1 assert msgs[0]["message"] == "Hey there!" assert msgs[0]["time_created"] == current_time + 2
def test_time_sent_past(create_input): with pytest.raises(InputError): message_sendlater_v1(create_input[0][1]["token"], create_input[1][1]["channel_id"], "Hey from the future!", int(time.time()) - 20)
def test_multiple_success(create_input): message_send_v2(create_input[0][0]["token"], create_input[1][0]["channel_id"], "This is the first normal message") assert len( channel_messages_v2(create_input[0][0]["token"], create_input[1][0]["channel_id"], 0)["messages"]) == 1 current_time = int(time.time()) msg = message_sendlater_v1(create_input[0][1]["token"], create_input[1][0]["channel_id"], "Hey there, I'm from the past!", current_time + 2) assert type(msg) == dict assert type(msg["message_id"]) == int msg = message_sendlater_v1(create_input[0][2]["token"], create_input[1][0]["channel_id"], "Hey, I'm from the distant past!", current_time + 4) assert type(msg) == dict assert type(msg["message_id"]) == int msg = message_sendlater_v1(create_input[0][0]["token"], create_input[1][0]["channel_id"], "Hey there, I'm from the past past!!", current_time + 3) assert type(msg) == dict assert type(msg["message_id"]) == int message_send_v2(create_input[0][2]["token"], create_input[1][0]["channel_id"], "This is the second normal message") # Make sure delayed message has not been sent yet assert len( channel_messages_v2(create_input[0][0]["token"], create_input[1][0]["channel_id"], 0)["messages"]) == 2 # Wait 2.5 seconds and check again; first delayed message should be there time.sleep(2.5) msgs = channel_messages_v2(create_input[0][1]["token"], create_input[1][0]["channel_id"], 0)["messages"] assert len(msgs) == 3 assert msgs[0]["message"] == "Hey there, I'm from the past!" assert msgs[0]["time_created"] == current_time + 2 assert msgs[1]["message"] == "This is the second normal message" assert msgs[1]["time_created"] == current_time assert msgs[2]["message"] == "This is the first normal message" assert msgs[2]["time_created"] == current_time # Wait 1 second; second delayed message should be there time.sleep(1) msgs = channel_messages_v2(create_input[0][1]["token"], create_input[1][0]["channel_id"], 0)["messages"] assert len(msgs) == 4 assert msgs[0]["message"] == "Hey there, I'm from the past past!!" assert msgs[0]["time_created"] == current_time + 3 assert msgs[1]["message"] == "Hey there, I'm from the past!" assert msgs[1]["time_created"] == current_time + 2 assert msgs[2]["message"] == "This is the second normal message" assert msgs[2]["time_created"] == current_time assert msgs[3]["message"] == "This is the first normal message" assert msgs[3]["time_created"] == current_time # Wait 1 more seconds; final delayed message should be there time.sleep(1) msgs = channel_messages_v2(create_input[0][1]["token"], create_input[1][0]["channel_id"], 0)["messages"] assert len(msgs) == 5 assert msgs[0]["message"] == "Hey, I'm from the distant past!" assert msgs[0]["time_created"] == current_time + 4 assert msgs[1]["message"] == "Hey there, I'm from the past past!!" assert msgs[1]["time_created"] == current_time + 3 assert msgs[2]["message"] == "Hey there, I'm from the past!" assert msgs[2]["time_created"] == current_time + 2 assert msgs[3]["message"] == "This is the second normal message" assert msgs[3]["time_created"] == current_time assert msgs[4]["message"] == "This is the first normal message" assert msgs[4]["time_created"] == current_time
def test_invalid_channel(create_input): with pytest.raises(InputError): message_sendlater_v1(create_input[0][0]["token"], 123, "This message won't be sent", int(time.time()) + 2)
def test_message_too_long(create_input): msg = 'x' * 1001 with pytest.raises(InputError): message_sendlater_v1(create_input[0][1]["token"], create_input[1][1]["channel_id"], msg, int(time.time()) + 2)
def test_not_member(create_input): with pytest.raises(AccessError): message_sendlater_v1(create_input[0][4]["token"], create_input[1][0]["channel_id"], "I'm not in this channel :(", int(time.time()) + 2)
def test_invalid_token(create_input): with pytest.raises(AccessError): message_sendlater_v1(123, create_input[1][1]["channel_id"], "I don't exist :(((", int(time.time()) + 2)