def test_channels_listall(): clear() #create two user and take their id and token user1 = auth.auth_register('*****@*****.**', 'password', 'FirstN', 'LastN') user1 = auth.auth_login('*****@*****.**', 'password') u1_token = user1['token'] user2 = auth.auth_register('*****@*****.**', 'password', 'FirstN2', 'LastN2') user2 = auth.auth_login('*****@*****.**', 'password') u2_token = user2['token'] #create a channel by user1 in channels and return its channel id channel_1_id = channels.channels_create(u1_token, 'team', True).get('channel_id') #create a channel by user2 in channels and return its channel id channel_2_id = channels.channels_create(u2_token, 'team2', True).get('channel_id') #check if the function return them all l_test = channels.channels_listall(u1_token).get('channels') assert len(l_test) == 2 assert l_test[0]['channel_id'] == channel_1_id assert l_test[1]['channel_id'] == channel_2_id assert l_test[0]['name'] == 'team' assert l_test[1]['name'] == 'team2'
def test_users_all_add_new(): '''check the list after a new user adding in''' other.clear() # initialise the users list #create a user user1 = auth.auth_register('*****@*****.**', 'password', 'FirstN', 'LastN') user1 = auth.auth_login('*****@*****.**', 'password') u1_token = user1['token'] u1_id = user1['u_id'] #create another user user2 = auth.auth_register('*****@*****.**', 'password', 'FirstN2', 'LastN2') user2 = auth.auth_login('*****@*****.**', 'password') u2_id = user2['u_id'] i = other.users_all(u1_token).get('users') i_user1 = i[0] i_user2 = i[1] assert len(i) == 2 assert i_user1['u_id'] == u1_id assert i_user1['email'] == '*****@*****.**' assert i_user1['name_first'] == 'FirstN' assert i_user1['name_last'] == 'LastN' assert i_user2['u_id'] == u2_id assert i_user2['email'] == '*****@*****.**' assert i_user2['name_first'] == 'FirstN2' assert i_user2['name_last'] == 'LastN2'
def test_auth_login_input_error_nonexistent_email(): ''' tests whether login checks nonexistent emails''' clear() # - email is not in data structure i.e. user isn't registered with pytest.raises(InputError): # this was never registered previously auth.auth_login('*****@*****.**', 'password')
def test_auth_login_input_error_incorrect_password(): ''' tests whether login checks password''' clear() auth.auth_register('*****@*****.**', 'correct_password', 'test', 'person') # - password is not correct (we love storing raw passwords) # correct password is 'correct_password' with pytest.raises(InputError): auth.auth_login('*****@*****.**', 'incorrect_password')
def test_auth_login_input_error_invalid_email(): ''' tests if login checks valid email''' clear() # - spits out 'InputError' if: # - email is not a valid email (check with regex) with pytest.raises(InputError): auth.auth_login('invalidexample.com', 'password') with pytest.raises(InputError): auth.auth_login('invalid@example', 'password')
def test_standup_all_message_sent_out_twice(): '''check if the message in message package has been sent out after ending''' clear() #create three user and take their id and token user1 = auth.auth_register('*****@*****.**', 'password', 'FirstN1', 'LastN1') user1 = auth.auth_login('*****@*****.**', 'password') u1_token = user1['token'] u1_id = user1['u_id'] user2 = auth.auth_register('*****@*****.**', 'password', 'FirstN2', 'LastN2') user2 = auth.auth_login('*****@*****.**', 'password') u2_token = user2['token'] u2_id = user2['u_id'] user3 = auth.auth_register('*****@*****.**', 'password', 'FirstN3', 'LastN3') user3 = auth.auth_login('*****@*****.**', 'password') u3_token = user3['token'] u3_id = user3['u_id'] #create a channel by user1 in channels and invite other two users channel_1_id = channels.channels_create(u1_token, 'team', True).get('channel_id') channel.channel_invite(u1_token, channel_1_id, u2_id) channel.channel_invite(u1_token, channel_1_id, u3_id) #start a standup by u1 standup.standup_start(u1_token, channel_1_id, 3) standup.standup_send(u1_token, channel_1_id, 'WE') standup.standup_send(u2_token, channel_1_id, 'ARE') standup.standup_send(u3_token, channel_1_id, 'FRIENDS') #sleep until the standup ending time.sleep(4) #start another standup by u2 standup.standup_start(u2_token, channel_1_id, 3) standup.standup_send(u1_token, channel_1_id, 'SHE') standup.standup_send(u2_token, channel_1_id, 'HE') standup.standup_send(u3_token, channel_1_id, 'THEY') #sleep until the standup ending time.sleep(4) channels_t = data.return_channels() m_c_1 = channels_t[0]['message'][1] m_c_2 = channels_t[0]['message'][0] #check the message has been sent assert m_c_1['u_id'] == u1_id assert m_c_2['u_id'] == u2_id assert m_c_1['message'] == 'FirstN1: WE\nFirstN2: ARE\nFirstN3: FRIENDS\n' assert m_c_2['message'] == 'FirstN1: SHE\nFirstN2: HE\nFirstN3: THEY\n' #check the message package assert channels_t[0]['standup'][ 'message_package'] == 'FirstN1: SHE\nFirstN2: HE\nFirstN3: THEY\n'
def test_search_in_several_channel(): '''check how is the function working when there is more than one channel''' other.clear() #initialise the channels list #create two users and take their token user1 = auth.auth_register('*****@*****.**', 'password', 'FirstN', 'LastN') user1 = auth.auth_login('*****@*****.**', 'password') u1_token = user1['token'] u1_id = user1['u_id'] user2 = auth.auth_register('*****@*****.**', 'password', 'FirstN2', 'LastN2') user2 = auth.auth_login('*****@*****.**', 'password') u2_token = user2['token'] # create 2 channels for testing channel_test_id1 = channels.channels_create(u1_token, "channel_test1", True).get('channel_id') channel_test_id2 = channels.channels_create(u2_token, "channel_test2", True).get('channel_id') channel.channel_invite(u2_token, channel_test_id2, u1_id) #add some message to one channel message.message_send(u1_token, channel_test_id1, 'Today, I am the winner.') #t message.message_send(u1_token, channel_test_id1, 'What about you?') #f message.message_send(u1_token, channel_test_id1, 'Yesterday, I was the winner.') #t message.message_send(u1_token, channel_test_id1, 'Cool!') #f message.message_send(u1_token, channel_test_id1, 'Tomorrow, I will be the winner.') #t #add some message to another channel message.message_send(u2_token, channel_test_id2, 'We are the winner.') #t message.message_send(u2_token, channel_test_id2, 'I heard he is the winner') #t message.message_send(u1_token, channel_test_id2, 'the winner.') #t message.message_send(u2_token, channel_test_id2, 'I am pretty sure about that') #f message.message_send(u1_token, channel_test_id2, 'Our team is the winner.') #t i = other.search(u1_token, 'the winner').get('messages') assert len(i) == 7 assert i[0]['message'] == 'Our team is the winner.' assert i[1]['message'] == 'the winner.' assert i[2]['message'] == 'I heard he is the winner' assert i[3]['message'] == 'We are the winner.' assert i[4]['message'] == 'Tomorrow, I will be the winner.' assert i[5]['message'] == 'Yesterday, I was the winner.' assert i[6]['message'] == 'Today, I am the winner.'
def test_standup_start(): '''test for start function''' clear() #create a user and take the token user1 = auth.auth_register('*****@*****.**', 'password', 'FirstN', 'LastN') user1 = auth.auth_login('*****@*****.**', 'password') u1_token = user1['token'] #create a channel by user1 in channels and return its channel id channel_1_id = channels.channels_create(u1_token, 'team', True).get('channel_id') channels_t = data.return_channels() time_str1 = channels_t[0]['standup'][ 'finish_time'] #get the finish time before the standup starts now = datetime.datetime.utcnow() timestamp = int(now.replace(tzinfo=datetime.timezone.utc).timestamp()) time_str3 = timestamp #start a standup standup.standup_start(u1_token, channel_1_id, 1) channels_t = data.return_channels() time_str2 = channels_t[0]['standup'][ 'finish_time'] #get the finish time after the standup starts assert time_difference(time_str1, time_str2) < 0 assert time_difference(time_str2, time_str3) == 1 time.sleep(2)
def test_search_whitespace_insensitive(): ''' this function checks for whitespace insensitivity when it comes to message search ''' other.clear() #initialise the channels list #create the first user and take their token user1 = auth.auth_register('*****@*****.**', 'password', 'FirstN', 'LastN') user1 = auth.auth_login('*****@*****.**', 'password') u1_token = user1['token'] # create a channel for testing channel_test_id1 = channels.channels_create(u1_token, "channel_test1", True).get('channel_id') # add some message to one channel # the case is different in this case message.message_send(u1_token, channel_test_id1, 'Today, I am the \nwiNNer.') #t message.message_send(u1_token, channel_test_id1, 'What about you?') #f message.message_send(u1_token, channel_test_id1, 'Yesterday, I was the winn er.') #t message.message_send(u1_token, channel_test_id1, 'Cool!') #f message.message_send(u1_token, channel_test_id1, 'Tomorrow, I\t will be the W inner.') #t i = other.search(u1_token, 'the winner').get('messages') assert len(i) == 3 assert i[0]['message'] == 'Tomorrow, I\t will be the W inner.' assert i[1]['message'] == 'Yesterday, I was the winn er.' assert i[2]['message'] == 'Today, I am the \nwiNNer.'
def test_standup_active(): '''test for the active function''' clear() #create a user and take their id and token user1 = auth.auth_register('*****@*****.**', 'password', 'FirstN', 'LastN') user1 = auth.auth_login('*****@*****.**', 'password') u1_token = user1['token'] #create two channels by user1 in channels and return their channel id channel_1_id = channels.channels_create(u1_token, 'team', True).get('channel_id') channel_2_id = channels.channels_create(u1_token, 'team1', True).get('channel_id') standup_1 = standup.standup_start( u1_token, channel_1_id, 2) #start a standup in channel_1 but not in channel_2_id res1 = standup.standup_active(u1_token, channel_1_id) assert res1['is_active'] == True assert res1['time_finish'] == standup_1['time_finish'] res2 = standup.standup_active(u1_token, channel_2_id) assert res2['is_active'] == False assert res2['time_finish'] == None time.sleep(2.5)
def test_search_basic(): '''check if the function can collect all the message with query_str''' other.clear() #initialise the channels list #create the first user and take their token user1 = auth.auth_register('*****@*****.**', 'password', 'FirstN', 'LastN') user1 = auth.auth_login('*****@*****.**', 'password') u1_token = user1['token'] # create a channel for testing channel_test_id1 = channels.channels_create(u1_token, "channel_test1", True).get('channel_id') #add some message to one channel message.message_send(u1_token, channel_test_id1, 'Today, I am the winner.') #t message.message_send(u1_token, channel_test_id1, 'What about you?') #f message.message_send(u1_token, channel_test_id1, 'Yesterday, I was the winner.') #t message.message_send(u1_token, channel_test_id1, 'Cool!') #f message.message_send(u1_token, channel_test_id1, 'Tomorrow, I will be the winner.') #t i = other.search(u1_token, 'the winner').get('messages') assert len(i) == 3 assert i[0]['message'] == 'Tomorrow, I will be the winner.' assert i[1]['message'] == 'Yesterday, I was the winner.' assert i[2]['message'] == 'Today, I am the winner.'
def login(): """Log the user in.""" # get the user from json user = request.json token = auth.auth_login(user.get('email'), user.get('password')) # return token object as json return jsonify(token)
def test_admin_error_not_owner(): '''check the AccessError when the admin is not a owner''' other.clear() #initialise the users list #create a user user1 = auth.auth_register('*****@*****.**', 'password', 'FirstN', 'LastN') user1 = auth.auth_login('*****@*****.**', 'password') u1_id = user1['u_id'] #create another user user2 = auth.auth_register('*****@*****.**', 'password', 'FirstN2', 'LastN2') user2 = auth.auth_login('*****@*****.**', 'password') u2_token = user2['token'] #check the error when the token is from a user who is not a owner with pytest.raises(AccessError): other.admin_userpermission_change(u2_token, u1_id, 2)
def test_admin_error_invalid_permission_id(): '''when the permission_id is not valid''' other.clear() #initialise the users list #create a user user1 = auth.auth_register('*****@*****.**', 'password', 'FirstN', 'LastN') user1 = auth.auth_login('*****@*****.**', 'password') u1_token = user1['token'] #create another user user2 = auth.auth_register('*****@*****.**', 'password', 'FirstN2', 'LastN2') user2 = auth.auth_login('*****@*****.**', 'password') u2_id = user2['u_id'] #check the error when the permission_id is not valid with pytest.raises(InputError): other.admin_userpermission_change(u1_token, u2_id, 3)
def test_token_to_user(): '''test for the token_to_user function''' clear() #create a user and take their id and token user1 = auth.auth_register('*****@*****.**', 'password', 'FirstN', 'LastN') user1 = auth.auth_login('*****@*****.**', 'password') u1_token = user1['token'] assert standup.token_into_user_id(u1_token) != -1
def test_token_into_name(): '''test for the transformation from token into first name''' clear() #create a user and take their id and token user1 = auth.auth_register('*****@*****.**', 'password', 'FirstN', 'LastN') user1 = auth.auth_login('*****@*****.**', 'password') u1_token = user1['token'] assert standup.token_into_name(u1_token) == 'FirstN'
def test_admin_incalid_userid(): '''when the user id is not exit''' other.clear() #initialise the users list #create a user user1 = auth.auth_register('*****@*****.**', 'password', 'FirstN', 'LastN') user1 = auth.auth_login('*****@*****.**', 'password') u1_token = user1['token'] #create another user auth.auth_register('*****@*****.**', 'password', 'FirstN2', 'LastN2') auth.auth_login('*****@*****.**', 'password') #create a u_id which is not exit in data uid_temp = random.randint(0, 0xFFFFFFFF) #check the error when the user id is not exit with pytest.raises(InputError): other.admin_userpermission_change(u1_token, uid_temp, 2)
def test_admin_userpermission_change(): '''functions test''' other.clear() #initialise the users list #create a user user1 = auth.auth_register('*****@*****.**', 'password', 'FirstN', 'LastN') user1 = auth.auth_login('*****@*****.**', 'password') u1_token = user1['token'] #create another user user2 = auth.auth_register('*****@*****.**', 'password', 'FirstN2', 'LastN2') user2 = auth.auth_login('*****@*****.**', 'password') u2_id = user2['u_id'] #run the function and test other.admin_userpermission_change(u1_token, u2_id, 1) i = data.return_users() i_user1 = i[0] i_user2 = i[1] assert i_user1['permission_id'] == 1 assert i_user2['permission_id'] == 1
def test_send_not_member(): '''test for accessing the error when the user is not the member in the channel''' clear() #create two users and take their id and token user1 = auth.auth_register('*****@*****.**', 'password', 'FirstN', 'LastN') user1 = auth.auth_login('*****@*****.**', 'password') u1_token = user1['token'] user2 = auth.auth_register('*****@*****.**', 'password', 'FirstN2', 'LastN2') user2 = auth.auth_login('*****@*****.**', 'password') u2_token = user2['token'] #create a channel by user1 in channels and return its channel id channel_1_id = channels.channels_create(u1_token, 'team', True).get('channel_id') #start a standup standup.standup_start(u1_token, channel_1_id, 1) with pytest.raises(AccessError): standup.standup_send(u2_token, channel_1_id, '') time.sleep(1.5)
def test_standup_send(): '''test for the send function''' clear() #create three user and take their id and token user1 = auth.auth_register('*****@*****.**', 'password', 'FirstN1', 'LastN1') user1 = auth.auth_login('*****@*****.**', 'password') u1_token = user1['token'] user2 = auth.auth_register('*****@*****.**', 'password', 'FirstN2', 'LastN2') user2 = auth.auth_login('*****@*****.**', 'password') u2_token = user2['token'] u2_id = user2['u_id'] user3 = auth.auth_register('*****@*****.**', 'password', 'FirstN3', 'LastN3') user3 = auth.auth_login('*****@*****.**', 'password') u3_token = user3['token'] u3_id = user3['u_id'] #create a channel by user1 in channels and invite other two users channel_1_id = channels.channels_create(u1_token, 'team', True).get('channel_id') channel.channel_invite(u1_token, channel_1_id, u2_id) channel.channel_invite(u1_token, channel_1_id, u3_id) #start a standup standup.standup_start(u1_token, channel_1_id, 2) standup.standup_send(u1_token, channel_1_id, 'WE') standup.standup_send(u2_token, channel_1_id, 'ARE') standup.standup_send(u3_token, channel_1_id, 'FRIENDS') channels_t = data.return_channels() m_l = channels_t[0]['standup']['message_package'] assert m_l == 'FirstN1: WE\nFirstN2: ARE\nFirstN3: FRIENDS\n' time.sleep(3)
def test_send_no_active_standup(): '''test for checking the error when there is no standup running in the channel now''' clear() #create a user and take their id and token user1 = auth.auth_register('*****@*****.**', 'password', 'FirstN', 'LastN') user1 = auth.auth_login('*****@*****.**', 'password') u1_token = user1['token'] #create a channel by user1 in channels and return its channel id channel_1_id = channels.channels_create(u1_token, 'team', True).get('channel_id') with pytest.raises(InputError): standup.standup_send(u1_token, channel_1_id, '')
def test_standup_start_invalid_channel_id(): '''check the error input when the channel_id is invalid''' clear() #create a user and take their id and token user1 = auth.auth_register('*****@*****.**', 'password', 'FirstN', 'LastN') user1 = auth.auth_login('*****@*****.**', 'password') u1_token = user1['token'] #create a channel by user1 in channels and return its channel id channel_1_id = channels.channels_create(u1_token, 'team', True).get('channel_id') channel_tem = channel_1_id + 1 with pytest.raises(InputError): standup.standup_start(u1_token, channel_tem, 1)
def test_find_channel(): '''test for the find_channel function''' clear() #create a user and take their id and token user1 = auth.auth_register('*****@*****.**', 'password', 'FirstN', 'LastN') user1 = auth.auth_login('*****@*****.**', 'password') u1_token = user1['token'] #create two channels by user1 in channels and return their channel id channels.channels_create(u1_token, 'team', True) channel_2_id = channels.channels_create(u1_token, 'team1', True).get('channel_id') d = standup.find_channel(channel_2_id) assert d['name'] == 'team1'
def test_users_all_initial(): '''check the list when there is only one user''' other.clear() # initialise the users list #create a user user1 = auth.auth_register('*****@*****.**', 'password', 'FirstN', 'LastN') user1 = auth.auth_login('*****@*****.**', 'password') u1_token = user1['token'] u1_id = user1['u_id'] i = other.users_all(u1_token).get('users') i_user1 = i[0] assert len(i) == 1 assert i_user1['u_id'] == u1_id assert i_user1['email'] == '*****@*****.**' assert i_user1['name_first'] == 'FirstN' assert i_user1['name_last'] == 'LastN'
def test_auth_login_correct_return(): ''' checks correct return from login''' clear() # register a user auth_register_test = auth.auth_register('*****@*****.**', 'password', 'Test Person', 'Bam') # - Dict structure -> {u_id, token} auth_login_test = auth.auth_login('*****@*****.**', 'password') assert isinstance(auth_login_test, dict) assert auth_login_test['u_id'] assert auth_login_test['token'] # - u_id is an integer assert isinstance(auth_login_test['u_id'], int) # - token is a string assert isinstance(auth_login_test['token'], str) # - The correct u_id is returned assert auth_login_test['u_id'] == auth_register_test['u_id']
def test_standup_start_occupied(): '''check the error input when an active standup has been started''' clear() #create a user and take their id and token user1 = auth.auth_register('*****@*****.**', 'password', 'FirstN', 'LastN') user1 = auth.auth_login('*****@*****.**', 'password') u1_token = user1['token'] #create a channel by user1 in channels and return its channel id channel_1_id = channels.channels_create(u1_token, 'team', True).get('channel_id') standup.standup_start(u1_token, channel_1_id, 3) time.sleep(2) #sleep for 2s for test with pytest.raises(InputError): standup.standup_start(u1_token, channel_1_id, 1) time.sleep(2)
def test_standup_long_message(): '''test for inputing the error that the message is more than 1000 words''' clear() #create a user and take its id and token user1 = auth.auth_register('*****@*****.**', 'password', 'FirstN', 'LastN') user1 = auth.auth_login('*****@*****.**', 'password') u1_token = user1['token'] #create a channel by user1 in channels and return its channel id channel_1_id = channels.channels_create(u1_token, 'team', True).get('channel_id') #start a standup standup.standup_start(u1_token, channel_1_id, 1) #create a string with more than 1000 words message_test = "aaaaa" message_test = 3000 * message_test with pytest.raises(InputError): standup.standup_send(u1_token, channel_1_id, message_test) time.sleep(1.5)
def test_auth_logout_wrong_session(): ''' tests logout for user with invalid token ''' clear() # register the first time registration1 = auth.auth_register('*****@*****.**', 'password', 'Mate', 'Old') token1 = registration1['token'] # log them out assert auth.auth_logout(token1)['is_success'] # try logging them out again (doesn't work) assert auth.auth_logout(token1)['is_success'] is False # log the same user back in registration2 = auth.auth_login('*****@*****.**', 'password') token2 = registration2['token'] # try logging out with the first token assert auth.auth_logout(token1)['is_success'] is False # try logging out with the second token assert auth.auth_logout(token2)['is_success']
def test_admin_userpermission_change_permission_id(): '''check the initial permission_id of the users is true''' other.clear() #initialise the users list #create the first user auth.auth_register('*****@*****.**', 'password', 'FirstN', 'LastN') auth.auth_login('*****@*****.**', 'password') #create another 2 users auth.auth_register('*****@*****.**', 'password', 'FirstN2', 'LastN2') auth.auth_login('*****@*****.**', 'password') auth.auth_register('*****@*****.**', 'password', 'FirstN3', 'LastN3') auth.auth_login('*****@*****.**', 'password') #check the default value i = data.return_users() i_user1 = i[0] i_user2 = i[1] i_user3 = i[2] assert i_user1['permission_id'] == 1 assert i_user2['permission_id'] == 2 assert i_user3['permission_id'] == 2