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.º 2
0
    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
        })
Exemplo n.º 3
0
    def test_can_save_prefs(self):
        user = UserFactory.create(is_superuser=True)

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

        UsersViolationsPrefsFactory.create(user=user, key=key1, is_active=True)
        UsersViolationsPrefsFactory.create(user=user, key=key2, is_active=False)

        loaded_prefs = UsersViolationsPrefs.get_prefs(self.db, user)
        expect(loaded_prefs).to_length(2)
        expect(loaded_prefs).to_be_like({
            'some.random.1': True,
            'some.random.2': False
        })

        yield self.authenticated_fetch(
            '/users/violations-prefs/',
            user_email=user.email,
            method='POST',
            body=dumps([
                {'key': 'some.random.1', 'is_active': False},
                {'key': 'some.random.2', 'is_active': True},
            ])
        )

        loaded_prefs = UsersViolationsPrefs.get_prefs(self.db, user)
        expect(loaded_prefs).to_length(2)
        expect(loaded_prefs).to_be_like({
            'some.random.1': False,
            'some.random.2': True
        })
Exemplo n.º 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
            },
        })
Exemplo n.º 5
0
    def test_can_get_last_reviews(self):
        page = PageFactory.create()

        date_now = datetime(2013, 11, 12, 13, 25, 27)

        review = ReviewFactory.create(page=page,
                                      is_active=True,
                                      is_complete=False,
                                      completed_date=date_now,
                                      created_date=date_now)

        key1 = KeyFactory.create(name='fact')
        review.add_fact(key1, 'value')
        key2 = KeyFactory.create(name='violation')
        review.add_violation(key2, 'value', 100, page.domain)
        review.is_complete = True
        self.db.flush()

        response = yield self.authenticated_fetch('/last-reviews')

        expect(response.code).to_equal(200)

        dt = calendar.timegm(date_now.utctimetuple())

        expected = [{
            'domain':
            review.domain.name,
            'page':
            page.to_dict(),
            'uuid':
            str(review.uuid),
            'isComplete':
            True,
            'facts': [{
                u'key': u'fact',
                u'unit': u'value',
                u'value': u'value',
                u'title': u'unknown',
                u'category': u'unknown'
            }],
            'violations': [{
                u'points': 100,
                u'description': u'value',
                u'key': u'violation',
                u'title': u'undefined',
                u'category': 'undefined'
            }],
            'createdAt':
            dt,
            'completedAt':
            dt,
            'violationCount':
            1,
        }]

        expect(loads(response.body)).to_be_like(expected)
    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'
            }
        ])
Exemplo n.º 7
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'
        }])
    def test_can_get_prefs(self):
        user = UserFactory.create()
        key1 = KeyFactory.create()
        key2 = KeyFactory.create()

        UsersViolationsPrefs.insert_pref(self.db, user, key1, True)
        UsersViolationsPrefs.insert_pref(self.db, user, key2, False)

        data = UsersViolationsPrefs.get_prefs(self.db, user)

        expect(data).to_length(2)

        expect(data).to_be_like({key1.name: True, key2.name: False})
Exemplo n.º 9
0
    def test_can_get_review(self):
        dt = datetime(2010, 11, 12, 13, 14, 15)
        dt_timestamp = calendar.timegm(dt.utctimetuple())
        review = ReviewFactory.create(created_date=dt)

        key1 = KeyFactory.create(name='fact')
        review.add_fact(key1, 'value')
        key2 = KeyFactory.create(name='violation')
        review.add_violation(key2, 'value', 100, review.domain)

        self.db.flush()

        response = yield self.authenticated_fetch(
            '/page/%s/review/%s' % (review.page.uuid, review.uuid))

        expect(response.code).to_equal(200)

        expected = {
            'domain':
            review.domain.name,
            'page':
            review.page.to_dict(),
            'uuid':
            str(review.uuid),
            'isComplete':
            False,
            'facts': [{
                u'key': u'fact',
                u'value': u'value',
                u'title': u'unknown',
                u'unit': u'value',
                u'category': u'unknown'
            }],
            'violations': [{
                u'points': 100,
                u'description': u'value',
                u'key': u'violation',
                u'title': u'undefined',
                u'category': 'undefined'
            }],
            'createdAt':
            dt_timestamp,
            'completedAt':
            None,
            'violationPoints':
            100,
            'violationCount':
            1,
        }

        expect(loads(response.body)).to_be_like(expected)
    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_prefs(self):
        user = UserFactory.create()
        key1 = KeyFactory.create()
        key2 = KeyFactory.create()

        UsersViolationsPrefs.insert_pref(self.db, user, key1, True)
        UsersViolationsPrefs.insert_pref(self.db, user, key2, False)

        data = UsersViolationsPrefs.get_prefs(self.db, user)

        expect(data).to_length(2)

        expect(data).to_be_like(
            {key1.name: True, key2.name: False}
        )
