def test_react_succesfully(): clear() user_a, user_b = register_n_users(2) public_channel_id = channels_create(user_a["token"], "public_channel", True)["channel_id"] # user_b join the channel created by user_a channel_join(user_b["token"], public_channel_id) message_id = message_send(user_a["token"], public_channel_id, "Nice to see you mate")["message_id"] # user_a react to his own message message_react(user_a["token"], message_id, 1) # Message from the perspective of user_a message_a = channel_messages(user_a["token"], public_channel_id, 0)["messages"][0] # user_a reacted assert message_a["reacts"][0]["is_this_user_reacted"] == True assert user_a["u_id"] in message_a["reacts"][0]["u_ids"] # Message from the perspective of user_b message_b = channel_messages(user_b["token"], public_channel_id, 0)["messages"][0] # user_b haven't reacted assert message_b["reacts"][0]["is_this_user_reacted"] == False # user_b react to the message message_react(user_b["token"], message_id, 1) message_b = channel_messages(user_b["token"], public_channel_id, 0)["messages"][0] assert message_b["reacts"][0]["is_this_user_reacted"] == True assert user_b["u_id"] in message_b["reacts"][0]["u_ids"]
def react_message(): data = request.get_json() token = str(data["token"]) message_id = int(data["message_id"]) react_id = int(data["react_id"]) message_react(token, message_id, react_id) return dumps({})
def react(): """ This is a flask wrapper for the message_react function Parameters: No parameters Returns: (dictionary): Empty dictionary """ data = request.get_json() token = data['token'] react_id = int(data['react_id']) message_id = int(data['message_id']) if react_id != 1: raise InputError(description="Invalid react id") if not token_check(token): raise AccessError(description="Invalid user") user = token_check(token) if react_check(message_id, user['u_id'], react_id): raise InputError(description="Already reacted") message_react(token, message_id, 1) return dumps({})
def test_message_react_valid(): ''' Test: - Multiple users reacting to the same message Scenario: - Owner registers and creates channel - Random users register and join owner's channel - Owner sends message - random_user reacts to the message - Test checks that only 1 random user has reacted - From the owner's point of view, it shows that the owner has not reacted - random_user2 has reacted to the same message - Test checks that the message has been reacted to by both random users - From the random_user point of view, it shows that random_user has reacted to the msg ''' clear() # Making a normal channel f_owner = auth_register('*****@*****.**', 'password', 'Fox', 'Foxson') f_channel = channels_create(f_owner['token'], 'Main HUB', True) # Invite a random user to the channel random_user = auth_register('*****@*****.**', 'password', 'Random', 'User') channel_invite(f_owner['token'], f_channel['channel_id'], random_user['u_id']) # Invite a second user random_user2 = auth_register('*****@*****.**', 'password', 'Random2', 'Guy2') channel_invite(f_owner['token'], f_channel['channel_id'], random_user2['u_id']) # Owner sends message m_id1 = message_send(f_owner['token'], f_channel['channel_id'], 'I came first!')['message_id'] # Random user reacts to the message (the given react_id is 1) message_react(random_user['token'], m_id1, 1) messages = channel_messages(f_owner['token'], f_channel['channel_id'], 0)['messages'] # Check if the user has reacted and check if the owner itself has reacted for message in messages: if message['message_id'] == m_id1: assert message['reacts'][0]['react_id'] == 1 assert message['reacts'][0]['u_ids'] == [random_user['u_id']] assert message['reacts'][0]['is_this_user_reacted'] is False # Second user reacts to the same message message_react(random_user2['token'], m_id1, 1) # Check if another user has reacted and check if a random user itself has reacted messages = channel_messages(random_user['token'], f_channel['channel_id'], 0)['messages'] for message in messages: if message['message_id'] == m_id1: assert message['reacts'][0]['react_id'] == 1 assert message['reacts'][0]['u_ids'] == [random_user['u_id'], random_user2['u_id']] assert message['reacts'][0]['is_this_user_reacted']