def test_users_all_valid_token(user_1): """Test if token does not refer to a valid user """ auth.auth_logout(user_1['token']) with pytest.raises(AccessError): users_all(user_1['token']) clear()
def test_request_multiple_users(): """ Testing multiple users who have requested to reset password """ clear() email_1 = '*****@*****.**' email_2 = '*****@*****.**' email_3 = '*****@*****.**' email_4 = '*****@*****.**' result_1 = auth.auth_register(email_1, 'abcdefg', 'John', 'Smith') result_2 = auth.auth_register(email_2, 'abcdefg', 'John', 'Smith') result_3 = auth.auth_register(email_3, 'abcdefg', 'John', 'Smith') result_4 = auth.auth_register(email_4, 'abcdefg', 'John', 'Smith') auth.auth_passwordreset_request(email_1) auth.auth_passwordreset_request(email_2) auth.auth_passwordreset_request(email_3) auth.auth_passwordreset_request(email_4) data = pickle.load(open("data.p", "rb")) for user in data.get_reset_users(): if user['u_id'] == result_1['u_id']: assert user['email'] == email_1 if user['u_id'] == result_2['u_id']: assert user['email'] == email_2 if user['u_id'] == result_3['u_id']: assert user['email'] == email_3 if user['u_id'] == result_4['u_id']: assert user['email'] == email_4 clear()
def test_reset_password_multiple_user(): """ Testing that password is actually updated """ clear() email = '*****@*****.**' auth.auth_register('*****@*****.**', 'abcdefg', 'Jane', 'Smith') result = auth.auth_register(email, 'abcdefg', 'John', 'Smith') auth.auth_passwordreset_request(email) reset_code = '' data = pickle.load(open("data.p", "rb")) for user in data.get_reset_users(): if user['u_id'] == result['u_id']: reset_code = user['secret'] password = '******' auth.auth_passwordreset_reset(reset_code, password) # comparing hashed password hashed = hashlib.sha256(password.encode()).hexdigest() data = pickle.load(open("data.p", "rb")) for user in data.get_reset_users(): assert user['u_id'] != result['u_id'] # making sure new hashed password is stored data = pickle.load(open("data.p", "rb")) for user in data.get_users(): if user['u_id'] == result['u_id']: assert user['password'] == hashed clear()
def test_img_url_invalid_token(user_1, logout_user_1): """Test for a non-registered/invalid user. (Invalid token) """ img_url = "https://www.ottophoto.com/kirlian/kirlian_1/kirlian12.jpg" with pytest.raises(AccessError): user.user_profile_uploadphoto(user_1['token'], img_url, 0, 0, 1, 1) clear()
def test_img_url_not_jpg(user_1): """ Test case where image uploaded is not a JPG """ img_url = "https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png" with pytest.raises(InputError): user.user_profile_uploadphoto(user_1['token'], img_url, 0, 0, 100, 100) clear()
def test_user_id(user_1): """Test whether the user profile u_id matches the u_id returned by auth_register. """ user_details = user.user_profile(user_1['token'], user_1['u_id']) assert user_1['u_id'] == user_details['user']['u_id'] clear()
def test_handle_exists(user_1, user_2): """ Testing changing to a user handle that already exists. """ user.user_profile_sethandle(user_1['token'], 'sameHandle') with pytest.raises(InputError): user.user_profile_sethandle(user_2['token'], 'sameHandle') clear()
def test_channels_create_21char(user_1): """Test for 21 character name. """ with pytest.raises(InputError): channels.channels_create(user_1['token'], 'Channel_Name123456789', False) clear()
def test_channels_list(user_1, user_2): """Test for multiple created channels. """ # Create new channels. channel_1 = channels.channels_create(user_1['token'], 'Channel_1', True) channel_2 = channels.channels_create(user_1['token'], 'Channel_2', True) channel_3 = channels.channels_create(user_1['token'], 'Channel_3', True) channels.channels_create(user_2['token'], 'Channel_4', True) assert channels.channels_list(user_1['token']) == { 'channels': [ { 'channel_id': channel_1['channel_id'], 'name': 'Channel_1', }, { 'channel_id': channel_2['channel_id'], 'name': 'Channel_2', }, { 'channel_id': channel_3['channel_id'], 'name': 'Channel_3', }, ], } clear()
def test_standup_active_not_active(user_1, user_2, user_3, public_channel_1): """Testing when standup is not active """ assert channel.channel_invite(user_1['token'], public_channel_1['channel_id'], user_2['u_id']) == {} assert channel.channel_invite(user_2['token'], public_channel_1['channel_id'], user_3['u_id']) == {} standup_duration = 2 curr_time = int(datetime.now(tz=timezone.utc).timestamp()) information = standup.standup_start(user_1['token'], public_channel_1['channel_id'], standup_duration) assert (curr_time + standup_duration - STANDUP_DELAY) <= information['time_finish'] and\ information['time_finish'] <= (curr_time + standup_duration + STANDUP_DELAY) time.sleep(4) information = standup.standup_active(user_1['token'], public_channel_1['channel_id']) assert not information['is_active'] assert information['time_finish'] == None information = standup.standup_active(user_2['token'], public_channel_1['channel_id']) assert not information['is_active'] assert information['time_finish'] == None information = standup.standup_active(user_3['token'], public_channel_1['channel_id']) assert not information['is_active'] assert information['time_finish'] == None clear()
def test_standup_send_working_example(user_1, user_2, user_3, public_channel_1): """Testing when standup send is working, via message collation """ assert channel.channel_invite(user_1['token'], public_channel_1['channel_id'], user_2['u_id']) == {} assert channel.channel_invite(user_2['token'], public_channel_1['channel_id'], user_3['u_id']) == {} standup_duration = 5 curr_time = int(datetime.now(tz=timezone.utc).timestamp()) information = standup.standup_start(user_1['token'], public_channel_1['channel_id'], 2) assert (curr_time + standup_duration - STANDUP_DELAY) <= information['time_finish'] and\ information['time_finish'] <= (curr_time + standup_duration + STANDUP_DELAY) on_list = False user_one_handle = user.user_profile(user_1['token'], user_1['u_id'])['user']['handle_str'] assert standup.standup_send(user_1['token'], public_channel_1['channel_id'], 'Pizza!') == {} message_data = channel.channel_messages(user_1['token'], public_channel_1['channel_id'], 0) for messages in message_data['messages']: if messages['message'] == f'{user_one_handle}: Pizza!': on_list = True assert not on_list assert standup.standup_send(user_2['token'], public_channel_1['channel_id'], 'Water!') == {} assert standup.standup_send(user_3['token'], public_channel_1['channel_id'], 'Melon!') == {} time.sleep(7) on_list = False user_two_handle = user.user_profile(user_2['token'], user_2['u_id'])['user']['handle_str'] user_three_handle = user.user_profile(user_3['token'], user_3['u_id'])['user']['handle_str'] message_data = channel.channel_messages(user_1['token'], public_channel_1['channel_id'], 0) for messages in message_data['messages']: if messages['message'] == f'{user_one_handle}: Pizza!\n{user_two_handle}: Water!\n{user_three_handle}: Melon!': on_list = True assert on_list clear()
def test_search_standard(user_1, user_2, user_3, user_4, public_channel_1, public_channel_2, public_channel_3, public_channel_4): """Test searching messages in multiple channels """ message_str_1 = "Welcome to group 1!" message_str_2 = "Welcome to group 2!" message_str_3 = "Welcome to group 3!" message_str_4 = "Welcome to group 4!" message_str_5 = "Hiya guys!" message_str_6 = "sup" message_str_7 = "Let's get down to business!" query_str = "Welcome" channel.channel_join(user_1['token'], public_channel_2['channel_id']) channel.channel_join(user_1['token'], public_channel_3['channel_id']) channel.channel_join(user_1['token'], public_channel_4['channel_id']) message.message_send(user_1['token'], public_channel_1['channel_id'], message_str_1) message.message_send(user_2['token'], public_channel_2['channel_id'], message_str_2) message.message_send(user_3['token'], public_channel_3['channel_id'], message_str_3) message.message_send(user_4['token'], public_channel_4['channel_id'], message_str_4) message.message_send(user_1['token'], public_channel_1['channel_id'], message_str_5) message.message_send(user_1['token'], public_channel_2['channel_id'], message_str_6) message.message_send(user_1['token'], public_channel_2['channel_id'], message_str_7) msg_list = search(user_1['token'], query_str) msg_count = 0 msg_cmp_2 = [] for msg in msg_list['messages']: msg_cmp_2.append(msg['message']) msg_count += 1 assert msg_count == 4 msg_cmp_1 = [message_str_1, message_str_2, message_str_3, message_str_4] msg_cmp_1.sort() msg_cmp_2.sort() assert msg_cmp_1 == msg_cmp_2 clear()
def test_search_valid_token(user_1): """Test if token does not refer to a valid user """ auth.auth_logout(user_1['token']) with pytest.raises(AccessError): search(user_1['token'], "Test") clear()
def test_users_all(user_1, user_2, user_3, user_4): """Test if a list all users details is returned """ all_users = users_all(user_1['token']) user_count = 0 test_1 = False test_2 = False test_3 = False test_4 = False for user in all_users['users']: if user['u_id'] is user_1['u_id']: test_1 = True if user['u_id'] is user_2['u_id']: test_2 = True if user['u_id'] is user_3['u_id']: test_3 = True if user['u_id'] is user_4['u_id']: test_4 = True user_count += 1 assert user_count == 4 assert test_1 assert test_2 assert test_3 assert test_4 clear()
def test_update_invalid_token(user_1, logout_user_1): """ Testing user cant change name with an invalid token """ auth.auth_logout(user_1['token']) with pytest.raises(AccessError): user.user_profile_setname(user_1['token'], 'Bobby', 'Smith') clear()
def test_channels_listall(user_1, user_2): """Test list all channels. """ # Create new channels. channel_1 = channels.channels_create(user_1['token'], 'Channel_1', True) channel_2 = channels.channels_create(user_1['token'], 'Channel_2', True) channel_3 = channels.channels_create(user_1['token'], 'Channel_3', True) channel_4 = channels.channels_create(user_2['token'], 'Channel_4', True) channel_5 = channels.channels_create(user_2['token'], 'Channel_5', True) assert channels.channels_listall(user_1['token']) == { 'channels': [ { 'channel_id': channel_1['channel_id'], 'name': 'Channel_1', }, { 'channel_id': channel_2['channel_id'], 'name': 'Channel_2', }, { 'channel_id': channel_3['channel_id'], 'name': 'Channel_3', }, { 'channel_id': channel_4['channel_id'], 'name': 'Channel_4', }, { 'channel_id': channel_5['channel_id'], 'name': 'Channel_5', }, ] } clear()
def test_user_authorised_user(user_1, logout_user_1): """Test for returning profile of a non-registered/invalid user. (Invalid token and u_id) """ with pytest.raises(AccessError): user.user_profile(user_1['token'], user_1['u_id']) clear()
def test_channels_listall_empty(user_1): """Test for empty channels. """ list_channels = channels.channels_listall(user_1['token']) assert len(list_channels['channels']) == 0 clear()
def test_handle_prefix(user_1, user_2, user_3): """ Testing basic handle name changes. """ user.user_profile_sethandle(user_1['token'], 'newHandle') user.user_profile_sethandle(user_2['token'], 'newHandle1') user.user_profile_sethandle(user_3['token'], 'newHandle2') clear()
def test_channels_listall_private(user_1): """Test for private channels. """ # Channel 1 and 3 are private channels. channel_1 = channels.channels_create(user_1['token'], 'Channel_1', False) channel_2 = channels.channels_create(user_1['token'], 'Channel_2', True) channel_3 = channels.channels_create(user_1['token'], 'Channel_3', False) channel_4 = channels.channels_create(user_1['token'], 'Channel_4', True) assert channels.channels_listall(user_1['token']) == { 'channels': [ { 'channel_id': channel_1['channel_id'], 'name': 'Channel_1', }, { 'channel_id': channel_2['channel_id'], 'name': 'Channel_2', }, { 'channel_id': channel_3['channel_id'], 'name': 'Channel_3', }, { 'channel_id': channel_4['channel_id'], 'name': 'Channel_4', }, ], } clear()
def test_update_handle_invalid_token(user_1): """ Testing that an invalid token will not allow you to change the handle """ auth.auth_logout(user_1['token']) with pytest.raises(AccessError): user.user_profile_sethandle(user_1['token'], 'blahblah') clear()
def test_access_leave_valid_token1(user_1, logout_user_1): """Testing if token is valid in the channels create function. """ # Token should now be invalid. with pytest.raises(AccessError): channels.channels_create(user_1['token'], 'Group 1', False) clear()
def test_img_url_forbidden_access(user_1): """ Test case where image uploaded cannot fetch its url due to a forbidden access """ img_url = "http://pngimg.com/uploads/circle/circle_PNG62.png" with pytest.raises(InputError): user.user_profile_uploadphoto(user_1['token'], img_url, 0, 0, 100, 100) clear()
def test_access_leave_valid_token3(user_1, logout_user_1): """Testing if token is valid in the channels list all functions. """ # Token should now be invalid. with pytest.raises(AccessError): channels.channels_listall(user_1['token']) clear()
def test_img_url_no_server(user_1): """ Test case where server is not running in the background """ img_url = "https://www.ottophoto.com/kirlian/kirlian_1/kirlian12.jpg" with pytest.raises(AccessError): user.user_profile_uploadphoto(user_1['token'], img_url, 0, 0, 100, 100) clear()
def test_channels_create_private(user_1, user_2, private_channel_1): """Test for a private channel. (test_user created the new_channel and is therefore already a part of it). """ with pytest.raises(AccessError): channel.channel_join(user_2['token'], private_channel_1['channel_id']) clear()
def test_secret_unique_user(): """ Testing that the secret generated is unique each time requested """ clear() email_1 = '*****@*****.**' result_1 = auth.auth_register(email_1, 'abcdefg', 'John', 'Smith') auth.auth_passwordreset_request(email_1) secret_1 = '' data = pickle.load(open("data.p", "rb")) for user in data.get_reset_users(): if user['u_id'] == result_1['u_id']: secret_1 = user['secret'] auth.auth_passwordreset_request(email_1) secret_2 = '' data = pickle.load(open("data.p", "rb")) for user in data.get_reset_users(): if user['u_id'] == result_1['u_id']: secret_2 = user['secret'] auth.auth_passwordreset_request(email_1) secret_3 = '' data = pickle.load(open("data.p", "rb")) for user in data.get_reset_users(): if user['u_id'] == result_1['u_id']: secret_3 = user['secret'] assert secret_1 != secret_2 assert secret_1 != secret_3 assert secret_2 != secret_3 clear()
def test_update_consecutively(user_1): """ Testing wheter the same token allows users to continously change their name """ user.user_profile_setname(user_1['token'], 'Bobby', 'Smith') user.user_profile_setname(user_1['token'], 'Snake', 'City') user_list = users_all(user_1['token']) for account in user_list['users']: if account['u_id'] == user_1['u_id']: assert account['name_first'] == 'Snake' assert account['name_last'] == 'City' break user.user_profile_setname(user_1['token'], 'Goku', 'Vegeta') user_list = users_all(user_1['token']) for account in user_list['users']: if account['u_id'] == user_1['u_id']: assert account['name_first'] == 'Goku' assert account['name_last'] == 'Vegeta' break user.user_profile_setname(user_1['token'], 'Will', 'Smith') user_list = users_all(user_1['token']) for account in user_list['users']: if account['u_id'] == user_1['u_id']: assert account['name_first'] == 'Will' assert account['name_last'] == 'Smith' break clear()
def test_reset_logout(): """ Testing that once password is successfully reset, the user is logged out. """ clear() email = '*****@*****.**' result = auth.auth_register(email, 'abcdefg', 'John', 'Smith') auth.auth_passwordreset_request(email) reset_code = '' data = pickle.load(open("data.p", "rb")) for user in data.get_reset_users(): if user['u_id'] == result['u_id']: reset_code = user['secret'] password = '******' auth.auth_passwordreset_reset(reset_code, password) # comparing hashed password hashed = hashlib.sha256(password.encode()).hexdigest() data = pickle.load(open("data.p", "rb")) for user in data.get_reset_users(): assert user['u_id'] != result['u_id'] # making sure new hashed password is stored data = pickle.load(open("data.p", "rb")) for user in data.get_users(): if user['u_id'] == result['u_id']: assert user['password'] == hashed clear()
def test_access_admin_not_owner_else_member(user_1, user_2, user_3): """Testing whether a member can change someone else's permissions to member """ admin_userpermission_change(user_1["token"], user_3["u_id"], OWNER) with pytest.raises(AccessError): admin_userpermission_change(user_2["token"], user_3["u_id"], MEMBER) clear()