Exemple #1
0
    def test_can_update_by_domain(self):
        domain = DomainFactory.create(name='globo.com')

        DomainsViolationsPrefsFactory.create(domain=domain,
                                             key=Key(name='some.random'),
                                             value='whatever')

        loaded = DomainsViolationsPrefs.get_domains_violations_prefs_by_domain(
            self.db, domain.name)

        expect(loaded).not_to_be_null()
        expect(loaded).to_length(1)
        expect(loaded).to_be_like([{
            'key': 'some.random',
            'value': 'whatever'
        }])

        data = [{
            'key': 'some.random',
            'value': '10'
        }, {
            'invalid_key': 'some.random.1',
            'invalid_value': '101'
        }]

        DomainsViolationsPrefs.update_by_domain(self.db, self.cache, domain,
                                                data)

        loaded = DomainsViolationsPrefs.get_domains_violations_prefs_by_domain(
            self.db, domain.name)

        expect(loaded).not_to_be_null()
        expect(loaded).to_length(1)
        expect(loaded).to_be_like([{'key': 'some.random', 'value': '10'}])
Exemple #2
0
    def test_can_save_prefs_as_superuser(self):
        self.db.query(User).delete()
        user = UserFactory(email='*****@*****.**', is_superuser=True)
        domain = DomainFactory.create(name='globo.com')
        key = KeyFactory.create(name='some.random')
        DomainsViolationsPrefsFactory.create(domain=domain, key=key, value=100)

        loaded_prefs = DomainsViolationsPrefs.get_domains_violations_prefs_by_domain(
            self.db, domain.name)
        expect(loaded_prefs).to_length(1)
        expect(loaded_prefs[0]).to_be_like({
            'value': 100,
            'key': 'some.random'
        })

        yield self.authenticated_fetch('/domains/%s/violations-prefs/' %
                                       domain.name,
                                       user_email=user.email,
                                       method='POST',
                                       body=dumps([
                                           {
                                               'key': 'some.random',
                                               'value': 10
                                           },
                                       ]))

        loaded_prefs = DomainsViolationsPrefs.get_domains_violations_prefs_by_domain(
            self.db, domain.name)
        expect(loaded_prefs).to_length(1)
        expect(loaded_prefs[0]).to_be_like({'value': 10, 'key': 'some.random'})
Exemple #3
0
    def test_can_get_domain_violations_prefs(self):
        domain = DomainFactory.create(name='globo.com')

        self.sync_cache.redis.delete( 'violations-prefs-%s' % domain.name)

        for i in range(3):
            DomainsViolationsPrefsFactory.create(
                domain=domain,
                key=KeyFactory.create(name='some.random.%d' % i),
                value='v%d' % i
            )

        prefs = self.sync_cache.get_domain_violations_prefs('globo.com')
        expect(prefs).to_equal([
            {'value': u'v0', 'key': u'some.random.0'},
            {'value': u'v1', 'key': u'some.random.1'},
            {'value': u'v2', 'key': u'some.random.2'}
        ])

        # should get from cache
        self.sync_cache.db = None

        prefs = self.sync_cache.get_domain_violations_prefs('globo.com')
        expect(prefs).to_equal([
            {'value': u'v0', 'key': u'some.random.0'},
            {'value': u'v1', 'key': u'some.random.1'},
            {'value': u'v2', 'key': u'some.random.2'}
        ])
