def test_channels_create(): reset_data() # SETUP BEGIN # Assume all users have registered in function: test_channel_invite() auth_dic1 = auth_register("*****@*****.**", "123456", "firstone", "lastone") auth_dic1 = auth_login("*****@*****.**", "123456") token1 = auth_dic1['token'] auth_dic2 = auth_register("*****@*****.**", "123456", "firstone", "lastone") auth_dic2 = auth_login("*****@*****.**", "123456") token2 = auth_dic2['token'] # SETUP END # successful test # assume the channelid is 1 assert channels_create(token1, "channel 1", True) == {'channel_id': 0} with pytest.raises(ValueError): # Name is more than 20 characters long channels_create(token1, "qwertyuiopasdfghjklzxcvbnmqwertyuiop", True) # logout auth_logout(token1) auth_logout(token2) reset_data()
def test_user_profiles_uploadphoto(): reset_data() #SETUP Begin - initialisation of variables for calling the function authRegDict1 = auth_register("*****@*****.**", "qwerty", "John", "Smith") auth_login("*****@*****.**", "qwerty") token1 = authRegDict1['token'] # successful test user_profiles_uploadphoto( token1, "https://www.flowerglossary.com/wp-content/uploads/2019/04/blue-rose.jpg", 0, 0, 100, 100, 5001) # error test # Image uploaded is not a JPG. with pytest.raises(ValueError): user_profiles_uploadphoto(token1, "https://www.baidu.com", 0, 0, 10, 10, 5001) # any of x_start, y_start, x_end, y_end are not within the dimensions of the image at the URL. with pytest.raises(ValueError): user_profiles_uploadphoto( token1, "https://www.flowerglossary.com/wp-content/uploads/2019/04/blue-rose.jpg", 10, 10, 0, 0, 5001) auth_logout(token1)
def test_user_profile_setname(): reset_data() #SETUP Begin authRegDict1 = auth_register("*****@*****.**", "qwerty", "John", "Smith") auth_login("*****@*****.**", "qwerty") token1 = authRegDict1['token'] #SETUP End #Test Case 1: successful update of authorised user's first and last name user_profile_setname(token1, "Jim", "Smiths") #Test Case 2: Successful update of user's first and last name # containing allowed non-alphabetical character user_profile_setname(token1, "Karl-Anthony", "Towns") #Test Case 3 Unsuccessful update of user's first and last name # due to first name exceeding character limit with pytest.raises(ValueError, match='Invalid first name length'): user_profile_setname(token1, "a" * 51, "Smith") #Test Case 4: Unsuccessful update of first and last name due to last name exceeding limit with pytest.raises(ValueError, match='Invalid last name length'): user_profile_setname(token1, "John", "a" * 51) auth_logout(token1) reset_data()
def test_channels_listall(): reset_data() # SETUP BEGIN # Assume all users have registered in function: test_channel_invite() auth_dic1 = auth_register("*****@*****.**", "123456", "firstone", "lastone") auth_dic1 = auth_login("*****@*****.**", "123456") token1 = auth_dic1['token'] auth_dic2 = auth_register("*****@*****.**", "123456", "firstone", "lastone") auth_dic2 = auth_login("*****@*****.**", "123456") token2 = auth_dic2['token'] channels_create(token1, "channel 1", True) # SETUP END # successful test # because it output all channel, whatever the user is, the output should be same assert channels_listall(token1) == channels_listall(token2) # just for check # logout auth_logout(token1) auth_logout(token2) reset_data()
def test_user_profile_sethandle(): reset_data() #SETUP Begin authRegDict1 = auth_register("*****@*****.**", "qwerty", "John", "Smith") token1 = authRegDict1['token'] auth_login("*****@*****.**", "qwerty") authRegDict2 = auth_register("*****@*****.**", "qwerty1", "Johnn", "Smithh") token2 = authRegDict2['token'] auth_login("*****@*****.**", "qwerty1") #SETUP End #Test Case 1: Successful update of user's display name user_profile_sethandle(token1, "jsmith") #Test Case 2: Unsuccessful update of user's display name due to exceeding of character limit with pytest.raises(ValueError, match='Display name exceeds character limit'): user_profile_sethandle(token1, 'jsmith12345678dfghnmdfghj90qwerty') #Test Case 3: Unsuccessful update of user's display name because the handle is too short with pytest.raises(ValueError): user_profile_sethandle(token1, 'j') #Test Case 4: Unsuccessful update of user's display name due to it containing an offensive word with pytest.raises(ValueError): user_profile_sethandle(token2, 'jsmith') #User accidentally includes offensive word auth_logout(token1) auth_logout(token2) reset_data()
def test_user_profile_setemail(): reset_data() #SETUP Begin authRegDict1 = auth_register("*****@*****.**", "qwerty", "John", "Smith") auth_login("*****@*****.**", "qwerty") token1 = authRegDict1['token'] auth_register("*****@*****.**", "qwerty", "John", "Smith") #SETUP End #Test Case 1: Successful update of user's email user_profile_setemail(token1, "*****@*****.**") #Test Case 2: Unsuccessful update of user email because the email not change with pytest.raises(ValueError): user_profile_setemail(token1, "*****@*****.**") #Test Case 3: Unsuccessful update of user email due to conflict with existing email with pytest.raises(ValueError): user_profile_setemail(token1, "jim.gmail.com") #Test Case 3: Unsuccessful update of user email because the email has been used with pytest.raises(ValueError): user_profile_setemail(token1, "*****@*****.**") auth_logout(token1) reset_data()
def test_message_send(): reset_data() # SETUP BEGIN authRegisterDict1 = auth_register('*****@*****.**', '123456', 'hayden', 'Diego') auth_login('*****@*****.**', '123456') token1 = authRegisterDict1['token'] authRegisterDict2 = auth_register('*****@*****.**', '123456', 'sally', 'Juan') auth_login('*****@*****.**', '123456') token2 = authRegisterDict2['token'] channelidDict1 = channels_create(token1, 'channel_1', True) channel_id1 = channelidDict1['channel_id'] channelidDict2 = channels_create(token2, 'channel_8', True) channel_id2 = channelidDict2['channel_id'] # SETUP END # The right example message_send(token1, channel_id1, 'hello') # error test with pytest.raises(ValueError): # Message is more than 1000 characters message_send(token1, channel_id1, 'a' * 1001) with pytest.raises(AccessError): # token1 has not joined Channel channel_id2 message_send(token1, channel_id2, 'hello') # logout auth_logout(token1) auth_logout(token2) reset_data()
def test_channels_list(): reset_data() # SETUP BEGIN # Assume all users have registered in function: test_channel_invite() auth_dic1 = auth_register("*****@*****.**", "123456", "firstone", "lastone") auth_dic1 = auth_login("*****@*****.**", "123456") token1 = auth_dic1['token'] auth_dic2 = auth_register("*****@*****.**", "123456", "firstone", "lastone") auth_dic2 = auth_login("*****@*****.**", "123456") token2 = auth_dic2['token'] channels_create(token1, "channel 1", True) # SETUP END # successful test channels_list(token1) assert channels_list(token2) == {'channels': []} # logout auth_logout(token1) auth_logout(token2) reset_data()
def test_users_all(): reset_data() authRegDict1 = auth_register("*****@*****.**", "qwerty", "John", "Smith") token1 = authRegDict1['token'] auth_login("*****@*****.**", "qwerty") # successful test users_all(token1) auth_logout(token1) reset_data()
def test_channel_invite(): reset_data() # SETUP BEGIN auth_dic1 = auth_register("*****@*****.**", "123456", "firstone", "lastone") auth_dic1 = auth_login("*****@*****.**", "123456") token1 = auth_dic1['token'] uid1 = auth_dic1['u_id'] auth_dic2 = auth_register("*****@*****.**", "123456", "firstone", "lastone") auth_dic2 = auth_login("*****@*****.**", "123456") token2 = auth_dic2['token'] uid2 = auth_dic2['u_id'] auth_dic3 = auth_register("*****@*****.**", "123456", "firstone", "lastone") auth_dic3 = auth_login("*****@*****.**", "123456") token3 = auth_dic3['token'] channelid_dic1 = channels_create(token1, "channel 1", True) channel_id1 = channelid_dic1['channel_id'] channelid_dic2 = channels_create(token1, "channel 2", True) channel_id2 = channelid_dic2['channel_id'] # SETUP END # successful test assert channel_invite(token1, channel_id1, uid2) == {} # error test with pytest.raises(ValueError): # the channel if does not exist channel_invite(token1, 10, uid2) with pytest.raises(AccessError): # channel_id does not refer to a valid channel that the authorised user is part of. channel_invite(token3, channel_id2, uid2) with pytest.raises(ValueError): # u_id does not refer to a valid user # assume u_id 5 does not refer to a valid user channel_invite(token1, channel_id1, 10) with pytest.raises(ValueError): # u_id does not refer to a valid user # invite the user that is in channel (by calling function above, uid2 is in channel) channel_invite(token2, channel_id1, uid1) # logout auth_logout(token1) auth_logout(token2) auth_logout(token3) reset_data()
def test_logout(): ''' Tests auth logout ''' auth_register("*****@*****.**", "strong_pw", "A", "AA") # Test successful login -> logout assert auth_logout(auth_login("*****@*****.**", "strong_pw")['token'])['is_success'] # test logging out with an incorrect token auth_login("*****@*****.**", "strong_pw") assert not auth_logout("wrong_token")['is_success']
def test_message_sendlater(): reset_data() # SETUP BEGIN authRegisterDict1 = auth_register('*****@*****.**', '123456', 'hayden', 'Diego') auth_login('*****@*****.**', '123456') token1 = authRegisterDict1['token'] authRegisterDict2 = auth_register('*****@*****.**', '123456', 'sally', 'Juan') auth_login('*****@*****.**', '123456') token2 = authRegisterDict2['token'] channelidDict1 = channels_create(token1, 'channel_1', True) channel_id1 = channelidDict1['channel_id'] channelidDict2 = channels_create(token2, 'channel_8', True) channel_id2 = channelidDict2['channel_id'] # SETUP END # The right example ''' later_time = datetime.datetime.utcnow() + datetime.timedelta(seconds=50) str_later_time = later_time.strftime("%Y-%m-%d %H:%M:%S") timeArray = time.strptime(str_later_time, "%Y-%m-%d %H:%M:%S") timestamp = time.mktime(timeArray) print(timestamp) ''' timestamp = int(datetime.datetime.now().strftime("%s")) + 3 mid = message_sendlater(token1, channel_id1, 'hello', timestamp) assert mid['message_id'] == 1 # error example with pytest.raises(ValueError): # Message is more than 1000 characters message_sendlater(token1, channel_id1, 'a' * 1001, 1603066156) with pytest.raises(ValueError): # Assume Channel channel_id 3 does not exist (not valid channel) message_sendlater(token1, 3, 'hello', 1603066156) with pytest.raises(ValueError): # Assume Time 2018-10-01 00:09:06 is a time in the past (1538352546 in Timestamp) message_sendlater(token1, channel_id1, 'hello', 1538352546) with pytest.raises(AccessError): # token1 has not joined Channel channel_id2 message_sendlater(token1, channel_id2, 'hello', 1603066156) # logout auth_logout(token1) auth_logout(token2) reset_data()
def test_message_unpin(): reset_data() # SETUP BEGIN authRegisterDict1 = auth_register('*****@*****.**', '123456', 'hayden', 'Diego') auth_login('*****@*****.**', '123456') token1 = authRegisterDict1['token'] authRegisterDict2 = auth_register('*****@*****.**', '123456', 'sally', 'Juan') auth_login('*****@*****.**', '123456') token2 = authRegisterDict2['token'] authRegisterDict3 = auth_register('*****@*****.**', '123456', 'Nena', 'Smith') auth_login('*****@*****.**', '123456') token3 = authRegisterDict3['token'] authRegisterDict4 = auth_register('*****@*****.**', '123456', 'Carmen', 'Davis') auth_login('*****@*****.**', '123456') token4 = authRegisterDict4['token'] channelidDict1 = channels_create(token1, 'channel_1', True) channel_id1 = channelidDict1['channel_id'] channel_join(token2, channel_id1) messages_dic1 = message_send(token1, channel_id1, "hello") message_id1 = messages_dic1['message_id'] messagedic2 = message_send(token1, channel_id1, "helloooo") message_id2 = messagedic2['message_id'] message_pin(token1, message_id1) message_pin(token1, message_id2) # SETUP END # The right example assert message_unpin(token1, message_id1) == {} with pytest.raises(ValueError): # message_id 8 is not a valid message message_unpin(token1, 8) with pytest.raises(ValueError): # The authorised user token2 is not an admin message_unpin(token2, message_id2) with pytest.raises(ValueError): # Message message_1 is already unpinned message_unpin(token1, message_id1) with pytest.raises(AccessError): # The authorised user is not a member of the channel that the message is within message_unpin(token3, message_id2) # logout auth_logout(token1) auth_logout(token2) auth_logout(token3) auth_logout(token4) reset_data()
def test_admin_userpermission_change(): ''' assumption: [email protected] is the only registered email for now. 2.all the value errors have been modified into specfic words. 3.there is a permission id dictionary which contains 3 different types of permission id. [email protected] hasn't been registered. ''' #successful test: reset_data() auth_dic1 = auth_register("*****@*****.**", "123456", "firstone", "lastone") auth_dic1 = auth_login("*****@*****.**", "123456") token1 = auth_dic1['token'] uid1 = auth_dic1['u_id'] auth_dic2 = auth_register("*****@*****.**", "123456", "firstone", "lastone") auth_dic2 = auth_login("*****@*****.**", "123456") token2 = auth_dic2['token'] uid2 = auth_dic2['u_id'] auth_dic3 = auth_register("*****@*****.**", "123456", "firstone", "lastone") auth_dic3 = auth_login("*****@*****.**", "123456") token3 = auth_dic3['token'] assert admin_userpermission_change(token1, uid2, 1) == {} #test1:u_id does not refer to a valid user with pytest.raises(ValueError): admin_userpermission_change(token1, 10, 1) #test2:permission_id does not refer to a value permission with pytest.raises( ValueError, match=r'permission_id does not refer to a value permission'): admin_userpermission_change(token1, uid2, 10) #test3:The authorised user is not an admin or owner with pytest.raises(AccessError, match=r'the authorised user is not an admin or owner'): admin_userpermission_change(token3, uid1, 1) auth_logout(token1) auth_logout(token2) auth_logout(token3) reset_data()
def test_message_edit(): reset_data() # SETUP BEGIN authRegisterDict1 = auth_register('*****@*****.**', '123456', 'hayden', 'Diego') auth_login('*****@*****.**', '123456') token1 = authRegisterDict1['token'] authRegisterDict2 = auth_register('*****@*****.**', '123456', 'sally', 'Juan') auth_login('*****@*****.**', '123456') token2 = authRegisterDict2['token'] authRegisterDict3 = auth_register('*****@*****.**', '123456', 'Nena', 'Smith') auth_login('*****@*****.**', '123456') token3 = authRegisterDict3['token'] authRegisterDict4 = auth_register('*****@*****.**', '123456', 'Carmen', 'Davis') auth_login('*****@*****.**', '123456') token4 = authRegisterDict4['token'] channelidDict1 = channels_create(token1, 'channel_1', True) channel_id1 = channelidDict1['channel_id'] channel_join(token2, channel_id1) messages_dic1 = message_send(token1, channel_id1, "hello") message_id1 = messages_dic1['message_id'] # SETUP END # The right example # Assume the message id 1 is a valid id for user1 assert message_edit(token1, message_id1, 'hello') == {} with pytest.raises(ValueError): # Assume Message id 2 does not exist message_edit(token1, 10, 'hello') with pytest.raises(ValueError): # Message is more than 1000 characters message_edit(token1, message_id1, 'a' * 1001) with pytest.raises(AccessError): # message_id1 was not sent by the authorised user token2 making this request message_edit(token2, message_id1, 'hello') with pytest.raises(AccessError): # token3 is not an owner of this channel message_edit(token3, message_id1, 'hello') with pytest.raises(AccessError): # token4 is not an admin or owner of the slack message_edit(token4, message_id1, 'hello') # logout auth_logout(token1) auth_logout(token2) auth_logout(token3) auth_logout(token4) reset_data()
def test_search(): ''' assumption: [email protected] is the only registered email for now. 2.all the value errors have been modified into specfic words. 3.the channel called "channelname" hasn't been created ''' #successful test: #login reset_data() auth_register('*****@*****.**', 'q1w2e3r4', 'BEAR', 'XI') login1_dic = auth_login('*****@*****.**', 'q1w2e3r4') token = login1_dic['token'] #create a channel channelValue = channels_create(token, "channelname", True) channel_id = channelValue['channel_id'] #sending a message message_send(token, channel_id, "1q2w3e4r") #search messages_dic = search(token, "1q") assert messages_dic['messages'][0]['message'] == "1q2w3e4r" #logout auth_logout(token) reset_data()
def test_channel_messages(): reset_data() # SETUP BEGIN # Assume all users have registered in function: test_channel_invite() auth_dic1 = auth_register("*****@*****.**", "123456", "firstone", "lastone") auth_dic1 = auth_login("*****@*****.**", "123456") token1 = auth_dic1['token'] auth_dic2 = auth_register("*****@*****.**", "123456", "firstone", "lastone") auth_dic2 = auth_login("*****@*****.**", "123456") token2 = auth_dic2['token'] channelid_dic1 = channels_create(token1, "channel 1", True) channel_id1 = channelid_dic1['channel_id'] # SETUP END # successful test # assume the total number of messages in the channel is 0 # which means there is no message in the channel # assume the start number is 0 # send a message to channel1 message_send(token1, channel_id1, "hello") channel_messages(token1, channel_id1, 0) # error test with pytest.raises(ValueError): # Channel (based on ID) does not exist # assume the channel ID 2 does not exist channel_messages(token1, 10, 0) with pytest.raises(ValueError): # start is greater than the total number of messages in the channel # the total number of messages is 1, start is 2 channel_messages(token1, channel_id1, 2) with pytest.raises(AccessError): # Authorised user is not a member of channel with channel_id channel_messages(token2, channel_id1, 0) # logout auth_logout(token1) auth_logout(token2) reset_data()
def test_auth_login(): ''' assumption: [email protected] is the only registered email. 2.all the value errors have been modified into specfic words. ''' reset_data() #successful test user_dic = auth_register('*****@*****.**', '1q2w3e4r', 'HAHA', 'JIANG') login_dic = auth_login('*****@*****.**', '1q2w3e4r') assert user_dic['u_id'] == login_dic['u_id'] token = login_dic['token'] #test1: login with invalid email: with pytest.raises(ValueError, match=r'Invalid email'): auth_login('wrongemail.com', '1q2w3e4r') #test2: login with email which doesn't belong to a user: with pytest.raises(ValueError, match=r'Email entered does not belong to a user'): auth_login("*****@*****.**", "123456") #test3: login with a user's email but the password is wrong: with pytest.raises(ValueError, match=r'Password incorrect'): auth_login('*****@*****.**', 'wrongpassword') auth_logout(token) reset_data()
def test_channel_join(): reset_data() # SETUP BEGIN # Assume all users have registered in function: test_channel_invite() auth_dic1 = auth_register("*****@*****.**", "123456", "firstone", "lastone") auth_dic1 = auth_login("*****@*****.**", "123456") token1 = auth_dic1['token'] auth_dic2 = auth_register("*****@*****.**", "123456", "firstone", "lastone") auth_dic2 = auth_login("*****@*****.**", "123456") token2 = auth_dic2['token'] channelid_dic1 = channels_create(token1, "channel 1", True) channel_id1 = channelid_dic1['channel_id'] # create a private channel channelid_dic2 = channels_create(token1, "channel 2", False) channel_id2 = channelid_dic2['channel_id'] # SETUP END # successful test assert channel_join(token2, channel_id1) == {} # error test with pytest.raises(ValueError): # Channel (based on ID) does not exist # assume channel id 3 does not exist channel_join(token1, 10) with pytest.raises(ValueError): # if token is already a member in this channel channel_join(token2, channel_id1) with pytest.raises(AccessError): # channel_id refers to a channel that is private (when the authorised user is not an admin) channel_join(token2, channel_id2) # logout auth_logout(token1) auth_logout(token2) reset_data()
def test_message_react(): reset_data() # SETUP BEGIN authRegisterDict1 = auth_register('*****@*****.**', '123456', 'hayden', 'Diego') auth_login('*****@*****.**', '123456') token1 = authRegisterDict1['token'] authRegisterDict2 = auth_register('*****@*****.**', '123456', 'sally', 'Juan') auth_login('*****@*****.**', '123456') token2 = authRegisterDict2['token'] authRegisterDict3 = auth_register('*****@*****.**', '123456', 'Nena', 'Smith') auth_login('*****@*****.**', '123456') token3 = authRegisterDict3['token'] authRegisterDict4 = auth_register('*****@*****.**', '123456', 'Carmen', 'Davis') auth_login('*****@*****.**', '123456') token4 = authRegisterDict4['token'] channelidDict1 = channels_create(token1, 'channel_1', True) channel_id1 = channelidDict1['channel_id'] channel_join(token2, channel_id1) messages_dic1 = message_send(token1, channel_id1, "hello") message_id1 = messages_dic1['message_id'] # SETUP END # The right example # message_id1 is a valid message for user1 # assume the react id 1 is a valid id assert message_react(token1, message_id1, 1) == {} # error example with pytest.raises(ValueError): # assume message_id 2 is not a valid message message_react(token1, 2, 1) with pytest.raises(ValueError): # assume react_id id 3 is not a valid React ID message_react(token1, message_id1, 3) with pytest.raises(ValueError): # message_id 1 already contains an active React with react_id 1 message_react(token1, message_id1, 1) with pytest.raises(ValueError): # user3 is not a member of channel which contain messageid1 message_react(token3, message_id1, 1) # logout auth_logout(token1) auth_logout(token2) auth_logout(token3) auth_logout(token4)
def post_auth_login(): """ Login an existing user """ email = request.form.get('email') password = request.form.get('password') try: output = auth_login(email, password) save() except ValueError as e: return str(e) return dumps({'u_id': output['u_id'], 'token': str(output['token'])})
def test_user_profile(): reset_data() #SETUP Begin - initialisation of variables for calling the function authRegDict1 = auth_register("*****@*****.**", "qwerty", "John", "Smith") auth_login("*****@*****.**", "qwerty") token1 = authRegDict1['token'] userId1 = authRegDict1['u_id'] #SETUP End #Test Case 1: successful case where calling user_profile returns the desired result assert user_profile(token1, userId1)['email'] == "*****@*****.**" #Test Case 2: Unsuccessful case where an invalid user ID is provided with pytest.raises(ValueError, match='not a valid u_id'): # the uis 10 do not exist user_profile(token1, 10) auth_logout(token1) reset_data()
def test_channel_leave(): reset_data() # SETUP BEGIN # Assume all users have registered in function: test_channel_invite() auth_dic1 = auth_register("*****@*****.**", "123456", "firstone", "lastone") auth_dic1 = auth_login("*****@*****.**", "123456") token1 = auth_dic1['token'] auth_dic2 = auth_register("*****@*****.**", "123456", "firstone", "lastone") auth_dic2 = auth_login("*****@*****.**", "123456") token2 = auth_dic2['token'] channelid_dic1 = channels_create(token1, "channel 1", True) channel_id1 = channelid_dic1['channel_id'] channel_join(token2, channel_id1) # SETUP END # successful test assert channel_leave(token2, channel_id1) == {} # error test with pytest.raises(AccessError): # if the user is not a member of channel with channel id channel_leave(token2, channel_id1) # assume if no one in the channel, the channel will does not exist with pytest.raises(ValueError): # Channel (based on ID) does not exist channel_leave(token1, 10) # if there is only one owner in the channel, cannot remove with pytest.raises(ValueError): channel_leave(token1, channel_id1) # logout auth_logout(token1) auth_logout(token2) reset_data()
def test_channel_details(): reset_data() # SETUP BEGIN # Assume all users have registered in function: test_channel_invite() auth_dic1 = auth_register("*****@*****.**", "123456", "firstone", "lastone") auth_dic1 = auth_login("*****@*****.**", "123456") token1 = auth_dic1['token'] auth_dic2 = auth_register("*****@*****.**", "123456", "firstone", "lastone") auth_dic2 = auth_login("*****@*****.**", "123456") token2 = auth_dic2['token'] channelid_dic1 = channels_create(token1, "channel 1", True) channel_id1 = channelid_dic1['channel_id'] # SETUP END # successful test # assume channel_details function return the channel name # and the uid of the owner_members/members channel_dic = channel_details(token1, channel_id1) assert channel_dic['name'] == "channel 1" # error test with pytest.raises(ValueError): # Channel (based on ID) does not exist # assume channel ID 2 does not exist channel_details(token1, 10) ''' with pytest.raises(AccessError): # Authorised user is not a member of channel with channel_id # user2 is not a member of channel1 channel_details(token2, channel_id1) ''' # logout auth_logout(token1) auth_logout(token2) reset_data()
def test_auth_logout(): ''' assumption: [email protected] is the only registered email. 2.all the value errors have been modified into specfic words. ''' #successful test auth_register('*****@*****.**', 'q1w2e3r4', 'BEAR', 'XI') login_dic = auth_login('*****@*****.**', 'q1w2e3r4') assert auth_logout(login_dic['token']) == {'is_success': True} reset_data()
def test_auth_register(): ''' assumption: [email protected] is the only registered email for now. 2.all the value errors have been modified into specfic words. 3.the [email protected] is a valid email which hasn't been registered. ''' reset_data() #successful test register_dic = auth_register('*****@*****.**', 'q1w2e3r4', 'BEAR', 'XI') login_dic1 = auth_login('*****@*****.**', 'q1w2e3r4') token = login_dic1['token'] assert register_dic['u_id'] == login_dic1['u_id'] #test1: register with an invalid email with pytest.raises(ValueError, match=r'Invalid email'): auth_register('wrongemail.com', '1q2w3e4r', 'HAHA', 'JIANG') #test2: the email has been registered with pytest.raises(ValueError, match=r'email has been used'): auth_register('*****@*****.**', '1q2w3e4r', 'HHAHA', 'JJIANG') #test3: the password used for registering with wrong format with pytest.raises(ValueError, match=r'Invalid password'): auth_register('*****@*****.**', '123', 'HAHA', 'JIANG') #test4: the user's first name is too long with pytest.raises(ValueError, match=r'Invalid first name length'): auth_register('*****@*****.**', '123456', 'HAHA'*13, 'JIANG') #test5: the user's last name is too long with pytest.raises(ValueError, match=r'Invalid last name length'): auth_register('*****@*****.**', '123456', 'HAHA', 'JIANG'*11) auth_logout(token) reset_data()
def login(): email = request.form.get('email') password = request.form.get('password') dumpstring = auth.auth_login(email, password) return dumps(dumpstring)
def test_channel_addowner(): reset_data() # SETUP BEGIN # Assume all users have registered in function: test_channel_invite() auth_dic1 = auth_register("*****@*****.**", "123456", "firstone", "lastone") auth_login("*****@*****.**", "123456") token1 = auth_dic1['token'] auth_dic2 = auth_register("*****@*****.**", "123456", "firstone", "lastone") auth_dic2 = auth_login("*****@*****.**", "123456") token2 = auth_dic2['token'] uid2 = auth_dic2['u_id'] auth_dic3 = auth_register("*****@*****.**", "123456", "firstone", "lastone") auth_dic3 = auth_login("*****@*****.**", "123456") token3 = auth_dic3['token'] uid3 = auth_dic3['u_id'] channelid_dic1 = channels_create(token1, "channel 1", True) channel_id1 = channelid_dic1['channel_id'] channel_join(token2, channel_id1) channel_join(token3, channel_id1) # create a private channel channelid_dic2 = channels_create(token2, "channel 2", True) channel_id2 = channelid_dic2['channel_id'] channel_join(token3, channel_id2) # SETUP END # successful test assert channel_addowner(token1, channel_id1, uid2) == {} assert channel_addowner(token1, channel_id2, uid3) == {} # token 1 is the owner of the slackr # error test with pytest.raises(ValueError): # Channel (based on ID) does not exist channel_addowner(token1, 10, uid2) with pytest.raises(ValueError): # When user with user id u_id is already an owner of the channel channel_addowner(token1, channel_id1, uid2) with pytest.raises(ValueError): # When the user with u_id is not a member of the channel channel_addowner(token1, channel_id2, uid2) with pytest.raises(AccessError): # when the authorised user is not an owner of the slackr, or an owner of this channel channel_addowner(token3, channel_id1, uid3) # logout auth_logout(token1) auth_logout(token2) auth_logout(token3) reset_data()
def test_message_remove(): reset_data() # SETUP BEGIN authRegisterDict1 = auth_register('*****@*****.**', '123456', 'hayden', 'Diego') auth_login('*****@*****.**', '123456') token1 = authRegisterDict1['token'] authRegisterDict2 = auth_register('*****@*****.**', '123456', 'sally', 'Juan') auth_login('*****@*****.**', '123456') token2 = authRegisterDict2['token'] uid2 = authRegisterDict2['u_id'] authRegisterDict3 = auth_register('*****@*****.**', '123456', 'Nena', 'Smith') auth_login('*****@*****.**', '123456') token3 = authRegisterDict3['token'] authRegisterDict4 = auth_register('*****@*****.**', '123456', 'Carmen', 'Davis') auth_login('*****@*****.**', '123456') token4 = authRegisterDict4['token'] channelidDict1 = channels_create(token1, 'channel_1', True) channel_id1 = channelidDict1['channel_id'] channel_join(token2, channel_id1) channel_addowner(token1, channel_id1, uid2) channel_join(token3, channel_id1) messages_dic1 = message_send(token1, channel_id1, "hello") message_id = messages_dic1['message_id'] # SETUP END # The right example # the message_id is a valid message for user1 assert message_remove(token1, message_id) == {} # send again messages_dic1 = message_send(token1, channel_id1, "hello") message_id = messages_dic1['message_id'] # the right example # Message_id was not sent by the authorised user token2, # but the token2 is a owner of this channel assert message_remove(token2, message_id) == {} # send again messages_dic1 = message_send(token1, channel_id1, "hello") message_id = messages_dic1['message_id'] with pytest.raises(ValueError): # assume Message id 2 no longer exists message_remove(token1, 10) with pytest.raises(AccessError): # assume token3 is not an owner of this channel that contain message_id message_remove(token3, message_id) with pytest.raises(AccessError): # assume token4 is not an admin or owner of the slack message_remove(token4, message_id) # logout auth_logout(token1) auth_logout(token2) auth_logout(token3) auth_logout(token4)
def test_login(): ''' Tests auth_login ''' auth_register('*****@*****.**', 'strong_pw', 'vvv', 'wwww') # Invalid email with pytest.raises(ValueError): auth_login("invalidEmail", "strong_pw") # Invalid password with pytest.raises(ValueError): auth_login("*****@*****.**", "incorrectPW") # Email does not belong to user with pytest.raises(ValueError): # logging in for user B should fail auth_login("*****@*****.**", "random_pw") # logging in with wrong password should fail auth_login("*****@*****.**", "wrong_pw") # logging in with invalid password should fail auth_login("*****@*****.**", "1") # logging in with empty strings should fail # check output is returned with correct parameters output = auth_login("*****@*****.**", "strong_pw") assert output == { 'u_id': token_to_user(output['token']), 'token': output['token'] }