def test_remove_user_http(test_data): ''' Test to see if user has successfully been removed ''' a_data, b_data, c_data = test_data admin_user_remove_v1_http(a_data['token'], b_data['auth_user_id']) admin_user_remove_v1_http(a_data['token'], c_data['auth_user_id']) a_data_profile_img_url = generate_user_profile_photo( a_data["auth_user_id"])["profile_img_url"] b_data_profile_img_url = generate_user_profile_photo( b_data["auth_user_id"])["profile_img_url"] c_data_profile_img_url = generate_user_profile_photo( c_data["auth_user_id"])["profile_img_url"] result = users_all_v1_http(a_data['token']).json()['users'] print(result) assert result == [{ "u_id": a_data["auth_user_id"], "email": "*****@*****.**", "name_first": "Andy", "name_last": "Anderson", "handle_str": "andyanderson", "profile_img_url": a_data_profile_img_url, "unscramble_record": { "wins": 0, "time_stamp": 0 } }, { "u_id": b_data['auth_user_id'], "email": "*****@*****.**", "name_first": "Removed", "name_last": "User", "handle_str": "bobbobinson", "profile_img_url": b_data_profile_img_url, "unscramble_record": { "wins": 0, "time_stamp": 0 } }, { "u_id": c_data['auth_user_id'], "email": "*****@*****.**", "name_first": "Removed", "name_last": "User", "handle_str": "chriscon", "profile_img_url": c_data_profile_img_url, "unscramble_record": { "wins": 0, "time_stamp": 0 } }]
def test_user_profile_uploadphoto_v1(): clear_v1() auth_user_1_data = auth_register_v2("*****@*****.**", "123456", "Andy", "Dandy") auth_user_1_token = auth_user_1_data["token"] auth_user_1_id = auth_user_1_data["auth_user_id"] auth_user_1_img_url = generate_user_profile_photo(auth_user_1_id)["profile_img_url"] img_url = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTD9LU7ZYs8ge6HCNaZ-B5eDOnoZFfW4F6nXg&usqp=CAU" user_profile_uploadphoto_v1(auth_user_1_token, img_url, 0, 0, 1, 1) profile_img_url = user_profile_v2(auth_user_1_token, auth_user_1_id)["user"]["profile_img_url"] assert auth_user_1_img_url == profile_img_url
def user_profile_uploadphoto_v1(token, img_url, x_start, y_start, x_end, y_end): auth_user_id = token_check(token)["u_id"] response = requests.get(img_url) if response.status_code != 200: raise InputError("img_url returns an HTTP status other than 200.") path = generate_user_profile_photo(auth_user_id)["profile_img_path"] request.urlretrieve(img_url, path) image = Image.open(path) (left, top, right, bottom) = image.getbbox() if x_start < left or x_end > right or y_start < top or y_end > bottom: raise InputError( "any of x_start, y_start, x_end, y_end are not within the dimensions of the image at the URL." ) image = Image.open(path) img_crop = image.crop(box=(x_start, y_start, x_end, y_end)) img_crop.save(path) return {}
def test_normal(test_data): a_data, b_data, c_data = test_data a_data_profile_img_url = generate_user_profile_photo( a_data["auth_user_id"])["profile_img_url"] b_data_profile_img_url = generate_user_profile_photo( b_data["auth_user_id"])["profile_img_url"] c_data_profile_img_url = generate_user_profile_photo( c_data["auth_user_id"])["profile_img_url"] # info about themselves assert user_profile_v2_http(a_data["token"], a_data["auth_user_id"]).json() == { "user": { "u_id": a_data["auth_user_id"], "email": "*****@*****.**", "name_first": "Andy", "name_last": "Anderson", "handle_str": "andyanderson", "profile_img_url": a_data_profile_img_url, "unscramble_record": { "wins": 0, "time_stamp": 0 } } } assert user_profile_v2_http(a_data["token"], b_data["auth_user_id"]).json() == { "user": { "u_id": b_data["auth_user_id"], "email": "*****@*****.**", "name_first": "Bob", "name_last": "Bobinson", "handle_str": "bobbobinson", "profile_img_url": b_data_profile_img_url, "unscramble_record": { "wins": 0, "time_stamp": 0 } } } assert user_profile_v2_http(a_data["token"], c_data["auth_user_id"]).json() == { "user": { "u_id": c_data["auth_user_id"], "email": "*****@*****.**", "name_first": "Chris", "name_last": "Con", "handle_str": "chriscon", "profile_img_url": c_data_profile_img_url, "unscramble_record": { "wins": 0, "time_stamp": 0 } } } assert user_profile_v2_http(b_data["token"], a_data["auth_user_id"]).json() == { "user": { "u_id": a_data["auth_user_id"], "email": "*****@*****.**", "name_first": "Andy", "name_last": "Anderson", "handle_str": "andyanderson", "profile_img_url": a_data_profile_img_url, "unscramble_record": { "wins": 0, "time_stamp": 0 } } } assert user_profile_v2_http(b_data["token"], b_data["auth_user_id"]).json() == { "user": { "u_id": b_data["auth_user_id"], "email": "*****@*****.**", "name_first": "Bob", "name_last": "Bobinson", "handle_str": "bobbobinson", "profile_img_url": b_data_profile_img_url, "unscramble_record": { "wins": 0, "time_stamp": 0 } } } assert user_profile_v2_http(b_data["token"], c_data["auth_user_id"]).json() == { "user": { "u_id": c_data["auth_user_id"], "email": "*****@*****.**", "name_first": "Chris", "name_last": "Con", "handle_str": "chriscon", "profile_img_url": c_data_profile_img_url, "unscramble_record": { "wins": 0, "time_stamp": 0 } } } assert user_profile_v2_http(c_data["token"], a_data["auth_user_id"]).json() == { "user": { "u_id": a_data["auth_user_id"], "email": "*****@*****.**", "name_first": "Andy", "name_last": "Anderson", "handle_str": "andyanderson", "profile_img_url": a_data_profile_img_url, "unscramble_record": { "wins": 0, "time_stamp": 0 } } } assert user_profile_v2_http(c_data["token"], b_data["auth_user_id"]).json() == { "user": { "u_id": b_data["auth_user_id"], "email": "*****@*****.**", "name_first": "Bob", "name_last": "Bobinson", "handle_str": "bobbobinson", "profile_img_url": b_data_profile_img_url, "unscramble_record": { "wins": 0, "time_stamp": 0 } } } assert user_profile_v2_http(c_data["token"], c_data["auth_user_id"]).json() == { "user": { "u_id": c_data["auth_user_id"], "email": "*****@*****.**", "name_first": "Chris", "name_last": "Con", "handle_str": "chriscon", "profile_img_url": c_data_profile_img_url, "unscramble_record": { "wins": 0, "time_stamp": 0 } } }
def test_normal(): # impossible to have empty result as there must be a registered user to call the # function in the first place clear_v1() a_data = auth_register_v2('*****@*****.**', '123456', 'Andy', 'Anderson') a_data_profile_img_url = generate_user_profile_photo( a_data["auth_user_id"])["profile_img_url"] result = users_all_v1(a_data["token"])["users"] assert result == [{ "u_id": a_data["auth_user_id"], "email": "*****@*****.**", "name_first": "Andy", "name_last": "Anderson", "handle_str": "andyanderson", "profile_img_url": a_data_profile_img_url, "unscramble_record": { "wins": 0, "time_stamp": 0 } }] b_data = auth_register_v2('*****@*****.**', '123456', 'Bob', 'Bobinson') b_data_profile_img_url = generate_user_profile_photo( b_data["auth_user_id"])["profile_img_url"] result = users_all_v1(b_data["token"])["users"] assert result == [{ "u_id": a_data["auth_user_id"], "email": "*****@*****.**", "name_first": "Andy", "name_last": "Anderson", "handle_str": "andyanderson", "profile_img_url": a_data_profile_img_url, "unscramble_record": { "wins": 0, "time_stamp": 0 } }, { "u_id": b_data["auth_user_id"], "email": "*****@*****.**", "name_first": "Bob", "name_last": "Bobinson", "handle_str": "bobbobinson", "profile_img_url": b_data_profile_img_url, "unscramble_record": { "wins": 0, "time_stamp": 0 } }] c_data = auth_register_v2('*****@*****.**', '123456', 'Chris', 'Con') c_data_profile_img_url = generate_user_profile_photo( c_data["auth_user_id"])["profile_img_url"] result = users_all_v1(c_data["token"])["users"] assert result == [{ "u_id": a_data["auth_user_id"], "email": "*****@*****.**", "name_first": "Andy", "name_last": "Anderson", "handle_str": "andyanderson", "profile_img_url": a_data_profile_img_url, "unscramble_record": { "wins": 0, "time_stamp": 0 } }, { "u_id": b_data["auth_user_id"], "email": "*****@*****.**", "name_first": "Bob", "name_last": "Bobinson", "handle_str": "bobbobinson", "profile_img_url": b_data_profile_img_url, "unscramble_record": { "wins": 0, "time_stamp": 0 } }, { "u_id": c_data["auth_user_id"], "email": "*****@*****.**", "name_first": "Chris", "name_last": "Con", "handle_str": "chriscon", "profile_img_url": c_data_profile_img_url, "unscramble_record": { "wins": 0, "time_stamp": 0 } }] d_data = auth_register_v2('*****@*****.**', '123456', 'Dan', 'Don') d_data_profile_img_url = generate_user_profile_photo( d_data["auth_user_id"])["profile_img_url"] result = users_all_v1(d_data["token"])["users"] assert result == [{ "u_id": a_data["auth_user_id"], "email": "*****@*****.**", "name_first": "Andy", "name_last": "Anderson", "handle_str": "andyanderson", "profile_img_url": a_data_profile_img_url, "unscramble_record": { "wins": 0, "time_stamp": 0 } }, { "u_id": b_data["auth_user_id"], "email": "*****@*****.**", "name_first": "Bob", "name_last": "Bobinson", "handle_str": "bobbobinson", "profile_img_url": b_data_profile_img_url, "unscramble_record": { "wins": 0, "time_stamp": 0 } }, { "u_id": c_data["auth_user_id"], "email": "*****@*****.**", "name_first": "Chris", "name_last": "Con", "handle_str": "chriscon", "profile_img_url": c_data_profile_img_url, "unscramble_record": { "wins": 0, "time_stamp": 0 } }, { "u_id": d_data["auth_user_id"], "email": "*****@*****.**", "name_first": "Dan", "name_last": "Don", "handle_str": "dandon", "profile_img_url": d_data_profile_img_url, "unscramble_record": { "wins": 0, "time_stamp": 0 } }]
def auth_register_v2(email, password, name_first, name_last): """ Given users email, password, name_first and name_last, return auth_user_id Arguments: <name> (<data type>) - <description> <name> (<data type>) - <description> ... Exceptions: InputError - Occurs when Email entered is not a valid email using the method provided here (unless you feel you have a better method). InputError - Occurs when Email address is already being used by another user InputError - Occurs when Password entered is less than 6 characters long InputError - Occurs when name_first is not between 1 and 50 characters inclusively in length InputError - Occurs when name_last is not between 1 and 50 characters inclusively in length Return Value: Returns { token, auth_user_id } """ time_registered = datetime.now().replace(microsecond=0).timestamp() with open("data.json") as json_file: data = load(json_file) #Raise InputError if email is invalid if check_valid_email(email) is None: raise InputError("Invalid email") #Raise InputError if email is already taken for user in data["users"]: if user["email"] == email: raise InputError("Email is already taken") #Raise InputError if password is < 6 characters if len(password) < 6: raise InputError("Password is must be at least 6 characters long") #Raise InputError if name_first is not between 1 and 50 characters inclusively if len(name_first) < 1 or len(name_first) > 50: raise InputError("First name must be between 1 and 50 characters") #Raise InputError if name_last is not between 1 and 50 characters if len(name_last) < 1 or len(name_last) > 50: raise InputError("Last name must be between 1 and 50 characters") #Create new auth_user_id new_u_id = int(len(data["users"]) + 1) #Generate a handle for new user base_handle = (f"{name_first}{name_last}").lower() #Make sure handle length is no longer than 20 if len(base_handle) > 20: base_handle = base_handle[:20] #If handle is already taken append with smallest number starting with 0 add_num = 0 is_unique = False new_handle = base_handle while is_unique is False: is_unique = True for user in data["users"]: existing_handle = user["handle_str"] if existing_handle == new_handle: is_unique = False new_handle = base_handle + str(add_num) add_num += 1 break #All Dreams users are members by default, except for the very first user who signs up, who is an owner if data["users"] == []: permission_id = 1 else: permission_id = 2 session_id = random.randint(0, 10000) profile_img_path = generate_user_profile_photo( new_u_id)["profile_img_path"] default_photo_url = "https://www.online-tech-tips.com/wp-content/uploads/2019/09/discord.jpg.optimal.jpg" request.urlretrieve(default_photo_url, profile_img_path) profile_img_url = generate_user_profile_photo(new_u_id)["profile_img_url"] new_user = { "u_id": new_u_id, "email": email, "name_first": name_first, "name_last": name_last, "password": hashlib.sha256(password.encode()).hexdigest(), "handle_str": new_handle, "permission_id": permission_id, "sessions": [], "profile_img_url": profile_img_url, "unscramble_record": { "wins": 0, "time_stamp": 0 }, "user_stats": { "channels_joined": [{ "num_channels_joined": 0, "time_stamp": time_registered }], "dms_joined": [{ "num_dms_joined": 0, "time_stamp": time_registered }], "messages_sent": [{ "num_messages_sent": 0, "time_stamp": time_registered }], "involvement_rate": 0 } } #create session_id data["users"].append(new_user) token = create_token(new_u_id, session_id) #add token to tokens dict data["tokens"].append(token) with open("data.json", "w") as json_file: dump(data, json_file, indent=4) return {"token": token, "auth_user_id": new_u_id}