def test_can_validate_image_requests_on_globo_html(self):
        config = Config()
        config.MAX_IMG_REQUESTS_PER_PAGE = 50
        config.MAX_KB_SINGLE_IMAGE = 6
        config.MAX_IMG_KB_PER_PAGE = 100

        page = PageFactory.create()

        reviewer = Reviewer(
            api_url='http://localhost:2368',
            page_uuid=page.uuid,
            page_url=page.url,
            page_score=0.0,
            config=config,
            validators=[]
        )

        content = self.get_file('globo.html')

        result = {
            'url': page.url,
            'status': 200,
            'content': content,
            'html': lxml.html.fromstring(content)
        }
        reviewer.responses[page.url] = result
        reviewer.get_response = Mock(return_value=result)

        validator = ImageRequestsValidator(reviewer)
        validator.add_violation = Mock()
        validator.review.data = {
            'page.images': [
                (
                    'some_image.jpg',
                    Mock(status_code=200, text=self.get_file('2x2.png'))
                ) for i in xrange(60)
            ],
            'total.size.img': 106,
        }

        validator.validate()

        expect(validator.add_violation.call_args_list).to_include(
            call(
                key='total.requests.img',
                value={'total': 60, 'limit': 10},
                points=50
            ))

        expect(validator.add_violation.call_args_list).to_include(
            call(
                key='single.size.img',
                value={
                    'limit': 6,
                    'over_max_size': set([('some_image.jpg', 6.57421875)])
                },
                points=0.57421875
            ))
Example #2
0
    def test_can_get_default_violations_values(self):
        config = Config()
        config.MAX_KB_SINGLE_IMAGE = 70
        config.MAX_IMG_REQUESTS_PER_PAGE = 10
        config.MAX_IMG_KB_PER_PAGE = 5

        page = PageFactory.create()

        reviewer = Reviewer(api_url='http://localhost:2368',
                            page_uuid=page.uuid,
                            page_url=page.url,
                            page_score=0.0,
                            config=config,
                            validators=[])

        validator = ImageRequestsValidator(reviewer)

        violations_values = validator.get_default_violations_values(config)

        expect(violations_values).to_include('single.size.img')
        expect(violations_values).to_include('total.size.img')
        expect(violations_values).to_include('total.requests.img')

        expect(violations_values['single.size.img']).to_length(2)
        expect(violations_values['total.size.img']).to_length(2)
        expect(violations_values['total.requests.img']).to_length(2)

        expect(violations_values['single.size.img']).to_be_like({
            'value':
            config.MAX_KB_SINGLE_IMAGE,
            'description':
            config.get_description('MAX_KB_SINGLE_IMAGE')
        })

        expect(violations_values['total.size.img']).to_be_like({
            'value':
            config.MAX_IMG_KB_PER_PAGE,
            'description':
            config.get_description('MAX_IMG_KB_PER_PAGE')
        })

        expect(violations_values['total.requests.img']).to_be_like({
            'value':
            config.MAX_IMG_REQUESTS_PER_PAGE,
            'description':
            config.get_description('MAX_IMG_REQUESTS_PER_PAGE')
        })
    def test_can_get_default_violations_values(self):
        config = Config()
        config.MAX_KB_SINGLE_IMAGE = 70
        config.MAX_IMG_REQUESTS_PER_PAGE = 10
        config.MAX_IMG_KB_PER_PAGE = 5

        page = PageFactory.create()

        reviewer = Reviewer(
            api_url='http://localhost:2368',
            page_uuid=page.uuid,
            page_url=page.url,
            page_score=0.0,
            config=config,
            validators=[]
        )

        validator = ImageRequestsValidator(reviewer)

        violations_values = validator.get_default_violations_values(config)

        expect(violations_values).to_include('single.size.img')
        expect(violations_values).to_include('total.size.img')
        expect(violations_values).to_include('total.requests.img')

        expect(violations_values['single.size.img']).to_length(2)
        expect(violations_values['total.size.img']).to_length(2)
        expect(violations_values['total.requests.img']).to_length(2)

        expect(violations_values['single.size.img']).to_be_like({
            'value': config.MAX_KB_SINGLE_IMAGE,
            'description': config.get_description('MAX_KB_SINGLE_IMAGE')
        })

        expect(violations_values['total.size.img']).to_be_like({
            'value': config.MAX_IMG_KB_PER_PAGE,
            'description': config.get_description('MAX_IMG_KB_PER_PAGE')
        })

        expect(violations_values['total.requests.img']).to_be_like({
            'value': config.MAX_IMG_REQUESTS_PER_PAGE,
            'description': config.get_description('MAX_IMG_REQUESTS_PER_PAGE')
        })
    def test_can_validate_single_image_html(self):
        config = Config()
        config.MAX_IMG_REQUESTS_PER_PAGE = 50
        config.MAX_KB_SINGLE_IMAGE = 6
        config.MAX_IMG_KB_PER_PAGE = 100

        page = PageFactory.create(url="http://globo.com")

        reviewer = Reviewer(
            api_url='http://localhost:2368',
            page_uuid=page.uuid,
            page_url=page.url,
            page_score=0.0,
            config=config,
            validators=[]
        )

        content = "<html><img src='/some_image.jpg'/></html>"

        result = {
            'url': page.url,
            'status': 200,
            'content': content,
            'html': lxml.html.fromstring(content)
        }
        reviewer.responses[page.url] = result
        reviewer.get_response = Mock(return_value=result)

        validator = ImageRequestsValidator(reviewer)
        validator.add_violation = Mock()
        validator.review.data = {
            'page.images': [
                ('http://globo.com/some_image.jpg', Mock(status_code=200, text='bla'))
            ],
            'total.size.img': 60,
        }

        validator.validate()

        expect(validator.add_violation.called).to_be_false()
    def test_can_validate_image_404(self):
        config = Config()
        config.MAX_IMG_REQUESTS_PER_PAGE = 50
        config.MAX_KB_SINGLE_IMAGE = 6
        config.MAX_IMG_KB_PER_PAGE = 100

        page = PageFactory.create(url="http://globo.com")

        reviewer = Reviewer(
            api_url='http://localhost:2368',
            page_uuid=page.uuid,
            page_url=page.url,
            page_score=0.0,
            config=config,
            validators=[]
        )

        validator = ImageRequestsValidator(reviewer)
        validator.add_violation = Mock()

        img_url = 'http://globo.com/some_image.jpg'
        validator.review.data = {
            'page.images': [
                (img_url, Mock(status_code=404, text=None))
            ],
            'total.size.img': 60,
        }

        validator.validate()

        expect(validator.add_violation.call_args_list).to_include(
            call(
                key='broken.img',
                value=set(['http://globo.com/some_image.jpg']),
                points=50
            ))