def test_sendlater_invalid_channel_id(): ''' Testing for an error if the channel id is invalid. ''' clear() auth_register("*****@*****.**", "password", "First", "Last") user_1 = auth_register("*****@*****.**", "password", "First_1", "Last_1") channels_create(user_1.get('token'), 'channel_1', True) time = datetime.datetime.now().replace(microsecond=0) + datetime.timedelta( 0, 5) time_sent = time.timestamp() with pytest.raises(InputError): message_sendlater(user_1["token"], 123412341, 'Hello world', time_sent)
def test_search_invalid_token(): """ Tests that given a single channel that the user is a member of, and an invalid token, raises an Access Error """ clear() # Registering a user and creating a channel user = auth_register("*****@*****.**", "password", "firstName", "lastName") channel = channels_create(user['token'], "channel", True) # Forcing a message into channel['channel_messages']['messages'] message_send(user['token'], channel['channel_id'], "message string") # Calling a search with invalid token with pytest.raises(AccessError): search(-1, "message")
def test_userpermssion_change_member(): ''' Register two users, one owner as first to register, and the other member by default. Member will attempt to change owner's permission to member, only to get an AccessError. ''' clear() user_owner = auth_register('*****@*****.**', '123abc!@#*', 'Bob', 'Ross', None) user_member = auth_register('*****@*****.**', '123abc!@#*', 'Jo', 'Ross', None) member_token = user_member['token'] owner_id = user_owner['u_id'] member_level = 2 with pytest.raises(AccessError): admin_userpermission_change(member_token, owner_id, member_level)
def test_auth_register_email(): clear() with pytest.raises(InputError): assert auth_register("team1.team1\[email protected]", "wendy_pass", "first", "last") with pytest.raises(InputError): assert auth_register("team1.com", "wendy_pass", "first", "last") with pytest.raises(InputError): assert auth_register("team&@gamil.com", "wendy_pass", "first", "last") with pytest.raises(InputError): assert auth_register("*****@*****.**", "wendy_pass", "first", "last") with pytest.raises(InputError): assert auth_register("*****@*****.**", "who_pass", "first", "last") with pytest.raises(InputError): assert auth_register("", "no_one_pass", "first", "last") with pytest.raises(InputError): assert auth_register(" @google.com", "maybe_one_pass", "first", "last")
def http_clear(): ''' Wrapper function for clear (clears all data) DELETE: No input Returns JSON containing empty dict ''' return dumps(clear())
def test_message_react_invalid_reactid(): clear() test_user0 = create_one_test_user() # test_user0 creates 1 public channel channel_id = channels_create(test_user0['token'], "Public Channel", True) # test_user0 sends 1 message to public channel message0 = "inital message" message0_id = message_send(test_user0['token'], channel_id['channel_id'], message0) message_react(test_user0['token'], message0_id['message_id'], 1) with pytest.raises(InputError): assert message_unreact(test_user0['token'], message0_id['message_id'], 5)
def test_remove_owner_channel_permissions(): clear() user01, user02, user03 = register_n_users(3) channels_create(user01["token"], "channel01", is_public=True) channel02 = channels_create(user02["token"], "channel02", is_public=True) channel_join(user03["token"], channel02["channel_id"]) message = message_send(user03["token"], channel02["channel_id"], "test message") message_remove(user02["token"], message["message_id"]) assert message["message_id"] not in [ x["message_id"] for x in database["channels"][channel02["channel_id"]] ["messages"].values() ]
def test_message_pin_already_pinned(users): """ Testing that Input Error is raised when user is trying to pin a message which has already been pinned """ user1 = auth.auth_register("*****@*****.**", "iamyourfather", "Anakin", "Skywalker") chan = channels.channels_create(user1["token"], "Star Wars", True)['channel_id'] test_message1 = "Very proud of my new channel!" m_id1 = message.message_send(user1["token"], chan, test_message1)['message_id'] message.message_pin(user1["token"], m_id1) with pytest.raises(InputError): assert message.message_pin(user1["token"], m_id1) other.clear()
def test_message_unpin_already_unpinned(): """ Testing that Input Error is raised when user attempts to unpin a message which is already unpinned """ user1 = auth.auth_register("*****@*****.**", "password", "John", "Smith") chan = channels.channels_create(user1["token"], "General", True)['channel_id'] msg1 = "Hello" mess1 = message.message_send(user1["token"], chan, msg1)['message_id'] message.message_pin(user1["token"], mess1) message.message_unpin(user1["token"], mess1) with pytest.raises(InputError): assert message.message_unpin(user1["token"], mess1) other.clear()
def test_remove_not_owner_not_sender(user1): """ Tests that an error is raised when a person who is not the sender or owner is tries to remove a message """ user2, chan = user1 test_user2 = auth.auth_register("*****@*****.**", "password", "Bumble", "Bee") channel.channel_join(test_user2["token"], chan['channel_id']) message_exp = "Test 1 test 2 swiggity Swagg" message_id1 = message.message_send(user2["token"], chan['channel_id'], message_exp) with pytest.raises(AccessError): message.message_remove(test_user2["token"], message_id1["message_id"]) other.clear()
def test_send_valid(user1): """ Testing if a single message can be sent and be stored """ user1, chan = user1 message_exp = "Test 1 test 2 swiggity Swagg" message_id = message.message_send(user1["token"], chan['channel_id'], message_exp) message_from_channel = channel.channel_messages(user1["token"], chan['channel_id'], 0) assert message_exp == message_from_channel["messages"][0]["message"] assert user1["u_id"] == message_from_channel["messages"][0]["u_id"] assert message_id["message_id"] == message_from_channel["messages"][0][ "message_id"] other.clear()
def test_send_not_in_channel(users): """ Testing if a single message can be sent by someone not in the channel """ user1, user2, chan = users channel.channel_leave(user2['token'], chan['channel_id']) #The user has not joined the channel message_exp = "I'm not in the channel sad boi " with pytest.raises(AccessError): assert message.message_send(user2["token"], chan["channel_id"], message_exp) message_from_channel = channel.channel_messages(user1["token"], chan["channel_id"], 0) assert message_exp != message_from_channel["messages"] other.clear()
def test_owner_pin_from_outside_channel(): clear() auth_register("*****@*****.**", "password", "User_0", "User_last_0") user_1 = auth_register("*****@*****.**", "password", "User_1", "User_last_1") channel_1 = channels_create(user_1.get('token'), 'channel_1', True) message_info = message_send(user_1.get('token'), channel_1.get('channel_id'), 'Hello world') message_search = search(user_1['token'], 'Hello world') assert message_search['messages'][0].get('message') == 'Hello world' assert message_search['messages'][0].get('is_pinned') == False message_pin(user_1.get('token'), message_info.get('message_id')) message_search = search(user_1['token'], 'Hello world') assert message_search['messages'][0].get('message') == 'Hello world' assert message_search['messages'][0].get('is_pinned') == True
def test_sendlater_user_not_in_channel(): ''' Testing for an error if the user is not in the channel. ''' clear() user = auth_register("*****@*****.**", "password", "First", "Last") user_1 = auth_register("*****@*****.**", "password", "First_1", "Last_1") channel_1 = channels_create(user.get('token'), 'channel_1', True) time = datetime.datetime.now().replace(microsecond=0) + datetime.timedelta( 0, 5) time_sent = time.timestamp() with pytest.raises(AccessError): message_sendlater(user_1["token"], channel_1.get('channel_id'), 'w', time_sent)
def test_user_profile_single_user(): """ Tests that given a single registered user and a valid token, the user_profile function returns the correct data type """ clear() user = auth_register("*****@*****.**", "password", "firstName", "lastName") result = user_profile(user['token'], user['u_id']) assert result['user'] == { 'u_id': user['u_id'], 'email': "*****@*****.**", 'name_first': "firstName", 'name_last': "lastName", 'handle_str': "firstnamelastname", 'profile_img_url': '' }
def test_message_unpin_after_leaving(): """ Testing that Access Error is raised when owner of channel leaves and attempts to unpin a message in that channel """ user1 = auth.auth_register("*****@*****.**", "password", "John", "Smith") chan = channels.channels_create(user1["token"], "General", True)['channel_id'] msg = "Welcome!" mess = message.message_send(user1["token"], chan, msg)['message_id'] message.message_pin(user1["token"], mess) channel.channel_leave(user1["token"], chan) with pytest.raises(AccessError): assert message.message_unpin(user1["token"], mess) other.clear()
def test_unreact_curren_user_not_react(): """ This is to test the situation where the user is not reacted to a existing message, but other user has reacted to the message """ clear() user_a, user_b = register_n_users(2) channel_a_id = channels_create(user_a["token"], "channel_a", True)["channel_id"] channel_join(user_b["token"], channel_a_id) message_id = message_send(user_a["token"], channel_a_id, "Hi, user_b")["message_id"] message_react(user_b["token"], message_id, 1) # user_a react to a message that he hasn't reacted with pytest.raises(InputError): message_unreact(user_a["token"], message_id, 1)
def test_message_sendlater_success(): user = auth.auth_register('*****@*****.**', 'paswword', 'first_name', 'last_name') chan = channels.channels_create(user['token'], 'test_channel', True)['channel_id'] time = time_from_now(60) msg = 'Test message from the past!' mess = message.message_sendlater(user['token'], chan, msg, time)['message_id'] message_list = channel.channel_messages(user['token'], chan, 0) assert message_list['messages'][0]['message'] == msg assert message_list['messages'][0]['message_id'] == mess assert message_list['messages'][0]['u_id'] == user['u_id'] other.clear()
def test_send_message_too_long(): clear() usera = auth_register("*****@*****.**", "averylongpassword", "A", "LastA") channel_id = channels_create(usera["token"], "channela", is_public=True)["channel_id"] # shouldn't cause any problem message_send(usera["token"], channel_id, "a" * 1000) with pytest.raises(InputError): message_send(usera["token"], channel_id, "a" * 1001) # just to be safe with pytest.raises(InputError): message_send(usera["token"], channel_id, "a" * 1021)
def test_message_send_later_invalid_team_member(url): clear() test_data = creatData(url) wenyao_dict = test_data.register_wenyao() boyu_dict = test_data.register_boyu() channel = test_data.creat_channel(wenyao_dict['token'], 'team1', True) time_sent_1 = int(time.time()) time_sent_1 += 10 with pytest.raises(requests.exceptions.HTTPError): requests.post(url + 'message/sendlater', json={ 'token': boyu_dict['token'], 'channel_id': channel['channel_id'], 'message': "General Kenobi !", 'time_sent': time_sent_1 }).raise_for_status()
def test_admin_userpermission_change_invalid_permission_id(): ''' Test that an InputError is raised when admin_userpermission_change is given a permission_id that does not refer to a valid permission ''' clear() f_owner = auth_register('*****@*****.**', 'password', 'Bob', 'Bob') random_user = auth_register('*****@*****.**', 'password', 'Tim', 'Hall') with pytest.raises(InputError): admin_userpermission_change(f_owner['token'], random_user['u_id'], 0) with pytest.raises(InputError): admin_userpermission_change(f_owner['token'], random_user['u_id'], 3) with pytest.raises(InputError): admin_userpermission_change(f_owner['token'], random_user['u_id'], 100) with pytest.raises(InputError): admin_userpermission_change(f_owner['token'], random_user['u_id'], -5)
def test_password_reset_valid1(): '''Valid password reset by a member of flockr''' clear() # Valid information has been summitted to register from the first user auth_register("*****@*****.**", "ihfeh3hgi00d", "Yilang", "W") # Vadid information has been summitted to register from the second user auth_register("*****@*****.**", "VukkFs", "Bill", "Gates") # Vadid information has been summitted to register from the third user auth_register("*****@*****.**", "RFVtgb45678", "M", "Johnson") # User 2 send a password reset request auth_passwordreset_request("*****@*****.**") # User 2 change the password reset_code = data['users'][2]['reset_code'] auth_passwordreset_reset(reset_code, "Qwerty567") assert data['users'][2]['password'] == password_encode("Qwerty567")
def test_auth_login_email(): clear() with pytest.raises(InputError): assert auth_login("team1.team1\[email protected]", "wendy_pass") with pytest.raises(InputError): assert auth_login("team1.com", "wendy_pass") with pytest.raises(InputError): assert auth_login("team&@gamil.com", "wendy_pass") with pytest.raises(InputError): assert auth_login("*****@*****.**", "wendy_pass") with pytest.raises(InputError): assert auth_login("*****@*****.**", "who_pass") with pytest.raises(InputError): assert auth_login("", "no_one_pass") with pytest.raises(InputError): assert auth_login(" @google.com", "maybe_one_pass")
def test_password_reset_wrong_reset_code1(): '''reset_code is not a valid reset code''' clear() # Valid information has been summitted to register from the first user auth_register("*****@*****.**", "ihfeh3hgi00d", "Yilang", "W") # Vadid information has been summitted to register from the second user auth_register("*****@*****.**", "VukkFs", "Bill", "Gates") # Vadid information has been summitted to register from the third user auth_register("*****@*****.**", "RFVtgb45678", "M", "Johnson") # User 2 send a password reset request auth_passwordreset_request("*****@*****.**") # User 2 change the password with wrong reset_code reset_code = data['users'][2]['reset_code'] with pytest.raises(InputError): auth_passwordreset_reset(reset_code, "Qwer7")
def test_channels_listsall_private(): clear() test_user0 = create_one_test_user() # test_user0 creates 1 private channel priv_channel0 = channels_create(test_user0['token'], "Private Channel 0", False) assert(channels_listall(test_user0['token']) == { 'channels': [ { "channel_id": priv_channel0['channel_id'], "name": "Private Channel 0", }, ] })
def test_user_profile_setname_lastname_longerthan50(url): clear() test_data = creatData(url) boyu_dict = test_data.register_boyu() # set last name is longer than 20 with pytest.raises(requests.exceptions.HTTPError): requests.put( url + 'user/profile/setname', json={ 'token': boyu_dict['token'], 'first_name': 'xukun', 'last_name': 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' }).raise_for_status()
def test_search_single_channel_single_message_no_match(): """ Tests that given a single channel that the user is a member of, and a valid token, an empty list is returned if no match is found """ clear() # Registering a user and creating a channel user = auth_register("*****@*****.**", "password", "firstName", "lastName") channel = channels_create(user['token'], "channel", True) # Forcing a message into channel['channel_messages']['messages'] message_send(user['token'], channel['channel_id'], "message $$$4%%") # Calling a search with the query string "none" result = search(user['token'], "none") assert result['messages'] == []
def test_user_profile_request_self(user1): """ Testing successful uses of user_profile focusing on request oneselves profile """ user1_profile = { "u_id": 1, "email": "*****@*****.**", "name_first": "Kevin", "name_last": "Huang", "handle_str": "KevinHuang", "profile_img_url": "" } assert user.user_profile(user1["token"], user1["u_id"])["user"] == user1_profile other.clear()
def test_message_pin_unauthorised_user(): clear() user_01, user_02 = register_n_users(2) # Create a channel and send a message channel = channels_create(user_01["token"], "channel_01", is_public=True) channel_join(user_01["token"], channel["channel_id"]) message = message_send(user_01["token"], channel["channel_id"], "test") # Return messages in channel channel_msg = channel_messages(user_01["token"], channel["channel_id"], 0) assert channel_msg["messages"][0]["is_pinned"] == False # Ensure user_02 cannot pin message channel_join(user_02["token"], channel["channel_id"]) with pytest.raises(AccessError): message_pin(user_02["token"], message["message_id"])
def test_remove_flocker_owner_access(): ''' Testing for a valid message removed by a flockr owner. ''' clear() user = auth_register("*****@*****.**", "password", "First", "Last") user_1 = auth_register("*****@*****.**", "password", "First_1", "Last_1") channel_1 = channels_create(user_1.get('token'), 'channel_1', True) channel_join(user.get('token'), channel_1.get('channel_id')) message_info = message_send(user_1.get('token'), channel_1.get('channel_id'), 'Hello world') print(message_info) message_remove(user_1.get('token'), message_info.get('message_id')) message_search = search(user_1['token'], 'Hello world') assert message_search == {'messages': []}