def test_post_job_as_owner(self): """ Ensures that a company user of the site owner can post jobs for free and verify that they have been posted. PD-1463 Task - posting a job and verifying it is live (as site owner) - listing jobs """ with patch_settings(**self.OVERRIDES): num_jobs = Job.objects.count() self.login(self.admin) self.get(reverse('job_add')) self.make_job(for_admin=True) self.assertEqual(Job.objects.count(), num_jobs + 1) # If the previous bit was successfully added to solr, the following # page will have a job matching its description. self.get(reverse('all_jobs')) # We could easily .click() this but that would not properly append # the domain override. job_link = self.browser.find_element_by_link_text( self.job['id_title']).get_attribute('href') job = Job.objects.get(title=self.job['id_title'], description=self.job['id_description']) location = job.locations.get() self.TEST_OBJECTS.extend([location, job]) self.assertTrue(location.guid in job_link) self.get(job_link) element = self.browser.find_element_by_id( 'direct_jobDescriptionText') self.assertEqual(element.text, job.description) self.logout() # Trying this with a normal user fails. self.login(self.user) self.get(reverse('job_add')) with self.assertRaises(NoSuchElementException): self.browser.find_element_by_id( 'id_site_packages_add_link') self.logout() # Trying this instead with another company user is successful. # Due to the way one of the decorators works, this grabs the user's # company. Posting will not post to the current site, but to a site # determined by that company. Fixing this is outside the scope of # writing Selenium tests. self.login(self.admin_2) self.get(reverse('job_add')) with self.assertRaises(NoSuchElementException): self.browser.find_element_by_xpath( '//option[@value={site_pk}]'.format( site_pk=self.seo_site.pk)) self.browser.find_element_by_xpath( '//option[@value={site_pk}]'.format( site_pk=self.seo_site_2.pk))
def test_clear_cache_on_bu_save(self): with patch_settings(CACHES={"default": {"BACKEND": "django.core.cache.backends.locmem.LocMemCache"}}): site = factories.SeoSiteFactory() site.business_units.add(self.businessunit) config = factories.ConfigurationFactory(status=2, home_page_template="home_page/home_page_listing.html") config.save() site.configurations = [config] resp = self.client.get("/", HTTP_HOST=u"buckconsultants.jobs", follow=True) initial_jobs = resp.context["default_jobs"] self.assertEqual(resp.status_code, 200) self.assertGreater(len(initial_jobs), 0) self.assertEqual(len(initial_jobs), len(self.solr_docs)) for job in initial_jobs: self.assertContains(resp, job.guid) self.conn.delete(id=self.solr_docs[0]["id"]) resp = self.client.get("/", HTTP_HOST=u"buckconsultants.jobs") self.assertEqual(resp.status_code, 200) self.assertEqual(resp.context, None) # Deleted job should still be on cached page self.assertEqual(len(initial_jobs), len(self.solr_docs)) for job in initial_jobs: self.assertContains(resp, job.guid) # Clear cache to reflect removed job task_clear_bu_cache(self.businessunit.id) resp = self.client.get("/", HTTP_HOST=u"buckconsultants.jobs") new_jobs = resp.context["default_jobs"] self.assertEqual(len(initial_jobs) - 1, len(new_jobs)) solr_jobs = self.conn.search("*:*") self.assertEqual(len(solr_jobs), len(new_jobs))
def test_show_job_admin(self): """ Ensures that the main postajob admin is functional. A company user for the site owner should be able to see all options. A company user for a third party or a non-company-user should not. PD-1463 Task - jobs admin display """ with patch_settings(**self.OVERRIDES): for user, accessible in [(self.admin, True), (self.admin_2, False), (self.user, False)]: self.login(user) self.get(reverse('purchasedmicrosite_admin_overview')) for selector, expected in [ ('product-listing', 'Product Listing'), ('our-postings', 'Posted Jobs'), ('posting-admin', 'Partner Microsite')]: try: element = self.browser.find_element_by_id(selector) except NoSuchElementException: # If the user is not a company user for the owner, this # is expected; if not, we should reraise and fail. if accessible: raise else: self.assertEqual(element.text, expected) self.logout()
def tearDownClass(cls): """ Deletes all objects created during setup. """ cls.browser.quit() with patch_settings(**cls.OVERRIDES): cls.remove_objects() super(JobPostingTests, cls).tearDownClass()
def test_expire_site_on_config_save(self): """ Cache pages and site-related objects should be cleared when its config is saved """ with patch_settings( CACHES={ 'default': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache' } }): # Build a site with billboard images to check the billboard's # caching behavior as well site = factories.SeoSiteFactory.build(domain='oranges.jobs') site.save() config = factories.ConfigurationFactory.build( status=2, home_page_template= 'home_page/home_page_static_header_footer.html') config.meta = 'initial meta' config.save() site.configurations = [config] response = self.client.get('/', HTTP_HOST=u'oranges.jobs') self.assertContains(response, config.meta) # Update config without calling save() config.meta = 'new meta' models.Configuration.objects.filter(id=config.id).update( meta=config.meta) response = self.client.get('/', HTTP_HOST=u'oranges.jobs') # Old config should still be cached self.assertContains(response, 'initial meta') self.assertNotContains(response, config.meta) config.save() response = self.client.get('/', HTTP_HOST=u'oranges.jobs') config.save() self.assertContains(response, config.meta) self.assertNotContains(response, 'initial meta')
def test_clear_cache_on_bu_save(self): with patch_settings( CACHES={ 'default': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache' } }): site = factories.SeoSiteFactory() site.business_units.add(self.businessunit) config = factories.ConfigurationFactory( status=2, home_page_template='home_page/home_page_listing.html') config.save() site.configurations = [config] resp = self.client.get('/', HTTP_HOST=u'buckconsultants.jobs', follow=True) initial_jobs = resp.context['default_jobs'] self.assertEqual(resp.status_code, 200) self.assertGreater(len(initial_jobs), 0) self.assertEqual(len(initial_jobs), len(self.solr_docs)) for job in initial_jobs: self.assertContains(resp, job.guid) self.conn.delete(id=self.solr_docs[0]['id']) resp = self.client.get('/', HTTP_HOST=u'buckconsultants.jobs') self.assertEqual(resp.status_code, 200) self.assertEqual(resp.context, None) # Deleted job should still be on cached page self.assertEqual(len(initial_jobs), len(self.solr_docs)) for job in initial_jobs: self.assertContains(resp, job.guid) # Clear cache to reflect removed job task_clear_bu_cache(self.businessunit.id) resp = self.client.get('/', HTTP_HOST=u'buckconsultants.jobs') new_jobs = resp.context['default_jobs'] self.assertEqual(len(initial_jobs) - 1, len(new_jobs)) solr_jobs = self.conn.search('*:*') self.assertEqual(len(solr_jobs), len(new_jobs))
def setUpClass(cls): """ Sets up the test environment, overriding settings and modifying the db. """ environment = os.environ.get('SETTINGS', '').lower() if environment == 'qc': print 'Running test_posting with QC settings' cls.test_url = 'qc.www.my.jobs' qc = imp.load_source('settings.myjobs_qc', 'deploy/settings.myjobs_qc.py') cls.OVERRIDES = vars(qc) elif environment == 'staging': print 'Running test_posting with staging settings' cls.test_url = 'staging.www.my.jobs' staging = imp.load_source('settings.myjobs_staging', 'deploy/settings.myjobs_staging.py') cls.OVERRIDES = vars(staging) else: production = imp.load_source('settings.myjobs_prod', 'deploy/settings.myjobs_prod.py') assert (settings.DATABASES['default']['HOST'] != production.DATABASES['default']['HOST']), \ 'Running test_posting with production settings is unsupported' print 'Running test_posting with settings.py' # Assuming local; I have to pick a port and runserver defaults to # 8000, so... cls.test_port = ':8000' cls.browser = webdriver.PhantomJS() super(JobPostingTests, cls).setUpClass() with patch_settings(**cls.OVERRIDES): try: cls.setup_objects() except: # If anything happens during setup (someone cancels the # process, db issues, whatever), we need to roll back. Delete # everything we created and reraise the exception. cls.remove_objects() raise
def test_expire_site_on_config_save(self): """ Cache pages and site-related objects should be cleared when its config is saved """ with patch_settings(CACHES = {'default': {'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'}}): # Build a site with billboard images to check the billboard's # caching behavior as well site = factories.SeoSiteFactory.build(domain='oranges.jobs') site.save() config = factories.ConfigurationFactory.build(status=2, home_page_template='home_page/home_page_static_header_footer.html') config.meta = 'initial meta' config.save() site.configurations = [config] response = self.client.get('/', HTTP_HOST=u'oranges.jobs') self.assertContains(response, config.meta) # Update config without calling save() config.meta = 'new meta' models.Configuration.objects.filter(id=config.id).update( meta=config.meta) response = self.client.get('/', HTTP_HOST=u'oranges.jobs') # Old config should still be cached self.assertContains(response, 'initial meta') self.assertNotContains(response, config.meta) config.save() response = self.client.get('/', HTTP_HOST=u'oranges.jobs') config.save() self.assertContains(response, config.meta) self.assertNotContains(response, 'initial meta')
def test_expire_site_on_save(self): """ Cache pages and site-related objects should be cleared when an SeoSite is saved. """ with patch_settings( CACHES={ 'default': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache' } }): # Build a site with billboard images to check the billboard's # caching behavior as well site = factories.SeoSiteFactory.build(site_title='Initial Title') site.save() config = factories.ConfigurationFactory.build( status=2, show_home_microsite_carousel=True) config.save() site.configurations = [config] billboard = factories.BillboardImageFactory.build( image_url="http://initial_url") billboard.save() site.billboard_images.add(billboard) response = self.client.get('/', HTTP_HOST=u'buckconsultants.jobs') self.assertNotEqual(response.context['default_jobs'], 0) self.assertNotEqual(response.content.find('Initial Title'), -1) self.assertNotEqual(response.content.find(billboard.image_url), -1) # Make changes to the site instance and database without calling save() models.SeoSite.objects.filter(id=site.id).update( site_title='Updated Title') site.site_title = 'Updated Title' site.configurations.all().update(header='Unique Header') billboard.image_url = 'http://updated_url' site.billboard_images.update(image_url=billboard.image_url) # This response should still be cached and not reflect the site changes response = self.client.get('/', HTTP_HOST=u'buckconsultants.jobs') # Cached pages don't return a context self.assertEqual(response.context, None) self.assertNotEqual(response.content.find('Initial Title'), -1) self.assertEqual(response.content.find('Updated Title'), -1) self.assertEqual(response.content.find('Unique Header'), -1) self.assertNotEqual(response.content.find('http://initial_url'), -1) self.assertEqual(response.content.find('http://updated_url'), -1) billboard.save() site.save() response = self.client.get('/?q=foo', HTTP_HOST=u'buckconsultants.jobs') # This page isn't cached, confirm that our model caching has cleared # on site save self.assertNotEqual(response.content.find('Updated Title'), -1) self.assertNotEqual(response.content.find(billboard.image_url), -1) self.assertNotEqual(response.content.find('Unique Header'), -1) response = self.client.get('/', HTTP_HOST=u'buckconsultants.jobs') # The cache is cleared and the home_page should be updated self.assertNotEqual(response.context['default_jobs'], 0) self.assertNotEqual(response.content.find('Updated Title'), -1) self.assertNotEqual(response.content.find(billboard.image_url), -1) self.assertNotEqual(response.content.find('Unique Header'), -1) no_job_buid = factories.BusinessUnitFactory.build(id=562) no_job_buid.save() site.business_units = [no_job_buid] site.save() # One buid with no jobs attached to the site response = self.client.get('/', HTTP_HOST=u'buckconsultants.jobs') # Should be no jobs returned self.assertNotEqual(response.context, None) self.assertEqual(len(response.context['default_jobs']), 0) site.business_units.add(self.businessunit) site.save() # Cache should be refreshed and this buid should return jobs response = self.client.get('/', HTTP_HOST=u'buckconsultants.jobs') self.assertNotEqual(response.context, None) self.assertTrue(len(response.context['default_jobs']) > 0) # Saving a configuration should clear the cache site.configurations.all().update(header='Yet Another Header') site.configurations.all()[0].save() response = self.client.get('/', HTTP_HOST=u'buckconsultants.jobs') self.assertNotEqual(response.context, None) self.assertTrue(len(response.context['default_jobs']) > 0) self.assertNotEqual(response.content.find('Yet Another Header'), -1)
def tearDown(self): self.logout() with patch_settings(**self.OVERRIDES): for obj in self.TEST_OBJECTS[::-1]: obj.delete() super(JobPostingTests, self).tearDown()
def test_expire_site_on_save(self): """ Cache pages and site-related objects should be cleared when an SeoSite is saved. """ with patch_settings(CACHES = {'default': {'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'}}): # Build a site with billboard images to check the billboard's # caching behavior as well site = factories.SeoSiteFactory.build(site_title='Initial Title') site.save() config = factories.ConfigurationFactory.build(status=2, show_home_microsite_carousel=True) config.save() site.configurations = [config] billboard = factories.BillboardImageFactory.build(image_url="http://initial_url") billboard.save() site.billboard_images.add(billboard) response = self.client.get('/', HTTP_HOST=u'buckconsultants.jobs') self.assertNotEqual(response.context['default_jobs'], 0) self.assertNotEqual(response.content.find('Initial Title'), -1) self.assertNotEqual(response.content.find(billboard.image_url), -1) # Make changes to the site instance and database without calling save() models.SeoSite.objects.filter(id=site.id).update(site_title='Updated Title') site.site_title = 'Updated Title' site.configurations.all().update(header= 'Unique Header') billboard.image_url = 'http://updated_url' site.billboard_images.update(image_url=billboard.image_url) # This response should still be cached and not reflect the site changes response = self.client.get('/', HTTP_HOST=u'buckconsultants.jobs') # Cached pages don't return a context self.assertEqual(response.context, None) self.assertNotEqual(response.content.find('Initial Title'), -1) self.assertEqual(response.content.find('Updated Title'), -1) self.assertEqual(response.content.find('Unique Header'), -1) self.assertNotEqual(response.content.find('http://initial_url'), -1) self.assertEqual(response.content.find('http://updated_url'), -1) billboard.save() site.save() response = self.client.get('/?q=foo', HTTP_HOST=u'buckconsultants.jobs') # This page isn't cached, confirm that our model caching has cleared # on site save self.assertNotEqual(response.content.find('Updated Title'), -1) self.assertNotEqual(response.content.find(billboard.image_url), -1) self.assertNotEqual(response.content.find('Unique Header'), -1) response = self.client.get('/', HTTP_HOST=u'buckconsultants.jobs') # The cache is cleared and the home_page should be updated self.assertNotEqual(response.context['default_jobs'], 0) self.assertNotEqual(response.content.find('Updated Title'), -1) self.assertNotEqual(response.content.find(billboard.image_url), -1) self.assertNotEqual(response.content.find('Unique Header'), -1) no_job_buid = factories.BusinessUnitFactory.build(id=562) no_job_buid.save() site.business_units = [no_job_buid] site.save() # One buid with no jobs attached to the site response = self.client.get('/', HTTP_HOST=u'buckconsultants.jobs') # Should be no jobs returned self.assertNotEqual(response.context, None) self.assertEqual(len(response.context['default_jobs']), 0) site.business_units.add(self.businessunit) site.save() # Cache should be refreshed and this buid should return jobs response = self.client.get('/', HTTP_HOST=u'buckconsultants.jobs') self.assertNotEqual(response.context, None) self.assertTrue(len(response.context['default_jobs']) > 0) # Saving a configuration should clear the cache site.configurations.all().update(header= 'Yet Another Header') site.configurations.all()[0].save() response = self.client.get('/', HTTP_HOST=u'buckconsultants.jobs') self.assertNotEqual(response.context, None) self.assertTrue(len(response.context['default_jobs']) > 0) self.assertNotEqual(response.content.find('Yet Another Header'), -1)