Exemplo n.º 12
0
    def test_can_get_prefs(self):
        user = UserFactory.create()

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

        UsersViolationsPrefsFactory.create(user=user, key=key1, is_active=True)
        UsersViolationsPrefsFactory.create(user=user, key=key2, is_active=False)

        self.server.application.violation_definitions = {
            'some.random.1': {
                'title': 'some random 1',
                'category': 'SEO',
                'generic_description': 'my some random 1'
            },
            'some.random.2': {
                'title': 'some random 2',
                'category': 'HTTP',
                'generic_description': 'my some random 2'
            }
        }

        response = yield self.authenticated_fetch(
            '/users/violations-prefs/',
            user_email=user.email
        )

        expect(response.code).to_equal(200)

        prefs = loads(response.body)

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

        expect(prefs).to_be_like([{
            'category': 'SEO',
            'is_active': True,
            'name': 'some random 1',
            'key': u'some.random.1',
            'description': u'my some random 1'
        }, {
            'category': 'HTTP',
            'is_active': False,
            'name': 'some random 2',
            'key': u'some.random.2',
            'description': u'my some random 2'
        }])
Exemplo n.º 13
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'}
        ])
Exemplo n.º 14
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'}
        ])
    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'
        })
    def test_can_insert_prefs_without_user(self):
        user = None

        prefs = self.db.query(UsersViolationsPrefs).all()
        expect(prefs).to_length(0)

        items = []
        for x in range(3):
            name = 'key-test-%d' % x
            KeyFactory.create(name=name)
            items.append(name)

        UsersViolationsPrefs.insert_prefs(self.db, user, items)

        prefs = self.db.query(UsersViolationsPrefs).all()
        expect(prefs).to_length(0)
Exemplo n.º 18
0
    def test_can_get_violations(self):

        keys = [KeyFactory.create(name='random.fact.%s' % i) for i in range(3)]

        violation_definitions = {
            'random.fact.%s' % i: {
                'title': 'title.%d' % i,
                'category': 'category.%d' % i,
                'key': key
            } for i, key in enumerate(keys)
        }

        def extract_key_name(violation_definition):
            key = violation_definition.pop('key')
            violation_definition['key_name'] = key.name
            return violation_definition

        expected = [extract_key_name(vd) for vd in violation_definitions.values()]

        self.server.application.violation_definitions = violation_definitions

        response = yield self.authenticated_fetch('/violations')

        violations = loads(response.body)

        expect(violations).to_length(3)
        expect(violations).to_be_like(expected)
Exemplo n.º 19
0
    def test_can_get_most_common_violations(self):
        self.db.query(Violation).delete()
        self.db.query(Key).delete()

        category = KeysCategoryFactory.create(name="SEO")
        for i in range(3):
            key = KeyFactory.create(name="some.random.fact.%s" % i, category=category)
            for j in range(i):
                ViolationFactory.create(key=key, value="value", points=10 * i + j)

        violation_definitions = {
            "some.random.fact.1": {"title": "SEO", "category": "SEO"},
            "some.random.fact.2": {"title": "SEO", "category": "SEO"},
        }

        violations = Violation.get_most_common_violations(self.db, violation_definitions)

        expect(violations).to_be_like(
            [
                {"count": 1, "key": "some.random.fact.1", "category": "SEO", "title": "SEO"},
                {"count": 2, "key": "some.random.fact.2", "category": "SEO", "title": "SEO"},
            ]
        )

        violations = Violation.get_most_common_violations(self.db, violation_definitions, sample_limit=2)

        expect(violations).to_be_like([{"count": 2, "key": "some.random.fact.2", "category": "SEO", "title": "SEO"}])
