async def test_empty_field(self): motor_document = await MotorDocument.objects.create() result = await MotorDocument.objects.get(id=motor_document._id) expect(result).not_to_be_null() expect(result.number).to_be_null()
def test_can_integrate_backwards(self): motor_document = yield MotorDocument.objects.create(byte=six.b("other_string")) result = MongoDocument.objects.get(id=motor_document._id) expect(result.id).to_equal(motor_document._id) expect(result.byte).to_equal(motor_document.byte)
def test_to_dict(self): page_id = uuid4() review_id = uuid4() page = PageFactory.build(uuid=page_id) review = ReviewFactory.build(page=page, uuid=review_id) key1 = KeyFactory.create(name='some.random.key1') review.add_violation(key1, 'b', 100, review.domain) key2 = KeyFactory.create(name='some.random.key2') review.add_fact(key2, 'b') fact_definitions = {'some.random.key2': {}} violation_definitions = {'some.random.key1': {}} expect(review.to_dict(fact_definitions, violation_definitions)).to_be_like({ 'domain': review.domain.name, 'uuid': str(review_id), 'completedAt': None, 'facts': [ {'value': 'b', 'key': 'some.random.key2', 'unit': 'value', 'title': 'unknown', 'category': 'unknown'} ], 'violations': [ {'points': 100, 'key': 'some.random.key1', 'description': 'b', 'title': 'undefined', 'category': 'undefined'} ], 'page': { 'url': page.url, 'lastModified': None, 'expires': None, 'uuid': str(page_id), 'score': 0.0 }, 'createdAt': review.created_date, 'isComplete': False })
def is_expected_to_fail(item, expected): try: expect(item).to_be_like(expected) except AssertionError: return assert False, "Shouldn't have gotten this far"
def test_can_integrate(self): mongo_document = MongoDocument(byte=six.b("some_string")).save() result = yield MotorDocument.objects.get(mongo_document.id) expect(result._id).to_equal(mongo_document.id) expect(result.byte).to_equal(mongo_document.byte)
def test_userInfo_for_success(self, mock_requests): # def test_userInfo_for_success(self): config = dict({}) config['clientID'] = '88a73992-07f2-4714-ab4b-de782acd9c4d' config['clientSecret'] = 'a130adb7-aa51-49ac-9d02-0d4036b63541' config['sslCertPath'] = 'tests_certs/cert.pem' config['sslKeyPath'] = 'tests_certs/cert.key' config['tokenServerURL'] = 'https://iat-api.adp.com/auth/oauth/v2/token' config['disconnectURL'] = 'https://iat-accounts.adp.com/auth/oauth/v2/logout' config['apiRequestURL'] = 'https://iat-api.adp.com' config['grantType'] = 'client_credentials' try: ClientCredentialsConfiguration = ConnectionConfiguration().init(config) ccConnection = ADPAPIConnectionFactory().createConnection(ClientCredentialsConfiguration) ccConnection.connect() if (ccConnection.isConnectedIndicator()): ccConnection.getConfig().setGrantType('authorization_code') userInfoHelper = UserInfoHelper(ccConnection) userinfo = userInfoHelper.callAPI('/core/v1/userinfo') mock_requests.get.assert_called_with(config['apiRequestURL']+'/core/v1/userinfo', cert=('tests_certs/cert.pem', 'tests_certs/cert.key'), headers={'Authorization': 'Bearer ' + ccConnection.getAccessToken()}) expect(userinfo).to_equal(mock_requests.get.return_value) except Exception as apierr: print 'error is ' + str(apierr)
def test_should_detect_one_face(self): detection_result = ProfileDetector().detect(create_image('profile_face.jpg')) expect(detection_result).to_length(1) expect(detection_result[0][0]).to_be_numeric() expect(detection_result[0][1]).to_be_numeric() expect(detection_result[0][2]).to_be_numeric() expect(detection_result[0][3]).to_be_numeric()
def test_can_create_default_fetch_result(self): result = FetchResult() expect(result.normalized).to_be_false() expect(result.buffer).to_be_null() expect(result.engine).to_be_null() expect(result.successful).to_be_false() expect(result.loader_error).to_be_null()
def test_can_get_image_without_etags(self): response = self.fetch('/unsafe/image.jpg', headers={ "Accept": 'image/webp,*/*;q=0.8' }) expect(response.code).to_equal(200) expect(response.headers).not_to_include('Etag')
def test_should_optimize_jpeg(self): response = self.fetch('/unsafe/200x200/image.jpg') tmp_fd, tmp_file_path = tempfile.mkstemp(suffix='.jpg') f = os.fdopen(tmp_fd, 'w') f.write(response.body) f.close() exiftool = which('exiftool') if not exiftool: raise AssertionError('exiftool was not found. Please install it to run thumbor\'s tests.') command = [ exiftool, tmp_file_path, '-DeviceModel', '-EncodingProcess' ] try: with open(os.devnull) as null: output = subprocess.check_output(command, stdin=null) expect(response.code).to_equal(200) expect(output).to_equal('Encoding Process : Progressive DCT, Huffman coding\n') finally: os.remove(tmp_file_path)
def test_image_already_generated_by_thumbor_2_times(self): with open( normalize_unicode_path(u'./tests/fixtures/images/alabama1_ap620é.jpg'), 'r' ) as f: self.context.modules.storage.put( quote("http://test.com/smart/alabama1_ap620é"), f.read() ) crypto = CryptoURL('ACME-SEC') image_url = self.get_url( crypto.generate( image_url=quote(self.get_url( crypto.generate( image_url=quote("http://test.com/smart/alabama1_ap620é") ) )) ) ) url = crypto.generate( image_url=quote(image_url) ) response = self.fetch(url) expect(response.code).to_equal(200)
def test_convert_tif_16bit_per_channel_lsb_to_png(self): engine = Engine(self.context) with open(join(STORAGE_PATH, 'gradient_lsb_16bperchannel.tif'), 'r') as im: buffer = im.read() expect(buffer).not_to_equal(None) engine.convert_tif_to_png(buffer) expect(engine.extension).to_equal('.png')
def test_can_get_violation_definitions(self): reviewer = Mock() validator = RequiredMetaTagsValidator(reviewer) definitions = validator.get_violation_definitions() expect(definitions).to_length(1) expect('absent.meta.tags' in definitions).to_be_true()
def test_can_integrate_backwards(self): motor_document = yield MotorDocument.objects.create(uuid=uuid4()) result = MongoDocument.objects.get(id=motor_document._id) expect(result.id).to_equal(motor_document._id) expect(result.uuid).to_equal(motor_document.uuid)
def test_not_imported_cv2_failed_to_convert_tif_to_png(self, mockLogError): engine = Engine(self.context) with open(join(STORAGE_PATH, 'gradient_8bit.tif'), 'r') as im: buffer = im.read() returned_buffer = engine.convert_tif_to_png(buffer) expect(mockLogError.called).to_be_true() expect(buffer).to_equal(returned_buffer)
def test_should_format_multiple_properties_with_forced_double_quotes(self): reader = JsonReader('{hello:"world" ,value:123}') document = document_builder.build(reader) renderer = JsonDocumentRenderer(document, {'force_property_quotes': True}) output = renderer.render() expect(output).to_equal('{\n "hello": "world",\n "value": 123\n}')
def test_can_integrate(self): mongo_document = MongoDocument(uuid=uuid4()).save() result = yield MotorDocument.objects.get(mongo_document.id) expect(result._id).to_equal(mongo_document.id) expect(result.uuid).to_equal(mongo_document.uuid)
def test_should_format_array_of_objects(self): reader = JsonReader('{"arr":[{"int":123,"str":"hello"},{"int":123,"str":"hello"}]}') document = document_builder.build(reader) renderer = JsonDocumentRenderer(document) output = renderer.render() expect(output).to_equal('{\n "arr": [{\n "int": 123,\n "str": "hello"\n }, {\n "int": 123,\n "str": "hello"\n }]\n}')
def test_should_format_array_of_objects_with_tabs(self): reader = JsonReader('{"arr":[{"int":123,"str":"hello"},{"int":123,"str":"hello"}]}') document = document_builder.build(reader) renderer = JsonDocumentRenderer(document, {'indent_character': '\t'}) output = renderer.render() expect(output).to_equal('{\n\t"arr": [{\n\t\t"int": 123,\n\t\t"str": "hello"\n\t}, {\n\t\t"int": 123,\n\t\t"str": "hello"\n\t}]\n}')
def test_should_format_nested_object(self): reader = JsonReader('{"hello":"world" ,"obj":{"abc":123}}') document = document_builder.build(reader) renderer = JsonDocumentRenderer(document) output = renderer.render() expect(output).to_equal('{\n "hello": "world",\n "obj": {\n "abc": 123\n }\n}')
def test_should_format_object_with_array(self): reader = JsonReader('{"hello":"world" ,"arr":["hello","world"]}') document = document_builder.build(reader) renderer = JsonDocumentRenderer(document) output = renderer.render() expect(output).to_equal('{\n "hello": "world",\n "arr": ["hello", "world"]\n}')
def test_should_format_single_property(self): reader = JsonReader('{"hello":"world"}') document = document_builder.build(reader) renderer = JsonDocumentRenderer(document) output = renderer.render() expect(output).to_equal('{\n "hello": "world"\n}')
def test_should_format_scalar_array(self): reader = JsonReader('[1,2,3]') document = document_builder.build(reader) renderer = JsonDocumentRenderer(document) output = renderer.render() expect(output).to_equal('[1, 2, 3]')
def test_should_preserve_multiple_line_comments_block(self): reader = JsonReader('{\'"hello"\':"world", /* comment block */\n/*comment block*/ \'value\':123}') document = document_builder.build(reader) renderer = JsonDocumentRenderer(document, {'force_property_quotes': True}) output = renderer.render() expect(output).to_equal('{\n "\\"hello\\"": "world", /* comment block */\n /*comment block*/\n "value": 123\n}')
def test_should_format_empty_object(self): reader = JsonReader('{ }') document = document_builder.build(reader) renderer = JsonDocumentRenderer(document) output = renderer.render() expect(output).to_equal('{}')
def test_should_format_property_values_with_normalized_strings(self): reader = JsonReader('{\'hello\':"world" ,"value":123}') document = document_builder.build(reader) renderer = JsonDocumentRenderer(document, {'force_property_quotes': True, 'quote_char': '\'', 'normalize_strings': True}) output = renderer.render() expect(output).to_equal('{\n \'hello\': \'world\',\n \'value\': 123\n}')
def test_should_preserve_multiple_end_line_comment_forcing_new_line_for_subsequent_tokens(self): reader = JsonReader('{\'"hello"\':"world",\n// full line comment\n\'value\'://comment\n//comment\n123}') document = document_builder.build(reader) renderer = JsonDocumentRenderer(document, {'force_property_quotes': True}) output = renderer.render() expect(output).to_equal('{\n "\\"hello\\"": "world",\n // full line comment\n "value": //comment\n //comment\n 123\n}')
def test_should_format_array_of_arrays(self): reader = JsonReader('[[1,2,3],[4,5,6]]') document = document_builder.build(reader) renderer = JsonDocumentRenderer(document) output = renderer.render() expect(output).to_equal('[[1, 2, 3], [4, 5, 6]]')
def test_should_format_property_with_empty_string_and_normalized_strings(self): reader = JsonReader('{\'hello\':\'\'}') document = document_builder.build(reader) renderer = JsonDocumentRenderer(document, {'force_property_quotes': True, 'normalize_strings': True}) output = renderer.render() expect(output).to_equal('{\n "hello": ""\n}')
def test_should_format_multiple_properties(self): reader = JsonReader('{"hello":"world" ,"value":123}') document = document_builder.build(reader) renderer = JsonDocumentRenderer(document) output = renderer.render() expect(output).to_equal('{\n "hello": "world",\n "value": 123\n}')
def test_remove_unused_media(self): expect(get_unused_media()).to_be_empty() create_file_and_write(u'notused.txt') expect(get_unused_media()).Not.to_be_empty() remove_unused_media() expect(get_unused_media()).to_be_empty()
def test_document_with_auto_insert_datetime_field(self): class Model(Document): created = DateTimeField(auto_now_on_insert=True, auto_now_on_update=False) yield from self.drop_coll_async(Model.__collection__) model = Model() expect(model.created).to_be_null() yield from model.save() created = model.created expect(isinstance(created, datetime)).to_be_true() expect(created).to_equal(model.created) now_before = datetime.now() model = yield from Model.objects.create() created = model.created expect(created).to_equal(model.created) now_after = datetime.now() expect(model._id).not_to_be_null() expect(model.created).to_be_greater_or_equal_to(now_before) expect(model.created).to_be_lesser_or_equal_to(now_after) yield from model.save() expect(created).to_equal(model.created)
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_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_generated_url_1(self): url = self.crypto.generate(image_url=IMAGE_URL, width=300, height=200) expect(url).to_equal( "/8ammJH8D-7tXy6kU3lTvoXlhu4o=/300x200/my.server.com/some/path/to/image.jpg" )
def test_relative_path(self): FileFieldsModel.objects.create( file_field='./test_rel_path/file1.txt', ) expect(get_used_media()) \ .to_be_instance_of(list)\ .to_include(self.__make_abs_path('test_rel_path/file1.txt'))
def test_ascii_filenames(self): create_file_and_write(u'Тест.txt') used_media = get_unused_media() expect(used_media).to_be_instance_of(list).to_length(1) expect(used_media[0]).to_be_instance_of(six.text_type) expect(used_media[0]).to_equal(self.__make_abs_path(u'Тест.txt'))
def test_get_unused_media_subfolder(self): create_file_and_write(u'subfolder/notused.txt') used_media = get_unused_media() expect(used_media).to_be_instance_of(list).to_length(1) expect(used_media[0]).to_match(r'^.*subfolder/notused.txt$')
def test_can_initizalize_when_request_comes(self, mocked_initialize): expect(mocked_initialize.call_count).to_equal(0) self.fetch('/unsafe/smart/image.jpg') expect(mocked_initialize.call_count).to_equal(1)
def test_remove_media(self): expect(exists_media_path(u'file.txt')).to_be_false() create_file_and_write(u'file.txt') expect(exists_media_path(u'file.txt')).to_be_true() _remove_media([u'file.txt']) expect(exists_media_path(u'file.txt')).to_be_false()
def test_logger_should_not_be_an_error(): expect(logger).not_to_be_an_error()
def test_get_unused_media_empty(self): expect(get_unused_media()).to_be_empty()
def test_logger_should_be_instance_of_python_logger(): expect(logger).to_be_instance_of(logging.Logger)
def test_can_create_context_with_default_metrics(self): expect(self.context).not_to_be_null() expect(self.context.metrics).to_be_instance_of( thumbor.metrics.logger_metrics.Metrics)
def test_can_which_by_path(): result = which("/bin/ls") expect(result).to_include("/bin/ls") result = which("/tmp") expect(result).to_be_null()
def test_logger_should_not_be_null(): expect(logger).not_to_be_null()
def test_can_get_extension(): expect(EXTENSION.get("image/jpeg")).to_equal(".jpg") expect(EXTENSION.get("image/gif")).to_equal(".gif") expect(EXTENSION.get("image/png")).to_equal(".png") expect(EXTENSION.get("image/webp")).to_equal(".webp") expect(EXTENSION.get("video/mp4")).to_equal(".mp4") expect(EXTENSION.get("video/webm")).to_equal(".webm") expect(EXTENSION.get("image/svg+xml")).to_equal(".svg") expect(EXTENSION.get("image/tiff")).to_equal(".tif")
def test_can_which_by_env(): result = which("ls") expect(result).to_include("/bin/ls") result = which("invalid-command") expect(result).to_be_null()
def test_can_get_violation_definitions(self): reviewer = Mock() validator = RobotsValidator(reviewer) definitions = validator.get_violation_definitions() expect(definitions).to_length(5) expect('robots.not_found' in definitions).to_be_true() expect('robots.empty' in definitions).to_be_true() expect('robots.sitemap.not_found' in definitions).to_be_true() expect('robots.disallow.not_found' in definitions).to_be_true() expect('robots.disallow.root_path' in definitions).to_be_true()
def test_can_get_logger(): expect(logger.name).to_equal("thumbor")
def test_config_is_an_alias(self): Config.alias('OTHER_ENGINE', 'ENGINE') cfg = Config(OTHER_ENGINE='x') expect(cfg.ENGINE).to_equal('x') expect(cfg.OTHER_ENGINE).to_equal('x')
def test_can_get_content_type(): expect(CONTENT_TYPE.get(".jpg")).to_equal("image/jpeg") expect(CONTENT_TYPE.get(".jpeg")).to_equal("image/jpeg") expect(CONTENT_TYPE.get(".gif")).to_equal("image/gif") expect(CONTENT_TYPE.get(".png")).to_equal("image/png") expect(CONTENT_TYPE.get(".webp")).to_equal("image/webp") expect(CONTENT_TYPE.get(".mp4")).to_equal("video/mp4") expect(CONTENT_TYPE.get(".webm")).to_equal("video/webm") expect(CONTENT_TYPE.get(".svg")).to_equal("image/svg+xml") expect(CONTENT_TYPE.get(".tif")).to_equal("image/tiff") expect(CONTENT_TYPE.get(".tiff")).to_equal("image/tiff")
def test_can_format_value(self): expect(format_value("qwe")).to_equal("'qwe'") expect(format_value(["qwe", "rty"])).to_equal("[\n# qwe# rty#]") expect(format_value(230)).to_equal(230)
def test_config_is_an_aliased_key(self): Config.alias('LOADER_ALIAS', 'LOADER') cfg = Config(LOADER='y') expect(cfg.LOADER).to_equal('y') expect(cfg.LOADER_ALIAS).to_equal('y')
def test_load_image(self): engine = Engine(self.context) with open(join(STORAGE_PATH, 'animated.gif'), 'r') as im: buffer = im.read() image = engine.create_image(buffer) expect(image.format).to_equal('GIF')
def test_default_values(self): cfg = Config() for key, default_value in self.get_config(): config_value = getattr(cfg, key) expect(config_value).not_to_be_null() expect(config_value).to_equal(default_value)
def test_get_return_team_and_project(self): self.execute("default-set", team=self.team, project=self.project) result = self.execute("default-get") expected = "Default team is '%s'. Default project is '%s'." % ( self.team, self.project) expect(result).to_be_like(expected)
def test_can_generate_config(self, config_mock): generate_config() expect(config_mock.called).to_be_true()
def test_get_return_project_only(self): self.execute("default-set", project=self.project) result = self.execute("default-get") expected = "Default team not set. Default project is '%s'." % self.project expect(result).to_be_like(expected)
def test_create_engine(self): engine = Engine(self.context) expect(engine).to_be_instance_of(Engine)