def test_delete_folder(self): # when folder does not exist self.assertEqual(s3.delete_folder(PREFIX, BUCKET), True) # when folder exists s3.upload_file_wname(FILE_NAME, BUCKET, OBJ_NAME) self.assertEqual(s3.delete_folder(PREFIX, BUCKET), True)
def test_upload_and_delete_file_wname(self): self.assertEqual(s3.upload_file_wname(FILE_NAME, BUCKET), True) self.assertEqual(s3.delete_file_wname(FILE_NAME, BUCKET), True) self.assertEqual(s3.upload_file_wname(FILE_NAME, BUCKET, OBJ_NAME), True) self.assertEqual(s3.delete_file_wname(FILE_NAME, BUCKET), True)
def test_get_images(self): self.assertEqual(len(s3.get_images(USER_NAME, UNUSED_CATEGORY)), 0) # upload 4 jpg/png images s3.upload_file_wname(IMG1, BUCKET, USER_NAME + '/housing/1.jpg') s3.upload_file_wname(IMG2, BUCKET, USER_NAME + '/housing/2.jpg') s3.upload_file_wname(IMG3, BUCKET, USER_NAME + '/housing/3.jpg') s3.upload_file_wname(IMG4, BUCKET, USER_NAME + '/housing/4.jpg') links = s3.get_images(USER_NAME) self.assertEqual(len(links), 4)
def create_user(): session = current_app.config['DB_CONNECTION'] if request.method == 'OPTIONS': return generateResponse() requested_json = request.json if get_row_if_exists(User, session, **{'email': requested_json['email']}): message, status = 'Already Created', 200 return generateResponse(elem=message, status=status) user = add_user(requested_json['name'], requested_json['email'], datetime.now(), requested_json['phone'], requested_json["description"], requested_json["schoolYear"], requested_json["major"], session) login_session["user_id"] = user.id icon_path = './assets/profile_default_icons/' selected_icon = random.choice( os.listdir(icon_path)) path_name = "/".join(["user"+str(user.id), 'profile', "headshot.jpg"]) upload_file_wname(icon_path+selected_icon, 'houseit', path_name) json_response = {'name': requested_json['name'], 'email': requested_json['email'], 'access_token': login_session["access_token"], 'message': 'Successfully created room.', 'description': user.description, 'phone': user.phone, 'schoolYear': user.school_year, 'major': user.major, 'profile_photo': path_name } response = generateResponse(json_response, 201) response.set_cookie('access_token', login_session["access_token"]) return response
def generateMock(k=30): attributes = [] for attr in others: attributes.append(add_attribute(attr, 'other', session)) for attr in facilities: attributes.append(add_attribute(attr, 'facilities', session)) move_combos = [(random.choice(intervals), months[6], random.choice(intervals), months[7]) for _ in range(k)] move_ins = [add_move_in(elem[0], elem[1], elem[2], elem[3], session) for elem in move_combos] mock_room_types = [random.choice(room_types) for _ in range(k)] mock_prices = [random.randint(200, 1000) for _ in range(k)] houses = [random.choice(addresses_generate) for _ in range(k)] people = [users[i % len(users)] for i in range(k)] negotiables = [random.choice([True, False]) for _ in range(k)] mock_stay_periods = [random.choice(list(range(1, 13))) for _ in range(k)] mock_eta = [str(random.choice(list(range(1, 30)))) + " mins" for _ in range(k)] mock_baths = [random.choice(list(range(1, 4))) for _ in range(k)] mock_beds = [random.choice(list(range(1, 4))) for _ in range(k)] mock_attrs = [list(set(random.sample(attributes, 8))) for _ in range(k)] mock_rooms = [] print(mock_room_types) for i in range(k): temp_room = add_room(datetime.now(), mock_room_types[i], mock_prices[i], negotiables[i], "Damn it LOOOOOOL", mock_stay_periods[i], mock_eta[i], houses[i], people[i], move_ins[i], mock_beds[i], mock_baths[i], session) for temp_attr in mock_attrs[i]: add_house_attribute(temp_room, temp_attr, session) mock_rooms.append(temp_room) for i in range(k): path_name = "/".join(["user"+str(people[i].id), 'housing', str(mock_rooms[i].id)]) random_files = random.sample(os.listdir(file_dir), 4) for idx, file_name in enumerate(random_files): upload_file_wname(file_dir+file_name, 'houseit', path_name+"/"+str(idx)+".jpg")
def create_user(): """Create user function that is called when user is not in the database content_type: application/json example of valid json request format: "name": "CRISTIANO", "email": "*****@*****.**", "description": "YOU COULD NOT LIVE WITH YOUR OWN FAILURE, AND WHERE DID THAT BRING YOU? BACK TO ME", "phone": "858-777-2345", "schoolYear": "Grad", "major": "MARVEL SCIENCE" """ # handle pre-flight for browsers CORS access if request.method == "OPTIONS": return generate_response() # part1: verify that the request is legit session = current_app.config["DB_CONNECTION"] # verify the token checked_and_verified, response = check_verify_token(request, login_session) if checked_and_verified == False: return response try: # if some existing users are techie, troll them user_id = login_session["user_id"] response = generate_message(MESSAGE_CREATE_USER_DUPLICATE_REQUEST, 405) return response except KeyError: # nice error. This means we can proceed to process their json checked_json, response, requested_json = check_json_form( request, MESSAGE_BAD_JSON, MESSAGE_CREATE_USER_NO_JSON) if checked_json == False: return response try: user_name = requested_json["name"] # user must complete account creation in the same session # no need to handle key error since til now the login_user_email should exist user_email = login_session["login_user_email"] user_phone = requested_json["phone"] user_description = requested_json["description"] user_school_year = requested_json["school_year"] user_major = requested_json["major"] # verify types correct_format, valid_update_pairs, response = process_request_json( User, { "name": user_name, "email": user_email, "phone": user_phone, "description": user_description, "school_year": user_school_year, "major": user_major }) if correct_format == False: return response user = add_user(user_name, user_email, datetime.now(), user_phone, user_description, user_school_year, user_major, session) # update the user id in the session login_session["user_id"] = user.id # upload the image to S3 endpoint if not offline testing if current_app.config["OFFLINE_TESTING"] != True: # cris's favorite part: create randome icons icon_path = "app/assets/profile_default_icons/" selected_icon = random.choice(os.listdir(icon_path)) if current_app.config[ "TESTING"] == True: # if online testing mode user_prefix = "test_user" else: user_prefix = "user" photo_path_name = "/".join( [user_prefix + str(user.id), "profile", "headshot.jpg"]) upload_file_wname(icon_path + selected_icon, "houseit", photo_path_name) # finally, send the sensitive data to be displayed on frontend/some techie user json_response = generate_user_login_data( user, current_app.config["TESTING"]) json_response["message"] = "Welcome to be a new HOMIE! CONGRATS!" status_code = 201 response = generate_response(json_response, status_code) return response except KeyError: response = generate_message(MESSAGE_CREATE_USER_INCOMPLETE_JSON, 400) return response
"Computer Science and Engineering", session) keenan = add_user(KEENAN, "*****@*****.**", datetime.now(), "858-4675432", descriptions[KEENAN], 'Grad', "Computer Science and Engineering", session) users = [adam, cris, amit, keenan] file_dir = '../assets/room_mock_images/' for user in users: # create icons icon_path = '../assets/profile_default_icons/' selected_icon = random.choice( os.listdir(icon_path)) path_name = "/".join(["user"+str(user.id), 'profile', "headshot.jpg"]) upload_file_wname(icon_path+selected_icon, 'houseit', path_name) def generateMock(k=30): attributes = [] for attr in others: attributes.append(add_attribute(attr, 'other', session)) for attr in facilities: attributes.append(add_attribute(attr, 'facilities', session)) move_combos = [(random.choice(intervals), months[6], random.choice(intervals), months[7]) for _ in range(k)] move_ins = [add_move_in(elem[0], elem[1], elem[2], elem[3], session) for elem in move_combos] mock_room_types = [random.choice(room_types) for _ in range(k)] mock_prices = [random.randint(200, 1000) for _ in range(k)] houses = [random.choice(addresses_generate) for _ in range(k)]