def test_edit_not_owner_or_creator(users): """ Testing if an access error is raised if a user without authority tries to edit a message """ user1, user2, chan = users test_user2 = auth.auth_register('*****@*****.**', 'password', 'New', 'Guy') message_exp = 'Test 1 test 2 swiggity Swagg' message_exp2 = 'This is to stop there being no message in the channel' new_message = 'This is the edit message and changes' message.message_send(user1['token'], chan['channel_id'], message_exp2) message_id = message.message_send(user2['token'], chan['channel_id'], message_exp) message_from_channel = channel.channel_messages(user1['token'], chan['channel_id'], 0) #Checks that the message was added assert message_exp == message_from_channel['messages'][0]['message'] assert user2['u_id'] == message_from_channel['messages'][0]['u_id'] assert message_id['message_id'] == message_from_channel['messages'][0][ 'message_id'] new_message = "This is the new message" with pytest.raises(AccessError): message.message_edit(test_user2['token'], message_id['message_id'], new_message) other.clear()
def test_edit_msg_no_access(): """ Test if AccessError raised if user doesn't have permission to edit message """ data = create_pinned_message() user2 = auth_register("*****@*****.**", "password", "Bill", "Bill") channel_join(user2['token'], data['channel_id']) with pytest.raises(AccessError): message_edit(user2['token'], data['message_id'], "test")
def edit_message(): data = request.get_json() token = str(data["token"]) message_id = int(data["message_id"]) message = str(data["message"]) message_edit(token, message_id, message) return dumps({})
def test_message_edit_empty_string(): _, wenyao_dict, _, channel_team1, _ = initialise_data() message_id1 = message_send(wenyao_dict['token'], channel_team1['channel_id'], "hello") message_edit(wenyao_dict['token'], message_id1['message_id'], "") messages_detail = channel_messages(wenyao_dict['token'], channel_team1['channel_id'], 0) # check the return value from messages details assert len(messages_detail['messages']) == 0
def test_edit_no_message(users): """ Tests that a message edit with no message acts as a delete """ user1, user2, chan = users message_exp = 'Test 1 test 2 swiggity Swagg' message_exp2 = 'This is to stop there being no message in the channel' message.message_send(user1['token'], chan['channel_id'], message_exp2) message_id = message.message_send(user2['token'], chan['channel_id'], message_exp) message_from_channel = channel.channel_messages(user1['token'], chan['channel_id'], 0) #Checks that the message was added assert message_exp == message_from_channel['messages'][0]['message'] assert user2['u_id'] == message_from_channel['messages'][0]['u_id'] assert message_id['message_id'] == message_from_channel['messages'][0][ 'message_id'] #Uses the function message edit but has the message editted to an empty string message.message_edit(user2['token'], message_id['message_id'], '') new_message_from_channel = channel.channel_messages( user1['token'], chan['channel_id'], 0) #Checks that the message was removed assert message_exp != new_message_from_channel['messages'][0]['message'] assert user2['u_id'] != new_message_from_channel['messages'][0]['u_id'] assert message_id['message_id'] != new_message_from_channel['messages'][0][ 'message_id'] other.clear()
def test_message_edit(): database.reset() #send a message result = get_user("user1") u_id = result['u_id'] token = result['token'] temp = channels.channels_create(token, "channel1", True) channel_id = temp['channel_id'] msg = message.message_send(token, channel_id, "hello") message_id = msg['message_id'] #edit the message return_val = message.message_edit(token, message_id, "hi") assert return_val == {} #edit the message to an empty string so the message is deleted message.message_edit(token, message_id, "") #try to delete the message which is deleted with pytest.raises(InputError) as e: message.message_remove(token, message_id) #have a new user join the channel and send a message result = get_user("user2") u_id2 = result['u_id'] token2 = result['token'] channel.channel_join(token2, channel_id) msg2 = message.message_send(token2, channel_id, "hello") #admin edit the new message temp2 = message.message_edit(token, msg2['message_id'], "hi") assert temp2 == {} #new user edit him message temp3 = message.message_edit(token2, msg2['message_id'], "hillo") assert temp3 == {}
def test_edit_valid_owner(users): """ Testing if a single message can be sent, be stored and editted by owner """ user1, user2, chan = users message_exp = 'Test 1 test 2 swiggity Swagg' message_exp2 = 'This is to stop there being no message in the channel' message.message_send(user1['token'], chan['channel_id'], message_exp2) message_id = message.message_send(user2['token'], chan['channel_id'], message_exp) message_from_channel = channel.channel_messages(user1['token'], chan['channel_id'], 0) #Checks that the message was added assert message_exp == message_from_channel['messages'][0]['message'] assert user2['u_id'] == message_from_channel['messages'][0]['u_id'] assert message_id['message_id'] == message_from_channel['messages'][0][ 'message_id'] new_message = "This is the new message" message.message_edit(user1['token'], message_id['message_id'], new_message) new_message_from_channel = channel.channel_messages( user1['token'], chan['channel_id'], 0) #Checks that the message was changed assert new_message == new_message_from_channel['messages'][0]['message'] assert user2['u_id'] == new_message_from_channel['messages'][0]['u_id'] assert message_id['message_id'] == new_message_from_channel['messages'][0][ 'message_id'] other.clear()
def test_edit_long_message_invalid(users): """ Testing if a message over 1000 characters cannot be sent used to edit """ user1, user2, chan = users # greater then 1000 character length string message_exp = ("This is the original message and will be changed") new_message = ( 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula ' 'eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient ' 'montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, ' 'pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, ' 'aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis ' 'vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras ' 'dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ' 'ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus ' 'in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. ' 'Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ' 'ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum ' 'rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum...too long' ) message_id1 = message.message_send(user2['token'], chan['channel_id'], message_exp) message_from_channel = channel.channel_messages(user1['token'], chan['channel_id'], 0) assert message_exp == message_from_channel['messages'][0]['message'] assert user2['u_id'] == message_from_channel['messages'][0]['u_id'] assert message_id1['message_id'] == message_from_channel['messages'][0][ 'message_id'] with pytest.raises(InputError): message.message_edit(user1['token'], message_id1['message_id'], new_message) other.clear()
def test_msg_edit(initial_data, initial_msgs): '''test for msg_edit''' # 1. msg_edit works well message_edit(users[0]['token'], 10001, 'msg_new') message_edit(users[1]['token'], 10002, '') all_messages = channels[0]['messages'] assert len(all_messages) == 1 assert all_messages[0]['u_id'] == users[0]['u_id'] assert all_messages[0]['message'] == 'msg_new' assert all_messages[0]['message_id'] == 10001 message_edit(users[2]['token'], 20001, 'msg_new_2') all_messages = channels[1]['messages'] assert len(all_messages) == 1 assert all_messages[0]['u_id'] == users[2]['u_id'] assert all_messages[0]['message'] == 'msg_new_2' assert all_messages[0]['message_id'] == 20001 # 2. access error when given token does not refer to a valid user with pytest.raises(AccessError): message_edit('invalid_token', 20001, 'msg') # 3. access error when Message with message_id was sent by # the authorised user making this request # or The authorised user is an owner of this channel or the flockr with pytest.raises(AccessError): message_edit(users[1]['token'], 10001, 'msg')
def test_owner_edit_access(): ''' Testing for a valid message edited by a channel owner. ''' clear() auth_register("*****@*****.**", "password", "User_0", "User_last_0") user_1 = auth_register("*****@*****.**", "password", "User_1", "User_last_1") user_2 = auth_register("*****@*****.**", "password", "User_2", "User_last_2") auth_register("*****@*****.**", "password", "User_3", "User_last_3") channel_1 = channels_create(user_1.get('token'), 'channel_1', True) channel_join(user_2.get('token'), channel_1.get('channel_id')) message_info = message_send(user_2.get('token'), channel_1.get('channel_id'), 'Hello world') print(message_info) message_edit(user_2.get('token'), message_info.get('message_id'), 'Hellooo Worlldddd!!!!') message_search = search(user_1['token'], 'Hellooo Worlldddd!!!!') assert message_search['messages'][0].get('message_id') == message_info.get( 'message_id') assert message_search['messages'][0].get('u_id') == user_2['u_id'] assert message_search['messages'][0].get( 'message') == 'Hellooo Worlldddd!!!!'
def test_message_edit_empty_string(auth_fixture): ''' Test case for a message edit with empty string ''' (server_data, auth_fixture) = auth_fixture # get user details token1 = auth_fixture[1]['token'] # create channels channel_id = channels_create(server_data, token1, 'New_Channel', True)['channel_id'] # user1 sends message to channel msg1 = message_send(server_data, token1, channel_id, 'Hello there I am a user')['message_id'] # user1 edits own message with empty string message_edit(server_data, token1, msg1, '') # find the message msg_list = search(server_data, token1, '')['messages'] msg1edit = [i['message'] for i in msg_list if i['message_id'] == msg1] # make sure message doesnt exist assert msg1edit == []
def test_message_edit_invalid_access(auth_fixture): ''' Test case when the authorised user does not have the necessary access to edit the message - message was not sent by authorised user and authorised user is not an admin or owner of the channel or slack ''' (server_data, auth_fixture) = auth_fixture # get user details where user2 will not send the message and will not be an owner (slackr or channel) token1 = auth_fixture[1]['token'] token2 = auth_fixture[2]['token'] uid2 = auth_fixture[2]['u_id'] # create channel with user1 and invite user2 channel_id = channels_create(server_data, token1, 'New Channel', True)['channel_id'] channel_invite(server_data, token1, channel_id, uid2) # send msg with user1 msg1 = message_send( server_data, token1, channel_id, 'User 2 does not have access to edit this!')['message_id'] # check if user2 can edit this message (if so then raise Access Error) with pytest.raises(AccessError) as error_raise: message_edit(server_data, token2, msg1, 'I WANT TO CHANGE THIS')
def test_message_edit_not_member(): _, wenyao_dict, weiqiang_dict, channel_team1, _ = initialise_data() message_id = message_send(wenyao_dict['token'], channel_team1['channel_id'], "hello") with pytest.raises(AccessError): message_edit(weiqiang_dict['token'], message_id['message_id'], "hi there")
def test_non_existing_user_in_message_edit(): """Tests passing in an invalid user""" clear() new_user_1 = auth_register("*****@*****.**", "peter912", "Peter", "Wallace") new_channel = channels_create(new_user_1["token"], "UNSW Cool Channel", True) new_msg = message_send(new_user_1["token"], new_channel["channel_id"], "UNSW is fun") with pytest.raises(AccessError): message_edit("13211", new_msg["message_id"], "Chicken Nuggets")
def test_message_edit_invalid_token(): """Test passing an invalid token to message_edit""" clear() valid_user = auth_register("*****@*****.**", "validPassword", "Validate", "Me") new_channel = channels_create(valid_user["token"], "test_message_long", False) new_msg = message_send(valid_user["token"], new_channel["channel_id"], "HECK YEAH") with pytest.raises(AccessError): message_edit(2153, new_msg["message_id"], "HECK YEAH")
def edit(): data = request.get_json() token = data['token'] message_id = int(data['message_id']) message = data['message'] message_edit(token, message_id, message) return dumps(message_id)
def message_edit(): """ Function message edit route """ message_info = request.get_json() message.message_edit(message_info['token'], int(message_info['message_id']), message_info['message']) return dumps({})
def test_message_unauthorized_edit_from_non_creator(): """Testing for an AccessError when a non-creator attemps to edit a message""" clear() valid_user = auth_register("*****@*****.**", "validPassword", "Validate", "Me") new_channel = channels_create(valid_user["token"], "FAANG", False) message_send(valid_user["token"], new_channel["channel_id"], "Silly Billy!") pleb_user = auth_register("*****@*****.**", "plebssword", "Plebbleton", "Pleb") with pytest.raises(AccessError): message_edit(pleb_user["token"], 1, "Silly Billy!")
def test_message_edit_not_owner_or_sender(): reset_data() owner = auth.auth_register("*****@*****.**", "123456", "Sinha", "Nawa") member = auth.auth_register("*****@*****.**", "123456", "Sinha1", "Nawa1") channels.channels_create(owner['token'], "Test_channel", True) message.message_send(owner['token'], 1, "test") channel.channel_removeowner(owner['token'], 1, 1) with pytest.raises(AccessError): message.message_edit(member['token'], 1, "Wrong message id")
def test_message_edit_whitespaces(make_users): # setting up users and public channel user_ab, user_cd = make_users new_ch = channels_create(user_ab['token'], 'test_channel_public', True) msg_id = message_send(user_ab["token"], new_ch["channel_id"], "First message.")["message_id"] message_edit(user_ab["token"], msg_id, " ") assert search(user_ab["token"], " ")["messages"][0]["message"] == \ " "
def test_message_unauthorised_edit_from_non_owner(): """Testing for when a non-owner attempts to edit a message""" clear() valid_user = auth_register("*****@*****.**", "validPassword", "Validate", "Me") new_channel = channels_create(valid_user["token"], "Ediot", False) message_send(valid_user["token"], new_channel["channel_id"], "Ediots galore over here am \ i right?") silly_user = auth_register("*****@*****.**", "sillyPassword", "Silly", "Sallys") with pytest.raises(AccessError): message_edit(silly_user["token"], 1, "shut up idiot")
def test_message_edit_invalid_token(make_users): # setting up users and public channel user_ab, user_cd = make_users new_ch = channels_create(user_ab['token'], 'test_channel_public', True) msg_id = message_send(user_ab["token"], new_ch["channel_id"], "First message")["message_id"] with pytest.raises(AccessError): message_edit(user_ab["token"] + "invalid", msg_id, "This is an invalid edit")
def test_search_valid_substring(): ''' Testing valid substring search functionality for search ''' clear() f_owner = auth_register('*****@*****.**', 'password', 'Bob', 'Bob') f_channel = channels_create(f_owner['token'], 'Channel 1', True) random_user = auth_register('*****@*****.**', 'password', 'Random', 'User') channel_invite(f_owner['token'], f_channel['channel_id'], random_user['u_id']) msg = message_send(f_owner['token'], f_channel['channel_id'], 'First message') message_react(f_owner['token'], msg['message_id'], 1) # Test exact match messages = search(f_owner['token'], 'First message')['messages'] assert len(messages) == 1 assert messages[0]['message_id'] == msg['message_id'] assert messages[0]['u_id'] == f_owner['u_id'] # Test substring messages = search(f_owner['token'], 'First')['messages'] assert len(messages) == 1 assert messages[0]['message_id'] == msg['message_id'] assert messages[0]['u_id'] == f_owner['u_id'] # Edited message message_edit(f_owner['token'], msg['message_id'], 'Test message') messages = search(f_owner['token'], 'First')['messages'] assert len(messages) == 0 # Removed message message_remove(f_owner['token'], msg['message_id']) messages = search(f_owner['token'], 'First message')['messages'] assert len(messages) == 0 # Test substring for multiple in range(1, 51): # f_owner and random_user sent message_send(f_owner['token'], f_channel['channel_id'], 'A' * multiple) message_send(random_user['token'], f_channel['channel_id'], 'A' * multiple) # f_owner searched messages = search(f_owner['token'], 'A')['messages'] assert len(messages) == multiple * 2 # random_user searched messages = search(random_user['token'], 'A')['messages'] assert len(messages) == multiple * 2 # Caps-sensitive messages = search(f_owner['token'], 'a')['messages'] assert len(messages) == 0
def test_edit_unauthorised_user(): clear() user01, user02 = register_n_users(2) # Create channel with message from user01 channel = channels_create(user01["token"], "channel", is_public=True) channel_join(user02["token"], channel["channel_id"]) message = message_send(user01["token"], channel["channel_id"], "test message") # User02 tries to edit message from user01 with pytest.raises(AccessError): message_edit(user02["token"], message["message_id"], "edited message")
def test_message_edit_unauthorised(make_users): # setting up users and public channel user_ab, user_cd = make_users new_ch = channels_create(user_ab['token'], 'test_channel_public', True) channel_join(user_cd["token"], new_ch["channel_id"]) msg_id = message_send(user_ab["token"], new_ch["channel_id"], "Owner message")["message_id"] with pytest.raises(AccessError): message_edit(user_cd["token"], msg_id, "This should be an invalid edit")
def test_edit_standard(): info = starter() u1, c1 = info[1], info[3] # user 1 sends a message to c1 m_id = message.message_send(u1['token'], c1['channel_id'], "Original") # user 1 edits the message message.message_edit(u1['token'], m_id['message_id'], "Edited") # message should now be "Edited" c1_messages = channel.channel_messages(u1['token'], c1['channel_id'], 0) assert c1_messages['messages'][0]['message'] == "Edited"
def test_flocker_owner_access_other(): ''' Testing for a valid message edited by a flocker owner. ''' clear() user = auth_register("*****@*****.**", "password", "User_0", "User_last_0") user_1 = auth_register("*****@*****.**", "password", "User_1", "User_last_1") channel_1 = channels_create(user.get('token'), 'channel_1', True) channel_join(user_1.get("token"), channel_1.get("channel_id")) admin_userpermission_change(user.get('token'), user_1.get("u_id"), 1) message_info = message_send(user.get('token'), channel_1.get('channel_id'), 'Hello world') message_edit(user_1.get('token'), message_info.get('message_id'), 'Hellooo Worlldddd!!!!')
def test_edit_continuous_send(): clear() user = register_n_users(1) channel_id = channels_create(user["token"], "channela", is_public=True)["channel_id"] for i in range(1, 50): message = message_send(user["token"], channel_id, "message " + str(i)) message_edit(user["token"], message["message_id"], "edited message " + str(i)) assert channel_messages( user["token"], channel_id, 0)["messages"][0]["message"] == "edited message " + str(i)
def test_message_edit_msgdoesnotexist(): clear() test_user0 = create_one_test_user() # test_user0 creates 1 public channel channel_id = channels_create(test_user0['token'], "Public Channel", True) # test_user0 sends 1 message to public channel message0 = "inital message" message_send(test_user0['token'], channel_id['channel_id'], message0) with pytest.raises(InputError): message_edit(test_user0['token'], 2, 'edited message')
def edit(): ''' a route which calls message_edit from message ''' data = request.get_json() if not data["token"] or not data["message_id"] \ or (not data["message"] and not data["message"] == ""): raise RequestError(description="Missing data in request body") message.message_edit(data["token"], int(data["message_id"]), data["message"]) return dumps({})