Exemple #4
0
    def test_can_insert_default_violations_values_for_all_domains(self):
        DomainsViolationsPrefsFactory.create(domain=Domain(name='globo.com'),
                                             key=Key(name='some.random.fact'),
                                             value='whatever')

        for x in range(3):
            DomainFactory.create(name='g%d.com' % x)

        domains_violations_prefs = \
            DomainsViolationsPrefs.get_domains_violations_prefs(self.db)

        expect(domains_violations_prefs).to_length(1)

        default_violations_values = {
            'page.title.size': 100,
            'total.requests.img': 5,
        }

        page_title_size = KeyFactory.create(name='page.title.size')
        total_requests_img = KeyFactory.create(name='total.requests.img')

        violation_definitions = {
            'page.title.size': {
                'key': page_title_size,
                'default_value': 100
            },
            'total.requests.img': {
                'key': total_requests_img,
                'default_value': 5
            }
        }

        DomainsViolationsPrefs.insert_default_violations_values_for_all_domains(
            self.db, default_violations_values, violation_definitions,
            self.cache)

        domains_violations_prefs = \
            DomainsViolationsPrefs.get_domains_violations_prefs(self.db)

        expect(domains_violations_prefs).to_length(4)

        expect(domains_violations_prefs).to_be_like({
            'globo.com': {
                'some.random.fact': 'whatever',
                'total.requests.img': 5,
                'page.title.size': 100
            },
            'g0.com': {
                'page.title.size': 100,
                'total.requests.img': 5
            },
            'g1.com': {
                'page.title.size': 100,
                'total.requests.img': 5
            },
            'g2.com': {
                'page.title.size': 100,
                'total.requests.img': 5
            },
        })
    def test_can_get_domain_violations_prefs(self):
        domain = DomainFactory.create(name='globo.com')

        self.sync_cache.redis.delete( 'violations-prefs-%s' % domain.name)

        for i in range(3):
            DomainsViolationsPrefsFactory.create(
                domain=domain,
                key=KeyFactory.create(name='some.random.%d' % i),
                value='v%d' % i
            )

        prefs = self.sync_cache.get_domain_violations_prefs('globo.com')
        expect(prefs).to_equal([
            {'value': u'v0', 'key': u'some.random.0'},
            {'value': u'v1', 'key': u'some.random.1'},
            {'value': u'v2', 'key': u'some.random.2'}
        ])

        # should get from cache
        self.sync_cache.db = None

        prefs = self.sync_cache.get_domain_violations_prefs('globo.com')
        expect(prefs).to_equal([
            {'value': u'v0', 'key': u'some.random.0'},
            {'value': u'v1', 'key': u'some.random.1'},
            {'value': u'v2', 'key': u'some.random.2'}
        ])
Exemple #6
0
    def test_cant_save_prefs_as_normal_user(self):
        self.db.query(User).delete()
        user = UserFactory(email='*****@*****.**', is_superuser=False)
        domain = DomainFactory.create(name='globo.com')
        key = KeyFactory.create(name='some.random')
        DomainsViolationsPrefsFactory.create(domain=domain, key=key, value=100)

        loaded_prefs = DomainsViolationsPrefs.get_domains_violations_prefs_by_domain(
            self.db, domain.name)
        expect(loaded_prefs).to_length(1)
        expect(loaded_prefs[0]).to_be_like({
            'value': 100,
            'key': 'some.random'
        })

        try:
            yield self.authenticated_fetch('/domains/%s/violations-prefs/' %
                                           domain.name,
                                           user_email=user.email,
                                           method='POST',
                                           body=dumps([
                                               {
                                                   'key': 'some.random',
                                                   'value': 10
                                               },
                                           ]))
        except HTTPError, e:
            expect(e).not_to_be_null()
            expect(e.code).to_equal(401)
            expect(e.response.reason).to_be_like('Unauthorized')
    def test_cant_save_prefs_as_normal_user(self):
        self.db.query(User).delete()
        user = UserFactory(email='*****@*****.**', is_superuser=False)
        domain = DomainFactory.create(name='globo.com')
        key = KeyFactory.create(name='some.random')
        DomainsViolationsPrefsFactory.create(domain=domain, key=key, value=100)

        loaded_prefs = DomainsViolationsPrefs.get_domains_violations_prefs_by_domain(self.db, domain.name)
        expect(loaded_prefs).to_length(1)
        expect(loaded_prefs[0]).to_be_like({
            'value': 100,
            'key': 'some.random'
        })

        try:
            yield self.authenticated_fetch(
                '/domains/%s/violations-prefs/' % domain.name,
                user_email=user.email,
                method='POST',
                body=dumps([
                    {'key': 'some.random', 'value': 10},
                ])
            )
        except HTTPError, e:
            expect(e).not_to_be_null()
            expect(e.code).to_equal(401)
            expect(e.response.reason).to_be_like('Unauthorized')
    def test_can_save_prefs_as_superuser(self):
        self.db.query(User).delete()
        user = UserFactory(email='*****@*****.**', is_superuser=True)
        domain = DomainFactory.create(name='globo.com')
        key = KeyFactory.create(name='some.random')
        DomainsViolationsPrefsFactory.create(domain=domain, key=key, value=100)

        loaded_prefs = DomainsViolationsPrefs.get_domains_violations_prefs_by_domain(self.db, domain.name)
        expect(loaded_prefs).to_length(1)
        expect(loaded_prefs[0]).to_be_like({
            'value': 100,
            'key': 'some.random'
        })

        yield self.authenticated_fetch(
            '/domains/%s/violations-prefs/' % domain.name,
            user_email=user.email,
            method='POST',
            body=dumps([
                {'key': 'some.random', 'value': 10},
            ])
        )

        loaded_prefs = DomainsViolationsPrefs.get_domains_violations_prefs_by_domain(self.db, domain.name)
        expect(loaded_prefs).to_length(1)
        expect(loaded_prefs[0]).to_be_like({
            'value': 10,
            'key': 'some.random'
        })
