def test_can_validate_css_requests_on_globo_html(self):
        config = Config()
        config.MAX_CSS_REQUESTS_PER_PAGE = 1
        config.MAX_CSS_KB_PER_PAGE_AFTER_GZIP = 0.0

        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 = CSSRequestsValidator(reviewer)
        css = {
            'url': 'some_style.css',
            'status': 200,
            'content': '#id{display:none}',
            'html': None
        }
        validator.get_response = Mock(return_value=css)

        validator.add_violation = Mock()

        validator.review.data = {
            'total.requests.css': 7,
            'total.size.css.gzipped': 0.05
        }
        validator.validate()

        expect(validator.add_violation.call_args_list).to_include(
            call(
                key='total.requests.css',
                value={'over_limit': 6, 'total_css_files': 7},
                points=30
            ))

        expect(validator.add_violation.call_args_list).to_include(
            call(
                key='total.size.css',
                value=0.05,
                points=0
            ))
Example #2
0
    def test_can_get_default_violations_values(self):
        config = Config()
        config.MAX_CSS_KB_PER_PAGE_AFTER_GZIP = 70
        config.MAX_CSS_REQUESTS_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 = CSSRequestsValidator(reviewer)

        violations_values = validator.get_default_violations_values(config)

        expect(violations_values).to_include('total.size.css')

        expect(violations_values['total.size.css']).to_length(2)

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

        expect(violations_values).to_include('total.requests.css')

        expect(violations_values['total.requests.css']).to_length(2)

        expect(violations_values['total.requests.css']).to_be_like({
            'value': config.MAX_CSS_REQUESTS_PER_PAGE,
            'description': config.get_description('MAX_CSS_REQUESTS_PER_PAGE')
        })