def test_message_sendlater_invalid2(): '''User 1 sent a message 1 second before''' clear() # Register user 0 user0_info = auth_register("*****@*****.**", "ihfeh3hgi00d", "Yilang", "W") # Register user 1 user1_info = auth_register("*****@*****.**", "VukkFs", "Bill", "Gates") # Register user 2 user2_info = auth_register("*****@*****.**", "RFVtgb45678", "M", "Johnson") # User 0 create a channel channel0_info = channels_create(user0_info['token'], "channel0", True) # User 1 join the channel channel_join(user1_info['token'], channel0_info['channel_id']) # User 1 create another channel channel1_info = channels_create(user1_info['token'], "channel1", True) # User 2 join channel1 channel_join(user2_info['token'], channel1_info['channel_id']) # User 0 join channel1 channel_join(user0_info['token'], channel1_info['channel_id']) # User 2 send a message message_send(user2_info['token'], channel1_info['channel_id'], "Hello") # USer 1 send a message 1 second before one_sec_before = datetime.datetime.now() - datetime.timedelta(seconds=1) timestamp = int(datetime.datetime.timestamp(one_sec_before)) with pytest.raises(InputError): message_sendlater(user1_info['token'], channel1_info['channel_id'], "Hi", timestamp)
def test_message_sendlater_valid2(): '''User 1 sent a message "'H' * 1000" 0 second later''' clear() # Register user 0 user0_info = auth_register("*****@*****.**", "ihfeh3hgi00d", "Yilang", "W") # Register user 1 user1_info = auth_register("*****@*****.**", "VukkFs", "Bill", "Gates") # Register user 2 user2_info = auth_register("*****@*****.**", "RFVtgb45678", "M", "Johnson") # User 0 create a channel channel0_info = channels_create(user0_info['token'], "channel0", True) # User 1 join the channel channel_join(user1_info['token'], channel0_info['channel_id']) # User 1 create another channel channel1_info = channels_create(user1_info['token'], "channel1", True) # User 2 join channel1 channel_join(user2_info['token'], channel1_info['channel_id']) # User 0 join channel1 channel_join(user0_info['token'], channel1_info['channel_id']) # User 2 send a message message_send(user2_info['token'], channel1_info['channel_id'], "Hello") # USer 1 send a message 0 second later with invalid channel_id zero_sec_later = datetime.datetime.now() timestamp = int(datetime.datetime.timestamp(zero_sec_later)) message_sendlater(user1_info['token'], channel1_info['channel_id'], "H" * 1000, timestamp) # User 1 get the message info message_info = channel_messages(user1_info['token'], channel1_info['channel_id'], 0) assert message_info['messages'][1]['message'] == "H" * 1000
def test_message_sendlater_invalid1(): "User 1 sent a message that is over 1000 letters 0 second later in a channel" clear() # Register user 0 user0_info = auth_register("*****@*****.**", "ihfeh3hgi00d", "Yilang", "W") # Register user 1 user1_info = auth_register("*****@*****.**", "VukkFs", "Bill", "Gates") # Register user 2 user2_info = auth_register("*****@*****.**", "RFVtgb45678", "M", "Johnson") # User 0 create a channel channel0_info = channels_create(user0_info['token'], "channel0", True) # User 1 join the channel channel_join(user1_info['token'], channel0_info['channel_id']) # User 1 create another channel channel1_info = channels_create(user1_info['token'], "channel1", True) # User 2 join channel1 channel_join(user2_info['token'], channel1_info['channel_id']) # User 0 join channel1 channel_join(user0_info['token'], channel1_info['channel_id']) # User 2 send a message message_send(user2_info['token'], channel1_info['channel_id'], "Hello") # USer 1 send a message 0 second later with invalid channel_id zero_sec_later = datetime.datetime.now() timestamp = int(datetime.datetime.timestamp(zero_sec_later)) with pytest.raises(InputError): message_sendlater(user1_info['token'], channel1_info, "H" * 1001, timestamp)
def test_message_sendlater_more_channels(): _, wenyao_dict, _, channel_team1, _ = initialise_data() channel_team2 = channels_create(wenyao_dict['token'], "team2", True) time_sent = int(time.time()) time_sent += 3 message_sendlater(wenyao_dict['token'], channel_team1['channel_id'], "hi there1", time_sent) message_sendlater(wenyao_dict['token'], channel_team2['channel_id'], "hi there2", time_sent) # check the return value from messages details messages_detail1 = channel_messages(wenyao_dict['token'], channel_team1['channel_id'], 0) messages_detail2 = channel_messages(wenyao_dict['token'], channel_team2['channel_id'], 0) assert len(messages_detail1['messages']) == 0 assert len(messages_detail2['messages']) == 0 # sleep for 5s time.sleep(5) messages_detail1 = channel_messages(wenyao_dict['token'], channel_team1['channel_id'], 0) messages_detail2 = channel_messages(wenyao_dict['token'], channel_team2['channel_id'], 0) assert len(messages_detail1['messages']) == 1 assert len(messages_detail2['messages']) == 1 assert messages_detail1['messages'][0]['message'] == "hi there1" assert messages_detail2['messages'][0]['message'] == "hi there2"
def test_message_sendlater_valid1(): "User 1 sent a message 1 second later" clear() # Register user 0 user0_info = auth_register("*****@*****.**", "ihfeh3hgi00d", "Yilang", "W") # Register user 1 user1_info = auth_register("*****@*****.**", "VukkFs", "Bill", "Gates") # Register user 2 user2_info = auth_register("*****@*****.**", "RFVtgb45678", "M", "Johnson") # User 0 create a channel channel0_info = channels_create(user0_info['token'], "channel0", True) # User 1 join the channel channel_join(user1_info['token'], channel0_info['channel_id']) # User 1 create another channel channel1_info = channels_create(user1_info['token'], "channel1", True) # User 2 join channel1 channel_join(user2_info['token'], channel1_info['channel_id']) # User 0 join channel1 channel_join(user0_info['token'], channel1_info['channel_id']) # User 2 send a message message_send(user2_info['token'], channel1_info['channel_id'], "Hello") # USer 1 send a message 1 second later one_sec_later = datetime.datetime.now() + datetime.timedelta(seconds=1) timestamp = int(datetime.datetime.timestamp(one_sec_later)) message_sendlater(user1_info['token'], channel1_info['channel_id'], "Hi", timestamp) # User 0 get the info of messages 1 sec later time.sleep(2) message_info = channel_messages(user0_info['token'], channel1_info['channel_id'], 0) assert message_info['messages'][1]['message'] == "Hi"
def test_message_sendlater_invalid_member(): boyu_dict, _, _, channel_team1, _ = initialise_data() time_sent = int(time.time()) time_sent += 5 with pytest.raises(AccessError): message_sendlater(boyu_dict['token'], channel_team1['channel_id'], "hi there", time_sent)
def test_message_sendlater_invalid_time(): _, wenyao_dict, _, channel_team1, _ = initialise_data() time_sent = int(time.time()) time_sent -= 5 with pytest.raises(InputError): message_sendlater(wenyao_dict['token'], channel_team1['channel_id'], "hi there", time_sent)
def test_sendlater_single(): clear() # setup user = register_n_users(1) channel_id = channels_create(user["token"], "name", is_public=True)["channel_id"] # send message with delay send_at = int(round(time.time() + 1 * DELAY_SCALE)) message_sendlater(user["token"], channel_id, "message", send_at) # make sure there are now messages messages = channel_messages(user["token"], channel_id, start=0) assert int(round(time.time())) < send_at, ERR_TOO_SLOW assert len(messages["messages"]) == 0 # wait for the message to be sent time.sleep(2) # check that the message is there messages = channel_messages(user["token"], channel_id, start=0) assert len(messages["messages"]) == 1 assert messages["messages"][0]["message"] == "message" # if this fails, there has been a time difference of more than half a second # time.time() is a float, and floats shouldn't be compared with == assert messages["messages"][0]["time_created"] - send_at <= 1
def test_message_sendlater_invalid3(): '''the authorised user has not joined the channel they are trying to post to''' clear() user0_info = auth_register("*****@*****.**", "ihfeh3hgi00d", "Yilang", "W") # Register user 1 user1_info = auth_register("*****@*****.**", "VukkFs", "Bill", "Gates") # Register user 2 user2_info = auth_register("*****@*****.**", "RFVtgb45678", "M", "Johnson") # User 0 create a channel channel0_info = channels_create(user0_info['token'], "channel0", True) # User 1 join the channel channel_join(user1_info['token'], channel0_info['channel_id']) # User 1 create another channel channel1_info = channels_create(user1_info['token'], "channel1", True) # User 2 join channel1 channel_join(user2_info['token'], channel1_info['channel_id']) # User 2 send a message message_send(user2_info['token'], channel1_info['channel_id'], "Hello") # USer 1 send a message 0 second later zero_sec_later = datetime.datetime.now() timestamp = int(datetime.datetime.timestamp(zero_sec_later)) with pytest.raises(AccessError): message_sendlater(user0_info['token'], channel1_info['channel_id'], "Hi", timestamp)
def test_message_sendlater_exactly1000chars(): clear() test_user_0 = create_one_test_user() channel_name1 = channels_create(test_user_0['token'], "Main Channel", True) message_sendlater(test_user_0['token'], channel_name1['channel_id'], message2, (datetime.now()).timestamp()+1) sleep(1.5) assert message_send(test_user_0['token'], channel_name1['channel_id'], message2) == {'message_id': 1}
def test_sendlater_invalid_token(): clear() user = register_n_users(1) channel_id = channels_create(user["token"], "name", is_public=True)["channel_id"] with pytest.raises(AccessError): message_sendlater(-1, channel_id, "message", time.time() + 2)
def test_message_sendlater_base(): ''' Sees if valid message is sent after 10 seconds ''' clear() user1 = auth_register("*****@*****.**", "user1pass", "user1", "last1", None) token1 = user1['token'] ch_id1 = channels_create(token1, "FirstChannel", True)['channel_id'] moment = datetime.now() stamp_now = datetime.timestamp(moment) + 10 message_sendlater(token1, ch_id1, "Hello World!", stamp_now) # Sleep to let the message be sent after a delay sleep(10.9) for channel in channels: if channel['channel_id'] == ch_id1: assert len(channel['messages']) == 1 break clear()
def test_message_send_later_invalid_token(): """Test passing an invalid token to message_sendlater""" clear() valid_user = auth_register("*****@*****.**", "michaelpennnis2131", "Borat", "Me") new_channel = channels_create(valid_user["token"], "Non Cool Kids Club", False) with pytest.raises(AccessError): message_sendlater(2154, new_channel["channel_id"], "VERY NICE", \ (time.time() + 5))
def test_message_sendlater_long_message(): _, wenyao_dict, _, channel_team1, _ = initialise_data() time_sent = int(time.time()) time_sent += 5 letters = string.ascii_lowercase message_str = ''.join(random.choice(letters) for i in range(1001)) with pytest.raises(InputError): message_sendlater(wenyao_dict['token'], channel_team1['channel_id'], message_str, time_sent)
def test_message_send_later_invalid_channel_id(): """Tests that an InputError is raised when given channel_id is not a valid channel""" clear() valid_user = auth_register("*****@*****.**", "michaelpennnis2131", "Borat", "Me") with pytest.raises(InputError): message_sendlater(valid_user["token"], 420, "VERY NICE", \ (time.time() + 5))
def test_message_send_later_message_too_long(): """Tests that an InputError is raised when given message is more than 1000 characters long""" clear() valid_user = auth_register("*****@*****.**", "michaelpennnis2131", "Borat", "Me") with pytest.raises(InputError): message_sendlater(valid_user["token"], 420, "Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!Very nice!!", \ (time.time() + 5))
def test_invalid_channel(): reset_data() owner = auth.auth_register("*****@*****.**", "hellocse1", "Sinha", "Nawa") channels.channels_create(owner['token'], "New Channel", True) d_t = datetime.now() timestamp = d_t.replace(tzinfo=timezone.utc).timestamp() with pytest.raises(InputError): message.message_sendlater(owner['token'], 2, "Test", timestamp + 1000)
def test_message_sendlater_in_past(): reset_data() owner = auth.auth_register("*****@*****.**", "123456", "Sinha", "Nawa") channels.channels_create(owner['token'], "New Channel", True) d_t = datetime.utcnow() timestamp = d_t.replace(tzinfo=timezone.utc).timestamp() with pytest.raises(InputError): message.message_sendlater(owner['token'], 1, "Past", timestamp - 1000000)
def test_message_send_later_sent_time_is_in_past(): """Tests that an InputError is raised when given time to send is a value in the past""" clear() valid_user = auth_register("*****@*****.**", "michaelpennnis2131", "Borat", "Me") new_channel = channels_create(valid_user["token"], "Non Cool Kids Club", False) with pytest.raises(InputError): message_sendlater(valid_user["token"], new_channel["channel_id"], "VERY NICE", \ 46860)
def test_message_sendlater_in_another_channel(): reset_data() owner = auth.auth_register("*****@*****.**", "123456", "Sinha", "Nawa") member = auth.auth_register("*****@*****.**", "hellocse", "jiaqi", "zhu") channels.channels_create(owner['token'], "New Channel", True) d_t = datetime.now() timestamp = d_t.replace(tzinfo=timezone.utc).timestamp() with pytest.raises(AccessError): message.message_sendlater(member['token'], 1, "Test", timestamp + 1000)
def test_message_send_later_authorized_user_not_in_channel(): """Tests that an AccessError is raised when the authorised user has not joined the channel they are trying to post to""" clear() valid_user = auth_register("*****@*****.**", "michaelpennnis2131", "Borat", "Me") valid_user_1 = auth_register("*****@*****.**", "michaelpennnis21311", "Borat1", "Me1") new_channel = channels_create(valid_user["token"], "Non Cool Kids Club", False) with pytest.raises(AccessError): message_sendlater(valid_user_1["token"], new_channel["channel_id"], "VERY NICE", \ (time.time() + 5))
def test_sendlater_invalid_message(): clear() user = register_n_users(1) channel_id = channels_create(user["token"], "name", is_public=True)["channel_id"] message_sendlater(user["token"], channel_id, "m" * 1000, time.time() + 2) with pytest.raises(InputError): message_sendlater(user["token"], channel_id, "m" * 1001, time.time() + 2)
def test_message_sendlater_invalid_channel(): ''' tests the message_sendlater functionality with invalid channel ids ''' other.clear() send_time = int(time.time()) + 10 owner = auth.auth_register("*****@*****.**", "password", "Homer", "Simpson") with pytest.raises(error.InputError): message.message_sendlater(owner['token'], 1, 'this is the first test message', send_time)
def test_message_sendlater_invalid_time(): ''' tests the message_sendlater functionality ''' other.clear() send_time = int(time.time()) - 1000 owner = auth.auth_register("*****@*****.**", "password", "Homer", "Simpson") channel_1 = channels.channels_create(owner['token'], "channel_1", True) with pytest.raises(error.InputError): message.message_sendlater(owner['token'], channel_1['channel_id'], 'this is the first test message', send_time)
def test_message_sendlater_i_c(): """ test whether the invalid channel_id """ # Asumm the negative number is a invalided channel_id user_infor = auth_register("*****@*****.**", "ccc337992611", "Min", "Li") time_sent_date = datetime.datetime.now().replace( microsecond=0) + datetime.timedelta(0, 5) time_sent = time_sent_date.timestamp() with pytest.raises(InputError): message_sendlater(user_infor['token'], -1, 'a' * 99, time_sent)
def test_message_sendlater_in_order(): clear() test_user_0, test_user_1 = create_two_test_users() channel_name1 = channels_create(test_user_0['token'], "Main Channel", True) channel_join(test_user_1['token'],channel_name1['channel_id']) message_sendlater(test_user_1['token'], channel_name1['channel_id'], message1, (datetime.now()).timestamp()+2) message_send(test_user_1['token'], channel_name1['channel_id'], message1) message_send(test_user_0['token'], channel_name1['channel_id'], message1) message_sendlater(test_user_0['token'], channel_name1['channel_id'], message1, (datetime.now()).timestamp()+4) sleep(2.5) assert message_send(test_user_1['token'], channel_name1['channel_id'], message1) == {'message_id': 3} sleep(2.5) assert message_send(test_user_1['token'], channel_name1['channel_id'], message1) == {'message_id': 5}
def test_message_sendlater_message_too_long(): ''' tests that message_sendlater rejects messages longer than 1000 chars ''' other.clear() send_time = int(time.time()) + 10 message_for_sending = 'a' * 1001 owner = auth.auth_register("*****@*****.**", "password", "Homer", "Simpson") channel_1 = channels.channels_create(owner['token'], "channel_1", True) with pytest.raises(error.InputError): message.message_sendlater(owner['token'], channel_1['channel_id'], message_for_sending, send_time)
def test_message_sendlater_in_pass(): """ Test whether set the time to send in the pass will raise InputError. """ user_infor = auth_register("*****@*****.**", "ccc337992611", "Min", "Li") channel_infor = channels_create(user_infor['token'], 'test_one', True) time_sent_date = datetime.datetime.now().replace( microsecond=0) - datetime.timedelta(0, 100) time_sent = time_sent_date.timestamp() with pytest.raises(InputError): message_sendlater(user_infor['token'], channel_infor['channel_id'], 'a' * 100, time_sent)
def test_message_sendlater_too_long(): """ test whether the 1001 len string input will raise InputError """ user_infor = auth_register("*****@*****.**", "ccc337992611", "Min", "Li") channel_infor = channels_create(user_infor['token'], 'test_one', True) time_sent_date = datetime.datetime.now().replace( microsecond=0) + datetime.timedelta(0, 5) time_sent = time_sent_date.timestamp() with pytest.raises(InputError): message_sendlater(user_infor['token'], channel_infor['channel_id'], 'a' * 1001, time_sent)
def test_message_sendlater_data(): _, wenyao_dict, _, channel_team1, _ = initialise_data() time_sent = int(time.time()) time_sent += 3 message_sendlater(wenyao_dict['token'], channel_team1['channel_id'], "hi there", time_sent) # check the return value from messages details messages_detail = channel_messages(wenyao_dict['token'], channel_team1['channel_id'], 0) assert len(messages_detail['messages']) == 0 # sleep for 5s time.sleep(5) messages_detail = channel_messages(wenyao_dict['token'], channel_team1['channel_id'], 0) assert len(messages_detail['messages']) == 1 assert messages_detail['messages'][0]['message'] == "hi there"