Exemple #9
0
    def test_cant_save_prefs_as_anonymous_user(self):
        domain = DomainFactory.create(name='globo.com')
        key = KeyFactory.create(name='some.random')
        DomainsViolationsPrefsFactory.create(domain=domain, key=key, value=100)

        loaded_prefs = DomainsViolationsPrefs.get_domains_violations_prefs_by_domain(
            self.db, domain.name)
        expect(loaded_prefs).to_length(1)
        expect(loaded_prefs[0]).to_be_like({
            'value': 100,
            'key': 'some.random'
        })

        try:
            yield self.anonymous_fetch('/domains/%s/violations-prefs/' %
                                       domain.name,
                                       method='POST',
                                       body=dumps([
                                           {
                                               'key': 'some.random',
                                               'value': 10
                                           },
                                       ]))
        except HTTPError, e:
            expect(e).not_to_be_null()
            expect(e.code).to_equal(401)
            expect(e.response.reason).to_be_like('Unauthorized')
    def test_can_update_by_domain(self):
        domain = DomainFactory.create(name='globo.com')

        DomainsViolationsPrefsFactory.create(
            domain=domain,
            key=Key(name='some.random'),
            value='whatever'
        )

        loaded = DomainsViolationsPrefs.get_domains_violations_prefs_by_domain(self.db, domain.name)

        expect(loaded).not_to_be_null()
        expect(loaded).to_length(1)
        expect(loaded).to_be_like([{'key': 'some.random', 'value': 'whatever'}])

        data = [
            {'key': 'some.random', 'value': '10'},
            {'invalid_key': 'some.random.1', 'invalid_value': '101'}
        ]

        DomainsViolationsPrefs.update_by_domain(self.db, self.cache, domain, data)

        loaded = DomainsViolationsPrefs.get_domains_violations_prefs_by_domain(self.db, domain.name)

        expect(loaded).not_to_be_null()
        expect(loaded).to_length(1)
        expect(loaded).to_be_like([{'key': 'some.random', 'value': '10'}])
Exemple #11
0
    def test_can_validate_title_size_with_domain(self):
        self.db.query(Key).delete()
        self.db.query(KeysCategory).delete()

        config = Config()
        config.MAX_TITLE_SIZE = 70

        key = Key(name='page.title.size')
        domain = DomainFactory.create(name='globo.com', url='http://globo.com')
        page = PageFactory.create(domain=domain, url='http://globo.com/a.html')

        self.sync_cache.redis.delete('violations-prefs-%s' % domain.name)

        DomainsViolationsPrefsFactory.create(
            domain=domain,
            key=key,
            value='10'
        )

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

        title = 'a' * 80
        content = '<html><title>%s</title></html>' % title

        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)

        reviewer.violation_definitions = {
            'page.title.size': {'default_value': 70, 'key': key},
        }

        validator = TitleValidator(reviewer)
        validator.add_violation = Mock()
        validator.review.data = {
            'page.title_count': 1,
            'page.title': title
        }

        validator.validate()

        validator.add_violation.assert_called_once_with(
            key='page.title.size',
            value={'max_size': 10, 'page_url': page.url},
            points=10
        )