Exemplo n.º 20
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.º 21
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_can_update_by_user(self):
        user = UserFactory.create()

        key = KeyFactory.create(name='some.random')

        pref = UsersViolationsPrefsFactory.create(user=user,
                                                  key=key,
                                                  is_active=True)

        data = [{
            'key': pref.key.name,
            'is_active': False
        }, {
            'key': 'blah',
            'is_active': False
        }]

        UsersViolationsPrefs.update_by_user(self.db, user, data)

        prefs = self.db.query(UsersViolationsPrefs).all()

        expect(prefs).to_length(1)
        expect(prefs[0].key.name).to_equal(key.name)
        expect(prefs[0].user).to_equal(user)
        expect(prefs[0].is_active).to_equal(False)
Exemplo n.º 23
0
    def test_can_get_violations(self):

        keys = [KeyFactory.create(name='random.fact.%s' % i) for i in range(3)]

        violation_definitions = {
            'random.fact.%s' % i: {
                'title': 'title.%d' % i,
                'category': 'category.%d' % i,
                'key': key
            }
            for i, key in enumerate(keys)
        }

        def extract_key_name(violation_definition):
            key = violation_definition.pop('key')
            violation_definition['key_name'] = key.name
            return violation_definition

        expected = [
            extract_key_name(vd) for vd in violation_definitions.values()
        ]

        self.server.application.violation_definitions = violation_definitions

        response = yield self.authenticated_fetch('/violations')

        violations = loads(response.body)

        expect(violations).to_length(3)
        expect(violations).to_be_like(expected)
Exemplo n.º 24
0
    def test_get_by_name(self):
        key = KeyFactory.create(name='some.random.key')

        loadded_key = Key.get_by_name(self.db, 'some.random.key')

        expect(key).to_equal(loadded_key)
        expect(loadded_key.name).to_equal('some.random.key')
    def test_can_insert_prefs_without_user(self):
        user = None

        prefs = self.db.query(UsersViolationsPrefs).all()
        expect(prefs).to_length(0)

        items = []
        for x in range(3):
            name = 'key-test-%d' % x
            KeyFactory.create(name=name)
            items.append(name)

        UsersViolationsPrefs.insert_prefs(self.db, user, items)

        prefs = self.db.query(UsersViolationsPrefs).all()
        expect(prefs).to_length(0)
Exemplo n.º 26
0
    def test_can_create_key(self):
        key = KeyFactory.create(name='some.random.key')

        loaded_key = self.db.query(Key).get(key.id)

        expect(loaded_key.name).to_equal('some.random.key')
        expect(str(loaded_key)).to_be_like('%s' % key.name)
Exemplo n.º 27
0
    def test_get_by_name(self):
        key = KeyFactory.create(name='some.random.key')

        loadded_key = Key.get_by_name(self.db, 'some.random.key')

        expect(key).to_equal(loadded_key)
        expect(loadded_key.name).to_equal('some.random.key')
Exemplo n.º 28
0
    def test_can_create_key(self):
        key = KeyFactory.create(name='some.random.key')

        loaded_key = self.db.query(Key).get(key.id)

        expect(loaded_key.name).to_equal('some.random.key')
        expect(str(loaded_key)).to_be_like('%s' % key.name)
Exemplo n.º 29
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.º 30
0
    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
                              })
Exemplo n.º 31
0
    def test_can_get_last_reviews(self):
        page = PageFactory.create()

        date_now = datetime(2013, 11, 12, 13, 25, 27)

        review = ReviewFactory.create(
            page=page,
            is_active=True,
            is_complete=False,
            completed_date=date_now,
            created_date=date_now)

        key1 = KeyFactory.create(name='fact')
        review.add_fact(key1, 'value')
        key2 = KeyFactory.create(name='violation')
        review.add_violation(key2, 'value', 100, page.domain)
        review.is_complete = True
        self.db.flush()

        url = self.get_url('/last-reviews')
        response = yield self.http_client.fetch(url, method='GET')

        expect(response.code).to_equal(200)

        dt = calendar.timegm(date_now.utctimetuple())

        expected = [{
            'domain': review.domain.name,
            'page': page.to_dict(),
            'uuid': str(review.uuid),
            'isComplete': True,
            'facts': [
                {u'key': u'fact', u'unit': u'value', u'value': u'value',
                 u'title': u'unknown', u'category': u'unknown'}
            ],
            'violations': [
                {u'points': 100, u'description': u'value',
                 u'key': u'violation', u'title': u'undefined', u'category': 'undefined'}
            ],
            'createdAt': dt,
            'completedAt': dt,
            'violationCount': 1,
        }]

        expect(loads(response.body)).to_be_like(expected)
