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_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_remove_diffuser(): ''' Test remove as a different user in the channel ''' reset_data() # SETUP registered_user = auth_register("*****@*****.**", "123456", "John", "Smith") token = registered_user['token'] c_id = channel_create(token, "Channel1", 'true') # Add two messages message_send(token, c_id, "Hello world!") message_send(token, c_id, "another message") message_list = get_messages(c_id) assert find_message(message_list, "Hello world!") == "Hello world!" assert find_message(message_list, "another message") == "another message" msg1_id = get_message_id(message_list, "Hello world!") # Logout auth_logout(token) # Login as a new user registered_user = auth_register("*****@*****.**", "1password", "Bob", "Smith") token = registered_user['token'] # SETUP END with pytest.raises(AccessError, match=r"*"): message_remove(token, msg1_id)
def test_edit_diffuser(): ''' Test edit as a different user ''' reset_data() # SETUP registered_user = auth_register('*****@*****.**', '123456', 'John', 'Smith') token = registered_user['token'] c_id = channel_create(token, "Channel1", 'true') # SETUP END # Add two messages message_send(token, c_id, "Hello world!") message_send(token, c_id, "another message") message_list = get_messages(c_id) assert find_message(message_list, "Hello world!") == "Hello world!" assert find_message(message_list, "another message") == "another message" msg1_id = get_message_id(message_list, "Hello world!") # Logout auth_logout(token) # SETUP registered_user = auth_register('*****@*****.**', '123456', 'John', 'Smith') token = registered_user['token'] # SETUP END with pytest.raises(AccessError, match=r"*"): message_edit(token, msg1_id, "update")
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_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_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_standup_active(): reset_data() #SETUP Begin authRegDict1 = auth_register("*****@*****.**", "qwerty", "John", "Smith") token1 = authRegDict1['token'] channelCreateDict1 = channels_create(token1, 'New Channel', True) channel_id1 = channelCreateDict1['channel_id'] authRegDict2 = auth_register("*****@*****.**", "asdfgh", "Jim", "Smith") token2 = authRegDict2['token'] assert standup_active(token1, channel_id1) == {'is_active' : False, 'time_finish' : None} standup_start(token1, channel_id1, 5) standup_dic = standup_active(token1, channel_id1) assert standup_dic['is_active'] with pytest.raises(ValueError): standup_active(token1, 10) with pytest.raises(AccessError): standup_active(token2, channel_id1) auth_logout(token1) auth_logout(token2)
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_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_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_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_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_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_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_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_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_standup_send(): reset_data() #SETUP Begin authRegDict1 = auth_register("*****@*****.**", "qwerty", "John", "Smith") token1 = authRegDict1['token'] channelCreateDict1 = channels_create(token1, 'New Channel', True) channel_id1 = channelCreateDict1['channel_id'] authRegDict2 = auth_register("*****@*****.**", "asdfgh", "Jim", "Smith") token2 = authRegDict2['token'] channelCreateDict2 = channels_create(token2, 'New Channel2', True) channel_id2 = channelCreateDict2['channel_id'] standup_start(token1, channel_id1, 7) #User 2 has not joined the 'New Channel' created by user 1 #SETUP End #Test Case 1: Successful message sent to get bufferred in the standup queue assert standup_send(token1, channel_id1, 'Hello') == {} #Test Case 2: Unsuccessful message due to channel not existing with pytest.raises(ValueError): standup_send( token1, 10, 'Hello') #Invalid channel ID hence the unsuccessful message #Test Case 3: Unsuccessful message due to message being more than 1000 characters with pytest.raises(ValueError): standup_send(token1, channel_id1, 'a' * 1001) #Test Case 4: Unsuccessful message due to user not being a member of the channel with pytest.raises(AccessError): #user 2 has not joined the channel created by user 1 so the message is Unsuccessful standup_send(token2, channel_id1, 'Hello') #Test Case 5: Unsuccessful standup message due to standup not being active with pytest.raises(ValueError): standup_send(token2, channel_id2, 'Hello') 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 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_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 test_unauthorized_sendlater(): ''' Test sending a message later with an unauthorized token ''' reset_data() # SETUP registered_user = auth_register('*****@*****.**', '123456', 'John', 'Smith') token = registered_user['token'] c_id = channel_create(token, 'Channel1', 'true') # SETUP END # Login as new user not part of channel auth_logout(token) registered_user = auth_register('*****@*****.**', '123456', 'Bob', 'Smith') token = registered_user['token'] time_sent = t.time() + 10 with pytest.raises(AccessError, match=r"*"): message_sendlater(token, c_id, 'Hello world', time_sent) message_sendlater('', c_id, 'Hello world', time_sent)
def post_auth_logout(): """ Logout an existing user and invalidate their token """ token = request.form.get('token') try: output = auth_logout(token) except AccessError as e: return dumps({ 'code': 400, 'name': 'AccessError', 'message': str(e) }), 400 save() return dumps(output)
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_unauthorized(): ''' Test sending a message as an unauthorized user ''' reset_data() # SETUP registered_user = auth_register('*****@*****.**', '123456', 'John', 'Smith') token = registered_user['token'] c_id = channel_create(token, 'Channel1', 'true') # SETUP END # Login as new user not part of channel auth_logout(token) registered_user = auth_register('*****@*****.**', '123456', 'Bob', 'Smith') token = registered_user['token'] with pytest.raises(AccessError, match=r"*No message*"): message_send(token, c_id, 'Hello') message_send('', c_id, 'Hello') message_send(1234, c_id, 'Hello')