Exemple #12
0
    def test_can_validate_title_size_with_domain(self):
        self.db.query(Key).delete()
        self.db.query(KeysCategory).delete()

        config = Config()
        config.MAX_TITLE_SIZE = 70

        key = Key(name='page.title.size')
        domain = DomainFactory.create(name='globo.com', url='http://globo.com')
        page = PageFactory.create(domain=domain, url='http://globo.com/a.html')

        self.sync_cache.redis.delete('violations-prefs-%s' % domain.name)

        DomainsViolationsPrefsFactory.create(domain=domain,
                                             key=key,
                                             value='10')

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

        title = 'a' * 80
        content = '<html><title>%s</title></html>' % title

        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)

        reviewer.violation_definitions = {
            'page.title.size': {
                'default_value': 70,
                'key': key
            },
        }

        validator = TitleValidator(reviewer)
        validator.add_violation = Mock()
        validator.review.data = {'page.title_count': 1, 'page.title': title}

        validator.validate()

        validator.add_violation.assert_called_once_with(key='page.title.size',
                                                        value={
                                                            'max_size': 10,
                                                            'page_url':
                                                            page.url
                                                        },
                                                        points=10)
    def test_can_get_prefs(self):
        domain = DomainFactory.create(name='globo.com')

        key1 = KeyFactory.create(name='some.random.1')
        key2 = KeyFactory.create(name='some.random.2')

        DomainsViolationsPrefsFactory.create(domain=domain, key=key1, value=100)
        DomainsViolationsPrefsFactory.create(domain=domain, key=key2, value=2)

        self.server.application.violation_definitions = {
            'some.random.1': {
                'category': 'SEO',
                'default_value': 100,
                'default_value_description': 'My some.random.1',
                'unit': 'number'
            },
            'some.random.2': {
                'category': 'HTTP',
                'default_value': 2,
                'default_value_description': 'My some.random.2',
                'unit': 'number'
            },
        }

        response = yield self.authenticated_fetch(
            '/domains/%s/violations-prefs/' % domain.name
        )

        expect(response.code).to_equal(200)

        prefs = loads(response.body)

        expect(prefs).to_length(2)
        expect(prefs[0]).to_length(6)
        expect(prefs[1]).to_length(6)

        expect(prefs).to_be_like([
            {
                'category': 'SEO',
                'default_value': 100,
                'title': 'My some.random.1',
                'value': 100,
                'key': 'some.random.1',
                'unit': 'number'
            },{
                'category': 'HTTP',
                'default_value': 2,
                'title': 'My some.random.2',
                'value': 2,
                'key': 'some.random.2',
                'unit': 'number'
            }
        ])
Exemple #14
0
    def test_can_get_all_domains_violations_prefs(self):
        pref1 = DomainsViolationsPrefsFactory.create(value='whatever')
        pref2 = DomainsViolationsPrefsFactory.create(value='{"test": 10}')
        pref3 = DomainsViolationsPrefsFactory.create(value='["holmes", "ab"]')

        data = DomainsViolationsPrefs.get_all_domains_violations_prefs(self.db)

        expect(data).not_to_be_null()
        expect(data).to_length(3)
        expect(data[0]).to_equal(pref1)
        expect(data[1]).to_equal(pref2)
        expect(data[2]).to_equal(pref3)
    def test_can_get_all_domains_violations_prefs(self):
        pref1 = DomainsViolationsPrefsFactory.create(value='whatever')
        pref2 = DomainsViolationsPrefsFactory.create(value='{"test": 10}')
        pref3 = DomainsViolationsPrefsFactory.create(value='["holmes", "ab"]')

        data = DomainsViolationsPrefs.get_all_domains_violations_prefs(self.db)

        expect(data).not_to_be_null()
        expect(data).to_length(3)
        expect(data[0]).to_equal(pref1)
        expect(data[1]).to_equal(pref2)
        expect(data[2]).to_equal(pref3)
