def test_invalid_start_message(): #get stoken token = A['token'] #get invalid channelID channelID = Pub_channel['channel_id'] #start start = None with pytest.raises(TypeError): CF.channel_messages(token, channelID, start)
def test_invalid_id_message(): #get stoken token = A['token'] #get invalid channelID channelID = None #start start = '0' with pytest.raises(TypeError): CF.channel_messages(token, channelID, start)
def test_channel_ID(): #get stoken token = A['token'] auth_login("*****@*****.**", 'HoyaLee2019') #get channelID channelID = '6509779080987' #start start = '0' with pytest.raises(CHF.ValueError): CF.channel_messages(token, channelID, start)
def test_invalid_token_message(): #get the invalid token token = A['token'] + '10081' #get channelID channelID = Pub_channel['channel_id'] #start start = '0' with pytest.raises(TF.AccessError): CF.channel_messages(token, channelID, start)
def test_message_too_big(): reset_data() A = auth_register("*****@*****.**", 'HoyaLee2019', 'Hoya', 'Lee') Pub_channel = CF.channels_create(A['token'], 'Num0', 'True') #get token token = A['token'] #get channelID channelID = Pub_channel['channel_id'] #start start = '100' with pytest.raises(CF.ValueError): CF.channel_messages(token, channelID, start)
def test_Not_login(): #get token token = A['token'] #logout auth_logout(token) #get channelID channelID = Pub_channel['channel_id'] #start start = '0' with pytest.raises(CF.AccessError): CF.channel_messages(token, channelID, start)
def channel_messages(): """ Returns up to 50 messages from a channel """ token = request.args.get("token") channel_id = to_int(request.args.get("channel_id")) start = to_int(request.args.get("start")) return dumps(channel.channel_messages(token, channel_id, start))
def test_message_succ(): reset_data() A = auth_register("*****@*****.**", 'HoyaLee2019', 'Hoya', 'Lee') Pub_channel = CF.channels_create(A['token'], 'Num0', 'True') #get token token = A['token'] #get channelID channelID = Pub_channel['channel_id'] #start start = '0' assert(CF.channel_messages(token, channelID, start) == {'end': -1, 'messages': [], 'start': 0})
def test_channel_messages(): ''' Function tests for channel_messages ''' #Initialisation global_var.initialise_all() user1 = auth_register("*****@*****.**", "passworuser.d", \ "channel_messages", "test") user_id1 = user1["u_id"] token1 = user1["token"] user2 = auth_register("*****@*****.**", "password", \ "channel_messages2", "test2") token2 = user2["token"] #create a channel channel = func.channels_create(token1, "TestChannel", True) channel_id = channel["channel_id"] channel2 = func.channels_create(token1, "TestChannel2", True) channel_id2 = channel2["channel_id"] # Initialisation finished # Start index is invalid as there are no message with pytest.raises(ValueError, match="Start index is invalid"): func.channel_messages(token1, channel_id, 1) # send a message to the channel and check that return is correct assert message_send(token1, channel_id, '1 message') == {"message_id": 0} messages = func.channel_messages(token1, channel_id, 0) assert messages["start"] == 0 assert messages["end"] == -1 assert messages["messages"][0]["message_id"] == 0 assert messages["messages"][0]["u_id"] == user1["u_id"] assert messages["messages"][0]["message"] == "1 message" assert not messages["messages"][0]["is_pinned"] assert messages["messages"][0]["reacts"] == [{ "react_id": LIKE_REACT, "u_ids": [], "is_this_user_reacted": False }, { "react_id": HEART_REACT, "u_ids": [], "is_this_user_reacted": False }] # Check details return is changed when the message is reacted and pinned message_react(token1, 0, 1) message_pin(token1, 0) messages = func.channel_messages(token1, channel_id, 0) assert messages["messages"][0]["reacts"] == [{ "react_id": LIKE_REACT, "u_ids": [0], "is_this_user_reacted": True }, { "react_id": HEART_REACT, "u_ids": [], "is_this_user_reacted": False }] assert messages["messages"][0]["is_pinned"] # send a message to the channel and check that return is correct message_send(token1, channel_id, '2 message') messages = func.channel_messages(token1, channel_id, 0) assert messages["messages"][0]["message"] == "2 message" assert messages["messages"][1]["message"] == "1 message" # A total of 50 messages are sent for i in range(3, 51): message_send(token1, channel_id, f'{i} message') messages = func.channel_messages(token1, channel_id, 0) assert messages["start"] == 0 assert messages["end"] == -1 assert messages["messages"][0]["message"] == "50 message" assert messages["messages"][49]["message"] == "1 message" messages = func.channel_messages(token1, channel_id, 1) assert messages["start"] == 1 assert messages["end"] == -1 assert messages["messages"][0]["message"] == "49 message" assert messages["messages"][48]["message"] == "1 message" for i in range(0, 125): message_send(token1, channel_id2, f'{i}') messages = func.channel_messages(token1, channel_id2, 0) assert messages["start"] == 0 assert messages["end"] == 50 assert messages["messages"][0]["message"] == "124" assert messages["messages"][49]["message"] == "75" messages = func.channel_messages(token1, channel_id2, 50) assert messages["start"] == 50 assert messages["end"] == 100 assert messages["messages"][0]["message"] == "74" assert messages["messages"][49]["message"] == "25" messages = func.channel_messages(token1, channel_id2, 100) assert messages["start"] == 100 assert messages["end"] == -1 assert messages["messages"][0]["message"] == "24" assert messages["messages"][24]["message"] == "0" # Test exceptions with pytest.raises(ValueError, match="Channel does not exist"): func.channel_messages(token1, -1, 0) with pytest.raises(AccessError, match="Invalid token"): func.channel_messages("12345", channel_id, 0) with pytest.raises(ValueError, match="Start index is invalid"): func.channel_messages(token1, channel_id, 100) with pytest.raises(AccessError, match=\ "Authorised user is not a member of the channel"): func.channel_messages(token2, channel_id, 0)