def test_successful_react(): # successful thumb up user1 = user_from_token(token1) msg_id4 = data['channels'][0].messages[3].message_id assert msg_react(token1, msg_id4, 1) == {} assert data['channels'][0].messages[3].reactions[0].react_id == 1 assert data['channels'][0].messages[3].reactions[ 0].reacter == user_from_token(token1).u_id assert user1.reacted_msgs[0] == msg_id4
def test_admin_userpermission_change(): #admin_userpermission_change(token, u_id, permission_id), no return value #SETUP TESTS BEGIN #create new admin: authRegDict = auth_login("*****@*****.**", "password") token = authRegDict["token"] userId = authRegDict["u_id"] #create regular user: authRegDict2 = auth_register("*****@*****.**", "password", "Jeffrey", "Oh") token2 = authRegDict2["token"] userId2 = authRegDict2["u_id"] #create channel from admin: chanCreateDict = channels_create(token, "test channel", True) chanId = chanCreateDict["channel_id"] channel_join(token, chanId) #add regular user to first channel: channel_invite(token, chanId, userId2) #SETUP TESTS END with pytest.raises(ValueError): assert admin_userpermission_change(token, userId, 0) #invalid permission_id with pytest.raises(ValueError): assert admin_userpermission_change(token, userId, 4) with pytest.raises(AccessError): assert admin_userpermission_change(token, 55555, 3) #invalid user ID user2 = user_from_token(token2) # check user2 is a member (not an owner or admin) assert check_slackr_owner(user2) == False assert check_slackr_admin(user2) == False # check user2 is an owner admin_userpermission_change(token, userId2, 1) assert check_slackr_owner(user2) == True assert check_slackr_admin(user2) == False # check user2 is an admin admin_userpermission_change(token, userId2, 2) assert check_slackr_owner(user2) == False assert check_slackr_admin(user2) == True # change user2 back to member admin_userpermission_change(token, userId2, 3) # check user2 cannot change permission of user1 with pytest.raises(AccessError): admin_userpermission_change(token2, userId, 3) user = user_from_token(token) # check slackr owner can be made admin admin_userpermission_change(token, userId, 2) assert check_slackr_admin(user)
def channel_details(token, channel_id): global data # raise ValueError if channel_id doesn't exist (channel_index) index = channel_index(channel_id) # raise AccessError('authorised user is not in channel') acct = user_from_token(token) if not(channel_id in acct.in_channel): raise AccessError(description = 'authorised user is not in channel') channel_name = data['channels'][index].name owners_dict = [] members_dict = [] for i in data['channels'][index].owners: owner_member = user_from_uid(i) owners_dict.append({'u_id': i, 'name_first': owner_member.name_first, 'name_last': owner_member.name_last, 'profile_img_url': owner_member.prof_pic}) for i in data['channels'][index].members: member = user_from_uid(i) members_dict.append({'u_id': i, 'name_first': member.name_first, 'name_last': member.name_last, 'profile_img_url': member.prof_pic}) return { 'name': channel_name, 'owner_members': owners_dict, 'all_members': members_dict }
def test_successful_unreact(): # successful unreact a message that is reacted before user1 = user_from_token(token1) msg_id4 = data['channels'][0].messages[3].message_id assert msg_unreact(token1, msg_id4, 1) == {} assert len(data['channels'][0].messages[3].reactions) == 0 assert len(user1.reacted_msgs) == 0
def channels_list(token): global data channel_list = [] for channel in data['channels']: for user_id in channel.members: if user_id == user_from_token(token).u_id: list_dict = {'channel_id':channel.channel_id, 'name':channel.name} channel_list.append(list_dict) return { 'channels': channel_list }
def channel_messages(token, channel_id, start): global data # get the current user curr_user = user_from_token(token) # raise ValueError if channel_id doesn't exist (channel_index) index = channel_index(channel_id) # raise AccessError if authorised user isn't in channel if curr_user.u_id not in data['channels'][index].members: raise AccessError(description = 'authorised user is not in channel') # raise ValueError if start is greater than no. of total messages no_total_messages = len(data['channels'][index].messages) if start > no_total_messages: raise ValueError(description = 'start is greater than no. of total messages') end = -1 list_messages = [] i = start for item in data['channels'][index].messages[i:]: message = {} message['message_id'] = item.message_id message['u_id'] = item.sender message['message'] = item.message message['time_created'] = item.create_time message['is_pinned'] = item.is_pinned message['reacts'] = [] for react in item.reactions: reacter_list = [] reacter_list.append(react.reacter) message['reacts'].append({'react_id': react.react_id, 'u_ids': reacter_list, 'is_this_user_reacted': (curr_user.u_id in item.reacted_user)}) i = i + 1 list_messages.append(message) if i == no_total_messages: end = -1 break if i == (start + 50): end = i break return { 'messages': list_messages, 'start': start, 'end': end }
def msg_unreact(token, msg_id, react_id): global data found_msg = find_msg(msg_id) if react_id != 1: raise ValueError(description='Invalid React ID!') elif reaction_exist(found_msg.reactions, react_id) == False: raise ValueError( description= f'This message does not contain an active React with the react ID: {react_id}!' ) # unreact the message if no exceptions raised found_msg.reactions.remove(reaction_exist(found_msg.reactions, react_id)) unreacter = user_from_token(token) unreacter.reacted_msgs.remove(msg_id) return {}
def msg_react(token, msg_id, react_id): global data reacter = user_from_token(token) found_msg = find_msg(msg_id) if react_id != 1: raise ValueError(description='Invalid React ID!') elif reaction_exist(found_msg.reactions, react_id) is not False: raise ValueError( description= f'This message already contains an active React with react ID: {react_id}!' ) # give the message a reaction if no exceptions raised found_msg.reactions.append(Reacts(reacter.u_id, react_id)) found_msg.reacted_user.append(reacter.u_id) reacter.reacted_msgs.append(msg_id) return {}
def channel_add_owner(token, channel_id, u_id): global data # raise ValueError if channel_id doesn't exist (channel_index) index = channel_index(channel_id) # check if user with u_id is already owner if user_from_uid(u_id).u_id in data['channels'][index].owners: raise ValueError(description = 'User with u_id is already an owner') # check if authorised user is an owner of this channel if user_from_token(token).u_id not in data['channels'][index].owners: raise AccessError(description = 'Authorised user not an owner of this channel') data['channels'][index].owners.append(u_id) return {}
def channel_remove_owner(token, channel_id, u_id): global data # raise ValueError if channel_id doesn't exist (channel_index) index = channel_index(channel_id) # raise ValueError if u_id is not an owner if u_id not in data['channels'][index].owners: raise ValueError(description = 'u_id is not an owner of the channel') # raise AccessError if token is not an owner of this channel if user_from_token(token).u_id not in data['channels'][index].owners: raise AccessError(description = 'authorised user is not an owner of this channel') data['channels'][index].owners.remove(u_id) return {}
def channel_invite(token, channel_id, u_id): global data # raise AccessError if authorised user not in channel # check if channel_id is part of User's in_channel list acct = user_from_token(token) if (channel_id in acct.in_channel) == False: raise AccessError(description = 'authorised user is not in channel') # raise ValueError if channel_id doesn't exist (channel_index) index = channel_index(channel_id) data['channels'][index].members.append(u_id) # add channel to user's list of channels acct = user_from_uid(u_id) acct.in_channel.append(channel_id) return {}
def channel_join(token, channel_id): global data # raise ValueError if channel_id doesn't exist (channel_index) index = channel_index(channel_id) acct = user_from_token(token) # raise AccessError if channel is Private & authorised user is not an admin if data['channels'][index].is_public == False: # check if authorised user is an admin valid = 0 if acct.perm_id < perm_member: valid = 1 data['channels'][index].owners.append(acct.u_id) if valid == 0: raise AccessError(description = 'authorised user is not an admin of private channel') acct.in_channel.append(channel_id) data['channels'][index].members.append(acct.u_id) return {}
def msg_remove(token, msg_id): global data remover = user_from_token(token) found_msg = find_msg(msg_id) # find the channel where the message belongs to msg_channel = find_channel(found_msg.in_channel) if found_msg.sender != remover.u_id: raise AccessError( description= 'You do not have the permission to delete this message as you are not the sender!' ) elif not check_channel_owner(msg_channel, remover.u_id): raise AccessError( description= 'You do not have the permission as you are not the owner or admin of this channel!' ) # no exception raised, then remove the message msg_channel.messages.remove(found_msg) return {}
def msg_send(token, msg, chan_id): global data sending_time = datetime.now().replace(tzinfo=timezone.utc).timestamp() sender = user_from_token(token) current_channel = find_channel(chan_id) if len(msg) > 1000: raise ValueError(description='Message is more than 1000 words!') elif check_channel_member(current_channel, sender.u_id) == False: raise AccessError( description= 'You have not joined this channel yet, please join first!') else: # generate an unique id data['message_count'] += 1 msg_id = data['message_count'] # no exceptions raised, then add(send) the message to the current channel current_channel.messages.insert( 0, Mesg(sender.u_id, sending_time, msg, msg_id, chan_id, False)) return {'message_id': msg_id}
def msg_unpin(token, msg_id): global data unpinner = user_from_token(token) found_msg = find_msg(msg_id) msg_channel = find_channel(found_msg.in_channel) if check_channel_member(msg_channel, unpinner.u_id) == False: raise AccessError( description= 'You can not unpin the message as you are not a member of the channel' ) elif found_msg.is_pinned == False: raise ValueError(description='The message is already unpinned!') elif not check_slackr_admin(unpinner): raise ValueError( description= 'You can not unpin the message as you are not an Admin of the channel' ) # unpin the message if no exceptions raised found_msg.is_pinned = False return {}
def channels_create(token, name, is_public): global data if max_20_characters(name) == False: raise ValueError(description = 'name is more than 20 characters') channel_id = data['channel_count'] data['channel_count'] += 1 data['channels'].append(Channel(name, is_public, channel_id)) index = channel_index(channel_id) acct = user_from_token(token) acct = data['accounts'][0] data['channels'][index].owners.append(acct.u_id) data['channels'][index].members.append(acct.u_id) # add channel to user's list of channels acct.in_channel.append(channel_id) return { 'channel_id': channel_id }
def channel_leave(token, channel_id): global data # raise ValueError if channel_id doesn't exist (channel_index) index = channel_index(channel_id) acct = user_from_token(token) # raise AccessError if authorised user not in channel if (channel_id in acct.in_channel) == False: raise AccessError('authorised user is not in channel') for j in data['channels'][index].members: if j == acct.u_id: # use .remove() instead of .pop() as .remove() takes in the actual element data['channels'][index].members.remove(j) for j in data['channels'][index].owners: if j == acct.u_id: data['channels'][index].owners.remove(j) return {}
def msg_edit(token, msg_id, new_msg): global data editor = user_from_token(token) found_msg = find_msg(msg_id) msg_channel = find_channel(found_msg.in_channel) # iter3 update: if the new msg is empty, delete the message if new_msg == '': msg_remove(token, msg_id) elif len(new_msg) > 1000: raise ValueError(description='Message is more than 1000 words!') elif found_msg.sender != editor.u_id: raise AccessError( description= 'You do not have the permission to edit this message as you are not the sender!' ) elif not check_channel_owner(msg_channel, editor.u_id): raise AccessError( description= 'You do not have the permission as you are not the owner or admin of this channel or Slackr!' ) # edit the message if no exceptions raiseds found_msg.message = new_msg return {}
def test_successful_unpin(): user1 = user_from_token(token1) user3 = user_from_token(token3) msg_id1 = data['channels'][0].messages[0].message_id admin_userpermission_change(token1, user3.u_id, perm_admin) assert msg_unpin(token3, msg_id1) == {}