Exemple #16
0
    def test_can_get_prefs(self):
        domain = DomainFactory.create(name='globo.com')

        key1 = KeyFactory.create(name='some.random.1')
        key2 = KeyFactory.create(name='some.random.2')

        DomainsViolationsPrefsFactory.create(domain=domain,
                                             key=key1,
                                             value=100)
        DomainsViolationsPrefsFactory.create(domain=domain, key=key2, value=2)

        self.server.application.violation_definitions = {
            'some.random.1': {
                'category': 'SEO',
                'default_value': 100,
                'default_value_description': 'My some.random.1',
                'unit': 'number'
            },
            'some.random.2': {
                'category': 'HTTP',
                'default_value': 2,
                'default_value_description': 'My some.random.2',
                'unit': 'number'
            },
        }

        response = yield self.authenticated_fetch(
            '/domains/%s/violations-prefs/' % domain.name)

        expect(response.code).to_equal(200)

        prefs = loads(response.body)

        expect(prefs).to_length(2)
        expect(prefs[0]).to_length(6)
        expect(prefs[1]).to_length(6)

        expect(prefs).to_be_like([{
            'category': 'SEO',
            'default_value': 100,
            'title': 'My some.random.1',
            'value': 100,
            'key': 'some.random.1',
            'unit': 'number'
        }, {
            'category': 'HTTP',
            'default_value': 2,
            'title': 'My some.random.2',
            'value': 2,
            'key': 'some.random.2',
            'unit': 'number'
        }])
Exemple #17
0
    def test_can_get_prefs_with_invalid_violation_definition(self):
        domain = DomainFactory.create(name='globo.com')

        key = KeyFactory.create(name='some.random.1')
        DomainsViolationsPrefsFactory.create(domain=domain, key=key)

        self.server.application.violation_definitions = {}

        response = yield self.authenticated_fetch(
            '/domains/%s/violations-prefs/' % domain.name)

        expect(response.code).to_equal(200)
        expect(loads(response.body)).to_length(0)
    def test_can_get_prefs_with_invalid_violation_definition(self):
        domain = DomainFactory.create(name='globo.com')

        key = KeyFactory.create(name='some.random.1')
        DomainsViolationsPrefsFactory.create(domain=domain, key=key)

        self.server.application.violation_definitions = {}

        response = yield self.authenticated_fetch(
            '/domains/%s/violations-prefs/' % domain.name
        )

        expect(response.code).to_equal(200)
        expect(loads(response.body)).to_length(0)
    def test_can_insert_default_violations_values_for_all_domains(self):
        DomainsViolationsPrefsFactory.create(
            domain=Domain(name='globo.com'),
            key=Key(name='some.random.fact'),
            value='whatever'
        )

        for x in range(3):
            DomainFactory.create(name='g%d.com' % x)

        domains_violations_prefs = \
            DomainsViolationsPrefs.get_domains_violations_prefs(self.db)

        expect(domains_violations_prefs).to_length(1)

        default_violations_values = {
            'page.title.size': 100,
            'total.requests.img': 5,
        }

        page_title_size = KeyFactory.create(name='page.title.size')
        total_requests_img = KeyFactory.create(name='total.requests.img')

        violation_definitions = {
            'page.title.size': {'key': page_title_size, 'default_value': 100},
            'total.requests.img': {'key': total_requests_img, 'default_value': 5}
        }

        DomainsViolationsPrefs.insert_default_violations_values_for_all_domains(
            self.db,
            default_violations_values,
            violation_definitions,
            self.cache
        )

        domains_violations_prefs = \
            DomainsViolationsPrefs.get_domains_violations_prefs(self.db)

        expect(domains_violations_prefs).to_length(4)

        expect(domains_violations_prefs).to_be_like({
            'globo.com': {
                'some.random.fact': 'whatever',
                'total.requests.img': 5,
                'page.title.size': 100
            },
            'g0.com': {'page.title.size': 100, 'total.requests.img': 5},
            'g1.com': {'page.title.size': 100, 'total.requests.img': 5},
            'g2.com': {'page.title.size': 100, 'total.requests.img': 5},
        })
Exemple #20
0
    def test_domains_violations_prefs_str(self):
        data = DomainsViolationsPrefsFactory.create(
            domain=Domain(name='globo.com'),
            key=Key(name='some.random.fact'),
            value='whatever')

        loaded = self.db.query(DomainsViolationsPrefs).get(data.id)

        expect(
            str(loaded)).to_be_like('some.random.fact (globo.com): whatever')
    def test_domains_violations_prefs_str(self):
        data = DomainsViolationsPrefsFactory.create(
            domain=Domain(name='globo.com'),
            key=Key(name='some.random.fact'),
            value='whatever'
        )

        loaded = self.db.query(DomainsViolationsPrefs).get(data.id)

        expect(str(loaded)).to_be_like('some.random.fact (globo.com): whatever')
