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'
        })
Exemplo n.º 2
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'}])
    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'}])
Exemplo n.º 4
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'})
Exemplo n.º 5
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_insert_default_violations_values_for_domain(self):
        domain = DomainFactory.create()

        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}
        }

        keys = violation_definitions.keys()

        data = DomainsViolationsPrefs.get_domains_violations_prefs_by_domain(self.db, domain.name)
        expect(data).to_length(0)

        DomainsViolationsPrefs.insert_default_violations_values_for_domain(
            self.db,
            domain,
            keys,
            violation_definitions,
            self.cache
        )

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

        expect(data).not_to_be_null()
        expect(data).to_length(2)
        expect(data).to_be_like([
            {'value': 100, 'key': 'page.title.size'},
            {'value': 5, 'key': 'total.requests.img'}
        ])
Exemplo n.º 7
0
    def initialize(self):
        self.uuid = uuid4().hex
        self.working_url = None
        self.domain_name = None
        self.last_ping = None
        self.last_update_pages_score = None

        authnz_wrapper_class = self.load_authnz_wrapper()
        if authnz_wrapper_class:
            self.authnz_wrapper = authnz_wrapper_class(self.config)
        else:
            self.authnz_wrapper = None

        self.facters = self._load_facters()
        self.validators = self._load_validators()
        self.error_handlers = [handler(self.config) for handler in self.load_error_handlers()]

        self.connect_sqlalchemy()

        self.search_provider = self.load_search_provider()(
            config=self.config,
            db=self.db,
            authnz_wrapper=self.authnz_wrapper
        )

        self.connect_to_redis()
        self.start_otto()

        self.fact_definitions = {}
        self.violation_definitions = {}

        self.default_violations_values = {}

        for facter in self.facters:
            self.fact_definitions.update(facter.get_fact_definitions())

        Key.insert_keys(self.db, self.fact_definitions)

        for validator in self.validators:
            self.violation_definitions.update(validator.get_violation_definitions())

            self.default_violations_values.update(
                validator.get_default_violations_values(self.config)
            )

        Key.insert_keys(
            self.db, self.violation_definitions, self.default_violations_values
        )

        self.configure_material_girl()

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

        self.load_all_domains_violations_prefs()
    def test_can_update_by_domain_with_empty_data(self):
        domain = DomainFactory.create(name='globo.com')

        data = []

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

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

        expect(loaded).to_equal([])
Exemplo n.º 9
0
    def test_can_update_by_domain_with_empty_data(self):
        domain = DomainFactory.create(name='globo.com')

        data = []

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

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

        expect(loaded).to_equal([])
Exemplo n.º 10
0
    def initialize(self):
        self.uuid = uuid4().hex
        self.working_url = None
        self.domain_name = None
        self.last_ping = None

        authnz_wrapper_class = self.load_authnz_wrapper()
        if authnz_wrapper_class:
            self.authnz_wrapper = authnz_wrapper_class(self.config)
        else:
            self.authnz_wrapper = None

        self.facters = self._load_facters()
        self.validators = self._load_validators()
        self.error_handlers = [
            handler(self.config) for handler in self.load_error_handlers()
        ]

        self.connect_sqlalchemy()

        self.search_provider = self.load_search_provider()(
            config=self.config, db=self.db, authnz_wrapper=self.authnz_wrapper)

        self.connect_to_redis()
        self.start_otto()

        self.fact_definitions = {}
        self.violation_definitions = {}

        self.default_violations_values = {}

        for facter in self.facters:
            self.fact_definitions.update(facter.get_fact_definitions())

        Key.insert_keys(self.db, self.fact_definitions)

        for validator in self.validators:
            self.violation_definitions.update(
                validator.get_violation_definitions())

            self.default_violations_values.update(
                validator.get_default_violations_values(self.config))

        Key.insert_keys(self.db, self.violation_definitions,
                        self.default_violations_values)

        self.configure_material_girl()

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

        self.load_all_domains_violations_prefs()
Exemplo n.º 11
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')
    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},
        })
Exemplo n.º 13
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')
    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')
Exemplo n.º 15
0
    def get(self, domain_name):
        domain = Domain.get_domain_by_name(domain_name, self.db)

        if not domain:
            self.set_status(404, self._('Domain %s not found') % domain_name)
            return

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

        violation_defs = self.application.violation_definitions

        result = []

        for pref in prefs:
            key = violation_defs.get(pref.get('key'))

            if key is None:
                continue

            result.append({
                'key': pref.get('key'),
                'title': key.get('default_value_description', None),
                'category': key.get('category', None),
                'value': pref.get('value'),
                'default_value': key.get('default_value', None),
                'unit': key.get('unit', None)
            })

        self.write_json(result)
