def test_auth_passwordreset_reset(): """ password entered is less than 6 characters long """ server_data = Server_data() email = "*****@*****.**" auth_register(server_data, email, "abcde123", "Jay", "Chen") # get the user's full information by email # reset code should be empty user_full = server_data.get_user_by_email(email) old_password = user_full.password assert user_full.reset_code == "" # request to set a new password, will get a reset_code # after request send, user receive a secret reset_code auth_passwordreset_request(server_data, email) reset_code = user_full.reset_code assert reset_code != "" # reset the password by using valid code auth_passwordreset_reset(server_data, reset_code, "1234abc") # check if the password has change to the new one new_password = user_full.password assert new_password != old_password
def test_auth_passwordreset_request(): """ After using password request user should get a reset_code """ server_data = Server_data() email = "*****@*****.**" # assert check(email) == True user = auth_register(server_data, email, "abcde123", "Jay", "Chen") user_id = user['u_id'] token = user['token'] # assume the token is generated from using auth_register assert user_profile(server_data, token, user_id) == { 'user': {\ 'u_id': user_id, \ 'email': "*****@*****.**", \ 'name_first': 'Jay', \ 'name_last': 'Chen', \ 'profile_img_url': '', \ 'handle_str': 'jaychen', \ }, } # get the user's full information by email # reset code should be empty user_full = server_data.get_user_by_email(email) assert user_full.reset_code == "" # request to set a new password, will get a reset_code # after request send, user receive a secret reset_code auth_passwordreset_request(server_data, email) assert user_full.reset_code != ""
def auth_fixture(): """ auth_fixture can also be used to initialize the tests for user functions, as it only requires users """ server_data = Server_data() server_data.server_data_reset() user_list = register_users(server_data, data_set_one()) return (server_data, user_list)
def channels_fixture(): """ Python fixture for channels tests Creates a list of 5 registered users infos stored in an dictionary using data_set_one() above. """ server_data = Server_data() server_data.server_data_reset() user_list = register_channels(server_data, register_users(server_data, data_set_one()), data_set_channels()) assert server_data.num_channels() == 6 return (server_data, user_list)
def test_register_last_name_empty(): """ last name empty """ server_data = Server_data() with pytest.raises(InputError): auth_register(server_data, "*****@*****.**", "abc123", "Jay", "")
def test_register_handle(): """ registration success with len(first name + last name) > 20 handle cutoff at 20 characters """ server_data = Server_data() email = "*****@*****.**" # assert check(email) == True user = auth_register(server_data, email, "abcde123", "J" * 25, "Chen") user_id = user['u_id'] token = user['token'] # assume the token is generated from using auth_register # check the handle # handle should be cutoff at the 20th characters assert user_profile(server_data, token, user_id) == { 'user': {\ 'u_id': user_id, \ 'email': '*****@*****.**', \ 'name_first': 'J' * 25, \ 'name_last': 'Chen', \ 'profile_img_url': '', \ 'handle_str': 'j' * 20, \ }, }
def test_valid_token_single(): """ Tests user_profile with a valid token, querying the data of 1 user (among 6) only. """ server_data = Server_data() #Generate 5 users and collect their user ID user_1_id = generate_user_id_1(server_data) generate_user_id_2(server_data) generate_user_id_3(server_data) generate_user_id_4(server_data) generate_user_id_5(server_data) #Generate 1 additional user and collect their user ID and token user_6_details = generate_user_6(server_data) user_6_token = user_6_details["token"] #Test the function with user 6's token #Query for user 1's profile. assert user_profile(server_data, user_6_token, user_1_id)["user"] == { 'u_id': user_1_id, 'email': '*****@*****.**', 'name_first': 'Jessica', 'name_last': 'Wu', 'handle_str': 'jessicawu', 'profile_img_url': '' }
def test_invalid_token(): """ Attempts to set the name to a name that is valid with an invalid token. AccessError should occur. Verifies that the user details were not updated. """ server_data = Server_data() user_1 = generate_user_1(server_data) user_1_id = user_1["u_id"] #Set an invalid token. token = 0 name_first = "testfirstname" name_last = "testlastname" #InputError will occur because the token is not valid. with pytest.raises(AccessError): user_profile_setname(server_data, token, name_first, name_last) #Set token to valid token token = user_1["token"] #Verify that the name was not updated. updated_user = user_profile(server_data, token, user_1_id) assert updated_user["user"]["name_first"] == "Jessica" assert updated_user["user"]["name_last"] == "Wu"
def test_missing_last(): """ Attempts to set the last name to a name that is empty. InputError should occur. Verifies that the user details were not updated. """ server_data = Server_data() user_1 = generate_user_1(server_data) user_1_token = user_1["token"] user_1_id = user_1["u_id"] name_first = "testfirstname" name_last = "" #InputError will occur because # name_last is not between 1-50 chars in length (0 chars) with pytest.raises(InputError): user_profile_setname(server_data, user_1_token, name_first, name_last) #Verify that the name was not updated. updated_user = user_profile(server_data, user_1_token, user_1_id) assert updated_user["user"][ "name_first"] == "Jessica" #The name was set in the fixture. assert updated_user["user"][ "name_last"] == "Wu" #The name was set in the fixture.
def test_symbols(): """ Attempts to set the name to a valid name composed of symbols. Verifies that the user details were successfully updated. """ server_data = Server_data() user_1 = generate_user_1(server_data) user_1_token = user_1["token"] user_1_id = user_1["u_id"] name_first = "!@#$%^&*();+-_" name_last = "+_)(*&^%$#@" #This should still be considered a valid input. #Check whether the function call succeeds. assert user_profile_setname(server_data, user_1_token, name_first, name_last) == {} #Call the user function to check whether the details were updated correctly. updated_user = user_profile(server_data, user_1_token, user_1_id) assert updated_user["user"]["name_first"] == "!@#$%^&*();+-_" assert updated_user["user"]["name_last"] == "+_)(*&^%$#@"
def test_handle_already_taken(): """ Tests with valid token and handle that is already in use. InputError will occur due to handle-already-in-use. """ server_data = Server_data() #Generate user 1 (Jessica) with automatically generated handle "jessicawu" user_1 = generate_user_1(server_data) user_1_token = user_1["token"] user_1_id = user_1["u_id"] handle_str = "jessicawu" #Generate user 2 (Patrick) for second call user_2 = generate_user_2(server_data) user_2_token = user_2["token"] user_2_id = user_2["u_id"] #Attempt to assign the same handle to a different user. #InputError will occur as the handle is already in use. with pytest.raises(InputError): user_profile_sethandle(server_data, user_2_token, handle_str) #Verify that neither user (User 1 and 2) were not affected by the previous #attempt to set an already-in-use handle to a different user. updated_user_1 = user_profile(server_data, user_1_token, user_1_id) assert updated_user_1["user"]["handle_str"] == "jessicawu" updated_user_2 = user_profile(server_data, user_2_token, user_2_id) assert updated_user_2["user"]["handle_str"] == "patrickmacalin"
def test_auth_reset_invalid_email(): """ use a invalid email to request a password reset """ server_data = Server_data() with pytest.raises(InputError): auth_passwordreset_request(server_data, "jay.chen")
def test_long_names_50(): """ Normal test case. Attempts to set the name to a name that is long but valid. Verifies that the user details were successfully updated. """ server_data = Server_data() user_1 = generate_user_1(server_data) user_1_token = user_1["token"] user_1_id = user_1["u_id"] name_first = "ThisIsAValidFirstNameThatIsVeryLongForTestPurposes" #50 chars in length name_last = "ThisIsAValidLastNameThatIsVeryLongForTestPurposesA" #50 chars in length #This should work but this is at the upper limit of the name length (50 chars). #Check whether the function call succeeds. assert user_profile_setname(server_data, user_1_token, name_first, name_last) == {} #Call the user function to check whether the details were updated correctly. updated_user = user_profile(server_data, user_1_token, user_1_id) assert updated_user["user"]["name_first"] == name_first assert updated_user["user"]["name_last"] == name_last
def test_auth_reset_no_email(): """ use an non registered email to request a password reset """ server_data = Server_data() with pytest.raises(InputError): auth_passwordreset_request(server_data, "*****@*****.**")
def test_too_long(): """ Attempts to set the names to a name that is too long InputError should occur. Verifies that the user details were not updated. """ server_data = Server_data() user_1 = generate_user_1(server_data) user_1_token = user_1["token"] user_1_id = user_1["u_id"] name_first = "ThisIsAnInvalidFirstNameThatIsTooLongForTestPurposesItShouldNotWork" #67 chars name_last = "ThisIsAnInvalidLastNameThatIsTooLongForTestPurposesItShouldNotWork" #66 chars #InputError will occur because # name_first is not between 1-50 chars in length (67 chars - too many) # name_last is not between 1-50 chars in length (66 chars - too many) with pytest.raises(InputError): user_profile_setname(server_data, user_1_token, name_first, name_last) #Verify that the name was not updated. updated_user = user_profile(server_data, user_1_token, user_1_id) assert updated_user["user"]["name_first"] == "Jessica" assert updated_user["user"]["name_last"] == "Wu"
def test_short(): """ Attempts to set the names to a name that is too short InputError should occur. Verifies that the user details were not updated. """ server_data = Server_data() user_1 = generate_user_1(server_data) user_1_token = user_1["token"] user_1_id = user_1["u_id"] name_first = "f" #1 char in length name_last = "l" #1 char in length #This should work but this is at the lower limit of the name length (1 char). #Check whether the function call succeeds. assert user_profile_setname(server_data, user_1_token, name_first, name_last) == {} #Call the user function to check whether the details were updated correctly. updated_user = user_profile(server_data, user_1_token, user_1_id) assert updated_user["user"]["name_first"] == "f" assert updated_user["user"]["name_last"] == "l"
def test_register_valid_email(): """ Email entererd is not valid """ server_data = Server_data() with pytest.raises(InputError): auth_register(server_data, "jay.chen", "abcde123", "Jay", "Chen")
def test_register_last_name(): """ # last name is not between 1-50 characters in length """ server_data = Server_data() with pytest.raises(InputError): auth_register(server_data, "*****@*****.**", "abc123", "Jay", "C" * 51)
def test_register_short_password(): """ password entered is less than 6 characters long """ server_data = Server_data() with pytest.raises(InputError): auth_register(server_data, "*****@*****.**", "ab12", "Jay", "Chen")
def test_login_valid_email(): """ # Email entered is not a valid email """ server_data = Server_data() auth_register(server_data, "*****@*****.**", "abcde123", "Jay", "Chen") with pytest.raises(InputError): auth_login(server_data, "jay.chen", "abcde123")
def test_login_password(): """ # password is not correct """ server_data = Server_data() auth_register(server_data, "*****@*****.**", "abcde123", "Jay", "Chen") # try to login with incorrect password with pytest.raises(InputError): auth_login(server_data, "*****@*****.**", "2384u2ekjh28")
def test_login_user_email(): """ # Email entered does not belong to a user """ server_data = Server_data() auth_register(server_data, "*****@*****.**", "abcde123", "Jay", "Chen") # try to login with another random email with pytest.raises(InputError): auth_login(server_data, "*****@*****.**", "1234abc")
def test_register_double(): """ Email address is already being used by another user """ server_data = Server_data() email = "*****@*****.**" user = auth_register(server_data, email, "abcde123", "Jay", "Chen") # try to register by using the same email address with pytest.raises(InputError): auth_register(server_data, email, "abcde456", "Jacky", "Chan")
def test_reset_password_short(): """ password entered is less than 6 characters long """ server_data = Server_data() email = "*****@*****.**" auth_register(server_data, email, "abcde123", "Jay", "Chen") # get the user's full information by email # reset code should be empty user_full = server_data.get_user_by_email(email) assert user_full.reset_code == "" # request to set a new password, will get a reset_code # after request send, user receive a secret reset_code auth_passwordreset_request(server_data, email) reset_code = user_full.reset_code assert reset_code != "" with pytest.raises(InputError): auth_passwordreset_reset(server_data, reset_code, "abc")
def test_invalid_token_multiple(): """ Tests user_profile with an invalid token, querying the data of all 6 users. AccessError should occur due to invalid token. """ server_data = Server_data() #Generate 5 users and collect their user ID user_1_id = generate_user_id_1(server_data) user_2_id = generate_user_id_2(server_data) user_3_id = generate_user_id_3(server_data) user_4_id = generate_user_id_4(server_data) user_5_id = generate_user_id_5(server_data) #Generate 1 additional user and collect their user ID and token user_6_details = generate_user_6(server_data) user_6_id = user_6_details["u_id"] token = 0 #Test the function with invalid token #Query for user 1's profile. with pytest.raises(AccessError): user_profile(server_data, token, user_1_id) #Test the function with invalid token #Query for user 2's profile. with pytest.raises(AccessError): user_profile(server_data, token, user_2_id) #Test the function with invalid token #Query for user 3's profile. with pytest.raises(AccessError): user_profile(server_data, token, user_3_id) #Test the function with invalid token #Query for user 4's profile. with pytest.raises(AccessError): user_profile(server_data, token, user_4_id) #Test the function with invalid token #Query for user 5's profile. with pytest.raises(AccessError): user_profile(server_data, token, user_5_id) #Test the function with invalid token #Query for user 6's profile. with pytest.raises(AccessError): user_profile(server_data, token, user_6_id)
def test_empty_token(): """ Attempts to set email with an invalid token (empty token). An AccessError should occur as the token is not valid. """ server_data = Server_data() token = '' email = "*****@*****.**" #An AccessError should occur due to the invalid token. with pytest.raises(AccessError): user_profile_setemail(server_data, token, email)
def test_logout(): """ # logout successfully # user registers successfully->login automatically """ server_data = Server_data() user = auth_register(server_data, "*****@*****.**", "abcde123", "Jay", "Chen") user_login = auth_login(server_data, "*****@*****.**", "abcde123") token1 = user['token'] token2 = user_login['token'] assert auth_logout(server_data, token1) == {'is_success': True} assert auth_logout(server_data, token2) == {'is_success': True}
def create_server_data_obj(): """ a function to create a server_data object for this active server Input: - None Output: - (obj) a server_data object either read from file or create as new """ if data_storage.does_file_exist(): print("Found local storage, loading the server state...") return data_storage.load_state() print("No local storage found, creating a default server state...") return Server_data()
def test_invalid_token_invalid_id(): """ Attempts to find the data for a user that does not exist. Uses an invalid token. Tests user_profile with an invalid token, querying the data of all 6 users. AccessError should occur due to invalid token. """ server_data = Server_data() #Generate 5 users and collect their user ID user_1_id = generate_user_id_1(server_data) user_2_id = generate_user_id_2(server_data) user_3_id = generate_user_id_3(server_data) user_4_id = generate_user_id_4(server_data) user_5_id = generate_user_id_5(server_data) #Generate 1 additional user and collect their user ID and token user_6_details = generate_user_6(server_data) user_6_id = user_6_details["u_id"] #Invalid token. token = 0 #Generate a random number as the ID to query and ensure #that there is no user with that ID. test_id = 123 id_exists = True while id_exists: #Check whether the ID is the ID of a valid user. id_exists = False if test_id == user_1_id or test_id == user_2_id or test_id == user_3_id: id_exists = True if test_id == user_4_id or test_id == user_5_id or test_id == user_6_id: id_exists = True #If the ID to test is actually the ID of a valid user, change it (by adding 1). if id_exists: test_id += 1 #Test the function with user 6's token #Query for the profile of a nonexistent user (There is no user with the specified ID). #AccessError will occur because # Token is not valid. with pytest.raises(AccessError): user_profile(server_data, token, test_id)
def test_invalid_log_out(): """ # logout after logout/ logout before login """ server_data = Server_data() user = auth_register(server_data, "*****@*****.**", "abcde123", "Jay", "Chen") auth_login(server_data, "*****@*****.**", "abcde123") token = user['token'] # logout the user first assert auth_logout(server_data, token) == {'is_success': True} # already logout, this token should be invalid, cannot logout again # or logout before login assert auth_logout(server_data, token) == {'is_success': False}