Exemple #22
0
    def test_can_convert_to_dict(self):
        data = DomainsViolationsPrefsFactory.create(
            domain=Domain(name='globo.com'),
            key=Key(name='some.random.fact'),
            value='whatever')

        expect(data.to_dict()).to_be_like({
            'domain': 'globo.com',
            'key': 'some.random.fact',
            'value': 'whatever',
        })
Exemple #23
0
    def test_can_create_domains_violations_prefs(self):
        data = DomainsViolationsPrefsFactory.create(
            domain=Domain(name='globo.com'),
            key=Key(name='some.random.fact'),
            value='whatever')

        loaded = self.db.query(DomainsViolationsPrefs).get(data.id)

        expect(loaded.domain.name).to_equal('globo.com')
        expect(loaded.key.name).to_equal('some.random.fact')
        expect(loaded.value).to_equal('whatever')
Exemple #24
0
    def test_can_get_domains_violations_prefs(self):
        data = DomainsViolationsPrefsFactory.create(
            domain=Domain(name='globo.com'),
            key=Key(name='some.random.fact'),
            value='whatever')

        data = DomainsViolationsPrefs.get_domains_violations_prefs(self.db)

        expect(data).to_be_like(
            {'globo.com': {
                'some.random.fact': 'whatever'
            }})
    def test_can_create_domains_violations_prefs(self):
        data = DomainsViolationsPrefsFactory.create(
            domain=Domain(name='globo.com'),
            key=Key(name='some.random.fact'),
            value='whatever'
        )

        loaded = self.db.query(DomainsViolationsPrefs).get(data.id)

        expect(loaded.domain.name).to_equal('globo.com')
        expect(loaded.key.name).to_equal('some.random.fact')
        expect(loaded.value).to_equal('whatever')
    def test_can_convert_to_dict(self):
        data = DomainsViolationsPrefsFactory.create(
            domain=Domain(name='globo.com'),
            key=Key(name='some.random.fact'),
            value='whatever'
        )

        expect(data.to_dict()).to_be_like({
            'domain': 'globo.com',
            'key': 'some.random.fact',
            'value': 'whatever',
        })
    def test_can_get_domains_violations_prefs_by_key(self):
        self.db.query(DomainsViolationsPrefs).delete()
        self.db.query(Page).delete()
        self.db.query(Domain).delete()
        self.db.query(Key).delete()

        domain = DomainFactory.create(name='t.com')
        page = PageFactory.create(domain=domain, url='http://t.com/a.html')

        prefs = DomainsViolationsPrefsFactory.create(
            domain=domain,
            key=Key(name='page.title.size'),
            value='70'
        )

        reviewer = self.get_reviewer(
            page_uuid=page.uuid,
            page_url=page.url,
            cache=self.sync_cache
        )

        # Get default value

        reviewer.violation_definitions = None

        self.sync_cache.redis.delete('violations-prefs-%s' % domain.name)
        prefs = reviewer.get_domains_violations_prefs_by_key(None)
        expect(prefs).to_be_null()

        prefs = reviewer.get_domains_violations_prefs_by_key('my-key')
        expect(prefs).to_be_null()

        reviewer.violation_definitions = {
            'page.title.size': {'default_value': '70'},
        }

        self.sync_cache.redis.delete('violations-prefs-%s' % domain.name)
        prefs = reviewer.get_domains_violations_prefs_by_key('my-key')
        expect(prefs).to_be_null()

        self.sync_cache.redis.delete('violations-prefs-%s' % domain.name)
        prefs = reviewer.get_domains_violations_prefs_by_key('page.title.size')
        expect(prefs).to_equal('70')

        # Get configured value

        data = [{'key': 'page.title.size', 'value': '10'}]
        DomainsViolationsPrefs.update_by_domain(self.db, self.sync_cache, domain, data)
        prefs = reviewer.get_domains_violations_prefs_by_key('page.title.size')
        expect(prefs).to_equal('10')
    def test_can_get_domains_violations_prefs(self):
        data = DomainsViolationsPrefsFactory.create(
            domain=Domain(name='globo.com'),
            key=Key(name='some.random.fact'),
            value='whatever'
        )

        data = DomainsViolationsPrefs.get_domains_violations_prefs(self.db)

        expect(data).to_be_like({
            'globo.com': {
                'some.random.fact': 'whatever'
            }
        })
