def test_message_unpin(): storage.clear_data_store() # set up user (assuming user has admin privlages) dic1 = authentication.auth_register('*****@*****.**', 'password1', 'Bob', 'James') token1 = dic1['token'] # creat a valid channel in the name of user1 dic = channel_utils.channels_create(token1, 'name', True) channel_id_valid = dic['channel_id'] # message to pin/unpin message = "unPin me :)" token = token1 message_id = message_utils.message_send(token, channel_id_valid, message) message_utils.message_pin(token, message_id) # second user dic2 = authentication.auth_register('*****@*****.**', 'password2', 'Bobb', 'Jamesss') token2 = dic2['token'] # The authorised user isn't a member of the channel that contains the message # ACCESS ERROR token = token2 with pytest.raises(errors.AccessError): message_utils.message_unpin(token, message_id) # make the user a member of the channel, but not an admin channel_utils.channel_join(token2, channel_id_valid) # The authoriserd user isn't an admin # VALUE ERROR token = token2 with pytest.raises(ValueError): message_utils.message_unpin(token, message_id) # Everything valid token = token1 message_utils.message_unpin(token, message_id) # Prove that it worked channel = channel_utils.get_channel(channel_id_valid) assert channel['messages'][0]['is_pinned'] == False # Message_id isn't a valid message # VALUE ERROR token = token1 bad_message_id = -99 with pytest.raises(ValueError): message_utils.message_unpin(token, bad_message_id) # Message id is already unpinned # VALUE ERROR token = token1 with pytest.raises(ValueError): message_utils.message_unpin(token, message_id) pass
def test_message_unreact(): storage.clear_data_store() # set up user dic1 = authentication.auth_register('*****@*****.**', 'password1', 'Bob', 'James') token1 = dic1['token'] # creat a valid channel in the name of user1 dic = channel_utils.channels_create(token1, 'name', False) channel_id_valid = dic['channel_id'] # message to be unreacted message = "unReact to me :)" message_id = message_utils.message_send(token1, channel_id_valid, message) # Setting the message to have been reacted to by token1 with a react_id 1 react_id = 1 message_utils.message_react(token1, message_id, react_id) # Everything valid. token = token1 react_id = 1 message_utils.message_unreact(token, message_id, react_id) # Prove that it worked channel = channel_utils.get_channel(channel_id_valid) u_id = authentication.decode_token(token)["u_id"] assert channel['messages'][0]['reacts'] == [{'is_this_user_reacted': False, \ 'react_id': 1, 'u_ids': []}] # message_id isn't a valid message within a channel that the user has joined # VALUE ERROR token = token1 message_id = -99 react_id = 1 with pytest.raises(ValueError): message_utils.message_unreact(token, message_id, react_id) # react id isn't valid # VALUE ERROR token = token1 react_id = -99 with pytest.raises(ValueError): message_utils.message_unreact(token, message_id, react_id) # message with id message_id does not contain an active react with given id # VALUE ERROR token = token1 react_id = 1 with pytest.raises(ValueError): message_utils.message_unreact(token, message_id, react_id) # Invalid token token = "Invalidtoken" react_id = 1 with pytest.raises(ValueError): message_utils.message_unreact(token, message_id, react_id)
def channels_create(): token = request.form.get("token") name = request.form.get("name") is_public = request.form.get("is_public") channel_id = channel_utils.channels_create(token, name, is_public) return dumps({ "channel_id": channel_id })
def test_message_edit(): # set up user storage.clear_data_store() dic1 = authentication.auth_register('*****@*****.**', 'password1', 'Bob', 'James') token1 = dic1['token'] # creat a valid channel in the name of user1 dic = channel_utils.channels_create(token1, 'name', False) channel_id_valid = dic['channel_id'] # Message to be edited message = "Unedited message" message_id = message_utils.message_send(token1, channel_id_valid, message) # When all of the following isn't true: # Meassage with message_id wasn't sent by the authorised user making the request # Meassage with message_id was not sent by an owner of this channel # Meassage with message_id was not sent by an admin or owner of the slack # VALUE ERROR dic2 = authentication.auth_register('*****@*****.**', 'password2', 'Bobby', 'Jamesy') token2 = dic2['token'] token = token2 message = "Editted message." with pytest.raises(errors.AccessError): message_utils.message_edit(token, message_id, message) # Message is too long (over 1000 characters). token = token1 message = "0123456789" * 101 with pytest.raises(ValueError): message_utils.message_edit(token, message_id, message) # Message_id is invalid. token = token1 bad_message_id = -99 message = "Editted message." with pytest.raises(errors.AccessError): message_utils.message_edit(token, bad_message_id, message) # Everything valid. token = token1 message = "Editted message." message_utils.message_edit(token, message_id, message) # Prove that it worked data = channel_utils.get_data() assert data["channels"][0]['messages'][0] == "Editted message." pass
def test_message_remove(): storage.clear_data_store() # set up user dic1 = authentication.auth_register('*****@*****.**', 'password1', 'Bob', 'James') token1 = dic1['token'] # Channel to post message which will be deleated dic = channel_utils.channels_create(token1, 'name', False) channel_id_valid = dic['channel_id'] # Message to be deleated message_id = message_utils.message_send(token1, channel_id_valid, "Hello") # ValueError when message doesn't exist # VALUE ERROR token = token1 message_id1 = -99 with pytest.raises(errors.AccessError): message_utils.message_remove(token, message_id1) # When all of the following isn't true: # Meassage with message_id wasn't sent by the authorised user making the request # Meassage with message_id was not sent by an owner of this channel # Meassage with message_id was not sent by an admin or owner of the slack # ACCESS ERROR # second user without access dic2 = authentication.auth_register('*****@*****.**', 'password2', 'Bobby', 'Jamesy') token2 = dic2['token'] token = token2 with pytest.raises(errors.AccessError): message_utils.message_remove(token, message_id) # Everything valid token = token1 message_utils.message_remove(token, message_id) # Check channel = channel_utils.get_channel(channel_id_valid) assert channel['messages'] == [] pass
def test_standup_start(): storage.clear_data_store() # set up user dic1 = authentication.auth_register('*****@*****.**', 'password1', 'Bob', 'James') token1 = dic1['token'] # creat a valid channel in the name of user1 dic = channel_utils.channels_create(token1, 'name', False) channel_id_valid = dic['channel_id'] # set up 2nd user dic2 = authentication.auth_register('*****@*****.**', 'password11', 'Bobby', 'Jamesy') token2 = dic2['token'] # Valid token, valid channel_id, but no access: usertoken = token2 channel_id = channel_id_valid with pytest.raises(errors.AccessError): standup.standup_start(usertoken, channel_id) # Valid token, valid channel_id: usertoken = token1 channel_id = channel_id_valid time = standup.standup_start(usertoken, channel_id) data = channel_utils.get_data() assert time < datetime.datetime.now() + datetime.timedelta(minutes=16) assert time > datetime.datetime.now() + datetime.timedelta(minutes=14) assert time == data["channels"][0]["standup_time"] # invalid token, valid channel_id: usertoken = "invalidToken" channel_id = channel_id_valid with pytest.raises(ValueError): standup.standup_start(usertoken, channel_id) # valid token, invalid channel_id: usertoken = token1 channel_id = 9191 with pytest.raises(ValueError): standup.standup_start(usertoken, channel_id)
def test_standup_send(): storage.clear_data_store() # set up user dic1 = authentication.auth_register('*****@*****.**', 'password1', 'Bob', 'James') token1 = dic1['token'] # creat a valid channel in the name of user1 dic = channel_utils.channels_create(token1, 'name', False) channel_id_valid = dic['channel_id'] # No active standup usertoken = token1 channel_id = channel_id_valid message = "No start up" with pytest.raises(ValueError): standup.standup_send(usertoken, channel_id, message) # Start the standup standup.standup_start(usertoken, channel_id) # Valid token, valid channel_id: usertoken = token1 channel_id = channel_id_valid message = "Stand up active" standup.standup_send(usertoken, channel_id, message) data = channel_utils.get_data() assert data["channels"][0]["standup_time"] == data["channels"][0][ "messages"][0]["time_created"] # Error when Channel does not exist message = "Hello There" usertoken = "mytoken1234" channel_id = 12123 # this is an invalid channel with pytest.raises(ValueError): standup.standup_send(usertoken, channel_id, message) # Error when message > 1000 chars message = "H" * 1001 usertoken = "mytoken1234" channel_id = 1 with pytest.raises(ValueError): standup.standup_send(usertoken, channel_id, message) # set up 2nd user, which isn't a member of the channel dic2 = authentication.auth_register('*****@*****.**', 'password12', 'Boby', 'Jamesy') token2 = dic2['token'] # AccessError if the user is authorised, but not a member of the channel message = "Hello There" usertoken = token2 channel_id = channel_id_valid with pytest.raises(errors.AccessError): standup.standup_send(usertoken, channel_id, message) # Error if user doesn't exist message = "Hello There" usertoken = "mytoken1234" channel_id = channel_id_valid with pytest.raises(ValueError): standup.standup_send(usertoken, channel_id, message)
def test_search(): storage.clear_data_store() # User to search dic1 = authentication.auth_register('*****@*****.**', 'password1', 'Bob', 'James') token1 = dic1['token'] # Second user dic2 = authentication.auth_register('*****@*****.**', 'password3', 'Bb', 'Js') token2 = dic2['token'] # channel to search through dic = channel_utils.channels_create(token1, 'name', False) channel_id_valid = dic['channel_id'] # Submit messages message = "Valid message" message_id = message_utils.message_send(token1, channel_id_valid, message) message = "second message" message_id = message_utils.message_send(token1, channel_id_valid, message) message = "third message" message_id = message_utils.message_send(token1, channel_id_valid, message) # Messages in the channel # Search using a valid token for a query which doesn't exist token = token1 queryString = "hello" assert search.search(token, queryString) == [] token = token1 queryString = "Va" assert search.search(token, queryString)[0]['message'] == "Valid message" token = token1 queryString = " mess" assert search.search(token, queryString)[1]['message'] == "second message" # Serch using a valid token, without access to the channel where the messages are token = token2 queryString = " mess" assert search.search(token, queryString) == [] # search using an invalid token for a valid string. token = "invalid" queryString = "wassup" with pytest.raises(ValueError): search.search(token, queryString) # handle whitespace as a query string. token = token1 queryString = " " assert search.search(token, queryString) == [] # handle 1000 characters - Different words. token = token1 queryString = "Hello There 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. N" with pytest.raises(ValueError): search.search(token, queryString) # handle 1001 characters - Same letter. token = token1 queryString = "H" * 1001 with pytest.raises(ValueError): search.search(token, queryString)
def test_message_send(): storage.clear_data_store() # set up user dic1 = authentication.auth_register('*****@*****.**', 'password1', 'Bob', 'James') token1 = dic1['token'] # creat a valid channel in the name of user1 dic = channel_utils.channels_create(token1, 'name', False) channel_id_valid = dic['channel_id'] # Value error when message too long # VALUE ERROR token = token1 channel_id = channel_id_valid message = "0123456789" * 101 with pytest.raises(ValueError): message_utils.message_send(token, channel_id, message) # Invalid token. token = "InvalidToken" channel_id = channel_id_valid message = "Valid message" with pytest.raises(ValueError): message_utils.message_send(token, channel_id, message) # Channel_id doesn't exist. token = token1 channel_id = -1 message = "Valid message" with pytest.raises(ValueError): message_utils.message_send(token, channel_id, message) # set up another channel, which the original user doesn't have access too dic2 = authentication.auth_register('*****@*****.**', 'password2', 'Bobby', 'Jamesy') token2 = dic2['token'] # creat a valid channel in the name of user1 dic_a = channel_utils.channels_create(token2, 'matth', False) channel_id_valid1 = dic_a['channel_id'] # User lacks access to current channel trying to post in. token = token1 channel_id = channel_id_valid1 message = "Valid message" with pytest.raises(errors.AccessError): message_utils.message_send(token, channel_id, message) # Everything Valid token = token1 channel_id = channel_id_valid message = "Valid message" message_id = message_utils.message_send(token, channel_id, message) u_id = authentication.decode_token(token)["u_id"] channel = channel_utils.get_channel(channel_id) assert channel['messages'][0]['message_id'] == message_id assert channel['messages'][0]['u_id'] == u_id assert channel['messages'][0]['message'] == "Valid message" assert channel['messages'][0]['reacts'] == [{ 'react_id': 1, 'u_ids': [], 'is_this_user_reacted': False }] assert channel['messages'][0]['is_pinned'] == False # send a second message message = "Second message" token = token1 message_id2 = message_utils.message_send(token, channel_id, message) channel = channel_utils.get_channel(channel_id) assert channel['messages'][0]['message_id'] == message_id2 assert channel['messages'][1]['message_id'] == message_id
def test_message_sendlater(): storage.clear_data_store() # set up user dic1 = authentication.auth_register('*****@*****.**', 'password1', 'Bob', 'James') token1 = dic1['token'] # creat a valid channel in the name of user1 dic = channel_utils.channels_create(token1, 'name', False) channel_id_valid = dic['channel_id'] # Everything valid. token = token1 channel_id = channel_id_valid message = "Valid message." time_sent = datetime(2019, 10, 31, 10, 10) message_id = message_utils.message_sendlater(token, channel_id, message, time_sent) # Prove it worked u_id = authentication.decode_token(token)["u_id"] channel = channel_utils.get_channel(channel_id) assert channel['messages'][0]['message_id'] == message_id # Token invaid token = "NotValid" channel_id = channel_id_valid message = "Valid message." time_sent = datetime(2019, 10, 31, 10, 10) with pytest.raises(ValueError): message_utils.message_sendlater(token, channel_id, message, time_sent) # The channel which the message is getting posted too doesn't exist # VALUE ERROR token = token1 channel_id = -12 message = "Valid message." time_sent = datetime(2019, 10, 31, 10, 10) # use channel_list with pytest.raises(ValueError): message_utils.message_sendlater(token, channel_id, message, time_sent) # set up another channel, which the original user doesn't have access too dic2 = authentication.auth_register('*****@*****.**', 'password2', 'Bobby', 'Jamesy') token2 = dic2['token'] # creat a valid channel in the name of user1 dic_a = channel_utils.channels_create(token2, 'name', False) channel_id_valid1 = dic_a['channel_id'] # User lacks access to current channel trying to post in. token = token1 channel_id = channel_id_valid1 message = "Valid message" time_sent = datetime(2019, 10, 31, 10, 10) with pytest.raises(errors.AccessError): message_utils.message_sendlater(token, channel_id, message, time_sent) # Message is too long (over 1000 characters). # VALUE ERROR token = token1 channel_id = channel_id_valid message = "0123456789" * 101 time_sent = datetime(2019, 10, 31, 10, 10) with pytest.raises(ValueError): message_utils.message_sendlater(token, channel_id, message, time_sent) # Time sent is in the past # VALUE ERROR token = token1 channel_id = channel_id_valid message = "Valid message." time_sent = datetime(2019, 8, 31, 10, 10) with pytest.raises(ValueError): message_utils.message_sendlater(token, channel_id, message, time_sent) pass