def test_user_profile_uploadphoto_invalid_url(url): ''' see if it throws Inputerror with an incorrect url ''' clear() # register a new user registration = auth.auth_register('*****@*****.**', 'password', 'Mate', 'Old') token = registration['token'] # test InputError for a png url_test = url + '/thisdoesntexist' with pytest.raises(InputError): user.user_profile_uploadphoto(token, url_test, 0, 0, 10, 10)
def test_user_profile_uploadphoto_txt(url): ''' test to see if it throws InputError on files that aren't a jpg ''' clear() # register a new user registration = auth.auth_register('*****@*****.**', 'password', 'Mate', 'Old') token = registration['token'] # test InputError for a png url_test = url + '/txt' with pytest.raises(InputError): user.user_profile_uploadphoto(token, url_test, 0, 0, 10, 10)
def test_user_profile_uploadphoto_small(url): ''' test InputError if the dimensions are too big ''' clear() # register a new user registration = auth.auth_register('*****@*****.**', 'password', 'Mate', 'Old') token = registration['token'] # test InputError when index is out of picture url_test = url + '/one/crop' with pytest.raises(InputError): user.user_profile_uploadphoto(token, url_test, 0, 0, -1000, -50) with pytest.raises(InputError): user.user_profile_uploadphoto(token, url_test, -5, -5, 1, 1)
def test_currect_u_id_for_photo(url): ''' test that photos with correct u_ids are saved ''' clear() # register a new user registration = auth.auth_register('*****@*****.**', 'password', 'Mate', 'Old') token = registration['token'] u_id = registration['u_id'] url_test = url + 'one' url_cropped = url_test + '/crop' # get the first already cropped image from the test server r = requests.get(url_cropped, stream=True) test_image = Image.open(r.raw) # test that the function doesn't crash assert user.user_profile_uploadphoto(token, url_test, 0, 0, 10, 10) == {} # get the saved image from saved path path = 'src/data/profiles/{u_id}.jpg'.format(u_id=str(u_id)) saved_image = Image.open(path) # check that image was cropped correctly assert compare_images(test_image, saved_image) == True
def uploadphoto(): # get the request json r = request.json user = decode_token(r.get('token')) u_id = str(user.get('u_id')) # crop the uploaded photo userB.user_profile_uploadphoto(r.get('token'), r.get('img_url'), r.get('x_start'), r.get('y_start'), r.get('x_end'), r.get('y_end')) # get the cropped photo path and return it cropped = 'src/data/profiles/' + u_id + '.jpg' # updata the user profile_img_url user = decode_token(r.get('token')) user['profile_img_url'] = data.get_profile_photo_url(user['u_id']) data.updateByEmail(user, user['email']) return send_file(cropped, mimetype='image/gif')
def test_user_profile_uploadphoto_two(url): ''' Given a URL of an image on the internet, crops the image within bounds (x_start, y_start) and (x_end, y_end). Position (0,0) is the top left. (token, img_url, x_start, y_start, x_end, y_end) {} InputError when any of: img_url returns an HTTP status other than 200. any of x_start, y_start, x_end, y_end are not within the dimensions of the image at the URL. Image uploaded is not a JPG ''' clear() # register a new user registration = auth.auth_register('*****@*****.**', 'password', 'Mate', 'Old') token = registration['token'] u_id = registration['u_id'] url_test = url + 'two' url_cropped = url_test + '/crop' # get the first already cropped image from the test server r = requests.get(url_cropped, stream=True) test_image = Image.open(r.raw) # test that the function doesn't crash assert user.user_profile_uploadphoto(token, url_test, 400, 400, 800, 800) == {} # get the saved image from saved path path = 'src/data/profiles/{u_id}.jpg'.format(u_id=str(u_id)) saved_image = Image.open(path) # check that image was cropped correctly assert compare_images(test_image, saved_image) == True