Exemple #29
0
    def test_load_all_domains_violations_prefs(self):
        worker = HolmesWorker(['-c', join(self.root_path, 'tests/unit/test_worker.conf'), '--concurrency=10'])

        worker.initialize()

        # Same instance of DB, for sqlalchemy runs on Vegas
        bkp_db = worker.db
        worker.db = self.db
        worker.cache.db = self.db

        domain = DomainFactory.create(name='globo.com')

        worker.cache.redis.delete('violations-prefs-%s' % domain.name)

        prefs = worker.cache.redis.get('violations-prefs-%s' % domain.name)
        expect(prefs).to_be_null()

        for i in range(3):
            DomainsViolationsPrefsFactory.create(
                domain=domain,
                key=KeyFactory.create(name='some.random.%d' % i),
                value='v%d' % i
            )

        worker.load_all_domains_violations_prefs()

        prefs = worker.cache.get_domain_violations_prefs('globo.com')

        expect(prefs).to_equal([
            {'value': u'v0', 'key': u'some.random.0'},
            {'value': u'v1', 'key': u'some.random.1'},
            {'value': u'v2', 'key': u'some.random.2'}
        ])

        # Back to the wonderland
        worker.db = bkp_db
        worker.cache.db = bkp_db
    def test_cant_save_prefs_as_anonymous_user(self):
        domain = DomainFactory.create(name='globo.com')
        key = KeyFactory.create(name='some.random')
        DomainsViolationsPrefsFactory.create(domain=domain, key=key, value=100)

        loaded_prefs = DomainsViolationsPrefs.get_domains_violations_prefs_by_domain(self.db, domain.name)
        expect(loaded_prefs).to_length(1)
        expect(loaded_prefs[0]).to_be_like({
            'value': 100,
            'key': 'some.random'
        })

        try:
            yield self.anonymous_fetch(
                '/domains/%s/violations-prefs/' % domain.name,
                method='POST',
                body=dumps([
                    {'key': 'some.random', 'value': 10},
                ])
            )
        except HTTPError, e:
            expect(e).not_to_be_null()
            expect(e.code).to_equal(401)
            expect(e.response.reason).to_be_like('Unauthorized')
Exemple #31
0
    def test_can_get_domains_violations_prefs_by_key(self):
        self.db.query(DomainsViolationsPrefs).delete()
        self.db.query(Page).delete()
        self.db.query(Domain).delete()
        self.db.query(Key).delete()

        domain = DomainFactory.create(name='t.com')
        page = PageFactory.create(domain=domain, url='http://t.com/a.html')

        prefs = DomainsViolationsPrefsFactory.create(
            domain=domain, key=Key(name='page.title.size'), value='70')

        reviewer = self.get_reviewer(page_uuid=page.uuid,
                                     page_url=page.url,
                                     cache=self.sync_cache)

        # Get default value

        reviewer.violation_definitions = None

        self.sync_cache.redis.delete('violations-prefs-%s' % domain.name)
        prefs = reviewer.get_domains_violations_prefs_by_key(None)
        expect(prefs).to_be_null()

        prefs = reviewer.get_domains_violations_prefs_by_key('my-key')
        expect(prefs).to_be_null()

        reviewer.violation_definitions = {
            'page.title.size': {
                'default_value': '70'
            },
        }

        self.sync_cache.redis.delete('violations-prefs-%s' % domain.name)
        prefs = reviewer.get_domains_violations_prefs_by_key('my-key')
        expect(prefs).to_be_null()

        self.sync_cache.redis.delete('violations-prefs-%s' % domain.name)
        prefs = reviewer.get_domains_violations_prefs_by_key('page.title.size')
        expect(prefs).to_equal('70')

        # Get configured value

        data = [{'key': 'page.title.size', 'value': '10'}]
        DomainsViolationsPrefs.update_by_domain(self.db, self.sync_cache,
                                                domain, data)
        prefs = reviewer.get_domains_violations_prefs_by_key('page.title.size')
        expect(prefs).to_equal('10')