Exemplo n.º 32
0
    def test_can_convert_key_to_dict(self):
        key = KeyFactory.create()

        key_dict = key.to_dict()

        expect(key_dict).to_be_like({
            'name': str(key.name),
            'category': str(key.category.name)
        })
Exemplo n.º 33
0
    def test_can_convert_key_to_dict(self):
        key = KeyFactory.create()

        key_dict = key.to_dict()

        expect(key_dict).to_be_like({
            'name': str(key.name),
            'category': str(key.category.name)
        })
Exemplo n.º 34
0
    def test_can_get_most_common_violations_names(self):
        for i in range(3):
            key = KeyFactory.create(name='some.random.fact.%s' % i)
            for j in range(i):
                ViolationFactory.create(key=key)

        violations = Violation.get_most_common_violations_names(self.db)

        expect(violations).to_be_like([('some.random.fact.1', 1), ('some.random.fact.2', 2)])
Exemplo n.º 35
0
    def test_can_get_most_common_violations_names(self):
        for i in range(3):
            key = KeyFactory.create(name='some.random.fact.%s' % i)
            for j in range(i):
                ViolationFactory.create(key=key)

        violations = Violation.get_most_common_violations_names(self.db)

        expect(violations).to_be_like([('some.random.fact.1', 1), ('some.random.fact.2', 2)])
Exemplo n.º 36
0
    def test_can_append_facts(self):
        review = ReviewFactory.build()
        expect(review.facts).to_length(0)

        key = KeyFactory.create(name='a')

        review.add_fact(key, 'b')
        expect(review.facts).to_length(1)
        expect(review.facts[0].key.name).to_equal('a')
        expect(review.facts[0].value).to_equal('b')
    def test_can_insert_pref(self):
        user = UserFactory.create()
        key1 = KeyFactory.create()
        key2 = KeyFactory.create()

        UsersViolationsPrefs.insert_pref(self.db, user, key1, True)
        UsersViolationsPrefs.insert_pref(self.db, user, key2, False)

        prefs = self.db.query(UsersViolationsPrefs).all()

        expect(prefs).to_length(2)

        expect(prefs[0].key).to_equal(key1)
        expect(prefs[0].user).to_equal(user)
        expect(prefs[0].is_active).to_equal(True)

        expect(prefs[1].key).to_equal(key2)
        expect(prefs[1].user).to_equal(user)
        expect(prefs[1].is_active).to_equal(False)
    def test_can_insert_pref(self):
        user = UserFactory.create()
        key1 = KeyFactory.create()
        key2 = KeyFactory.create()

        UsersViolationsPrefs.insert_pref(self.db, user, key1, True)
        UsersViolationsPrefs.insert_pref(self.db, user, key2, False)

        prefs = self.db.query(UsersViolationsPrefs).all()

        expect(prefs).to_length(2)

        expect(prefs[0].key).to_equal(key1)
        expect(prefs[0].user).to_equal(user)
        expect(prefs[0].is_active).to_equal(True)

        expect(prefs[1].key).to_equal(key2)
        expect(prefs[1].user).to_equal(user)
        expect(prefs[1].is_active).to_equal(False)
Exemplo n.º 39
0
    def test_can_get_review(self):
        dt = datetime(2010, 11, 12, 13, 14, 15)
        dt_timestamp = calendar.timegm(dt.utctimetuple())
        review = ReviewFactory.create(created_date=dt)

        key1 = KeyFactory.create(name='fact')
        review.add_fact(key1, 'value')
        key2 = KeyFactory.create(name='violation')
        review.add_violation(key2, 'value', 100, review.domain)

        self.db.flush()

        url = self.get_url(
            '/page/%s/review/%s' % (
                review.page.uuid,
                review.uuid
            )
        )

        response = yield self.http_client.fetch(url)

        expect(response.code).to_equal(200)

        expected = {
            'domain': review.domain.name,
            'page': review.page.to_dict(),
            'uuid': str(review.uuid),
            'isComplete': False,
            'facts': [
                {u'key': u'fact', u'value': u'value', u'title': u'unknown',
                 u'unit': u'value', u'category': u'unknown'}
            ],
            'violations': [
                {u'points': 100, u'description': u'value',
                 u'key': u'violation', u'title': u'undefined', u'category': 'undefined'}
            ],
            'createdAt': dt_timestamp,
            'completedAt': None,
            'violationPoints': 100,
            'violationCount': 1,
        }

        expect(loads(response.body)).to_be_like(expected)