Exemplo n.º 16
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')
Exemplo n.º 17
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')
Exemplo n.º 18
0
    def add_domain(cls, url, db, publish_method, config, girl, default_violations_values, violation_definitions, cache):

        from holmes.models import Domain, DomainsViolationsPrefs
        from holmes.material import expire_materials

        domain_name, domain_url = get_domain_from_url(url)

        domains = (
            db.query(Domain)
            .filter(
                or_(
                    Domain.name == domain_name,
                    Domain.name == domain_name.rstrip("/"),
                    Domain.name == "%s/" % domain_name,
                )
            )
            .all()
        )

        if not domains:
            domain = None
        else:
            domain = domains[0]

        if not domain:
            url_hash = hashlib.sha512(domain_url).hexdigest()
            domain = Domain(url=domain_url, url_hash=url_hash, name=domain_name)
            db.add(domain)
            db.flush()

            expire_materials(girl)

            publish_method(dumps({"type": "new-domain", "domainUrl": str(domain_url)}))

            keys = default_violations_values.keys()

            DomainsViolationsPrefs.insert_default_violations_values_for_domain(
                db, domain, keys, violation_definitions, cache
            )

            from holmes.models import Limiter

            connections = config.DEFAULT_NUMBER_OF_CONCURRENT_CONNECTIONS
            Limiter.add_or_update_limiter(db, domain_url, connections)

        return domain
Exemplo n.º 19
0
    def add_domain(cls, url, db, publish_method, config, girl,
                   default_violations_values, violation_definitions, cache):

        from holmes.models import Domain, DomainsViolationsPrefs
        from holmes.material import expire_materials

        domain_name, domain_url = get_domain_from_url(url)

        domains = db.query(Domain).filter(
            or_(Domain.name == domain_name,
                Domain.name == domain_name.rstrip('/'),
                Domain.name == "%s/" % domain_name)).all()

        if not domains:
            domain = None
        else:
            domain = domains[0]

        if not domain:
            url_hash = hashlib.sha512(domain_url).hexdigest()
            domain = Domain(url=domain_url,
                            url_hash=url_hash,
                            name=domain_name)
            db.add(domain)
            db.flush()

            expire_materials(girl)

            publish_method(
                dumps({
                    'type': 'new-domain',
                    'domainUrl': str(domain_url)
                }))

            keys = default_violations_values.keys()

            DomainsViolationsPrefs.insert_default_violations_values_for_domain(
                db, domain, keys, violation_definitions, cache)

            from holmes.models import Limiter
            connections = config.DEFAULT_NUMBER_OF_CONCURRENT_CONNECTIONS
            Limiter.add_or_update_limiter(db, domain_url, connections)

        return domain
Exemplo n.º 20
0
    def test_can_insert_domains_violations_prefs(self):
        domain = DomainFactory.create()
        key = KeyFactory.create()
        value = '{"page.size": 100}'

        DomainsViolationsPrefs.insert_domains_violations_prefs(
            self.db, domain, key, value)

        data = self.db \
            .query(DomainsViolationsPrefs) \
            .filter(
                DomainsViolationsPrefs.key_id == key.id,
                DomainsViolationsPrefs.domain_id == domain.id,
            ) \
            .all()

        expect(data).not_to_be_null()
        expect(data).to_length(1)
        expect(data[0].value).to_equal(value)
    def test_can_insert_domains_violations_prefs(self):
        domain = DomainFactory.create()
        key = KeyFactory.create()
        value = '{"page.size": 100}'

        DomainsViolationsPrefs.insert_domains_violations_prefs(
            self.db, domain, key, value
        )

        data = self.db \
            .query(DomainsViolationsPrefs) \
            .filter(
                DomainsViolationsPrefs.key_id == key.id,
                DomainsViolationsPrefs.domain_id == domain.id,
            ) \
            .all()

        expect(data).not_to_be_null()
        expect(data).to_length(1)
        expect(data[0].value).to_equal(value)
Exemplo n.º 22
0
    def post(self, domain_name):
        if not self.validate_superuser():
            return

        domain = Domain.get_domain_by_name(domain_name, self.db)

        if not domain:
            self.set_status(404, self._('Domain %s not found') % domain_name)
            return

        post_data = loads(self.request.body)

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

        self.write_json({
            'reason': self._('Preferences successfully saved!'),
            'description': self._('Preferences successfully saved!')
        })
Exemplo n.º 23
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)
Exemplo n.º 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_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)
Exemplo n.º 26
0
    def test_can_insert_default_violations_values_for_domain(self):
        domain = DomainFactory.create()

        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
            }
        }

        keys = violation_definitions.keys()

        data = DomainsViolationsPrefs.get_domains_violations_prefs_by_domain(
            self.db, domain.name)
        expect(data).to_length(0)

        DomainsViolationsPrefs.insert_default_violations_values_for_domain(
            self.db, domain, keys, violation_definitions, self.cache)

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

        expect(data).not_to_be_null()
        expect(data).to_length(2)
        expect(data).to_be_like([{
            'value': 100,
            'key': 'page.title.size'
        }, {
            'value': 5,
            'key': 'total.requests.img'
        }])
    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_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')
