def test_can_get_last_reviews_count_in_last_hour_filter_by_domain(self): dt = datetime.utcnow() domain1 = DomainFactory.create() domain2 = DomainFactory.create() page1 = PageFactory.create(domain=domain1) page2 = PageFactory.create(domain=domain2) ReviewFactory.create(is_active=True, completed_date=dt - timedelta(minutes=1), page=page1) ReviewFactory.create(is_active=True, completed_date=dt - timedelta(minutes=59), page=page1) ReviewFactory.create(is_active=True, completed_date=dt - timedelta(minutes=1), page=page2) ReviewFactory.create(is_active=True, completed_date=dt - timedelta(minutes=59), page=page2) self.db.flush() response = yield self.authenticated_fetch( '/reviews-in-last-hour?domain_filter=%s' % domain1.name) expect(response.code).to_equal(200) result = loads(response.body) expect(result['count']).to_equal(2)
def test_can_get_violation_count_and_points(self): domain = DomainFactory.create() domain2 = DomainFactory.create() DomainFactory.create() page = PageFactory.create(domain=domain) page2 = PageFactory.create(domain=domain) page3 = PageFactory.create(domain=domain2) ReviewFactory.create(domain=domain, page=page, is_active=True, number_of_violations=20) ReviewFactory.create(domain=domain, page=page2, is_active=True, number_of_violations=10) ReviewFactory.create(domain=domain2, page=page3, is_active=True, number_of_violations=30) violation_count = domain.get_violation_data(self.db) expect(violation_count).to_equal(30)
def test_can_get_domain_grouped_violations(self): domain1 = DomainFactory.create() domain2 = DomainFactory.create() page1 = PageFactory.create(domain=domain1) page2 = PageFactory.create(domain=domain1) page3 = PageFactory.create(domain=domain2) ReviewFactory.create(domain=domain1, page=page1, is_active=True, number_of_violations=5) ReviewFactory.create(domain=domain1, page=page2, is_active=True, number_of_violations=7) ReviewFactory.create(domain=domain2, page=page3, is_active=True, number_of_violations=9) self.server.application.violation_definitions = { 'key.%s' % i: { 'title': 'title.%s' % i, 'category': 'category.%s' % (i % 3), 'key': Key.get_or_create(self.db, 'key.%d' % i, 'category.%d' % (i % 3)) } for i in xrange(9) } response = yield self.http_client.fetch( self.get_url('/domains/%s/violations' % domain1.name) ) expect(response.code).to_equal(200) domain_violations = loads(response.body) expect(domain_violations).to_length(5) expect(domain_violations.keys()).to_be_like(['domainName', 'violations', 'total', 'domainURL', 'domainId']) expect(domain_violations['total']).to_equal(12) expect(domain_violations['violations']).to_length(3) counts = map(lambda v: v['count'], domain_violations['violations']) expect(counts).to_be_like([5, 4, 3])
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_domains_details(self): self.db.query(Domain).delete() details = Domain.get_domains_details(self.db) expect(details).to_length(0) domain = DomainFactory.create(name='domain-1.com', url='http://domain-1.com/') domain2 = DomainFactory.create(name='domain-2.com', url='http://domain-2.com/') DomainFactory.create() page = PageFactory.create(domain=domain) page2 = PageFactory.create(domain=domain) page3 = PageFactory.create(domain=domain2) ReviewFactory.create(domain=domain, page=page, is_active=True, number_of_violations=20) ReviewFactory.create(domain=domain, page=page2, is_active=True, number_of_violations=10) ReviewFactory.create(domain=domain2, page=page3, is_active=True, number_of_violations=30) RequestFactory.create(status_code=200, domain_name=domain.name, response_time=0.25) RequestFactory.create(status_code=304, domain_name=domain.name, response_time=0.35) RequestFactory.create(status_code=400, domain_name=domain.name, response_time=0.25) RequestFactory.create(status_code=403, domain_name=domain.name, response_time=0.35) RequestFactory.create(status_code=404, domain_name=domain.name, response_time=0.25) details = Domain.get_domains_details(self.db) expect(details).to_length(3) expect(details[0]).to_length(10) expect(details[0]['url']).to_equal('http://domain-1.com/') expect(details[0]['name']).to_equal('domain-1.com') expect(details[0]['violationCount']).to_equal(30) expect(details[0]['pageCount']).to_equal(2) expect(details[0]['reviewCount']).to_equal(2) expect(details[0]['reviewPercentage']).to_equal(100.0) expect(details[0]['errorPercentage']).to_equal(60.0) expect(details[0]['is_active']).to_be_true() expect(details[0]['averageResponseTime']).to_equal(0.3)
def test_can_get_homepage(self): domain = DomainFactory(is_active=True) page = PageFactory(url=domain.url, domain=domain, last_review_uuid='123') homepage = domain.get_homepage(self.db) expect(homepage.uuid).to_equal(page.uuid) expect(homepage.last_review_uuid).to_equal('123')
def test_can_get_active_domains(self): self.db.query(Domain).delete() domain = DomainFactory(is_active=True) DomainFactory(is_active=False) domains = Domain.get_active_domains(self.db) expect(domains).to_length(1) expect(domains[0].id).to_equal(domain.id)
def test_get_domain_names(self): self.db.query(Domain).delete() DomainFactory.create(name="g1.globo.com") DomainFactory.create(name="globoesporte.globo.com") domain_names = Domain.get_domain_names(self.db) expect(domain_names).to_be_like( ['g1.globo.com', 'globoesporte.globo.com'])
def test_key_category_not_found(self): DomainFactory.create(name='domain-0.com') try: yield self.authenticated_fetch('/domains/domain-0.com/violations/0') except HTTPError: err = sys.exc_info()[1] expect(err).not_to_be_null() expect(err.code).to_equal(404) expect(err.response.reason).to_be_like('Key category 0 not found') else: assert False, 'Should not have got this far'
def test_get_domain_names(self): self.db.query(Domain).delete() DomainFactory.create(name="g1.globo.com") DomainFactory.create(name="globoesporte.globo.com") domain_names = Domain.get_domain_names(self.db) expect(domain_names).to_be_like([ 'g1.globo.com', 'globoesporte.globo.com' ])
def test_key_category_not_found(self): DomainFactory.create(name='domain-0.com') try: yield self.authenticated_fetch( '/domains/domain-0.com/violations/0') except HTTPError: err = sys.exc_info()[1] expect(err).not_to_be_null() expect(err.code).to_equal(404) expect(err.response.reason).to_be_like('Key category 0 not found') else: assert False, 'Should not have got this far'
def test_can_get_domain_top_category_violations(self): domain1 = DomainFactory.create() domain2 = DomainFactory.create() page1 = PageFactory.create(domain=domain1) page2 = PageFactory.create(domain=domain1) page3 = PageFactory.create(domain=domain2) ReviewFactory.create(domain=domain1, page=page1, is_active=True, number_of_violations=5) ReviewFactory.create(domain=domain1, page=page2, is_active=True, number_of_violations=7) ReviewFactory.create(domain=domain2, page=page3, is_active=True, number_of_violations=9) self.server.application.violation_definitions = { 'key.%s' % i: { 'title': 'title.%s' % i, 'category': 'category.%s' % (i % 3), 'key': Key.get_or_create(self.db, 'key.%d' % i, 'category.%d' % (i % 3)) } for i in range(9) } key = Key.get_by_name(self.db, 'key.0') response = yield self.authenticated_fetch( '/domains/%s/violations/%d/' % (domain1.name, key.category_id)) expect(response.code).to_equal(200) domain_top_category = loads(response.body) expect(domain_top_category).to_length(5) expect(domain_top_category.keys()).to_be_like([ 'violations', 'domainId', 'categoryId', 'domainURL', 'domainName' ]) expect(domain_top_category['violations']).to_length(3) counts = map(lambda v: v['count'], domain_top_category['violations']) expect(counts).to_be_like([2, 1, 1])
def test_can_get_pages_per_domain(self): domain = DomainFactory.create() domain2 = DomainFactory.create() DomainFactory.create() PageFactory.create(domain=domain) PageFactory.create(domain=domain) PageFactory.create(domain=domain2) PageFactory.create(domain=domain2) PageFactory.create(domain=domain2) pages_per_domain = Domain.get_pages_per_domain(self.db) expect(pages_per_domain).to_be_like({domain.id: 2, domain2.id: 3})
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_last_reviews_with_domain_filter(self): dt1 = datetime(2010, 10, 10, 10, 10, 10) dt2 = datetime(2010, 10, 11, 10, 10, 10) dt3 = datetime(2010, 10, 12, 10, 10, 10) domain1 = DomainFactory.create() domain2 = DomainFactory.create() page1 = PageFactory.create(domain=domain1) page2 = PageFactory.create(domain=domain2) ReviewFactory.create(is_active=True, is_complete=True, page=page1, completed_date=dt1) ReviewFactory.create(is_active=True, is_complete=True, page=page1, completed_date=dt2) ReviewFactory.create(is_active=True, is_complete=True, page=page1, completed_date=dt3) ReviewFactory.create(is_active=True, is_complete=True, page=page2, completed_date=dt1) ReviewFactory.create(is_active=True, is_complete=True, page=page2, completed_date=dt2) ReviewFactory.create(is_active=True, is_complete=True, page=page2, completed_date=dt3) response = yield self.authenticated_fetch( '/last-reviews?domain_filter=%s' % domain1.name) expect(response.code).to_equal(200) expect(len(loads(response.body))).to_be_like(3) expect(all([x['domain'] == domain1.name for x in loads(response.body)])).to_be_true() response = yield self.authenticated_fetch('/last-reviews') expect(response.code).to_equal(200) expect(len(loads(response.body))).to_be_like(6)
def test_domain_not_found_in_materials(self): DomainFactory.create(name='domain-0.com') self.server.application.girl.get = Mock(return_value=[]) try: yield self.authenticated_fetch('/domains/domain-0.com') except HTTPError: err = sys.exc_info()[1] expect(err).not_to_be_null() expect(err.code).to_equal(404) expect(err.response.reason).to_be_like('Domain domain-0.com not found') else: assert False, 'Should not have got this far'
def test_increment_active_review_count(self): key = 'g.com-active-review-count' self.cache.redis.delete(key) gcom = DomainFactory.create(url='http://g.com', name='g.com') page = PageFactory.create(domain=gcom) ReviewFactory.create( is_active=True, is_complete=True, domain=gcom, page=page, number_of_violations=1 ) page = PageFactory.create(domain=gcom) ReviewFactory.create( is_active=False, is_complete=True, domain=gcom, page=page, number_of_violations=3 ) page_count = yield self.cache.get_active_review_count('g.com') expect(page_count).to_equal(1) yield self.cache.increment_active_review_count('g.com') page_count = yield self.cache.get_active_review_count('g.com') expect(page_count).to_equal(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_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_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_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_get_count(self): key = 'g.com-my-key' self.sync_cache.redis.delete(key) gcom = DomainFactory.create(url="http://g.com", name="g.com") PageFactory.create(domain=gcom) count = self.sync_cache.get_count( key, gcom.name, int(self.config.PAGE_COUNT_EXPIRATION_IN_SECONDS), lambda domain: domain.get_page_count(self.db) ) expect(count).to_equal(1) # should get from cache self.sync_cache.db = None count = self.sync_cache.get_count( key, gcom.name, int(self.config.PAGE_COUNT_EXPIRATION_IN_SECONDS), lambda domain: domain.get_page_count(self.db) ) expect(count).to_equal(1)
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_increment_active_review_count(self): key = 'g.com-active-review-count' self.sync_cache.redis.delete(key) gcom = DomainFactory.create(url='http://g.com', name='g.com') page = PageFactory.create(domain=gcom) ReviewFactory.create( is_active=True, is_complete=True, domain=gcom, page=page, number_of_violations=1 ) page = PageFactory.create(domain=gcom) ReviewFactory.create( is_active=False, is_complete=True, domain=gcom, page=page, number_of_violations=3 ) self.sync_cache.increment_active_review_count(gcom.name) active_review_count = self.sync_cache.redis.get(key) expect(active_review_count).to_equal('1') self.sync_cache.increment_active_review_count(gcom.name) active_review_count = self.sync_cache.redis.get(key) expect(active_review_count).to_equal('2')
def test_can_convert_to_dict(self): domain = DomainFactory.create() domain_dict = domain.to_dict() expect(domain_dict['url']).to_equal(domain.url) expect(domain_dict['name']).to_equal(domain.name)
def test_can_get_page_reviews(self): dt1 = datetime(2010, 11, 12, 13, 14, 15) dt1_timestamp = calendar.timegm(dt1.utctimetuple()) dt2 = datetime(2011, 12, 13, 14, 15, 16) dt2_timestamp = calendar.timegm(dt2.utctimetuple()) domain = DomainFactory.create(url="http://www.domain-details.com", name="domain-details.com") page = PageFactory.create(domain=domain) review1 = ReviewFactory.create(page=page, is_active=False, is_complete=True, completed_date=dt1, number_of_violations=20) review2 = ReviewFactory.create(page=page, is_active=False, is_complete=True, completed_date=dt2, number_of_violations=30) response = yield self.http_client.fetch( self.get_url('/page/%s/reviews/' % page.uuid) ) expect(response.code).to_equal(200) page_details = loads(response.body) expect(page_details[0]['violationCount']).to_equal(30) expect(page_details[0]['uuid']).to_equal(str(review2.uuid)) expect(page_details[0]['completedAt']).to_equal(dt2_timestamp) expect(page_details[1]['violationCount']).to_equal(20) expect(page_details[1]['uuid']).to_equal(str(review1.uuid)) expect(page_details[1]['completedAt']).to_equal(dt1_timestamp)
def test_can_decrement_next_jobs_count(self): self.db.query(Page).delete() self.db.query(Domain).delete() key = 'next-jobs' self.sync_cache.redis.delete(key) globocom = DomainFactory.create( url="http://globo.com", name="globo.com", is_active=True ) for i in range(2): PageFactory.create(domain=globocom) self.sync_cache.increment_next_jobs_count(-2) page_count = self.sync_cache.redis.get(key) expect(page_count).to_equal('0') # should get from cache self.sync_cache.db = None self.sync_cache.increment_next_jobs_count(10) page_count = self.sync_cache.redis.get(key) expect(page_count).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')
def test_can_get_page_count(self): domain = DomainFactory.create() domain2 = DomainFactory.create() DomainFactory.create() PageFactory.create(domain=domain) PageFactory.create(domain=domain) PageFactory.create(domain=domain2) PageFactory.create(domain=domain2) PageFactory.create(domain=domain2) pages_for_domain = domain.get_page_count(self.db) pages_for_domain_2 = domain2.get_page_count(self.db) expect(pages_for_domain).to_equal(2) expect(pages_for_domain_2).to_equal(3)
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_get_domain_details(self): domain = DomainFactory.create(url="http://www.domain-details.com", name="domain-details.com") page = PageFactory.create(domain=domain, url=domain.url) page2 = PageFactory.create(domain=domain) ReviewFactory.create(page=page, is_active=True, is_complete=True, number_of_violations=20) ReviewFactory.create(page=page2, is_active=True, is_complete=True, number_of_violations=30) response = yield self.authenticated_fetch('/domains/%s/' % domain.name) expect(response.code).to_equal(200) domain_details = loads(response.body) expect(domain_details['name']).to_equal('domain-details.com') expect(domain_details['pageCount']).to_equal(2) expect(domain_details['reviewCount']).to_equal(2) expect(domain_details['violationCount']).to_equal(50) expect(domain_details['reviewPercentage']).to_equal(100.00) expect(domain_details['errorPercentage']).to_equal(0) expect(domain_details['averageResponseTime']).to_equal(0) expect(domain_details['is_active']).to_be_true() expect(domain_details['homepageId']).to_equal(str(page.uuid)) expect(domain_details['homepageReviewId']).to_equal(str(page.last_review_uuid))
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_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_get_violations_per_day(self): dt = datetime(2013, 10, 10, 10, 10, 10) dt2 = datetime(2013, 10, 11, 10, 10, 10) dt3 = datetime(2013, 10, 12, 10, 10, 10) domain = DomainFactory.create() page = PageFactory.create(domain=domain) ReviewFactory.create(domain=domain, page=page, is_active=False, is_complete=True, completed_date=dt, number_of_violations=20) ReviewFactory.create(domain=domain, page=page, is_active=False, is_complete=True, completed_date=dt2, number_of_violations=10) ReviewFactory.create(domain=domain, page=page, is_active=True, is_complete=True, completed_date=dt3, number_of_violations=30) violations = domain.get_violations_per_day(self.db) expect(violations).to_be_like([ { "completedAt": "2013-10-10", "violation_count": 20, "violation_points": 190 }, { "completedAt": "2013-10-11", "violation_count": 10, "violation_points": 45 }, { "completedAt": "2013-10-12", "violation_count": 30, "violation_points": 435 } ])
def test_can_get_domain_details(self): domain = DomainFactory.create(url="http://www.domain-details.com", name="domain-details.com") page = PageFactory.create(domain=domain, url=domain.url) page2 = PageFactory.create(domain=domain) ReviewFactory.create(page=page, is_active=True, is_complete=True, number_of_violations=20) ReviewFactory.create(page=page2, is_active=True, is_complete=True, number_of_violations=30) response = yield self.authenticated_fetch('/domains/%s/' % domain.name) expect(response.code).to_equal(200) domain_details = loads(response.body) expect(domain_details['name']).to_equal('domain-details.com') expect(domain_details['pageCount']).to_equal(2) expect(domain_details['reviewCount']).to_equal(2) expect(domain_details['violationCount']).to_equal(50) expect(domain_details['reviewPercentage']).to_equal(100.00) expect(domain_details['errorPercentage']).to_equal(0) expect(domain_details['averageResponseTime']).to_equal(0) expect(domain_details['is_active']).to_be_true() expect(domain_details['homepageId']).to_equal(str(page.uuid)) expect(domain_details['homepageReviewId']).to_equal( str(page.last_review_uuid))
def test_can_get_page_reviews(self): dt1 = datetime(2010, 11, 12, 13, 14, 15) dt1_timestamp = calendar.timegm(dt1.utctimetuple()) dt2 = datetime(2011, 12, 13, 14, 15, 16) dt2_timestamp = calendar.timegm(dt2.utctimetuple()) domain = DomainFactory.create(url="http://www.domain-details.com", name="domain-details.com") page = PageFactory.create(domain=domain) review1 = ReviewFactory.create(page=page, is_active=False, is_complete=True, completed_date=dt1, number_of_violations=20) review2 = ReviewFactory.create(page=page, is_active=False, is_complete=True, completed_date=dt2, number_of_violations=30) response = yield self.authenticated_fetch( '/page/%s/reviews/' % page.uuid ) expect(response.code).to_equal(200) page_details = loads(response.body) expect(page_details[0]['violationCount']).to_equal(30) expect(page_details[0]['uuid']).to_equal(str(review2.uuid)) expect(page_details[0]['completedAt']).to_equal(dt2_timestamp) expect(page_details[1]['violationCount']).to_equal(20) expect(page_details[1]['uuid']).to_equal(str(review1.uuid)) expect(page_details[1]['completedAt']).to_equal(dt1_timestamp)
def test_can_get_reviews_for_domain(self): dt = datetime(2013, 10, 10, 10, 10, 10) dt2 = datetime(2013, 10, 11, 10, 10, 10) dt3 = datetime(2013, 10, 12, 10, 10, 10) domain = DomainFactory.create() page = PageFactory.create(domain=domain, last_review_date=dt3) ReviewFactory.create(page=page, is_active=False, is_complete=True, completed_date=dt, number_of_violations=20) ReviewFactory.create(page=page, is_active=False, is_complete=True, completed_date=dt2, number_of_violations=10) review = ReviewFactory.create(page=page, is_active=True, is_complete=True, completed_date=dt3, number_of_violations=30) reviews = domain.get_active_reviews(self.db) expect(reviews).to_length(1) expect(reviews[0].last_review_uuid).to_equal(str(review.uuid))
def test_can_get_domain_details(self): self.clean_cache('domain-details.com') domain = DomainFactory.create(url="http://www.domain-details.com", name="domain-details.com") page = PageFactory.create(domain=domain) page2 = PageFactory.create(domain=domain) ReviewFactory.create(page=page, is_active=True, is_complete=True, number_of_violations=20) ReviewFactory.create(page=page2, is_active=True, is_complete=True, number_of_violations=30) response = yield self.http_client.fetch( self.get_url('/domains/%s/' % domain.name) ) expect(response.code).to_equal(200) domain_details = loads(response.body) expect(domain_details['name']).to_equal('domain-details.com') expect(domain_details['pageCount']).to_equal(2) expect(domain_details['reviewCount']).to_equal(2) expect(domain_details['violationCount']).to_equal(50) expect(domain_details['reviewPercentage']).to_equal(100.00) expect(domain_details['statusCodeInfo']).to_equal([]) expect(domain_details['errorPercentage']).to_equal(0) expect(domain_details['averageResponseTime']).to_equal(0) expect(domain_details['is_active']).to_be_true()
def test_can_get_domain_reviews(self): dt = datetime(2010, 11, 12, 13, 14, 15) dt_timestamp = calendar.timegm(dt.utctimetuple()) dt2 = datetime(2011, 12, 13, 14, 15, 16) dt2_timestamp = calendar.timegm(dt2.utctimetuple()) domain = DomainFactory.create(url="http://www.domain-details.com", name="domain-details.com") page = PageFactory.create(domain=domain, last_review_date=dt) page2 = PageFactory.create(domain=domain, last_review_date=dt2) ReviewFactory.create(page=page, is_active=True, is_complete=True, completed_date=dt, number_of_violations=20) ReviewFactory.create(page=page, is_active=False, is_complete=True, completed_date=dt2, number_of_violations=30) ReviewFactory.create(page=page2, is_active=True, is_complete=True, completed_date=dt2, number_of_violations=30) ReviewFactory.create(page=page2, is_active=False, is_complete=True, completed_date=dt, number_of_violations=20) response = yield self.http_client.fetch( self.get_url('/domains/%s/reviews/' % domain.name) ) expect(response.code).to_equal(200) domain_details = loads(response.body) expect(domain_details['pages']).to_length(2) expect(domain_details['pages'][1]['url']).to_equal(page2.url) expect(domain_details['pages'][1]['uuid']).to_equal(str(page2.uuid)) expect(domain_details['pages'][1]['completedAt']).to_equal(dt2_timestamp) expect(domain_details['pages'][0]['url']).to_equal(page.url) expect(domain_details['pages'][0]['uuid']).to_equal(str(page.uuid)) expect(domain_details['pages'][0]['completedAt']).to_equal(dt_timestamp)
def test_can_get_response_time_avg(self): self.db.query(Request).delete() domain = DomainFactory.create() avg = domain.get_response_time_avg(self.db) expect(avg).to_be_like(0) RequestFactory.create(status_code=200, domain_name=domain.name, response_time=0.25) RequestFactory.create(status_code=304, domain_name=domain.name, response_time=0.35) avg = domain.get_response_time_avg(self.db) expect(avg).to_be_like(0.3) RequestFactory.create(status_code=400, domain_name=domain.name, response_time=0.25) RequestFactory.create(status_code=403, domain_name=domain.name, response_time=0.35) RequestFactory.create(status_code=404, domain_name=domain.name, response_time=0.25) avg = domain.get_response_time_avg(self.db) expect(avg).to_be_like(0.3)
def test_can_get_domains_full_data(self): domains = [] for i in range(3): domains.append(DomainFactory.create(name='domain-%d.com' % i)) pages = [] for i, domain in enumerate(domains): pages.append([]) for j in range(3): pages[i].append(PageFactory.create(domain=domain)) requests = reviews = [] for i, (domain, page) in enumerate(zip(domains, pages)): for j in range(i + 1): reviews.append( ReviewFactory.create(domain=domain, page=page[j], is_active=True, number_of_violations=(5 + 2 * j))) requests.append( RequestFactory.create(status_code=200 if j % 2 == 0 else 404, domain_name=domain.name, response_time=0.25 * (i + 1))) self.server.application.violation_definitions = { 'key.%s' % i: { 'title': 'title.%s' % i, 'category': 'category.%s' % (i % 3), 'key': Key.get_or_create(self.db, 'key.%d' % i, 'category.%d' % (i % 3)) } for i in range(9) } response = yield self.authenticated_fetch('/domains-details') expect(response.code).to_equal(200) full_data = loads(response.body) expect(full_data).to_length(3) expect(full_data[0].keys()).to_length(10) expect(map(lambda d: d['name'], full_data)).to_be_like( ['domain-0.com', 'domain-1.com', 'domain-2.com']) expect(map(lambda d: d['pageCount'], full_data)).to_be_like([3, 3, 3]) expect(map(lambda d: d['reviewCount'], full_data)).to_be_like([1, 2, 3]) expect(map(lambda d: d['violationCount'], full_data)).to_be_like([5, 12, 21]) expect(map(lambda d: d['reviewPercentage'], full_data)).to_be_like([33.33, 66.67, 100.0]) expect(map(lambda d: d['errorPercentage'], full_data)).to_be_like([0.0, 50.0, 33.33]) expect(map(lambda d: d['averageResponseTime'], full_data)).to_be_like([0.25, 0.5, 0.75])
def test_domain_not_found_in_materials(self): DomainFactory.create(name='domain-0.com') self.server.application.girl.get = Mock(return_value=[]) try: yield self.authenticated_fetch('/domains/domain-0.com') except HTTPError: err = sys.exc_info()[1] expect(err).not_to_be_null() expect(err.code).to_equal(404) expect(err.response.reason).to_be_like( 'Domain domain-0.com not found') else: assert False, 'Should not have got this far'
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_pages_per_domain(self): domain = DomainFactory.create() domain2 = DomainFactory.create() DomainFactory.create() PageFactory.create(domain=domain) PageFactory.create(domain=domain) PageFactory.create(domain=domain2) PageFactory.create(domain=domain2) PageFactory.create(domain=domain2) pages_per_domain = Domain.get_pages_per_domain(self.db) expect(pages_per_domain).to_be_like({ domain.id: 2, domain2.id: 3 })
def test_can_get_all_limiters(self): self.db.query(Limiter).delete() limiter = LimiterFactory.create(url='http://test.com/') LimiterFactory.create() LimiterFactory.create() limiters = Limiter.get_all(self.db) expect(limiters).not_to_be_null() expect(limiters).to_length(3) expect(limiters).to_include(limiter) DomainFactory.create(name='test.com', url='http://test.com') limiters = Limiter.get_all(self.db, domain_filter='test.com') expect(limiters).not_to_be_null() expect(limiters).to_length(1)
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_domains_info(self): DomainFactory.create(url="http://globo.com", name="globo.com") DomainFactory.create(url="http://g1.globo.com", name="g1.globo.com") response = yield self.authenticated_fetch('/domains') expect(response.code).to_equal(200) domains = loads(response.body) expect(domains).to_length(2) expect(domains[0]['name']).to_equal("g1.globo.com") expect(domains[0]['url']).to_equal("http://g1.globo.com") expect(domains[0]['is_active']).to_be_true() expect(domains[1]['name']).to_equal("globo.com") expect(domains[1]['url']).to_equal("http://globo.com") expect(domains[1]['is_active']).to_be_true()
def test_can_get_next_job_when_domain_limited(self): self.db.query(Domain).delete() self.db.query(Page).delete() domain_a = DomainFactory.create() domain_b = DomainFactory.create() LimiterFactory.create(url=domain_a.url, value=2) pages_a = [] pages_b = [] workers = [] for i in range(10): for j in range(2): workers.append(WorkerFactory.create()) pages_a.append(PageFactory.create(domain=domain_a, url="%s/%d.html" % (domain_a.url, i), score=i * 10)) pages_b.append(PageFactory.create(domain=domain_b, url="%s/%d.html" % (domain_b.url, i), score=i)) # first one should not be limited next_job = Page.get_next_job( self.db, expiration=100, cache=self.sync_cache, lock_expiration=1, avg_links_per_page=10 ) expect(next_job).not_to_be_null() expect(next_job['page']).to_equal(str(pages_a[-1].uuid)) workers[0].current_url = next_job['url'] self.db.flush() # second one should be limited (2 / 10 = 0.2, rounded up = 1 job at a time) next_job = Page.get_next_job( self.db, expiration=100, cache=self.sync_cache, lock_expiration=1 ) expect(next_job).not_to_be_null() expect(next_job['page']).to_equal(str(pages_b[-1].uuid))
def test_can_set_domain_to_inactive(self): domain = DomainFactory.create(url="http://www.domain.com", name="domain.com", is_active=True) response = yield self.authenticated_fetch( '/domains/%s/change-status/' % domain.name, method='POST', body='') expect(response.code).to_equal(200) domain_from_db = Domain.get_domain_by_name(domain.name, self.db) expect(domain_from_db.is_active).to_be_false()
def test_get_limiters_for_domains(self): self.db.query(Limiter).delete() self.db.query(Domain).delete() for i in range(2): DomainFactory.create() value = 2 url = 'http://globo.com/' Limiter.add_or_update_limiter(self.db, url, value) DomainFactory.create(url=url, name='globo.com') active_domains = self.db.query(Domain).all() domains = Limiter.get_limiters_for_domains(self.db, active_domains) expect(domains).to_length(1) expect(str(domains[0])).to_equal(url)
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([])