def test_message_unpin(): helper.reset_data() # Set up valid parameters token = helper.token_admin()["token"] not_admin = helper.token_account_1()["token"] channel_id = helper.get_valid_channel(not_admin) message_id = helper.get_valid_message(not_admin, channel_id) stub.channel_join(token, channel_id) stub.message_pin(token, message_id) # Set up invalid parameters invalid_message_id = -1 # message_id is not a valid message with pytest.raises(validation_helper.ValueError): stub.message_unpin(token, invalid_message_id) # Error when the authorised user is not an admin with pytest.raises(validation_helper.ValueError): stub.message_unpin(not_admin, message_id) # Error when the admin is not a member of the channel that the message is within with pytest.raises(validation_helper.AccessError): stub.channel_leave(token, channel_id) stub.message_unpin(channel_id, message_id) stub.channel_join(token, channel_id) # successfully unpin a message assert stub.message_unpin(token, message_id) == {} # try and unpin a message again with pytest.raises(validation_helper.ValueError): stub.message_unpin(token, message_id)
def get_valid_channel(token): """ This function returns a valid channel_id for the provided token. The token would be guaranteed to be in that channel. usage: pass in a valid token Notes: It will try and find a valid channel to join, else it will create one and return that. This is to prevent creating more unnecessesary channels This makes sure the channel_id is valid for the user. """ # token = token_account_1() # No channels exist if stub.channels_listall(token)["channels"] == []: # Create a new channel channel_id = stub.channels_create(token, "test_channel", True)["channel_id"] # There is at least 1 channel so we will join the first one we find else: channel_id = stub.channels_listall(token)["channels"][0]["channel_id"] stub.channel_join(token, channel_id) return channel_id
def test_message_remove(): helper.reset_data() # Valid parameters token = helper.token_account_1()["token"] channel_id = helper.get_valid_channel(token) message_id = helper.get_valid_message(token, channel_id) message = "This message was produced by the get_valid_message function." # Invalid parameters unauthorised_token = helper.token_account_2()["token"] stub.channel_leave(unauthorised_token, channel_id) # Test that it raises an access erorr if the user is not in the channel # to remove a message with pytest.raises(validation_helper.AccessError): stub.message_remove(unauthorised_token, message_id) # Test that it raises an access erorr if the user does not have the proper # authority to remove a message with pytest.raises(validation_helper.AccessError): stub.channel_join(unauthorised_token, channel_id) stub.message_remove(unauthorised_token, message_id) # Attempt to successfully remove a message assert stub.message_remove(token, message_id) == {} # Test that it raises an error if trying to remove a already removed message with pytest.raises(validation_helper.ValueError): stub.message_remove(token, message_id) # Test that the message disappears from search assert message_id not in stub.search(token, message)
def test_channels_list(): helper.reset_data() # SETUP START user1 = helper.token_admin() user2 = helper.token_account_1() channel1_id = helper.channelid_public(user1["token"]) channel2_id = helper.channelid_private(user1["token"]) # SETUP END assert stub.channels_list(user1["token"]) == { "channels": [ {"channel_id": channel1_id, "name": "Public Channel"}, {"channel_id": channel2_id, "name": "Private Channel"} ] } # Invalid token with pytest.raises(validation_helper.AccessError): stub.channels_list("fake token") # check that no channels are returned if user is not in any channel assert stub.channels_list(user2["token"]) == {"channels": []} stub.channel_join(user2["token"], channel1_id) # check that output changes once a user joins a channel assert stub.channels_list(user2["token"]) == {"channels": [{"channel_id": channel1_id, "name": "Public Channel"}]} # check that output changes once a user leaves a channel stub.channel_leave(user1["token"], channel1_id) assert stub.channels_list(user1["token"]) == {"channels": [{"channel_id": channel2_id, "name": "Private Channel"}]}
def test_message_edit(): helper.reset_data() # Valid parameters token = helper.token_account_1()["token"] channel_id = helper.get_valid_channel(token) message_id = helper.get_valid_message(token, channel_id) message = "This message was edited by the message_edit function." # Invalid parameters invalid_message = message*3000 invalid_message_id = -1 unauthorised_token = helper.token_account_2()["token"] # Test that it raises an error if a user edits a message that is not theirs. with pytest.raises(validation_helper.AccessError): stub.channel_join(unauthorised_token, channel_id) stub.message_edit(unauthorised_token, message_id, message) stub.channel_leave(unauthorised_token, channel_id) # Test that it raises an error if invalid message_id with pytest.raises(validation_helper.ValueError): stub.message_edit(token, invalid_message_id, message) # Test that it raises an error if it is a invalid message being edited with pytest.raises(validation_helper.ValueError): stub.message_edit(token, message_id, invalid_message) # Attempt to successfully edit a message assert stub.message_edit(token, message_id, message) == {} # Test that the message is edited in search assert stub.search(token, message)["messages"] != [] # Test that message is deleted if edited with an empty string stub.message_edit(token, message_id, "") assert stub.channel_messages(token, channel_id, 0)["messages"] == []
def channel_join(): """ uses stub.py interface function to add a user to a channel """ return dumps(stub.channel_join(request.form.get('token'), int(request.form.get('channel_id'))))
def test_channel_join(): helper.reset_data() # SETUP START user1 = helper.token_admin() user2 = helper.token_account_1() public_channel_id = helper.channelid_public(user1["token"]) private_channel_id = helper.channelid_private(user1["token"]) # SETUP END # Check that error is raised if an invalid token is passed in with pytest.raises(validation_helper.AccessError): stub.channel_join("fake token", public_channel_id) # Check that error is raised if an invalid channel_id is passed in with pytest.raises(validation_helper.ValueError): stub.channel_join(user1["token"], -1) with pytest.raises(validation_helper.ValueError): # check that validation_helper.ValueError is raised if channel does not exist # Assumption: channel_id cannot be negative stub.channel_join(user1["token"], -1) # check that nothing happens if user is already a member of the channel (assumption) stub.channel_join(user1["token"], public_channel_id) # should raise no errors # check that channel_join is successful when channel is public stub.channel_join(user2["token"], public_channel_id) assert validation_helper.is_member(user2["u_id"], public_channel_id) with pytest.raises(validation_helper.AccessError): # check that validation_helper.AccessError is raised if channel # is private and user is not an admin stub.channel_join(user2["token"], private_channel_id) # check that channel_join is successful when channel is private and user is an admin stub.admin_userpermission_change(user1["token"], user2["u_id"], 2) stub.channel_join(user2["token"], private_channel_id) assert validation_helper.is_member(user2["u_id"], private_channel_id)