def test_chan_create_invalid_name(): test_user = auth_register("*****@*****.**", "1234567", "John", "Smith") with pytest.raises(InputError): channels_create(test_user['token'], 'n' * 21, True) #Assumption: channels_create raises an InputError if given empty name with pytest.raises(InputError): channels_create(test_user['token'], '', True)
def test_sending_message_without_channel_access(new_channel_and_user): """Tests that a user sending a message to a channel they do not have access to, throws an error """ unauthorized_user = auth_register("*****@*****.**", "password", "first_name1", "last_name1") with pytest.raises(AccessError): assert message_send(unauthorized_user['token'], new_channel_and_user['channel_id'], 'a')
def test_chan_create_invalid_token(): test_user = auth_register("*****@*****.**", "1234567", "John", "Smith") token = test_user['token'] assert auth_logout(token) #invalidating token with pytest.raises(AccessError): auth_logout(token) #confirming that token is an invalid token with pytest.raises(AccessError): channels_create(token, 'name', True)
def test_editing_authored_message(new_channel_and_user): """ Tests the successful edit of a message from a user that sent it""" author = auth_register("*****@*****.**", "password", "first_name1", "last_name1") channel_invite(new_channel_and_user['token'], new_channel_and_user['channel_id'], author['u_id']) message = message_send(author['token'], new_channel_and_user['channel_id'], 'a') message_edit(author['token'], message['message_id'], 'new message to replace existing message')
def test_register_email_invalid(): """Tests that registration throws an input error for invalid emails, invalid emails are defined by https://www.geeksforgeeks.org/check-if-email-address-valid-or-not-in-python/ """ with pytest.raises(InputError): assert auth_register('1234', 'password', 'placeholder_first_name', 'placeholder_last_name') with pytest.raises(InputError): assert auth_register('@unsw.edu.au', 'password', 'placeholder_first_name', 'placeholder_last_name') with pytest.raises(InputError): assert auth_register('username', 'password', 'placeholder_first_name', 'placeholder_last_name') with pytest.raises(InputError): assert auth_register('', 'password', 'placeholder_first_name', 'placeholder_last_name') with pytest.raises(InputError): assert auth_register('z55555555.unsw.edu.au', 'password', 'placeholder_first_name', 'placeholder_last_name')
def test_searching_multiple_channels_with_no_access(): '''search should find all messages containing the query string THAT THE USER HAS ACCESS TO''' andrew = auth_register("*****@*****.**", "password", "andrew", "taylor") andrews_channel = channels_create(andrew['token'], "andrews channel", False) message_send(andrew['token'], andrews_channel['channel_id'], 'this is a test message in andrews channel') john = auth_register("*****@*****.**", "password", "john", "smith") johns_channel = channels_create(john['token'], "channel_b", False) message_send(john['token'], johns_channel['channel_id'], 'this is a test message in johns channel') andrews_search_results = search(andrew['token'], 'this is a test message') assert isinstance(andrews_search_results, dict) assert len(andrews_search_results['messages']) == 1 johns_search_results = search(john['token'], 'this is a test message') assert isinstance(johns_search_results, dict) assert len(johns_search_results['messages']) == 1
def test_login_valid_credentials(): """Tests logging in with valid information returns a dictionary described in the spec""" new_user = auth_register('*****@*****.**', 'password', 'placeholder_first_name', 'placeholder_last_name') login = auth_login('*****@*****.**', 'password') assert isinstance(login, dict) assert 'u_id' in login assert isinstance(login['u_id'], int) assert 'token' in login assert isinstance(login['token'], str) assert login['u_id'] == new_user['u_id']
def test_editing_authored_message_with_empty_message(new_channel_and_user): """ Tests replacing a message with an empty message removes the messages""" author = auth_register("*****@*****.**", "password", "first_name1", "last_name1") channel_invite(new_channel_and_user['token'], new_channel_and_user['channel_id'], author['u_id']) message = message_send(author['token'], new_channel_and_user['channel_id'], 'a') message_edit(author['token'], message['message_id'], '') with pytest.raises(InputError): message_edit(author['token'], message['message_id'], '')
def test_removing_a_message_unauthorized_user(new_channel_and_user): """Tests that an error is thrown when the user is not authorised to remove the message A user is not authorised if they are not authorised to see the channel """ message = message_send(new_channel_and_user['token'], new_channel_and_user['channel_id'], 'a') unauthorized_user = auth_register("*****@*****.**", "password", "first_name1", "last_name1") with pytest.raises(AccessError): assert message_remove(unauthorized_user['token'], message['message_id'])
def test_users_all_normal_test(): # set up the user user_andrew = auth_register("*****@*****.**", "password", "andrew", "taylor") user_chris = auth_register("*****@*****.**", "pilotpassword", "chris", "chen") # Access the users info by andrew's token users = users_all(user_andrew['token'])['users'] assert len(users) == 2 for user in users: if user['u_id'] == user_andrew['u_id']: assert user['email'] == "*****@*****.**" assert user['name_first'] == "andrew" assert user['name_last'] == "taylor" else: assert user['email'] == "*****@*****.**" assert user['name_first'] == "chris" assert user['name_last'] == "chen"
def test_searching_multiple_channels(): '''search should find all messages containing the query string regardless of channel''' andrew = auth_register("*****@*****.**", "password", "andrew", "taylor") new_channel_a = channels_create(andrew['token'], "channel_a", True) new_channel_b = channels_create(andrew['token'], "channel_b", True) message_send(andrew['token'], new_channel_a['channel_id'], f'this is a test message in channel a') message_send(andrew['token'], new_channel_b['channel_id'], f'this is a test message in channel b') search_results = search(andrew['token'], 'this is a test message') assert isinstance(search_results, dict) assert len(search_results['messages']) == 2
def user1(): email = "*****@*****.**" name_first = "John" name_last = "Smith" user = auth_register(email, "1234567", name_first, name_last) return { 'token': user['token'], 'u_id': user['u_id'], 'email': email, 'name_first': name_first, 'name_last': name_last }
def user2(): email = "*****@*****.**" name_first = "Jane" name_last = "Doe" user = auth_register(email, "1234567", name_first, name_last) return { 'token': user['token'], 'u_id': user['u_id'], 'email': email, 'name_first': name_first, 'name_last': name_last, }
def test_removing_a_message_neither_author_nor_owner(new_channel_and_user): """Tests that an error is thrown when the user is not authorised to remove the message A user is not authorised if they are i not the author of the message and ii: not an admin/owner of the chat """ not_author = auth_register("*****@*****.**", "password", "first_name1", "last_name1") message = message_send(new_channel_and_user['token'], new_channel_and_user['channel_id'], 'a') channel_invite(new_channel_and_user['token'], new_channel_and_user['channel_id'], not_author['u_id']) with pytest.raises(AccessError): assert message_remove(not_author['token'], message['message_id'])
def test_register_name_length(): """Tests that registration throws an input error when the name entered is less than 1 character or more than 50 characters""" with pytest.raises(InputError): auth_register('*****@*****.**', 'password', '', 'placeholder_last_name') with pytest.raises(InputError): auth_register('*****@*****.**', 'password', '1' * 51, 'placeholder_last_name') with pytest.raises(InputError): auth_register('*****@*****.**', 'password', 'placeholder_first_name', '') with pytest.raises(InputError): auth_register('*****@*****.**', 'password', 'placeholder_first_name', '1' * 51)
def test_listall_return_two(): test_user = auth_register("*****@*****.**", "1234567", "John", "Smith") channel1_name = 'My Channel' channel2_name = 'My Second Channel' channel1 = channels_create(test_user['token'], channel1_name, True) channel2 = channels_create(test_user['token'], channel2_name, False) chan_lst = channels_listall(test_user['token'])['channels'] assert len(chan_lst) == 2 channel1_details = chan_lst[0] channel2_details = chan_lst[1] assert channel1['channel_id'] == channel1_details['channel_id'] assert channel1_name == channel1_details['name'] # Checking that channel2's details match assert channel2['channel_id'] == channel2_details['channel_id'] assert channel2_name == channel2_details['name']
def test_list_return_one(): test_user = auth_register("*****@*****.**", "1234567", "John", "Smith") channel_name = 'My Channel' channel = channels_create(test_user['token'], channel_name, True) chan_lst = channels_list(test_user['token'])['channels'] assert isinstance(chan_lst, list) # One channel created. len should be 1 assert len(chan_lst) == 1 assert isinstance(chan_lst[0], dict) assert 'channel_id' in chan_lst[0].keys() assert 'name' in chan_lst[0].keys() # Checking that the returned details match the channel that was created #Assumption: channels_list lists channels in the order that the the user became a member of them assert channel['channel_id'] == chan_lst[0]['channel_id'] assert channel_name == chan_lst[0]['name']
def test_application_clean(): ''' Tests that all global variables have been emptied by the reset ''' for new_user in range(100): user = auth_register("z55555" + str(new_user) + "@unsw.edu.au", "f for hayden rip", "hydaen", "smith") channels_create(user['token'], "test channel" + str(new_user), True) workspace_reset() assert len(get_channels().keys()) == 0 assert len(get_users().keys()) == 0 assert len(get_users()) == 0 assert len(get_slackr_owners()) == 0 assert len(get_valid_tokens()) == 0 original_image_folder = os.path.join(os.getcwd(), 'images/original') assert len(os.listdir(original_image_folder)) == 1 cropped_image_folder = os.path.join(os.getcwd(), 'images/cropped') assert len(os.listdir(cropped_image_folder)) == 1
def test_search_for_one_message(): '''searching with valid paramters and checking the returned object is correct''' andrew = auth_register("*****@*****.**", "password", "andrew", "taylor") new_channel = channels_create(andrew['token'], "channel_1", True) new_message = message_send(andrew['token'], new_channel['channel_id'], f'this is a test message') search_results = search(andrew['token'], 'test message') assert isinstance(search_results, dict) assert len(search_results['messages']) == 1 message = search_results['messages'][0] assert isinstance(message, dict) assert 'message_id' in message assert isinstance(message['message_id'], int) assert message['message_id'] == new_message['message_id'] assert 'u_id' in message assert isinstance(message['u_id'], int) assert 'message' in message assert isinstance(message['message'], str) assert 'time_created' in message assert isinstance(message['time_created'], int)
def test_login_password_incorrect(): """Tests that logging in throws an error when the password is incorrect""" auth_register('*****@*****.**', 'password', 'placeholder_first_name', 'placeholder_last_name') with pytest.raises(InputError): auth_login('*****@*****.**', 'incorrect password')
def test_search_with_empty_query_string(): '''searching with an empty query string throws an input error''' andrew = auth_register("*****@*****.**", "password", "andrew", "taylor") messages = search(andrew['token'], '')['messages'] assert len(messages) == 0
def auth_register_wsgi(): json = request.get_json() return jsonify( auth_register(json['email'], str(json['password']), json['name_first'], json['name_last']))
def new_user_4(): """create a new user""" return auth_register("*****@*****.**", "password4", "first_name4", "last_name4")
def new_user_2(): """create a new user""" return auth_register("*****@*****.**", "password2", "first_name2", "last_name2")
def new_user_3(): """create a new user""" return auth_register("*****@*****.**", "password3", "first_name3", "last_name3")
def new_user(): """creates a new user""" return auth_register("*****@*****.**", "password", "first_name", "last_name")
def user_chas(): return auth_register("*****@*****.**", "chas123", "chas", "zhu")
def user_dav(): return auth_register("*****@*****.**", "dav123", "dav", "zhu")
def test_logout(): """Tests logging out after logging in""" new_user = auth_register('*****@*****.**', 'password', 'placeholder_first_name', 'placeholder_last_name') assert auth_logout(new_user['token']) == {'is_success': True}
def test_profile_invalid_token(inv_token): test_user = auth_register("*****@*****.**", "1234567", "John", "Smith") with pytest.raises(AccessError): user_profile(inv_token, test_user['u_id'])