Exemplo n.º 40
0
    def test_can_append_facts(self):
        review = ReviewFactory.build()
        expect(review.facts).to_length(0)

        key = KeyFactory.create(name='a')

        review.add_fact(key, 'b')
        expect(review.facts).to_length(1)
        expect(review.facts[0].key.name).to_equal('a')
        expect(review.facts[0].value).to_equal('b')
Exemplo n.º 41
0
    def test_can_create_facts_float(self):
        review = ReviewFactory.create()

        key = KeyFactory.create(name="some.random.fact")
        review.add_fact(key, value=1203.01)

        loaded_review = self.db.query(Review).get(review.id)

        expect(loaded_review.facts).to_length(1)
        expect(loaded_review.facts[0].key.name).to_equal("some.random.fact")
        expect(loaded_review.facts[0].value).to_equal(1203.01)
Exemplo n.º 42
0
    def test_can_create_facts_float(self):
        review = ReviewFactory.create()

        key = KeyFactory.create(name="some.random.fact")
        review.add_fact(key, value=1203.01)

        loaded_review = self.db.query(Review).get(review.id)

        expect(loaded_review.facts).to_length(1)
        expect(loaded_review.facts[0].key.name).to_equal("some.random.fact")
        expect(loaded_review.facts[0].value).to_equal(1203.01)
Exemplo n.º 43
0
    def test_can_append_violations(self):
        review = ReviewFactory.build()
        expect(review.violations).to_length(0)

        key = KeyFactory.create(name='a')

        review.add_violation(key, 'b', 100, review.domain)
        expect(review.violations).to_length(1)
        expect(review.violations[0].key.name).to_equal('a')
        expect(review.violations[0].value).to_equal('b')
        expect(review.violations[0].points).to_equal(100)
Exemplo n.º 44
0
    def test_can_append_violations(self):
        review = ReviewFactory.build()
        expect(review.violations).to_length(0)

        key = KeyFactory.create(name='a')

        review.add_violation(key, 'b', 100, review.domain)
        expect(review.violations).to_length(1)
        expect(review.violations[0].key.name).to_equal('a')
        expect(review.violations[0].value).to_equal('b')
        expect(review.violations[0].points).to_equal(100)
Exemplo n.º 45
0
    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,
            }
        )
Exemplo n.º 46
0
    def test_get_group_by_key_id_for_all_domains(self):
        domains = [DomainFactory.create(name='g%d.com' % i) for i in range(2)]
        keys = [KeyFactory.create(name='random.fact.%s' % i) for i in range(3)]

        for i in range(3):
            for j in range(i + 1):
                ViolationFactory.create(key=keys[i], domain=domains[j % 2])

        violations = Violation.get_group_by_key_id_for_all_domains(self.db)

        expect(violations).to_length(5)
        expect(violations[0]).to_be_like((keys[2].id, 'g0.com', 2))
Exemplo n.º 47
0
    def test_can_get_top_in_category_for_all_domains(self):
        domains = [DomainFactory.create(name="g%d.com" % i) for i in range(2)]
        keys = [KeyFactory.create(name="random.fact.%s" % i) for i in range(3)]

        for i in range(3):
            for j in range(i + 1):
                ViolationFactory.create(key=keys[i], domain=domains[j % 2])
        violations = Violation.get_top_in_category_for_all_domains(self.db)

        expect(violations).to_length(5)
        top = ("g0.com", keys[2].category_id, str(keys[2]), 2)
        expect(violations[0]).to_be_like(top)
Exemplo n.º 48
0
    def test_get_group_by_key_id_for_all_domains(self):
        domains = [DomainFactory.create(name="g%d.com" % i) for i in range(2)]
        keys = [KeyFactory.create(name="random.fact.%s" % i) for i in range(3)]

        for i in range(3):
            for j in range(i + 1):
                ViolationFactory.create(key=keys[i], domain=domains[j % 2])

        violations = Violation.get_group_by_key_id_for_all_domains(self.db)

        expect(violations).to_length(5)
        expect(violations[0]).to_be_like((keys[2].id, "g0.com", 2))
