def test_passing_all_params(self): image_args = { 'image_url': 'globo.com/media/img/my_image.jpg', 'halign': 'left', 'valign': 'middle', 'meta': True, 'smart': True, 'width': 400, 'height': 400, 'flip': True, 'flop': True } url_query = {} url_query.update(image_args) url_query.update({ 'crop_top': 100, 'crop_left': 100, 'crop_bottom': 200, 'crop_right': 200 }) image_args.update({'crop': ((100, 100), (200, 200))}) crypto = CryptoURL(THUMBOR_SECURITY_KEY) response = self.get('/gen_url/', **url_query) assert HTTP_OK == response.code, "Got %d" % response.code assert response.body == THUMBOR_SERVER + crypto.generate( **image_args).strip("/")
def test_passing_all_params(self): image_args = { 'image_url': 'globo.com/media/img/my_image.jpg', 'halign': 'left', 'valign': 'middle', 'meta': True, 'smart': True, 'width': 400, 'height': 400, 'flip': True, 'flop': True } self.url_query.update(image_args) self.url_query.update({ 'crop_top': 100, 'crop_left': 100, 'crop_bottom': 200, 'crop_right': 200 }) image_args.update({'crop': ((100, 100), (200, 200))}) crypto = CryptoURL(settings.THUMBOR_SECURITY_KEY) response = self.client.get('/gen_url/?' + self.url_query.urlencode()) assert HTTP_OK == response.status_code, "Got %d" % response.status_code assert response.content == b(settings.THUMBOR_SERVER + crypto.generate(**image_args).strip("/"))
def test_generate_url_with_params_via_get(self): crypto = CryptoURL(THUMBOR_SECURITY_KEY) response = self.get('/gen_url/', image_url='globo.com/media/img/my_image.jpg') assert HTTP_OK == response.code, "Got %d" % response.code assert response.body == THUMBOR_SERVER + crypto.generate(image_url='globo.com/media/img/my_image.jpg').strip("/")
def test_decryption9(): '''test_decryption9 Given A security key of 'my-security-key' And an image URL of "my.server.com/some/path/to/image.jpg" And a manual crop left-top point of (10, 20) And a manual crop right-bottom point of (30, 40) When I ask my library for an encrypted URL And I call the aforementioned 'decrypt_in_thumbor' method Then I get a decrypted dictionary that contains the following: crop['left'] = 10 crop['top'] = 20 crop['right'] = 30 crop['bottom'] = 40 image_hash = 84996242f65a4d864aceb125e1c4c5ba ''' crypto = CryptoURL(KEY) url = crypto.generate(image_url=IMAGE_URL, crop=((10, 20), (30, 40))) decrypted = decrypt_in_thumbor(url) assert decrypted['crop']['left'] == 10 assert decrypted['crop']['top'] == 20 assert decrypted['crop']['right'] == 30 assert decrypted['crop']['bottom'] == 40 assert decrypted['image_hash'] == '84996242f65a4d864aceb125e1c4c5ba'
def test_passing_all_params(self): image_args = { "image_url": "globo.com/media/img/my_image.jpg", "halign": "left", "valign": "middle", "meta": True, "smart": True, "width": 400, "height": 400, "flip": True, "flop": True, } self.url_query.update(image_args) self.url_query.update({ "crop_top": 100, "crop_left": 100, "crop_bottom": 200, "crop_right": 200 }) image_args.update({"crop": ((100, 100), (200, 200))}) crypto = CryptoURL(settings.THUMBOR_SECURITY_KEY) response = self.client.get("/gen_url/?" + self.url_query.urlencode()) expect(response.status_code).to_equal(HTTP_OK) expect(response.content).to_equal(settings.THUMBOR_SERVER + crypto.generate( **image_args).strip("/"))
def test_generate_url_with_params_via_get(self): crypto = CryptoURL(settings.THUMBOR_SECURITY_KEY) image_args = {'image_url': 'globo.com/media/img/my_image.jpg'} self.url_query.update(image_args) response = self.client.get('/gen_url/?' + self.url_query.urlencode()) assert HTTP_OK == response.status_code, "Got %d" % response.status_code assert response.content == settings.THUMBOR_SERVER + crypto.generate(**image_args).strip("/")
def test_generate_url_with_params_via_get(self): crypto = CryptoURL(settings.THUMBOR_SECURITY_KEY) image_args = {'image_url': 'globo.com/media/img/my_image.jpg'} self.url_query.update(image_args) response = self.client.get('/gen_url/?' + self.url_query.urlencode()) assert HTTP_OK == response.status_code, "Got %d" % response.status_code assert response.content == b(settings.THUMBOR_SERVER + crypto.generate(**image_args).strip("/"))
def test_generate_url_with_params_via_get(self): crypto = CryptoURL(THUMBOR_SECURITY_KEY) response = self.get('/gen_url/', image_url='globo.com/media/img/my_image.jpg') assert HTTP_OK == response.code, "Got %d" % response.code assert response.body == THUMBOR_SERVER + crypto.generate( image_url='globo.com/media/img/my_image.jpg').strip("/")
def generate_url(request): if request.method != "GET": return HttpResponseNotAllowed(["GET"]) crypto = CryptoURL(THUMBOR_SECURITY_KEY) args = request.GET args = dict(zip(map(str, args.keys()), args.values())) error_message = None try: if "width" in args: args["width"] = int(args["width"]) except ValueError: error_message = "The width value '%s' is not an integer." % args[ "width"] try: if "height" in args: args["height"] = int(args["height"]) except ValueError: error_message = "The height value '%s' is not an integer." % args[ "height"] try: if ("crop_top" in args or "crop_left" in args or "crop_right" in args or "crop_bottom" in args): args["crop"] = ( (int(args["crop_left"]), int(args["crop_top"])), (int(args["crop_right"]), int(args["crop_bottom"])), ) except KeyError: error_message = """ Missing values for cropping. Expected all 'crop_left', 'crop_top', 'crop_right', 'crop_bottom' values. """ except ValueError: error_message = """ Invalid values for cropping. Expected all 'crop_left', 'crop_top', 'crop_right', 'crop_bottom' to be integers. """ if error_message is not None: logging.warning(error_message) return HttpResponseBadRequest(error_message) try: return HttpResponse( THUMBOR_SERVER + crypto.generate(**args).strip("/"), content_type="text/plain", ) except (ValueError, KeyError) as error: error_message = str(error) logging.warning(error_message) return HttpResponseBadRequest(error_message)
def test_generate_url_with_params_via_get(self): crypto = CryptoURL(settings.THUMBOR_SECURITY_KEY) image_args = {"image_url": "globo.com/media/img/my_image.jpg"} self.url_query.update(image_args) response = self.client.get("/gen_url/?" + self.url_query.urlencode()) expect(response.status_code).to_equal(HTTP_OK) expect(response.content).to_equal(settings.THUMBOR_SERVER + crypto.generate( **image_args).strip("/"))
def generate_url(request): if request.method != 'GET': return HttpResponseNotAllowed(['GET']) crypto = CryptoURL(THUMBOR_SECURITY_KEY) args = request.GET args = dict(zip(map(str, args.keys()), args.values())) error_message = None try: if 'width' in args: args['width'] = int(args['width']) except ValueError as e: error_message = "The width value '%s' is not an integer." % \ args['width'] try: if 'height' in args: args['height'] = int(args['height']) except ValueError as e: error_message = "The height value '%s' is not an integer." % \ args['height'] try: if 'crop_top' in args or 'crop_left' in args or 'crop_right' in args or 'crop_bottom' in args: args['crop'] = ((int(args['crop_left']), int(args['crop_top'])), (int(args['crop_right']), int(args['crop_bottom']))) except KeyError as e: error_message = ''' Missing values for cropping. Expected all 'crop_left', 'crop_top', 'crop_right', 'crop_bottom' values. ''' except ValueError as e: error_message = ''' Invalid values for cropping. Expected all 'crop_left', 'crop_top', 'crop_right', 'crop_bottom' to be integers. ''' if error_message is not None: logging.warning(error_message) return HttpResponseBadRequest(error_message) try: return HttpResponse(THUMBOR_SERVER + crypto.generate(**args).strip("/"), content_type="text/plain") except (ValueError, KeyError) as e: error_message = str(e) logging.warning(error_message) return HttpResponseBadRequest(error_message)
class GenerateWithUnsafeTestCase(TestCase): def setUp(self): self.crypto = CryptoURL(KEY) def test_should_pass_unsafe_to_generate_and_get_an_unsafe_url(self): url = self.crypto.generate(image_url=IMAGE_URL, crop=((10, 20), (30, 40)), unsafe=True) self.assertTrue(url.startswith('unsafe'), "url should starts with unsafe") def test_should_not_get_an_unsafe_url_when_unsafe_is_false(self): url = self.crypto.generate(image_url=IMAGE_URL, crop=((10, 20), (30, 40)), unsafe=False) self.assertFalse(url.startswith('unsafe'), "url should not starts with unsafe")
def generate_url(request): if request.method != 'GET': return HttpResponseNotAllowed(['GET']) crypto = CryptoURL(THUMBOR_SECURITY_KEY) args = request.GET args = dict(zip(map(str, args.keys()), args.values())) error_message = None try: if 'width' in args: args['width'] = int(args['width']) except ValueError as e: error_message = "The width value '%s' is not an integer." % \ args['width'] try: if 'height' in args: args['height'] = int(args['height']) except ValueError as e: error_message = "The height value '%s' is not an integer." % \ args['height'] try: if 'crop_top' in args or 'crop_left' in args or 'crop_right' in args or 'crop_bottom' in args: args['crop'] = ( (int(args['crop_left']), int(args['crop_top'])), (int(args['crop_right']), int(args['crop_bottom'])) ) except KeyError as e: error_message = ''' Missing values for cropping. Expected all 'crop_left', 'crop_top', 'crop_right', 'crop_bottom' values. ''' except ValueError as e: error_message = ''' Invalid values for cropping. Expected all 'crop_left', 'crop_top', 'crop_right', 'crop_bottom' to be integers. ''' if error_message is not None: logging.warning(error_message) return HttpResponseBadRequest(error_message) try: return HttpResponse(THUMBOR_SERVER + crypto.generate(**args).strip("/"), content_type="text/plain") except (ValueError, KeyError) as e: error_message = str(e) logging.warning(error_message) return HttpResponseBadRequest(error_message)
def image(self, **kwargs): with current_app.app_context(): global crypto_url if crypto_url == None: crypto_url = CryptoURL(key=current_app.config['THUMBOR_SECURITY_KEY']) if self and len(self) > 0: _img = '/'.join(self.split('/')[2:]) _url = urlparse(current_app.config['THUMBOR_HOST']) _url = '{u.scheme}://{u.netloc}'.format(u=_url) _img = crypto_url.generate(image_url=_img, **kwargs) _url = urljoin(_url, _img) return _url return ''
def image(self, **kwargs): with current_app.app_context(): global crypto_url if crypto_url == None: crypto_url = CryptoURL( key=current_app.config['THUMBOR_SECURITY_KEY']) if len(self) > 0: _url = urljoin( '{u.scheme}://{u.netloc}'.format( u=urlparse(current_app.config['THUMBOR_HOST'])), crypto_url.generate(image_url='/'.join( self.split('/')[2:]), **kwargs)) return _url return ''
class NewFormatUrl(TestCase): def setUp(self): self.crypto = CryptoURL(KEY) def test_generated_url_1(self): url = self.crypto.generate(image_url=IMAGE_URL, width=300, height=200) assert url == '/8ammJH8D-7tXy6kU3lTvoXlhu4o=/300x200/my.server.com/some/path/to/image.jpg' def test_generated_url_2(self): url = self.crypto.generate(image_url=IMAGE_URL, width=300, height=200, crop=((10,10), (200,200))) assert url == '/B35oBEIwztbc3jm7vsdqLez2C78=/10x10:200x200/300x200/my.server.com/some/path/to/image.jpg' def test_generated_url_3(self): url = self.crypto.generate(image_url=IMAGE_URL, width=300, height=200, crop=((10,10), (200,200)), filters=("brightness(20)", "contrast(10)")) assert url == '/as8U2DbUUtTMgvPF26LkjS3MocY=/10x10:200x200/300x200/filters:brightness(20):contrast(10)/my.server.com/some/path/to/image.jpg'
class GenerateWithUnsafeTestCase(TestCase): def setUp(self): self.crypto = CryptoURL(KEY) def test_should_pass_unsafe_to_generate_and_get_an_unsafe_url(self): url = self.crypto.generate(image_url=IMAGE_URL, crop=((10, 20), (30, 40)), unsafe=True) expect(url.startswith("unsafe")).to_be_true() def test_should_not_get_an_unsafe_url_when_unsafe_is_false(self): url = self.crypto.generate(image_url=IMAGE_URL, crop=((10, 20), (30, 40)), unsafe=False) expect(url.startswith("unsafe")).to_be_false()
def test_decription2(): '''test_decription2 Given A security key of 'my-security-key' And an image URL of "my.server.com/some/path/to/image.jpg" And a width of 300 And a height of 200 When I ask my library for an encrypted URL And I call the aforementioned 'decrypt_in_thumbor' method Then I get a decrypted dictionary that contains the following: horizontal_flip = False vertical_flip = False smart = False fit_in = False meta = False crop['left'] = 0 crop['top'] = 0 crop['right'] = 0 crop['bottom'] = 0 valign = "middle" halign = "center" width = 300 height = 200 image_hash = 84996242f65a4d864aceb125e1c4c5ba ''' crypto = CryptoURL(KEY) url = crypto.generate(image_url=IMAGE_URL, width=300, height=200, old=True) decrypted = decrypt_in_thumbor(url) assert decrypted['horizontal_flip'] == False assert decrypted['vertical_flip'] == False assert decrypted['smart'] == False assert decrypted['fit_in'] == False assert decrypted['meta'] == False assert decrypted['crop']['left'] == 0 assert decrypted['crop']['top'] == 0 assert decrypted['crop']['right'] == 0 assert decrypted['crop']['bottom'] == 0 assert decrypted['valign'] == "middle" assert decrypted['halign'] == "center" assert decrypted['width'] == 300 assert decrypted['height'] == 200 assert decrypted['image_hash'] == '84996242f65a4d864aceb125e1c4c5ba'
def test_decription2(): '''test_decription2 Given A security key of 'my-security-key' And an image URL of "my.server.com/some/path/to/image.jpg" And a width of 300 And a height of 200 When I ask my library for an encrypted URL And I call the aforementioned 'decrypt_in_thumbor' method Then I get a decrypted dictionary that contains the following: horizontal_flip = False vertical_flip = False smart = False fit_in = False meta = False crop['left'] = 0 crop['top'] = 0 crop['right'] = 0 crop['bottom'] = 0 valign = "middle" halign = "center" width = 300 height = 200 image_hash = 84996242f65a4d864aceb125e1c4c5ba ''' crypto = CryptoURL(KEY) url = crypto.generate(image_url=IMAGE_URL, width=300, height=200) decrypted = decrypt_in_thumbor(url) assert decrypted['horizontal_flip'] == False assert decrypted['vertical_flip'] == False assert decrypted['smart'] == False assert decrypted['fit_in'] == False assert decrypted['meta'] == False assert decrypted['crop']['left'] == 0 assert decrypted['crop']['top'] == 0 assert decrypted['crop']['right'] == 0 assert decrypted['crop']['bottom'] == 0 assert decrypted['valign'] == "middle" assert decrypted['halign'] == "center" assert decrypted['width'] == 300 assert decrypted['height'] == 200 assert decrypted['image_hash'] == '84996242f65a4d864aceb125e1c4c5ba'
def test_decryption1(): '''test_decryption1 Given A security key of 'my-security-key' And an image URL of "my.server.com/some/path/to/image.jpg" And a width of 300 And a height of 200 When I ask my library for an encrypted URL Then I get '/l42l54VqaV_J-EcB5quNMP6CnsN9BX7htrh-QbPuDv0C7adUXX7LTo6DHm_woJtZ/my.server.com/some/path/to/image.jpg' as url ''' crypto = CryptoURL(KEY) url = crypto.generate(image_url=IMAGE_URL, width=300, height=200) assert url == '/l42l54VqaV_J-EcB5quNMP6CnsN9BX7htrh-QbPuDv0C7adUXX7' + \ 'LTo6DHm_woJtZ/my.server.com/some/path/to/image.jpg'
def test_decryption3(): '''test_decryption3 Given A security key of 'my-security-key' And an image URL of "my.server.com/some/path/to/image.jpg" And the meta flag When I ask my library for an encrypted URL And I call the aforementioned 'decrypt_in_thumbor' method Then I get a decrypted dictionary that contains the following: meta = True image_hash = 84996242f65a4d864aceb125e1c4c5ba ''' crypto = CryptoURL(KEY) url = crypto.generate(image_url=IMAGE_URL, meta=True) decrypted = decrypt_in_thumbor(url) assert decrypted['meta'] == True assert decrypted['image_hash'] == '84996242f65a4d864aceb125e1c4c5ba'
def test_decryption10(): '''test_decryption10 Given A security key of 'my-security-key' And an image URL of "my.server.com/some/path/to/image.jpg" And a quality filter with 20% quality And a brightness filter with 10% improvement When I ask my library for an encrypted URL And I call the aforementioned 'decrypt_in_thumbor' method Then I get a decrypted dictionary that contains the following: filters = ["quality(20)", "brightness(10)"] image_hash = 84996242f65a4d864aceb125e1c4c5ba ''' crypto = CryptoURL(KEY) url = crypto.generate(image_url=IMAGE_URL, filters=["quality(20)", "brightness(10)"], old=True) decrypted = decrypt_in_thumbor(url) assert "quality(20)" in decrypted['filters'] assert "brightness(10)" in decrypted['filters'] assert decrypted['image_hash'] == '84996242f65a4d864aceb125e1c4c5ba'
def generate_url(request): if request.method != 'GET': return HttpResponseNotAllowed(['GET']) crypto = CryptoURL(THUMBOR_SECURITY_KEY) args = request.GET # convert Django QueryDict to a python dict args = dict(zip(map(str, args.keys()), args.values())) error_message = None try: if 'width' in args: args['width'] = int(args['width']) except ValueError, e: error_message = "The width value '%s' is not an integer." % args['width']
def setUp(self): self.crypto = CryptoURL(unicode(KEY))
def generate_url(self, parameters): crypto = CryptoURL(self.settings['thumbor_security_key']) return crypto.generate(**parameters).strip("/")
def setUp(self): self.crypto = CryptoURL(KEY)
def setUp(self): self.crypto = CryptoURL(text_type(KEY))
def setUp(self): self.crypto = CryptoURL(ensure_text(KEY))