def test_user_profile_setemail(): '''test successful user profile check''' #reset workspace urllib.request.urlopen(urllib.request.Request(f"{BASE_URL}/workspace/reset", method='POST')) #create a new user data = json.dumps({"email": "*****@*****.**", "password": hash_password("ilovecse"), "name_first": "Robbie", "name_last": "Caldwell"}).encode("utf-8") response = urllib.request.urlopen(urllib.request.Request( f"{BASE_URL}/auth/register", data=data, headers={"Content-Type": "application/json"}, method='POST' )) #changing the users email payload = json.load(response) token = payload["token"] data = json.dumps({"token": token, "email": "*****@*****.**"}).encode("utf-8") response = urllib.request.urlopen(urllib.request.Request( f"{BASE_URL}/user/profile/setemail", data=data, headers={"Content-Type": "application/json"}, method='PUT' )) #chcking that the email change was successful response = urllib.request.urlopen(f"{BASE_URL}/users/all?token={token}") payload = json.load(response) assert payload == { "users": [{ "u_id": 1, "email": "*****@*****.**", "name_first": "Robbie", "name_last": "Caldwell", "handle_str":get_handle("Robbie", "Caldwell"), }] }
def test_user_profile_self(): '''test successful check in someones own profile''' #reset workspace urllib.request.urlopen(urllib.request.Request(f"{BASE_URL}/workspace/reset", method='POST')) #create a new user data = json.dumps({"email": "*****@*****.**", "password": hash_password("ilovecse"), "name_first": "Robbie", "name_last": "Caldwell"}).encode("utf-8") response = urllib.request.urlopen(urllib.request.Request( f"{BASE_URL}/auth/register", data=data, headers={"Content-Type": "application/json"}, method='POST' )) payload = json.load(response) token = payload["token"] u_id = payload["u_id"] #checking the name change was successful response = urllib.request.urlopen(f"{BASE_URL}/user/profile?token={token}&u_id={u_id}") payload = json.load(response) assert payload == { "user": { "u_id": u_id, "email": "*****@*****.**", "name_first": "Robbie", "name_last": "Caldwell", "handle_str":get_handle("Robbie", "Caldwell"), } }
def auth_register(email, password, name_first, name_last): '''This program registers a user''' data = get_data() # check email if check(email) is False: raise InputError(description="Invalid Email") # Invalid password if len(password) < 6: raise InputError(description="Invalid Password") # Invalid firstname if not name_first or len(name_first) > 50: raise InputError(description="Invalid First Name") # Invalid Lastname if not name_last or len(name_last) > 50: raise InputError(description="Invalid Last Name") # Email already in use for user in data['users']: if user['email'] == email: raise InputError(description="Email already in use") # New user for backend u_id = get_max_u_id() + 1 # Assume that you are logged in once you register token = generate_token(u_id) new_user = { 'u_id': u_id, 'name_first': name_first, 'name_last': name_last, 'password': str(hash_password(password)), 'email': email, 'token': token, 'reset_code': 0, 'logged_in': 1, 'handle_str': get_handle(name_first, name_last), 'permission_id': 2, 'profile_img_url': '' } data['users'].append(new_user) # Owner permision id = 1 normal memeber id = 2 if u_id == 1: new_user['permission_id'] = 1 return {"u_id": u_id, "token": token}
def test_auth_register(): '''test successful registration''' urllib.request.urlopen(urllib.request.Request(f"{BASE_URL}/workspace/reset", method='POST')) data = json.dumps({"email": "*****@*****.**", "password": hash_password("ilovecse"), "name_first": "Jiaqi", "name_last": "Zhu"}).encode("utf-8") response = urllib.request.urlopen(urllib.request.Request( f"{BASE_URL}/auth/register", data=data, headers={"Content-Type": "application/json"}, method='POST' )) payload = json.load(response) token = payload["token"] response = urllib.request.urlopen(f"{BASE_URL}/users/all?token={token}") payload = json.load(response) assert payload == { "users": [{ "u_id": 1, "email": "*****@*****.**", "name_first": "Jiaqi", "name_last": "Zhu", "handle_str":get_handle("Jiaqi", "Zhu"), }] }
def test_user_profile_sethandle_taken(): '''test when handle is already taken''' #reset workspace urllib.request.urlopen(urllib.request.Request(f"{BASE_URL}/workspace/reset", method='POST')) #create a new user data = json.dumps({"email": "*****@*****.**", "password": hash_password("ilovecse"), "name_first": "Robbie", "name_last": "Caldwell"}).encode("utf-8") response = urllib.request.urlopen(urllib.request.Request( f"{BASE_URL}/auth/register", data=data, headers={"Content-Type": "application/json"}, method='POST' )) #register for a second new user data = json.dumps({"email": "*****@*****.**", "password": hash_password("ilovecse"), "name_first": "Jiaqi", "name_last": "Zhu"}).encode("utf-8") response = urllib.request.urlopen(urllib.request.Request( f"{BASE_URL}/auth/register", data=data, headers={"Content-Type": "application/json"}, method='POST' )) #changing the users handle payload = json.load(response) token = payload["token"] data = json.dumps({"token": token, "handle": get_handle("Robbie", "Caldwell")}).encode("utf-8") with pytest.raises(urllib.error.HTTPError): urllib.request.urlopen(urllib.request.Request( f"{BASE_URL}/user/profile/sethandle", data=data, headers={"Content-Type": "application/json"}, method='PUT' ))