Exemplo n.º 49
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)
Exemplo n.º 50
0
    def test_can_add_category(self):
        key = KeyFactory.create(name='some.random.key')

        category = KeysCategoryFactory(name='SEO')

        key.category = category

        self.db.flush()

        loaded_category = self.db.query(Key).get(key.id).category

        expect(str(loaded_category.name)).to_be_like('%s' % category.name)
        expect(loaded_category.name).to_equal(category.name)
Exemplo n.º 51
0
    def test_can_get_by_key_id_group_by_domain(self):
        domains = [DomainFactory.create(name="g%d.com" % i) for i in xrange(2)]
        keys = [KeyFactory.create(name="random.fact.%s" % i) for i in xrange(3)]

        for i in range(3):
            for j in range(i + 1):
                ViolationFactory.create(key=keys[i], domain=domains[j % 2])

        violations = Violation.get_by_key_id_group_by_domain(self.db, keys[1].id)
        expect(violations).to_be_like([("g0.com", 1), ("g1.com", 1)])

        violations = Violation.get_by_key_id_group_by_domain(self.db, keys[2].id)
        expect(violations).to_be_like([("g0.com", 2), ("g1.com", 1)])
Exemplo n.º 52
0
    def test_can_add_category(self):
        key = KeyFactory.create(name='some.random.key')

        category = KeysCategoryFactory(name='SEO')

        key.category = category

        self.db.flush()

        loaded_category = self.db.query(Key).get(key.id).category

        expect(str(loaded_category.name)).to_be_like('%s' % category.name)
        expect(loaded_category.name).to_equal(category.name)
Exemplo n.º 53
0
    def test_cant_append_violations_after_complete(self):
        review = ReviewFactory.build()
        expect(review.facts).to_length(0)
        review.is_complete = True

        try:
            key = KeyFactory.create(name='a')
            review.add_violation(key, 'b', 10, review.domain)
        except ValueError:
            err = sys.exc_info()[1]
            expect(err).to_have_an_error_message_of("Can't add anything to a completed review.")
        else:
            assert False, 'Should not have gotten this far'
Exemplo n.º 54
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_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)
Exemplo n.º 56
0
    def test_cant_append_violations_after_complete(self):
        review = ReviewFactory.build()
        expect(review.facts).to_length(0)
        review.is_complete = True

        try:
            key = KeyFactory.create(name='a')
            review.add_violation(key, 'b', 10, review.domain)
        except ValueError:
            err = sys.exc_info()[1]
            expect(err).to_have_an_error_message_of(
                "Can't add anything to a completed review.")
        else:
            assert False, 'Should not have gotten this far'
Exemplo n.º 57
0
    def test_can_get_top_in_category_for_all_domains(self):
        domains = [DomainFactory.create(name='g%d.com' % i) for i in range(2)]
        keys = [KeyFactory.create(name='random.fact.%s' % i) for i in range(3)]

        for i in range(3):
            for j in range(i + 1):
                ViolationFactory.create(
                    key=keys[i],
                    domain=domains[j % 2]
                )
        violations = Violation.get_top_in_category_for_all_domains(self.db)

        expect(violations).to_length(5)
        top = ('g0.com', keys[2].category_id, str(keys[2]), 2)
        expect(violations[0]).to_be_like(top)
Exemplo n.º 58
0
    def test_can_get_by_key_name_domains(self):
        domains = [DomainFactory.create(name='g%d.com' % i) for i in range(2)]
        keys = [KeyFactory.create(name='random.fact.%s' % i) for i in range(3)]

        for i in range(3):
            for j in range(i + 1):
                ViolationFactory.create(key=keys[i], domain=domains[j % 2])

        self.db.flush()

        self.server.application.violation_definitions = {
            'random.fact.%s' % i: {
                'title': 'SEO',
                'category': 'SEO',
                'generic_description': 'Desc',
                'key': keys[i]
            }
            for i in range(3)
        }

        response = yield self.authenticated_fetch('/violation/%s/domains' %
                                                  keys[2].name)

        violation = loads(response.body)

        expected_violation = {
            'domains': [{
                'name': 'g0.com',
                'count': 2
            }, {
                'name': 'g1.com',
                'count': 1
            }],
            'total':
            3,
            'title':
            'SEO',
            'category':
            'SEO',
            'description':
            'Desc'
        }

        expect(response.code).to_equal(200)
        expect(violation).to_length(5)
        expect(violation['domains']).to_length(2)
        expect(violation).to_be_like(expected_violation)