Exemplo n.º 29
0
    def after_start(self, io_loop):
        if self.db is not None:
            self.application.db = self.db
        else:
            self.application.db = self.application.get_sqlalchemy_session()

        if self.debug:
            from sqltap import sqltap
            self.sqltap = sqltap.start()

        authnz_wrapper_class = self._load_authnz_wrapper()
        if authnz_wrapper_class:
            self.application.authnz_wrapper = authnz_wrapper_class(self.application.config)
        else:
            self.application.authnz_wrapper = None

        self.application.facters = self._load_facters()
        self.application.validators = self._load_validators()
        self.application.error_handlers = [handler(self.application.config) for handler in self._load_error_handlers()]

        self.application.search_provider = self._load_search_provider()(
            config=self.application.config,
            db=self.application.db,
            authnz_wrapper=self.application.authnz_wrapper,
            io_loop=io_loop
        )

        self.application.fact_definitions = {}
        self.application.violation_definitions = {}

        self.application.default_violations_values = {}

        for facter in self.application.facters:
            self.application.fact_definitions.update(facter.get_fact_definitions())

        Key.insert_keys(self.application.db, self.application.fact_definitions)

        for validator in self.application.validators:
            self.application.violation_definitions.update(validator.get_violation_definitions())

            self.application.default_violations_values.update(
                validator.get_default_violations_values(self.application.config)
            )

        Key.insert_keys(
            self.application.db,
            self.application.violation_definitions,
            self.application.default_violations_values
        )

        self.application.event_bus = EventBus(self.application)
        self.application.http_client = AsyncHTTPClient(io_loop=io_loop)

        self.application.cache = Cache(self.application)

        self.configure_material_girl()

        self.configure_i18n()

        DomainsViolationsPrefs.insert_default_violations_values_for_all_domains(
            self.application.db,
            self.application.default_violations_values,
            self.application.violation_definitions,
            self.application.cache
        )
            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')
        else:
            assert False, 'Should not have got this far'

        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'
        })

    @gen_test
    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({
Exemplo n.º 31
0
    def after_start(self, io_loop):
        if self.db is not None:
            self.application.db = self.db
        else:
            self.application.db = self.application.get_sqlalchemy_session()

        if self.debug:
            from sqltap import sqltap
            self.sqltap = sqltap.start()

        authnz_wrapper_class = self._load_authnz_wrapper()
        if authnz_wrapper_class:
            self.application.authnz_wrapper = authnz_wrapper_class(
                self.application.config)
        else:
            self.application.authnz_wrapper = None

        self.application.facters = self._load_facters()
        self.application.validators = self._load_validators()
        self.application.error_handlers = [
            handler(self.application.config)
            for handler in self._load_error_handlers()
        ]

        self.application.search_provider = self._load_search_provider()(
            config=self.application.config,
            db=self.application.db,
            authnz_wrapper=self.application.authnz_wrapper,
            io_loop=io_loop)

        self.application.fact_definitions = {}
        self.application.violation_definitions = {}

        self.application.default_violations_values = {}

        for facter in self.application.facters:
            self.application.fact_definitions.update(
                facter.get_fact_definitions())

        Key.insert_keys(self.application.db, self.application.fact_definitions)

        for validator in self.application.validators:
            self.application.violation_definitions.update(
                validator.get_violation_definitions())

            self.application.default_violations_values.update(
                validator.get_default_violations_values(
                    self.application.config))

        Key.insert_keys(self.application.db,
                        self.application.violation_definitions,
                        self.application.default_violations_values)

        self.application.event_bus = EventBus(self.application)
        self.application.http_client = AsyncHTTPClient(io_loop=io_loop)

        self.application.cache = Cache(self.application)

        self.configure_material_girl()

        self.configure_i18n()

        DomainsViolationsPrefs.insert_default_violations_values_for_all_domains(
            self.application.db, self.application.default_violations_values,
            self.application.violation_definitions, self.application.cache)
Exemplo n.º 32
0
 def get_domain_violations_prefs(self, domain_name):
     return self.get_data(
         'violations-prefs-%s' % (domain_name),
         int(self.config.DOMAINS_VIOLATIONS_PREFS_EXPIRATION_IN_SECONDS),
         lambda: DomainsViolationsPrefs.get_domains_violations_prefs_by_domain(self.db, domain_name)
     )
Exemplo n.º 33
0
                                           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')
        else:
            assert False, 'Should not have got this far'

        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'
        })

    @gen_test
    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)