def test_multiple_user(): clear_v1() result1 = auth_register_v2('*****@*****.**', 'password', 'one', 'aye') result2 = auth_register_v2('*****@*****.**', 'password', 'two', 'bee') result3 = auth_register_v2('*****@*****.**', 'password', 'three', 'cee') output1 = user_profile_v2(result1['token'], result1['auth_user_id']) output2 = user_profile_v2(result1['token'], result2['auth_user_id']) output3 = user_profile_v2(result1['token'], result3['auth_user_id']) assert len(output1) == 1 assert len(output1['user']) == 6 assert output1['user']['email'] == '*****@*****.**' assert output1['user']['name_first'] == 'one' assert output1['user']['name_last'] == 'aye' assert output1['user']['handle_str'] == 'oneaye' assert len(output2) == 1 assert len(output2['user']) == 6 assert output2['user']['email'] == '*****@*****.**' assert output2['user']['name_first'] == 'two' assert output2['user']['name_last'] == 'bee' assert output2['user']['handle_str'] == 'twobee' assert len(output3) == 1 assert len(output3['user']) == 6 assert output3['user']['email'] == '*****@*****.**' assert output3['user']['name_first'] == 'three' assert output3['user']['name_last'] == 'cee' assert output3['user']['handle_str'] == 'threecee' clear_v1()
def test_invalid_token(): """ Tests return of expected output when given a token that doesn't exist, or if the user is not in the channel/dm. """ clear_v1() # Token doesnt exist user_1 = auth_register_v2('*****@*****.**', 'happydays1', 'Eric', 'Zheng') with pytest.raises(AccessError): notifications_get_v1(user_1['token'] + 'bug') # User not in channel user_2 = auth_register_v2('*****@*****.**', 'happydays2', 'Josh', 'Hatton') channel_1 = channels_create_v2(user_1['token'], 'Channel 1', True) user_2_handle = user_profile_v2(user_1['token'], user_2['auth_user_id']) message_send_v2(user_1['token'], channel_1['channel_id'], f"Hi @{user_2_handle['user']['handle_str']}") assert notifications_get_v1(user_2['token']) == {'notifications': []} # User not in dm dm_1 = dm_create_v1(user_1['token'], [user_2['auth_user_id']]) user_3 = auth_register_v2('*****@*****.**', 'hihihi!!!', 'Bunny', 'Dong') user_3_handle = user_profile_v2(user_1['token'], user_3['auth_user_id']) message_senddm_v1(user_1['token'], dm_1['dm_id'], f"Hello @{user_3_handle['user']['handle_str']}") assert notifications_get_v1(user_3['token']) == {'notifications': []}
def test_user_invalid_u_id(): clear_v1() result1 = auth_register_v2('*****@*****.**', 'password', 'comp', 'student') invalid_id = result1['auth_user_id'] + 1 with pytest.raises(InputError): user_profile_v2(result1['token'], invalid_id) clear_v1()
def test_in_edge_case(test_data): a_data, b_data, _ = test_data user_profile_sethandle_v1(a_data["token"], "123") assert user_profile_v2(a_data["token"], a_data["auth_user_id"])["user"]["handle_str"] == "123" user_profile_sethandle_v1(b_data["token"], "12345678901234567890") assert user_profile_v2(a_data["token"], b_data["auth_user_id"])["user"]["handle_str"] == "12345678901234567890"
def test_logout_with_multiple_users( reg_user, user_details): # Test that token generated by login can be logged out clear_v2() user_token = reg_user(0)['token'] user_2 = reg_user(1) auth_logout_v1(user_token) user_profile_v2(user_2['token'], user_2['auth_user_id'] ) # Verify that the second user's token is still valid
def test_basic_sethandle(): clear_v1() result1 = auth_register_v2('*****@*****.**', 'password', 'one', 'aye') output1 = user_profile_v2(result1['token'], result1['auth_user_id']) assert output1['user']['handle_str'] == 'oneaye' user_profile_sethandle_v1(result1['token'], 'newhandle') output2 = user_profile_v2(result1['token'], result1['auth_user_id']) assert output2['user']['handle_str'] == 'newhandle' clear_v1()
def test_basic_setemail(): clear_v1() result1 = auth_register_v2('*****@*****.**', 'password', 'one', 'aye') output1 = user_profile_v2(result1['token'], result1['auth_user_id']) assert output1['user']['email'] == '*****@*****.**' user_profile_setemail_v2(result1['token'], '*****@*****.**') output2 = user_profile_v2(result1['token'], result1['auth_user_id']) assert output2['user']['email'] == '*****@*****.**' clear_v1()
def test_simple(): clear_v1() result1 = auth_register_v2('*****@*****.**', 'password', 'comp', 'student') user1 = user_profile_v2(result1['token'], result1['auth_user_id']) user_profile_uploadphoto_v1( result1['token'], 'https://media.vanityfair.com/photos/5a9069131d14714f6de43ff5/7:3/w_1995,h_855,c_limit/tout-and-lede_Lisa-Simpson.jpg', 0, 0, 200, 200) user2 = user_profile_v2(result1['token'], result1['auth_user_id']) assert user1['user']['profile_img_url'] != user2['user']['profile_img_url']
def test_successful_remove(create_input): user_1, user_2, user_3, user_4, user_5 = create_input[0] assert admin_user_remove_v1(user_1["token"], user_2["auth_user_id"]) == {} assert admin_user_remove_v1(user_1["token"], user_3["auth_user_id"]) == {} assert admin_user_remove_v1(user_1["token"], user_4["auth_user_id"]) == {} assert admin_user_remove_v1(user_1["token"], user_5["auth_user_id"]) == {} # check to see that these users are indeed removed # only user_1 should be shown when calling users_all assert users_all_v1(user_1["token"]) == { "users": [user_profile_v2(user_1["token"], user_1["auth_user_id"])["user"]] } # calling channels list should raise an input error as their u_id's are invalid with pytest.raises(AccessError): channels_list_v2(user_2["token"]) channels_list_v2(user_3["token"]) channels_list_v2(user_4["token"]) channels_list_v2(user_5["token"]) # Check to see each removed user's profile is retrievable and their name is # now 'Removed user' for user in [user_2, user_3, user_4, user_5]: user_profile = user_profile_v2(user_1["token"], user["auth_user_id"]) assert type(user_profile) == dict assert ( f"{user_profile['user']['name_first']}{user_profile['user']['name_last']}" == "Removed user" or f"{user_profile['user']['name_first']} {user_profile['user']['name_last']}" == "Removed user") # CHECK THAT MESSAGES SENT ARE 'REMOVED USER' # check channel messages for user 2 channel_join_v2(user_1["token"], create_input[1][1]["channel_id"]) channel_1_messages = channel_messages_v2(user_1["token"], create_input[1][1]["channel_id"], 0) for message in channel_1_messages["messages"]: if message["message_id"] == create_input[2][1]: assert message["message"] == "Removed user" # check channel messages for user 3 channel_join_v2(user_1["token"], create_input[1][0]["channel_id"]) channel_2_messages = channel_messages_v2(user_1["token"], create_input[1][0]["channel_id"], 0) for message in channel_2_messages["messages"]: if message["message_id"] == create_input[2][2]: assert message["message"] == "Removed user" clear_v1()
def test_change_email_then_change_back(reg_user, email): clear_v2() user = reg_user(0) original_email = email(0) new_email = email(1) user_profile_setemail_v1(user['token'], new_email) user_details = user_profile_v2(user['token'], user['auth_user_id'])['user'] assert user_details['email'] == new_email user_profile_setemail_v1(user['token'], original_email) user_details = user_profile_v2(user['token'], user['auth_user_id'])['user'] assert user_details['email'] == original_email
def test_handle_cut_off_and_appengding_number(): clear_v2() user1 = auth_register_v2("*****@*****.**", "password", "azir", "thisIsLongerThanTwenty") user2 = auth_register_v2("*****@*****.**", "password", "azir", "thisIsLongerThanTwenty") user_1 = user_profile_v2(user1['token'], user1['auth_user_id'])['user'] user_2 = user_profile_v2(user2['token'], user2['auth_user_id'])['user'] assert user_1['handle_str'] == "azirthisislongerthan" assert user_2['handle_str'] == "azirthisislongerthan0"
def test_basic_setname(): clear_v1() result1 = auth_register_v2('*****@*****.**', 'password', 'one', 'aye') output1 = user_profile_v2(result1['token'], result1['auth_user_id']) assert output1['user']['name_first'] == 'one' assert output1['user']['name_last'] == 'aye' user_profile_setname_v2(result1['token'], 'hello', 'world') output2 = user_profile_v2(result1['token'], result1['auth_user_id']) assert output2['user']['name_first'] == 'hello' assert output2['user']['name_last'] == 'world' clear_v1()
def test_standup_send_v1(): clear_v1() auth_user_1_data = auth_register_v2("*****@*****.**", "123456", "Andy", "Dandy") auth_user_1_token = auth_user_1_data["token"] auth_user_1_id = auth_user_1_data["auth_user_id"] auth_user_1_handle = user_profile_v2(auth_user_1_token, auth_user_1_id)["user"]["handle_str"] auth_user_2_data = auth_register_v2("*****@*****.**", "123456", "Bob", "Hobs") auth_user_2_token = auth_user_2_data["token"] auth_user_2_id = auth_user_2_data["auth_user_id"] auth_user_2_handle = user_profile_v2(auth_user_2_token, auth_user_2_id)["user"]["handle_str"] auth_user_3_data = auth_register_v2("*****@*****.**", "123456", "Chris", "Wiz") auth_user_3_token = auth_user_3_data["token"] auth_user_3_id = auth_user_3_data["auth_user_id"] auth_user_3_handle = user_profile_v2(auth_user_3_token, auth_user_3_id)["user"]["handle_str"] channel_id_1 = channels_create_v2(auth_user_1_token, "Andys channel", True)["channel_id"] channel_join_v2(auth_user_2_token, channel_id_1) channel_join_v2(auth_user_3_token, channel_id_1) finish_time = standup_start_v1(auth_user_1_token, channel_id_1, 3)["time_finish"] standup_send_v1(auth_user_1_token, channel_id_1, "Hello") standup_send_v1(auth_user_2_token, channel_id_1, "Hi") standup_send_v1(auth_user_3_token, channel_id_1, "Hey") message_list = channel_messages_v2(auth_user_1_token, channel_id_1, 0)["messages"] assert len(message_list) == 0 time.sleep(finish_time - time.time() + 1) message_list = channel_messages_v2(auth_user_1_token, channel_id_1, 0)["messages"] assert len(message_list) == 1 assert message_list[0]["u_id"] == auth_user_1_id message_sent_after_standup = f"""{auth_user_1_handle}: Hello {auth_user_2_handle}: Hi {auth_user_3_handle}: Hey""" assert message_list[0]["message"] == message_sent_after_standup
def channel_details_v2(token, channel_id): """ Gets all details of particular channel Arguments: token (string) - string that stores user data of caller channel_id (int) - Id of channel to get details Exceptions: InputError - Occurs when Channel ID is not a valid channel AccessError - Authorised user is not a member of channel with channel_id, token is invalid Return value: details (dict) - Channel details """ with open("data.json") as json_file: data = load(json_file) details = { "name": "", "is_public": None, "owner_members": [], "all_members": [], } errorcheck_channel_details_v2(token, channel_id) # Find information for channel in data["channels"]: if channel["channel_id"] == channel_id: found_channel = channel details["name"] = found_channel["name"] details["is_public"] = found_channel["is_public"] # assumes that channel_join/channel_leave maintains the member list correctly for u_id in found_channel["owner_members"]: details["owner_members"].append(user_profile_v2(token, u_id)["user"]) details["all_members"].append(user_profile_v2(token, u_id)["user"]) # Members (listed via Owner order then u_id) # Since we already added all the owners in the previous loop; # Get the set difference for u_id in list( set(found_channel["all_members"]) - set(found_channel["owner_members"])): details["all_members"].append(user_profile_v2(token, u_id)["user"]) return details
def test_change_last(reg_user): clear_v2() new_user = reg_user(0) user_token = new_user['token'] user_id = new_user['auth_user_id'] user_details = user_profile_v2(user_token, user_id)['user'] name_first = user_details['name_first'] name_last = 'puppy' user_profile_setname_v2(user_token, name_first, name_last) user_details = user_profile_v2(user_token, user_id)['user'] assert user_details['name_first'] == name_first assert user_details['name_last'] == 'puppy'
def test_invalid_token(): clear_v1() result1 = auth_register_v2('*****@*****.**', 'password', 'comp', 'student') with pytest.raises(AccessError): user_profile_setemail_v2('', '*****@*****.**') with pytest.raises(AccessError): user_profile_sethandle_v1('', 'compstudent01') with pytest.raises(AccessError): user_profile_setname_v2('', 'jordan', 'milch') with pytest.raises(AccessError): user_profile_v2('', result1['auth_user_id']) with pytest.raises(AccessError): users_all_v1('') clear_v1()
def test_change_email_then_other_user_change_to_original_email( reg_user, email): clear_v2() user_1 = reg_user(0) user_2 = reg_user(1) user_1_original_email = email(0) user_1_new_email = email(2) user_profile_setemail_v1(user_1['token'], user_1_new_email) user_profile_setemail_v1(user_2['token'], user_1_original_email) user_1_profile = user_profile_v2(user_1['token'], user_1['auth_user_id'])['user'] user_2_profile = user_profile_v2(user_2['token'], user_2['auth_user_id'])['user'] assert user_1_profile['email'] == user_1_new_email assert user_2_profile['email'] == user_1_original_email
def users_all_v1(token): """ Returns a list of all users and their associated details Arguments: token (string) - jwt encoded data structure of auth_user with session_id Exceptions: AccessError - Occurs when token is invalid Return value: user (list of dict) - list of dict containing user_id, email, first name, last name and handle """ with open("data.json") as json_file: data = load(json_file) token_check(token) all_profiles = [] # we can be sure that there are no errors raised here because token has been # checked and u_id is valid because we are getting it straight from data for user in data["users"]: all_profiles.append(user_profile_v2(token, user["u_id"])["user"]) return {"users": all_profiles}
def test_single_change(reg_user, email): clear_v2() user = reg_user(0) new_email = email(1) user_profile_setemail_v1(user['token'], new_email) user_details = user_profile_v2(user['token'], user['auth_user_id'])['user'] assert user_details['email'] == new_email
def test_handle_atsign(): clear_v1() result1 = auth_register_v2('*****@*****.**', '123abc!@#', 'Comp@@', 'Student') output = user_profile_v2(result1['token'], result1['auth_user_id']) assert output['user']['handle_str'] == 'compstudent' clear_v1()
def test_handle_with_name_over_20_characters(): clear_v1() user_1 = auth_register_v2('*****@*****.**', 'random', 'Jordan', 'Milchabcdefghijk') output = user_profile_v2(user_1['token'], user_1['auth_user_id']) assert output['user']['handle_str'] == 'jordanmilchabcdefghi' clear_v1()
def test_standup_start_v1(): clear_v1() auth_user_1_data = auth_register_v2("*****@*****.**", "123456", "Andy", "Dandy") auth_user_1_token = auth_user_1_data["token"] auth_user_1_id = auth_user_1_data["auth_user_id"] auth_user_1_handle = user_profile_v2(auth_user_1_token, auth_user_1_id)["user"]["handle_str"] channel_id_1 = channels_create_v2(auth_user_1_token, "Andys channel", True)["channel_id"] message_id_1 = message_send_v2(auth_user_1_token, channel_id_1, "Hey Guys!")["message_id"] standup_start_v1(auth_user_1_token, channel_id_1, 3) standup_send_v1(auth_user_1_token, channel_id_1, "Hello") message_list = channel_messages_v2(auth_user_1_token, channel_id_1, 0)["messages"] assert len(message_list) == 1 assert message_list[0]["message_id"] == message_id_1 assert message_list[0]["message"] == "Hey Guys!" time.sleep(5) message_list_1 = channel_messages_v2(auth_user_1_token, channel_id_1, 0)["messages"] assert len(message_list_1) == 2 assert message_list_1[0]["message_id"] == message_id_1 + 1 assert message_list_1[0]["message"] == auth_user_1_handle + ": Hello" assert message_list_1[1]["message_id"] == message_id_1 assert message_list_1[1]["message"] == "Hey Guys!"
def new_added_notification(token, channel_id, dm_id, u_id): ''' Returns a notification dictionary in the expected format after a user is added to a channel/dm Arguments: token (str) - token of the auth user who added the new user channel_id (int) - id of the channel in which it occured dm_id (int) - id of the dm in which it occured u_id (int) - id of the user being added Return value: { 'u_id': u_id 'channel_id': channel_id, 'dm_id': dm_id, 'notification_message': message } ''' database = getData() auth_user_id = findUser(token) user_handle = user_profile_v2(token, auth_user_id)['user']['handle_str'] # Find the channel/dm name given channel/dm id src_name = get_channel_dm_name(database, channel_id, dm_id) return { 'u_id': u_id, 'channel_id': channel_id, 'dm_id': dm_id, 'notification_message': create_add_notif(user_handle, src_name) }
def users_profile(): token = request.args.get('token') u_id = request.args.get('u_id') output = user_profile_v2(token, int(u_id)) return dumps(output)
def test_handle_repeat(): clear_v2() user1 = auth_register_v2("*****@*****.**", "goodpass123", "Stress", "Puppy") user2 = auth_register_v2("*****@*****.**", "goodpass123", "Stress", "Puppy") user3 = auth_register_v2("*****@*****.**", "goodpass123", "Stress", "Puppy") user_1 = user_profile_v2(user1['token'], user1['auth_user_id'])['user'] user_2 = user_profile_v2(user2['token'], user2['auth_user_id'])['user'] user_3 = user_profile_v2(user3['token'], user3['auth_user_id'])['user'] assert user_1['handle_str'] == "stresspuppy" assert user_2['handle_str'] == "stresspuppy0" assert user_3['handle_str'] == "stresspuppy1"
def test_trailing_int_handle(): clear_v1() auth_register_v2('*****@*****.**', 'random', 'Jordan', 'Milch') user_2 = auth_register_v2('*****@*****.**', 'random', 'Jordan', 'Milch') output = user_profile_v2(user_2['token'], user_2['auth_user_id']) assert output['user']['handle_str'] == 'jordanmilch0' clear_v1()
def test_normal(test_data): a_data, b_data, c_data = test_data user_profile_sethandle_v1(a_data["token"], "customhandle") assert user_profile_v2(a_data["token"], a_data["auth_user_id"])["user"]["handle_str"] == "customhandle" user_profile_sethandle_v1(b_data["token"], "1238141214") assert user_profile_v2(a_data["token"], b_data["auth_user_id"])["user"]["handle_str"] == "1238141214" user_profile_sethandle_v1(c_data["token"], "!*%&!@$@!#!_") assert user_profile_v2(a_data["token"], c_data["auth_user_id"])["user"]["handle_str"] == "!*%&!@$@!#!_" user_profile_sethandle_v1(a_data["token"], "AUwaduan*(@!2n") assert user_profile_v2(a_data["token"], a_data["auth_user_id"])["user"]["handle_str"] == "AUwaduan*(@!2n" user_profile_sethandle_v1(b_data["token"], " ") assert user_profile_v2(a_data["token"], b_data["auth_user_id"])["user"]["handle_str"] == " "
def test_more_change(reg_user): clear_v2() new_user = reg_user(0) user_token = new_user['token'] user_id = new_user['auth_user_id'] handle_str = 'stresspuppy' user_profile_sethandle_v1(user_token, handle_str) first_details = user_profile_v2(user_token, user_id)['user'] second_user = reg_user(1) second_token = second_user['token'] second_id = second_user['auth_user_id'] handle_second = 'sweetsong' user_profile_sethandle_v1(second_token, handle_second) second_details = user_profile_v2(second_token, second_id)['user'] assert first_details['handle_str'] == 'stresspuppy' assert second_details['handle_str'] == 'sweetsong'
def test_single_change(reg_user): clear_v2() new_user = reg_user(0) user_token = new_user['token'] user_id = new_user['auth_user_id'] handle_str = 'stresspuppy' user_profile_sethandle_v1(user_token, handle_str) user_details = user_profile_v2(user_token, user_id)['user'] assert user_details['handle_str'] == 'stresspuppy'
def test_access_own_profile(user_details): clear_v2() eAddress, pword, fName, lName = user_details(0) user = auth_register_v2(eAddress, pword, fName, lName) user_profile = user_profile_v2(user['token'], user['auth_user_id'])['user'] assert user_profile['u_id'] == user['auth_user_id'] assert user_profile['email'] == eAddress assert user_profile['name_first'] == fName assert user_profile['name_last'] == lName assert user_profile['handle_str'] == f"{fName}{lName}"