def test_site_package_make_unique_for_company(self): package = SitePackage.objects.create(**self.site_package_data) package.make_unique_for_company(self.company) self.assertEqual(self.company.site_package, package) package.delete() package = SitePackage.objects.create(**self.site_package_data) first = [] for x in range(1000, 1003): site = SeoSiteFactory(id=x, domain="%s.jobs" % x) package.sites.add(site) first.append(site.pk) self.assertItemsEqual(package.sites.all().values_list('id', flat=True), first) second = [self.site.pk] for x in range(100, 103): site = SeoSiteFactory(id=x, domain="%s.jobs" % x) site.business_units.add(self.bu) site.save() second.append(site.pk) # Site packages with existing sites associated with it should # only end up with the sites for a company. package.make_unique_for_company(self.company) self.assertEqual(self.company.site_package, package) self.assertItemsEqual(package.sites.all().values_list('id', flat=True), second)
def test_new_buid(self): site = SeoSiteFactory() site.business_units.add(self.existing_bu) site.save() url = '/ajax/update_buid/?key=%s&old_buid=%s&new_buid=%s' % ( self.key, self.existing_bu.id, 105) resp = self.client.get(url) self.assertEqual('{"new_bu": "105", "sites": "buckconsultants.jobs"}', resp.content) buid = BusinessUnit.objects.get(pk=105) self.assertTrue(buid.enable_markdown)
def test_new_buid(self): site = SeoSiteFactory() site.business_units.add(self.existing_bu) site.save() url = '/ajax/update_buid/?key=%s&old_buid=%s&new_buid=%s' % (self.key, self.existing_bu.id, 105) resp = self.client.get(url) self.assertEqual('{"new_bu": "105", "sites": "buckconsultants.jobs"}', resp.content) buid = BusinessUnit.objects.get(pk=105) self.assertTrue(buid.enable_markdown)
def test_page_all_blocks(self): blocks = [] [blocks.append(factories.ContentBlockFactory()) for x in range(0, 5)] row = factories.RowFactory() [models.BlockOrder.objects.create(row=row, block=block, order=block.id) for block in blocks] [blocks.append(factories.LoginBlockFactory()) for x in range(0, 5)] row2 = factories.RowFactory() [models.BlockOrder.objects.create(row=row2, block=block, order=block.id) for block in blocks] page = factories.PageFactory(sites=(SeoSiteFactory(), )) models.RowOrder.objects.create(page=page, row=row, order=row.id) models.RowOrder.objects.create(page=page, row=row2, order=row2.id) all_blocks = page.all_blocks() all_blocks_ids = [block.id for block in all_blocks] block_ids = [block.id for block in blocks] self.assertItemsEqual(block_ids, all_blocks_ids)
def setUp(self): super(BreadboxTests, self).setUp() self.site = SeoSiteFactory() settings.SITE = self.site settings.SITE_ID = self.site.pk settings.STANDARD_FACET = [] for x in range(1, 4): facet = CustomFacetFactory(name_slug='custom-facet-%s' % x, name="Custom Facet %s" % x, always_show=True, show_production=1) SeoSiteFacetFactory(customfacet=facet, seosite=self.site) settings.STANDARD_FACET.append(facet) kwargs = {'seositefacet__seosite': self.site} self.custom_facets = CustomFacet.objects.filter(**kwargs) self.filters = { 'title_slug': None, 'location_slug': None, 'moc_slug': None, 'facet_slug': None, 'company_slug': None, }
def test_wildcard_redirect(self): """ Test the wildcard redirect. This makes a call to a url with an invalid subdomain, which should result in a 301 redirect to the root domain. """ site = SeoSiteFactory.build(domain='www.my.jobs',name=u'www.my.jobs') site.save() settings.WILDCARD_REDIRECT = True # check that the redirect ignores domains without a subdomain resp = self.client.get('/', HTTP_HOST='domain.jobs') self.assertEqual(resp.status_code, 200) # check that IP addresses are NOT redirected resp = self.client.get('/', HTTP_HOST='127.0.0.1') self.assertEqual(resp.status_code, 200) # check that the redirect ignores domains in the exception list resp = self.client.get( '/', HTTP_HOST='ec2-50-16-227-119.compute-1.amazonaws.com' ) self.assertEqual(resp.status_code, 200) # check that it redirects when it supposed to resp = self.client.get('/', HTTP_HOST='fake.usa.jobs', follow=True) self.assertEqual(resp.status_code, 301) WildcardMiddlewareTestCase._check_external_redirect(self, resp,"http://usa.jobs") # check that is respects the setting toggle settings.WILDCARD_REDIRECT = False resp = self.client.get('/', HTTP_HOST='wrong.www.my.jobs') self.assertEqual(resp.status_code, 200)
def test_product_filter_by_site_multiple_sites(self): site_in_both_packages = SeoSiteFactory(domain='secondsite.jobs', id=7) single_site_package = SitePackageFactory(owner=self.company) single_site_package.make_unique_for_site(site_in_both_packages) both_sites_package = SitePackageFactory(owner=self.company) both_sites_package.sites.add(site_in_both_packages) both_sites_package.sites.add(self.site) both_sites_package.save() ProductFactory(package=single_site_package, owner=self.company) ProductFactory(package=both_sites_package, owner=self.company) self.assertEqual(Product.objects.all().count(), 2) # Confirm that filtering by both sites gets both jobs. both_sites = [site_in_both_packages, self.site] count = Product.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) # Confirm that filtering by the site that only has one job only # gets one job. count = Product.objects.filter_by_sites([self.site]).count() self.assertEqual(count, 1) # Confirm that filtering by the site the site that has both jobs gets # both jobs. count = (Product.objects.filter_by_sites([site_in_both_packages ]).count()) self.assertEqual(count, 2)
def test_invoice_filter_by_sites(self): role = RoleFactory(company=self.company) self.user.roles.add(role) for x in range(8800, 8815): domain = 'testsite-%s.jobs' % x site = SeoSiteFactory(id=x, domain=domain, name=domain) site_package = SitePackageFactory(owner=self.company) site_package.make_unique_for_site(site) product = ProductFactory(package=site_package, owner=self.company) for y in range(1, 5): # OfflinePurchaseFactory() automatically creates the invoice. purchase = OfflinePurchaseFactory(owner=self.company, created_by=self.user) OfflineProduct.objects.create(product=product, offline_purchase=purchase) # Confirm it correctly picks up Invoices associated with # OfflinePurchases. self.assertEqual( Invoice.objects.filter_by_sites([site]).count(), 4) for y in range(1, 5): # PurchasedProductFactory() also automatically creates # the invoice. PurchasedProductFactory(product=product, owner=self.company) # Confirm it correctly picks up Invoices associated with # PurchasedProducts. self.assertEqual( Invoice.objects.filter_by_sites([site]).count(), 8) self.assertEqual(Invoice.objects.all().count(), 120)
def test_site_package_make_unique_for_site(self): package = SitePackage.objects.create(**self.site_package_data) package.make_unique_for_site(self.site) self.assertEqual(self.site.site_package, package) package.delete() package = SitePackage.objects.create(**self.site_package_data) for x in range(100, 110): site = SeoSiteFactory(id=x, domain="%s.jobs" % x) package.sites.add(site) # Site packages with existing sites associated with it should still # only end up with one associated site. site = SeoSiteFactory(id=4000, domain="4000.jobs") package.make_unique_for_site(site) self.assertEqual(site.site_package, package)
def test_wildcard_redirect(self): """ Test the wildcard redirect. This makes a call to a url with an invalid subdomain, which should result in a 301 redirect to the root domain. """ site = SeoSiteFactory.build(domain='www.my.jobs', name=u'www.my.jobs') site.save() settings.WILDCARD_REDIRECT = True # check that the redirect ignores domains without a subdomain resp = self.client.get('/', HTTP_HOST='domain.jobs') self.assertEqual(resp.status_code, 200) # check that IP addresses are NOT redirected resp = self.client.get('/', HTTP_HOST='127.0.0.1') self.assertEqual(resp.status_code, 200) # check that the redirect ignores domains in the exception list resp = self.client.get( '/', HTTP_HOST='ec2-50-16-227-119.compute-1.amazonaws.com' ) self.assertEqual(resp.status_code, 200) # check that it redirects when it supposed to resp = self.client.get('/', HTTP_HOST='fake.usa.jobs', follow=True) self.assertEqual(resp.status_code, 301) self.assertRedirects(resp, 'http://usa.jobs', status_code=301, target_status_code=301) # check that is respects the setting toggle settings.WILDCARD_REDIRECT = False resp = self.client.get('/', HTTP_HOST='wrong.www.my.jobs') self.assertEqual(resp.status_code, 200)
def setUp(self): super(TestSecureBlocks, self).setUp() self.staff_user = UserFactory(is_staff=True) self.client = TestClient() self.client.login_user(self.staff_user) self.sb_url = reverse('secure_blocks') SeoSiteFactory(domain='jobs.example.com')
def setUp(self): super(MultiHostMiddlewareTestCase, self).setUp() self.bu = BusinessUnit(id=42, date_crawled=datetime.datetime.now(), date_updated=datetime.datetime.now()) self.test_site = SeoSiteFactory(domain=u'buckconsultants.jobs', name=u'buckconsultants')
def test_job_filter_by_site_multiple_sites(self): site_in_both_packages = SeoSiteFactory(domain='secondsite.jobs', id=7) single_site_package = SitePackageFactory(owner=self.company) single_site_package.make_unique_for_site(site_in_both_packages) both_sites_package = SitePackageFactory(owner=self.company) both_sites_package.sites.add(site_in_both_packages) both_sites_package.sites.add(self.site) both_sites_package.save() job_on_both = JobFactory(owner=self.company, created_by=self.user) job_on_both.site_packages.add(both_sites_package) job_on_both.save() job_on_new_site = JobFactory(owner=self.company, created_by=self.user) job_on_new_site.site_packages.add(single_site_package) job_on_new_site.save() self.assertEqual(Job.objects.all().count(), 2) # Confirm that filtering by both sites gets both jobs. both_sites = [site_in_both_packages, self.site] count = Job.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) # Confirm that filtering by the site that only has one job only # gets one job. self.assertEqual(Job.objects.filter_by_sites([self.site]).count(), 1) # Confirm that filtering by the site the site that has both jobs gets # both jobs. count = Job.objects.filter_by_sites([site_in_both_packages]).count() self.assertEqual(count, 2)
def test_feed_on_protected_site_no_access(self): from seo.tests.factories import SeoSiteFactory site_id = settings.PROTECTED_SITES.keys()[0] site = SeoSiteFactory(pk=site_id, id=site_id) url = "http://%s?q=query" % site.domain result = update_url_if_protected(url, self.user) self.assertEqual(result, url)
def setUp(self): super(SiteRedirectMiddlewareTestCase, self).setUp() self.test_site = SeoSiteFactory.build() self.test_site.save() self.ssr1 = SeoSiteRedirectFactory.build(seosite=self.test_site) self.ssr1.save() self.ssr2 = SeoSiteRedirectFactory.build(redirect_url=u'test.%s' % self.test_site.domain, seosite=self.test_site) self.ssr2.save()
def setUp(self): super(SiteRedirectMiddlewareTestCase, self).setUp() self.test_site = SeoSiteFactory.build() self.test_site.save() self.ssr1 = SeoSiteRedirectFactory.build(seosite=self.test_site) self.ssr1.save() self.ssr2 = SeoSiteRedirectFactory.build( redirect_url=u'test.%s' % self.test_site.domain, seosite=self.test_site) self.ssr2.save()
def setUp(self): super(RedirectOverrideMiddlewareTestCase, self).setUp() self.site = SeoSite.objects.get() self.other_site = SeoSiteFactory(domain=u'buckconsultants.jobs', name=u'buckconsultants') self.redirect = QueryRedirect.objects.create( site=self.site, old_path='/jobs/', new_path='https://www.google.com')
def setUp(self): super(DirectSeoTCWithSiteAndConfig, self).setUp() self.site = SeoSiteFactory() self.site.business_units.add(self.businessunit) self.config = ConfigurationFactory.build(status=2) self.config.save() self.site.configurations.add(self.config) # ensure tests in this class use the correct domain self.client = Client(HTTP_HOST=self.site.domain)
def test_product_filter_by_sites(self): for x in range(8800, 8815): domain = 'testsite-%s.jobs' % x site = SeoSiteFactory(id=x, domain=domain, name=domain) site_package = SitePackageFactory(owner=self.company) site_package.make_unique_for_site(site) for y in range(1, 5): ProductFactory(package=site_package, owner=self.company) self.assertEqual( Product.objects.filter_by_sites([site]).count(), 4) self.assertEqual(Product.objects.all().count(), 60)
def setUp(self): super(MyJobsAdminTests, self).setUp() self.user.set_password('5UuYquA@') self.user.is_superuser = True self.user.save() self.account_owner = UserFactory(email='*****@*****.**') SeoSiteFactory(domain='secure.my.jobs') mail.outbox = [] self.data = { '_selected_action': [unicode(self.account_owner.pk)], 'action': 'request_account_access' }
def test_job_filter_by_sites(self): for x in range(8800, 8815): domain = 'testsite-%s.jobs' % x site = SeoSiteFactory(id=x, domain=domain, name=domain) site_package = SitePackageFactory(owner=self.company) site_package.make_unique_for_site(site) for y in range(1, 5): job = JobFactory(owner=self.company, created_by=self.user) job.site_packages.add(site_package) job.save() self.assertEqual(Job.objects.filter_by_sites([site]).count(), 4) self.assertEqual(Job.objects.all().count(), 60)
def test_feed_on_protected_site_with_access(self): from seo.tests.factories import SeoSiteFactory site_id = settings.PROTECTED_SITES.keys()[0] site = SeoSiteFactory(pk=site_id, id=site_id) group_id = settings.PROTECTED_SITES.values()[0][0] Group.objects.create(pk=group_id, name='Test Group') self.user.groups.add(group_id) self.user.save() url = "http://%s?q=query" % site.domain expected_result = "%s&key=%s" % (url, settings.SEARCH_API_KEY) result = update_url_if_protected(url, self.user) self.assertEqual(result, expected_result)
def setUp(self): super(ModelTests, self).setUp() self.user = User.objects.create(email='*****@*****.**') self.company = CompanyFactory() CompanyProfile.objects.create(company=self.company) self.site = SeoSiteFactory() #domain='buckconsoltants.jobs') self.bu = BusinessUnitFactory() self.site.business_units.add(self.bu) self.site.save() self.company.job_source_ids.add(self.bu) self.company.save() # Use the newly created site for testing instead of secure.my.jobs. settings.SITE = self.site self.request_data = { 'title': 'title', 'company': self.company.id, 'reqid': '1', 'description': 'sadfljasdfljasdflasdfj', 'link': 'www.google.com', 'on_sites': '0', 'apply_info': '', } self.request_location = { 'city': 'Indianapolis', 'state': 'Indiana', 'state_short': 'IN', 'country': 'United States', 'country_short': 'USA', 'zipcode': '46268', } self.site_package_data = { 'name': 'Test Site Package', }
def test_sitepackage_filter_by_sites(self): for x in range(8800, 8815): domain = 'testsite-%s.jobs' % x site = SeoSiteFactory(id=x, domain=domain, name=domain) for y in range(1, 5): site_package = SitePackageFactory(owner=self.company) site_package.sites.add(site) site_package.save() count = SitePackage.objects.filter_by_sites([site]).count() self.assertEqual(count, 4) count = Package.objects.filter_by_sites([site]).count() self.assertEqual(count, 4) self.assertEqual(SitePackage.objects.all().count(), 60) self.assertEqual(Package.objects.all().count(), 60)
def test_productgrouping_filter_by_sites(self): for x in range(8800, 8815): domain = 'testsite-%s.jobs' % x site = SeoSiteFactory(id=x, domain=domain, name=domain) site_package = SitePackageFactory(owner=self.company) site_package.make_unique_for_site(site) product = ProductFactory(package=site_package, owner=self.company) for y in range(1, 5): grouping = ProductGroupingFactory(owner=self.company, display_order=y) ProductOrder.objects.create(product=product, group=grouping, display_order=y) count = ProductGrouping.objects.filter_by_sites([site]).count() self.assertEqual(count, 4) self.assertEqual(ProductGrouping.objects.all().count(), 60)
def test_request_filter_by_site_multiple_sites(self): site_in_both_packages = SeoSiteFactory(domain='secondsite.jobs', id=7) single_site_package = SitePackageFactory(owner=self.company) single_site_package.make_unique_for_site(site_in_both_packages) both_sites_package = SitePackageFactory(owner=self.company) both_sites_package.sites.add(site_in_both_packages) both_sites_package.sites.add(self.site) both_sites_package.save() single_site_product = ProductFactory(package=single_site_package, owner=self.company) single_site_purchasedproduct = PurchasedProductFactory( product=single_site_product, owner=self.company) # Unapproved PurchasedJobs generate Requests. PurchasedJobFactory(owner=self.company, created_by=self.user, purchased_product=single_site_purchasedproduct) both_sites_product = ProductFactory(package=both_sites_package, owner=self.company) both_sites_purchasedproduct = PurchasedProductFactory( product=both_sites_product, owner=self.company) PurchasedJobFactory(owner=self.company, created_by=self.user, purchased_product=both_sites_purchasedproduct) self.assertEqual(Request.objects.all().count(), 2) # Confirm that filtering by both sites gets both products. both_sites = [site_in_both_packages, self.site] count = Request.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) # Confirm that filtering by the site that only has one product only # gets one product. count = Request.objects.filter_by_sites([self.site]).count() self.assertEqual(count, 1) # Confirm that filtering by the site the site that has both products # gets both jobs. count = (Request.objects.filter_by_sites([site_in_both_packages ]).count()) self.assertEqual(count, 2)
def test_offlinepurchase_filter_by_sites(self): role = RoleFactory(company=self.company) self.user.roles.add(role) for x in range(8800, 8815): domain = 'testsite-%s.jobs' % x site = SeoSiteFactory(id=x, domain=domain, name=domain) site_package = SitePackageFactory(owner=self.company) site_package.make_unique_for_site(site) product = ProductFactory(package=site_package, owner=self.company) for y in range(1, 5): purchase = OfflinePurchaseFactory(owner=self.company, created_by=self.user) OfflineProduct.objects.create(product=product, offline_purchase=purchase) count = OfflinePurchase.objects.filter_by_sites([site]).count() self.assertEqual(count, 4) self.assertEqual(OfflinePurchase.objects.all().count(), 60)
def test_request_filter_by_sites(self): for x in range(8800, 8815): domain = 'testsite-%s.jobs' % x site = SeoSiteFactory(id=x, domain=domain, name=domain) site_package = SitePackageFactory(owner=self.company) site_package.make_unique_for_site(site) product = ProductFactory(package=site_package, owner=self.company) purchased_product = PurchasedProductFactory(product=product, owner=self.company) for y in range(1, 5): # Unapproved purchased jobs should create Requests. PurchasedJobFactory(owner=self.company, created_by=self.user, purchased_product=purchased_product) self.assertEqual( Request.objects.filter_by_sites([site]).count(), 4) self.assertEqual(Request.objects.all().count(), 60)
def test_purchasedjob_filter_by_sites(self): for x in range(8800, 8815): domain = 'testsite-%s.jobs' % x site = SeoSiteFactory(id=x, domain=domain, name=domain) site_package = SitePackageFactory(owner=self.company) site_package.make_unique_for_site(site) product = ProductFactory(package=site_package, owner=self.company) purchased_product = PurchasedProductFactory(product=product, owner=self.company) for y in range(1, 5): job = PurchasedJobFactory(owner=self.company, created_by=self.user, purchased_product=purchased_product) job.site_packages.add(site_package) job.save() count = PurchasedJob.objects.filter_by_sites([site]).count() self.assertEqual(count, 4) self.assertEqual(PurchasedJob.objects.all().count(), 60)
def test_invoice_from_offlinepurchase_filter_by_site_multiple_sites(self): role = RoleFactory(company=self.company) self.user.roles.add(role) site_in_both_packages = SeoSiteFactory(domain='secondsite.jobs', id=7) single_site_package = SitePackageFactory(owner=self.company) single_site_package.make_unique_for_site(site_in_both_packages) both_sites_package = SitePackageFactory(owner=self.company) both_sites_package.sites.add(site_in_both_packages) both_sites_package.sites.add(self.site) both_sites_package.save() single_site_product = ProductFactory(package=single_site_package, owner=self.company) single_site_purchase = OfflinePurchaseFactory(owner=self.company, created_by=self.user) OfflineProduct.objects.create(product=single_site_product, offline_purchase=single_site_purchase) both_sites_product = ProductFactory(package=both_sites_package, owner=self.company) both_sites_purchase = OfflinePurchaseFactory(owner=self.company, created_by=self.user) OfflineProduct.objects.create(product=both_sites_product, offline_purchase=both_sites_purchase) # Confirm that filtering by both sites gets both groupings. both_sites = [site_in_both_packages, self.site] count = Invoice.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) # Confirm that filtering by the site that only has one grouping only # gets one grouping. count = Invoice.objects.filter_by_sites([self.site]).count() self.assertEqual(count, 1) # Confirm that filtering by the site the site that has both jobs gets # both jobs. count = (Invoice.objects.filter_by_sites([site_in_both_packages ]).count()) self.assertEqual(count, 2)
def test_productgrouping_filter_by_site_multiple_sites(self): site_in_both_packages = SeoSiteFactory(domain='secondsite.jobs', id=7) single_site_package = SitePackageFactory(owner=self.company) single_site_package.make_unique_for_site(site_in_both_packages) both_sites_package = SitePackageFactory(owner=self.company) both_sites_package.sites.add(site_in_both_packages) both_sites_package.sites.add(self.site) both_sites_package.save() single_site_product = ProductFactory(package=single_site_package, owner=self.company) single_site_grouping = ProductGroupingFactory(owner=self.company) ProductOrder.objects.create(product=single_site_product, group=single_site_grouping) both_sites_product = ProductFactory(package=both_sites_package, owner=self.company) both_sites_grouping = ProductGroupingFactory(owner=self.company) ProductOrder.objects.create(product=both_sites_product, group=both_sites_grouping) # Confirm that filtering by both sites gets both groupings. both_sites = [site_in_both_packages, self.site] count = ProductGrouping.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) # Confirm that filtering by the site that only has one grouping only # gets one grouping. count = ProductGrouping.objects.filter_by_sites([self.site]).count() self.assertEqual(count, 1) # Confirm that filtering by the site the site that has both jobs gets # both jobs. objs = ProductGrouping.objects.filter_by_sites([site_in_both_packages]) count = objs.count() self.assertEqual(count, 2)
def setUp(self): super(ModelTests, self).setUp() self.user = User.objects.create(email='*****@*****.**') self.company = CompanyFactory() CompanyProfile.objects.create(company=self.company) self.site = SeoSiteFactory()#domain='buckconsoltants.jobs') self.bu = BusinessUnitFactory() self.site.business_units.add(self.bu) self.site.save() self.company.job_source_ids.add(self.bu) self.company.save() # Use the newly created site for testing instead of secure.my.jobs. settings.SITE = self.site self.request_data = { 'title': 'title', 'company': self.company.id, 'reqid': '1', 'description': 'sadfljasdfljasdflasdfj', 'link': 'www.google.com', 'on_sites': '0', 'apply_info': '', } self.request_location = { 'city': 'Indianapolis', 'state': 'Indiana', 'state_short': 'IN', 'country': 'United States', 'country_short': 'USA', 'zipcode': '46268', } self.site_package_data = { 'name': 'Test Site Package', }
def setUp(self): self.aa = SeoSiteFactory(domain='aa.com') self.aa2 = SeoSiteFactory(domain='teach.aa.com') self.bb = SeoSiteFactory(domain='bb.com')
class ModelTests(MyJobsBase): def setUp(self): super(ModelTests, self).setUp() self.user = User.objects.create(email='*****@*****.**') self.company = CompanyFactory() CompanyProfile.objects.create(company=self.company) self.site = SeoSiteFactory() #domain='buckconsoltants.jobs') self.bu = BusinessUnitFactory() self.site.business_units.add(self.bu) self.site.save() self.company.job_source_ids.add(self.bu) self.company.save() # Use the newly created site for testing instead of secure.my.jobs. settings.SITE = self.site self.request_data = { 'title': 'title', 'company': self.company.id, 'reqid': '1', 'description': 'sadfljasdfljasdflasdfj', 'link': 'www.google.com', 'on_sites': '0', 'apply_info': '', } self.request_location = { 'city': 'Indianapolis', 'state': 'Indiana', 'state_short': 'IN', 'country': 'United States', 'country_short': 'USA', 'zipcode': '46268', } self.site_package_data = { 'name': 'Test Site Package', } def test_job_creation_and_deletion(self): locations = JobLocationFactory.create_batch(5) job = JobFactory(owner=self.company, created_by=self.user) job.locations = locations job.save() self.assertEqual(Job.objects.all().count(), 1) self.assertEqual(JobLocation.objects.all().count(), 5) job.delete() self.assertEqual(Job.objects.all().count(), 0) self.assertEqual(JobLocation.objects.all().count(), 0) def test_add_remove_job_locations(self): """ Regression test, job locations that were removed from a job weren't being removed from Solr Tests that adding or removing locations results in Solr updates after a job save """ locations = JobLocationFactory.create_batch(2) job = JobFactory(owner=self.company, created_by=self.user, locations=locations) self.assertEqual(self.ms_solr.search('*:*').hits, 2) guid = locations[0].guid job.locations.remove(locations[0]) self.assertEqual(self.ms_solr.search('guid:%s' % guid).hits, 0) self.assertEqual( self.ms_solr.search('guid:%s' % locations[1].guid).hits, 1) job.locations.remove(locations[1]) self.assertEqual(self.ms_solr.search('*:*').hits, 0) def test_job_add_to_solr(self): job = JobFactory(owner=self.company, created_by=self.user) job.locations.add(JobLocationFactory()) job.add_to_solr() guids = " OR ".join(job.guids()) query = "guid:%s" % guids self.assertEqual(self.ms_solr.search(query).hits, 1) def test_job_remove_from_solr(self): job = JobFactory(owner=self.company, created_by=self.user) locations = JobLocationFactory.create_batch(2) job.locations = locations job.save() job.remove_from_solr() guids = " OR ".join(job.guids()) query = "guid:%s" % guids self.assertEqual(self.ms_solr.search(query).hits, 0) def test_job_generate_guid(self): guid = '1' * 32 # Confirm that pre-assigned guids are not being overwritten. location = JobLocationFactory(guid=guid) self.assertEqual(guid, location.guid) location.delete() # Confirm that if a guid isn't assigned one is getting assigned # to it properly. location = JobLocationFactory() self.assertIsNotNone(location.guid) self.assertNotEqual(location.guid, guid) def test_site_package_make_unique_for_site(self): package = SitePackage.objects.create(**self.site_package_data) package.make_unique_for_site(self.site) self.assertEqual(self.site.site_package, package) package.delete() package = SitePackage.objects.create(**self.site_package_data) for x in range(100, 110): site = SeoSiteFactory(id=x, domain="%s.jobs" % x) package.sites.add(site) # Site packages with existing sites associated with it should still # only end up with one associated site. site = SeoSiteFactory(id=4000, domain="4000.jobs") package.make_unique_for_site(site) self.assertEqual(site.site_package, package) def test_site_package_make_unique_for_company(self): package = SitePackage.objects.create(**self.site_package_data) package.make_unique_for_company(self.company) self.assertEqual(self.company.site_package, package) package.delete() package = SitePackage.objects.create(**self.site_package_data) first = [] for x in range(1000, 1003): site = SeoSiteFactory(id=x, domain="%s.jobs" % x) package.sites.add(site) first.append(site.pk) self.assertItemsEqual(package.sites.all().values_list('id', flat=True), first) second = [self.site.pk] for x in range(100, 103): site = SeoSiteFactory(id=x, domain="%s.jobs" % x) site.business_units.add(self.bu) site.save() second.append(site.pk) # Site packages with existing sites associated with it should # only end up with the sites for a company. package.make_unique_for_company(self.company) self.assertEqual(self.company.site_package, package) self.assertItemsEqual(package.sites.all().values_list('id', flat=True), second) def create_purchased_job(self, pk=None): if not hasattr(self, 'package'): self.package = SitePackageFactory(owner=self.company) if not hasattr(self, 'product'): self.product = ProductFactory(package=self.package, owner=self.company) if not hasattr(self, 'purchased_product'): self.purchased_product = PurchasedProductFactory( product=self.product, owner=self.company) exp_date = date.today() + timedelta(self.product.posting_window_length) self.assertEqual(self.purchased_product.expiration_date, exp_date) return PurchasedJobFactory(owner=self.company, created_by=self.user, purchased_product=self.purchased_product, pk=pk) def test_purchased_job_add(self): self.create_purchased_job() self.assertEqual(PurchasedJob.objects.all().count(), 1) self.assertEqual(SitePackage.objects.all().count(), 1) self.assertEqual(Request.objects.all().count(), 1) package = SitePackage.objects.get() job = PurchasedJob.objects.get() self.assertItemsEqual(job.site_packages.all(), [package]) def test_purchased_job_add_to_solr(self): job = self.create_purchased_job() job.locations.add(JobLocationFactory()) job.save() # Add to solr and delete from solr shouldn't be called until # the job is approved. guids = " OR ".join(job.guids()) query = "guid:%s" % guids self.assertEqual(self.ms_solr.search(query).hits, 0) job.is_approved = True # Jobs won't be added/deleted until it's confirmed that the # purchased product is paid for as well. job.purchased_product.paid = True job.purchased_product.save() job.save() # Now that the job is approved, it should've been sent to solr. guids = " OR ".join(job.guids()) query = "guid:%s" % guids self.assertEqual(self.ms_solr.search(query).hits, 1) def test_purchased_product_jobs_remaining(self): num_jobs_allowed = Product._meta.get_field_by_name('num_jobs_allowed') expected_num_jobs = num_jobs_allowed[0].default for x in range(50, 50 + expected_num_jobs): if hasattr(self, 'purchased_product'): self.assertTrue(self.purchased_product.can_post_more()) self.create_purchased_job() expected_num_jobs -= 1 self.assertEqual(self.purchased_product.jobs_remaining, expected_num_jobs) self.assertFalse(self.purchased_product.can_post_more()) def test_invoice_sent_to_specific_email(self): """ When `other_recipients` is specified, `send_invoice_email` should send invoices to those email addresses. """ self.create_purchased_job() group, _ = Group.objects.get_or_create(name=Product.ADMIN_GROUP_NAME) # No one to receive the email. self.purchased_product.invoice.send_invoice_email() self.assertEqual(len(mail.outbox), 0) # Only recipient is specified recipient. other_recipients = ['*****@*****.**', '*****@*****.**'] self.purchased_product.invoice.send_invoice_email( other_recipients=other_recipients) self.assertItemsEqual(mail.outbox[0].to, other_recipients) def test_invoice_sent_to_admins(self): """ When a company has postajob admins, invoices should be sent to each of them, unless the behavior is disabled by passing `send_to_admins=False`. """ self.create_purchased_job() group, _ = Group.objects.get_or_create(name=Product.ADMIN_GROUP_NAME) # No one to receive the email. self.purchased_product.invoice.send_invoice_email() self.assertEqual(len(mail.outbox), 0) # Only recipients are admins. role = RoleFactory(company=self.company) self.user.roles.add(role) self.user.groups.add(group) self.purchased_product.invoice.send_invoice_email() self.assertItemsEqual(mail.outbox[0].to, [u'*****@*****.**']) self.assertItemsEqual(mail.outbox[0].from_email, '*****@*****.**') self.purchased_product.invoice.send_invoice_email( send_to_admins=False, other_recipients=['*****@*****.**']) # Only one in .to should be [email protected] self.assertEquals(len(mail.outbox[1].to), 1) self.assertItemsEqual(mail.outbox[1].to, ['*****@*****.**']) def test_invoice_sent_to_admins_and_others(self): """ If a company has postajob admins and `other_recipients` is specified, emails should be sent to both groups. """ self.create_purchased_job() group, _ = Group.objects.get_or_create(name=Product.ADMIN_GROUP_NAME) # No one to receive the email. self.purchased_product.invoice.send_invoice_email() self.assertEqual(len(mail.outbox), 0) role = RoleFactory(company=self.company) self.user.roles.add(role) self.user.groups.add(group) self.site.email_domain = 'test.domain' self.site.save() # Recipients are admins + specified recipients. self.purchased_product.invoice.send_invoice_email( other_recipients=['*****@*****.**']) self.assertItemsEqual(mail.outbox[0].to, ['*****@*****.**', u'*****@*****.**']) self.assertEqual(mail.outbox[0].from_email, '*****@*****.**') def test_invoice_unchanged_after_purchased_product_deletion(self): self.create_purchased_job() invoice = self.purchased_product.invoice invoice.send_invoice_email(other_recipients=['*****@*****.**']) old_email = mail.outbox.pop() self.purchased_product.delete() invoice.send_invoice_email(other_recipients=['*****@*****.**']) new_email = mail.outbox.pop() self.assertEqual(old_email.body, new_email.body) def test_invoice_unchanged_after_product_changed(self): self.create_purchased_job() invoice = self.purchased_product.invoice invoice.send_invoice_email(other_recipients=['*****@*****.**']) old_email = mail.outbox.pop() self.purchased_product.product.name = 'new name' self.purchased_product.product.save() invoice.send_invoice_email(other_recipients=['*****@*****.**']) new_email = mail.outbox.pop() self.assertEqual(old_email.body, new_email.body) def test_productgrouping_add_delete(self): self.create_purchased_job() ProductGrouping.objects.create(display_title='Test Grouping', explanation='Test Grouping', name='Test Grouping', owner=self.company) self.assertEqual(ProductGrouping.objects.all().count(), 1) grouping = ProductGrouping.objects.get() order = ProductOrder.objects.create(product=self.product, group=grouping) grouping_products = grouping.products.all().values_list('pk', flat=True) self.assertItemsEqual(grouping_products, [order.product.pk]) grouping.delete() self.assertEqual(ProductGrouping.objects.all().count(), 0) self.assertEqual(ProductOrder.objects.all().count(), 0) def test_request_generation(self): role = RoleFactory(company=self.company) self.user.roles.add(role) self.user.make_purchased_microsite_admin() self.create_purchased_job() self.assertEqual(PurchasedJob.objects.all().count(), 1) self.assertEqual(Request.objects.all().count(), 1) self.assertEqual(len(mail.outbox), 1) self.assertItemsEqual(mail.outbox[0].from_email, '*****@*****.**') mail.outbox = [] # Already approved jobs should not generate an additional request. PurchasedJobFactory(owner=self.company, created_by=self.user, purchased_product=self.purchased_product, is_approved=True) self.assertEqual(PurchasedJob.objects.all().count(), 2) self.assertEqual(Request.objects.all().count(), 1) self.assertEqual(len(mail.outbox), 0) def test_offlinepurchase_create_purchased_products(self): role = RoleFactory(company=self.company) self.user.roles.add(role) offline_purchase = OfflinePurchaseFactory(owner=self.company, created_by=self.user) package = SitePackageFactory(owner=self.company) product = ProductFactory(package=package, owner=self.company) for x in range(1, 15): PurchasedProduct.objects.all().delete() OfflineProduct.objects.all().delete() OfflineProductFactory(product=product, offline_purchase=offline_purchase, product_quantity=x) offline_purchase.create_purchased_products(self.company) self.assertEqual(PurchasedProduct.objects.all().count(), x) product_two = ProductFactory(package=package, owner=self.company) for x in range(1, 15): PurchasedProduct.objects.all().delete() OfflineProduct.objects.all().delete() OfflineProductFactory(product=product, offline_purchase=offline_purchase, product_quantity=x) OfflineProductFactory(product=product_two, offline_purchase=offline_purchase, product_quantity=x) offline_purchase.create_purchased_products(self.company) self.assertEqual(PurchasedProduct.objects.all().count(), x * 2) def test_offlinepurchase_filter_by_sites(self): role = RoleFactory(company=self.company) self.user.roles.add(role) for x in range(8800, 8815): domain = 'testsite-%s.jobs' % x site = SeoSiteFactory(id=x, domain=domain, name=domain) site_package = SitePackageFactory(owner=self.company) site_package.make_unique_for_site(site) product = ProductFactory(package=site_package, owner=self.company) for y in range(1, 5): purchase = OfflinePurchaseFactory(owner=self.company, created_by=self.user) OfflineProduct.objects.create(product=product, offline_purchase=purchase) count = OfflinePurchase.objects.filter_by_sites([site]).count() self.assertEqual(count, 4) self.assertEqual(OfflinePurchase.objects.all().count(), 60) def test_invoice_filter_by_sites(self): role = RoleFactory(company=self.company) self.user.roles.add(role) for x in range(8800, 8815): domain = 'testsite-%s.jobs' % x site = SeoSiteFactory(id=x, domain=domain, name=domain) site_package = SitePackageFactory(owner=self.company) site_package.make_unique_for_site(site) product = ProductFactory(package=site_package, owner=self.company) for y in range(1, 5): # OfflinePurchaseFactory() automatically creates the invoice. purchase = OfflinePurchaseFactory(owner=self.company, created_by=self.user) OfflineProduct.objects.create(product=product, offline_purchase=purchase) # Confirm it correctly picks up Invoices associated with # OfflinePurchases. self.assertEqual( Invoice.objects.filter_by_sites([site]).count(), 4) for y in range(1, 5): # PurchasedProductFactory() also automatically creates # the invoice. PurchasedProductFactory(product=product, owner=self.company) # Confirm it correctly picks up Invoices associated with # PurchasedProducts. self.assertEqual( Invoice.objects.filter_by_sites([site]).count(), 8) self.assertEqual(Invoice.objects.all().count(), 120) def test_product_filter_by_sites(self): for x in range(8800, 8815): domain = 'testsite-%s.jobs' % x site = SeoSiteFactory(id=x, domain=domain, name=domain) site_package = SitePackageFactory(owner=self.company) site_package.make_unique_for_site(site) for y in range(1, 5): ProductFactory(package=site_package, owner=self.company) self.assertEqual( Product.objects.filter_by_sites([site]).count(), 4) self.assertEqual(Product.objects.all().count(), 60) def test_request_filter_by_sites(self): for x in range(8800, 8815): domain = 'testsite-%s.jobs' % x site = SeoSiteFactory(id=x, domain=domain, name=domain) site_package = SitePackageFactory(owner=self.company) site_package.make_unique_for_site(site) product = ProductFactory(package=site_package, owner=self.company) purchased_product = PurchasedProductFactory(product=product, owner=self.company) for y in range(1, 5): # Unapproved purchased jobs should create Requests. PurchasedJobFactory(owner=self.company, created_by=self.user, purchased_product=purchased_product) self.assertEqual( Request.objects.filter_by_sites([site]).count(), 4) self.assertEqual(Request.objects.all().count(), 60) def test_purchasedproduct_filter_by_sites(self): for x in range(8800, 8815): domain = 'testsite-%s.jobs' % x site = SeoSiteFactory(id=x, domain=domain, name=domain) site_package = SitePackageFactory(owner=self.company) site_package.make_unique_for_site(site) for y in range(1, 5): product = ProductFactory(package=site_package, owner=self.company) PurchasedProductFactory(product=product, owner=self.company) count = PurchasedProduct.objects.filter_by_sites([site]).count() self.assertEqual(count, 4) self.assertEqual(PurchasedProduct.objects.all().count(), 60) def test_job_filter_by_sites(self): for x in range(8800, 8815): domain = 'testsite-%s.jobs' % x site = SeoSiteFactory(id=x, domain=domain, name=domain) site_package = SitePackageFactory(owner=self.company) site_package.make_unique_for_site(site) for y in range(1, 5): job = JobFactory(owner=self.company, created_by=self.user) job.site_packages.add(site_package) job.save() self.assertEqual(Job.objects.filter_by_sites([site]).count(), 4) self.assertEqual(Job.objects.all().count(), 60) def test_purchasedjob_filter_by_sites(self): for x in range(8800, 8815): domain = 'testsite-%s.jobs' % x site = SeoSiteFactory(id=x, domain=domain, name=domain) site_package = SitePackageFactory(owner=self.company) site_package.make_unique_for_site(site) product = ProductFactory(package=site_package, owner=self.company) purchased_product = PurchasedProductFactory(product=product, owner=self.company) for y in range(1, 5): job = PurchasedJobFactory(owner=self.company, created_by=self.user, purchased_product=purchased_product) job.site_packages.add(site_package) job.save() count = PurchasedJob.objects.filter_by_sites([site]).count() self.assertEqual(count, 4) self.assertEqual(PurchasedJob.objects.all().count(), 60) def test_productgrouping_filter_by_sites(self): for x in range(8800, 8815): domain = 'testsite-%s.jobs' % x site = SeoSiteFactory(id=x, domain=domain, name=domain) site_package = SitePackageFactory(owner=self.company) site_package.make_unique_for_site(site) product = ProductFactory(package=site_package, owner=self.company) for y in range(1, 5): grouping = ProductGroupingFactory(owner=self.company, display_order=y) ProductOrder.objects.create(product=product, group=grouping, display_order=y) count = ProductGrouping.objects.filter_by_sites([site]).count() self.assertEqual(count, 4) self.assertEqual(ProductGrouping.objects.all().count(), 60) def test_sitepackage_filter_by_sites(self): for x in range(8800, 8815): domain = 'testsite-%s.jobs' % x site = SeoSiteFactory(id=x, domain=domain, name=domain) for y in range(1, 5): site_package = SitePackageFactory(owner=self.company) site_package.sites.add(site) site_package.save() count = SitePackage.objects.filter_by_sites([site]).count() self.assertEqual(count, 4) count = Package.objects.filter_by_sites([site]).count() self.assertEqual(count, 4) self.assertEqual(SitePackage.objects.all().count(), 60) self.assertEqual(Package.objects.all().count(), 60) def test_product_filter_by_site_multiple_sites(self): site_in_both_packages = SeoSiteFactory(domain='secondsite.jobs', id=7) single_site_package = SitePackageFactory(owner=self.company) single_site_package.make_unique_for_site(site_in_both_packages) both_sites_package = SitePackageFactory(owner=self.company) both_sites_package.sites.add(site_in_both_packages) both_sites_package.sites.add(self.site) both_sites_package.save() ProductFactory(package=single_site_package, owner=self.company) ProductFactory(package=both_sites_package, owner=self.company) self.assertEqual(Product.objects.all().count(), 2) # Confirm that filtering by both sites gets both jobs. both_sites = [site_in_both_packages, self.site] count = Product.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) # Confirm that filtering by the site that only has one job only # gets one job. count = Product.objects.filter_by_sites([self.site]).count() self.assertEqual(count, 1) # Confirm that filtering by the site the site that has both jobs gets # both jobs. count = (Product.objects.filter_by_sites([site_in_both_packages ]).count()) self.assertEqual(count, 2) def test_job_filter_by_site_multiple_sites(self): site_in_both_packages = SeoSiteFactory(domain='secondsite.jobs', id=7) single_site_package = SitePackageFactory(owner=self.company) single_site_package.make_unique_for_site(site_in_both_packages) both_sites_package = SitePackageFactory(owner=self.company) both_sites_package.sites.add(site_in_both_packages) both_sites_package.sites.add(self.site) both_sites_package.save() job_on_both = JobFactory(owner=self.company, created_by=self.user) job_on_both.site_packages.add(both_sites_package) job_on_both.save() job_on_new_site = JobFactory(owner=self.company, created_by=self.user) job_on_new_site.site_packages.add(single_site_package) job_on_new_site.save() self.assertEqual(Job.objects.all().count(), 2) # Confirm that filtering by both sites gets both jobs. both_sites = [site_in_both_packages, self.site] count = Job.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) # Confirm that filtering by the site that only has one job only # gets one job. self.assertEqual(Job.objects.filter_by_sites([self.site]).count(), 1) # Confirm that filtering by the site the site that has both jobs gets # both jobs. count = Job.objects.filter_by_sites([site_in_both_packages]).count() self.assertEqual(count, 2) def test_purchasedproduct_filter_by_site_multiple_sites(self): site_in_both_packages = SeoSiteFactory(domain='secondsite.jobs', id=7) single_site_package = SitePackageFactory(owner=self.company) single_site_package.make_unique_for_site(site_in_both_packages) both_sites_package = SitePackageFactory(owner=self.company) both_sites_package.sites.add(site_in_both_packages) both_sites_package.sites.add(self.site) both_sites_package.save() single_site_product = ProductFactory(package=single_site_package, owner=self.company) PurchasedProductFactory(product=single_site_product, owner=self.company) both_sites_product = ProductFactory(package=both_sites_package, owner=self.company) PurchasedProductFactory(product=both_sites_product, owner=self.company) self.assertEqual(PurchasedProduct.objects.all().count(), 2) # Confirm that filtering by both sites gets both products. both_sites = [site_in_both_packages, self.site] count = PurchasedProduct.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) # Confirm that filtering by the site that only has one product only # gets one product. count = PurchasedProduct.objects.filter_by_sites([self.site]).count() self.assertEqual(count, 1) # Confirm that filtering by the site the site that has both products # gets both jobs. objs = (PurchasedProduct.objects.filter_by_sites( [site_in_both_packages])) count = objs.count() self.assertEqual(count, 2) def test_purchasedjob_filter_by_site_multiple_sites(self): site_in_both_packages = SeoSiteFactory(domain='secondsite.jobs', id=7) single_site_package = SitePackageFactory(owner=self.company) single_site_package.make_unique_for_site(site_in_both_packages) both_sites_package = SitePackageFactory(owner=self.company) both_sites_package.sites.add(site_in_both_packages) both_sites_package.sites.add(self.site) both_sites_package.save() single_site_product = ProductFactory(package=single_site_package, owner=self.company) single_site_purchasedproduct = PurchasedProductFactory( product=single_site_product, owner=self.company) both_sites_product = ProductFactory(package=both_sites_package, owner=self.company) both_sites_purchasedproduct = PurchasedProductFactory( product=both_sites_product, owner=self.company) PurchasedJobFactory(owner=self.company, created_by=self.user, purchased_product=single_site_purchasedproduct) PurchasedJobFactory(owner=self.company, created_by=self.user, purchased_product=both_sites_purchasedproduct) self.assertEqual(PurchasedJob.objects.all().count(), 2) # Confirm that filtering by both sites gets both jobs. both_sites = [site_in_both_packages, self.site] count = PurchasedJob.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) # Confirm that filtering by the site that only has one job only # gets one job. count = PurchasedJob.objects.filter_by_sites([self.site]).count() self.assertEqual(count, 1) # Confirm that filtering by the site the site that has both jobs gets # both jobs. objs = PurchasedJob.objects.filter_by_sites([site_in_both_packages]) count = objs.count() self.assertEqual(count, 2) def test_sitepackage_filter_by_site_multiple_sites(self): site_in_both_packages = SeoSiteFactory(domain='secondsite.jobs', id=7) single_site_package = SitePackageFactory(owner=self.company) single_site_package.make_unique_for_site(site_in_both_packages) both_sites_package = SitePackageFactory(owner=self.company) both_sites_package.sites.add(site_in_both_packages) both_sites_package.sites.add(self.site) both_sites_package.save() self.assertEqual(SitePackage.objects.all().count(), 2) self.assertEqual(Package.objects.all().count(), 2) # Confirm that filtering by both sites gets both packages. both_sites = [site_in_both_packages, self.site] count = SitePackage.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) count = Package.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) # Confirm that filtering by the site that only has one package only # gets one package. count = SitePackage.objects.filter_by_sites([self.site]).count() self.assertEqual(count, 1) count = Package.objects.filter_by_sites([self.site]).count() self.assertEqual(count, 1) # Confirm that filtering by the site the site that has both packages # gets both packages. objs = SitePackage.objects.filter_by_sites([site_in_both_packages]) count = objs.count() self.assertEqual(count, 2) count = (Package.objects.filter_by_sites([site_in_both_packages ]).count()) self.assertEqual(count, 2) def test_productgrouping_filter_by_site_multiple_sites(self): site_in_both_packages = SeoSiteFactory(domain='secondsite.jobs', id=7) single_site_package = SitePackageFactory(owner=self.company) single_site_package.make_unique_for_site(site_in_both_packages) both_sites_package = SitePackageFactory(owner=self.company) both_sites_package.sites.add(site_in_both_packages) both_sites_package.sites.add(self.site) both_sites_package.save() single_site_product = ProductFactory(package=single_site_package, owner=self.company) single_site_grouping = ProductGroupingFactory(owner=self.company) ProductOrder.objects.create(product=single_site_product, group=single_site_grouping) both_sites_product = ProductFactory(package=both_sites_package, owner=self.company) both_sites_grouping = ProductGroupingFactory(owner=self.company) ProductOrder.objects.create(product=both_sites_product, group=both_sites_grouping) # Confirm that filtering by both sites gets both groupings. both_sites = [site_in_both_packages, self.site] count = ProductGrouping.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) # Confirm that filtering by the site that only has one grouping only # gets one grouping. count = ProductGrouping.objects.filter_by_sites([self.site]).count() self.assertEqual(count, 1) # Confirm that filtering by the site the site that has both jobs gets # both jobs. objs = ProductGrouping.objects.filter_by_sites([site_in_both_packages]) count = objs.count() self.assertEqual(count, 2) def test_offlinepurchase_filter_by_site_multiple_sites(self): role = RoleFactory(company=self.company) self.user.roles.add(role) site_in_both_packages = SeoSiteFactory(domain='secondsite.jobs', id=7) single_site_package = SitePackageFactory(owner=self.company) single_site_package.make_unique_for_site(site_in_both_packages) both_sites_package = SitePackageFactory(owner=self.company) both_sites_package.sites.add(site_in_both_packages) both_sites_package.sites.add(self.site) both_sites_package.save() single_site_product = ProductFactory(package=single_site_package, owner=self.company) single_site_purchase = OfflinePurchaseFactory(owner=self.company, created_by=self.user) OfflineProduct.objects.create(product=single_site_product, offline_purchase=single_site_purchase) both_sites_product = ProductFactory(package=both_sites_package, owner=self.company) both_sites_purchase = OfflinePurchaseFactory(owner=self.company, created_by=self.user) OfflineProduct.objects.create(product=both_sites_product, offline_purchase=both_sites_purchase) # Confirm that filtering by both sites gets both groupings. both_sites = [site_in_both_packages, self.site] count = OfflinePurchase.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) # Confirm that filtering by the site that only has one grouping only # gets one grouping. count = OfflinePurchase.objects.filter_by_sites([self.site]).count() self.assertEqual(count, 1) # Confirm that filtering by the site the site that has both jobs gets # both jobs. objs = OfflinePurchase.objects.filter_by_sites([site_in_both_packages]) count = objs.count() self.assertEqual(count, 2) def test_invoice_from_offlinepurchase_filter_by_site_multiple_sites(self): role = RoleFactory(company=self.company) self.user.roles.add(role) site_in_both_packages = SeoSiteFactory(domain='secondsite.jobs', id=7) single_site_package = SitePackageFactory(owner=self.company) single_site_package.make_unique_for_site(site_in_both_packages) both_sites_package = SitePackageFactory(owner=self.company) both_sites_package.sites.add(site_in_both_packages) both_sites_package.sites.add(self.site) both_sites_package.save() single_site_product = ProductFactory(package=single_site_package, owner=self.company) single_site_purchase = OfflinePurchaseFactory(owner=self.company, created_by=self.user) OfflineProduct.objects.create(product=single_site_product, offline_purchase=single_site_purchase) both_sites_product = ProductFactory(package=both_sites_package, owner=self.company) both_sites_purchase = OfflinePurchaseFactory(owner=self.company, created_by=self.user) OfflineProduct.objects.create(product=both_sites_product, offline_purchase=both_sites_purchase) # Confirm that filtering by both sites gets both groupings. both_sites = [site_in_both_packages, self.site] count = Invoice.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) # Confirm that filtering by the site that only has one grouping only # gets one grouping. count = Invoice.objects.filter_by_sites([self.site]).count() self.assertEqual(count, 1) # Confirm that filtering by the site the site that has both jobs gets # both jobs. count = (Invoice.objects.filter_by_sites([site_in_both_packages ]).count()) self.assertEqual(count, 2) def test_invoice_from_purchasedproduct_filter_by_site_multiple_sites(self): site_in_both_packages = SeoSiteFactory(domain='secondsite.jobs', id=7) single_site_package = SitePackageFactory(owner=self.company) single_site_package.make_unique_for_site(site_in_both_packages) both_sites_package = SitePackageFactory(owner=self.company) both_sites_package.sites.add(site_in_both_packages) both_sites_package.sites.add(self.site) both_sites_package.save() single_site_product = ProductFactory(package=single_site_package, owner=self.company) PurchasedProductFactory(product=single_site_product, owner=self.company) both_sites_product = ProductFactory(package=both_sites_package, owner=self.company) PurchasedProductFactory(product=both_sites_product, owner=self.company) self.assertEqual(Invoice.objects.all().count(), 2) # Confirm that filtering by both sites gets both products. both_sites = [site_in_both_packages, self.site] count = Invoice.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) # Confirm that filtering by the site that only has one product only # gets one product. count = Invoice.objects.filter_by_sites([self.site]).count() self.assertEqual(count, 1) # Confirm that filtering by the site the site that has both products # gets both jobs. count = (Invoice.objects.filter_by_sites([site_in_both_packages ]).count()) self.assertEqual(count, 2) def test_request_filter_by_site_multiple_sites(self): site_in_both_packages = SeoSiteFactory(domain='secondsite.jobs', id=7) single_site_package = SitePackageFactory(owner=self.company) single_site_package.make_unique_for_site(site_in_both_packages) both_sites_package = SitePackageFactory(owner=self.company) both_sites_package.sites.add(site_in_both_packages) both_sites_package.sites.add(self.site) both_sites_package.save() single_site_product = ProductFactory(package=single_site_package, owner=self.company) single_site_purchasedproduct = PurchasedProductFactory( product=single_site_product, owner=self.company) # Unapproved PurchasedJobs generate Requests. PurchasedJobFactory(owner=self.company, created_by=self.user, purchased_product=single_site_purchasedproduct) both_sites_product = ProductFactory(package=both_sites_package, owner=self.company) both_sites_purchasedproduct = PurchasedProductFactory( product=both_sites_product, owner=self.company) PurchasedJobFactory(owner=self.company, created_by=self.user, purchased_product=both_sites_purchasedproduct) self.assertEqual(Request.objects.all().count(), 2) # Confirm that filtering by both sites gets both products. both_sites = [site_in_both_packages, self.site] count = Request.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) # Confirm that filtering by the site that only has one product only # gets one product. count = Request.objects.filter_by_sites([self.site]).count() self.assertEqual(count, 1) # Confirm that filtering by the site the site that has both products # gets both jobs. count = (Request.objects.filter_by_sites([site_in_both_packages ]).count()) self.assertEqual(count, 2) def test_request_on_purchasedjob_update(self): # Creating a PurchasedJob for a Product that requires approval # creates a Request job = PurchasedJobFactory(owner=self.company, created_by=self.user) self.assertEqual(Request.objects.count(), 1) # Re-saving that job should not create a new request. job.save() self.assertEqual(Request.objects.count(), 1) # Approving the job should also not create a new request. job.is_approved = True job.save() self.assertEqual(Request.objects.count(), 1) # Marking the job as not approved and saving it should not create # a new request if no action was taken on the old request. job.is_approved = False job.save() self.assertEqual(Request.objects.count(), 1) # Marking the job as not approved and saving it should create # a new request if there is no pending request. request = Request.objects.get() request.action_taken = True request.save() job.save() self.assertEqual(Request.objects.count(), 2) def test_enable_posting(self): """ Attempting to automatically enable posting for a company should result in a valid site package owned by that company. """ AppAccessFactory(name='Posting') company = CompanyFactory(name='Posting Company') site = SeoSite.objects.create(domain='somewhere.jobs') # sanity checks self.assertIsNone(site.canonical_company) self.assertEqual(company.enabled_access, []) package = enable_posting(company, site) # Django caches model instances company = Company.objects.get(pk=company.pk) site = SeoSite.objects.get(pk=site.pk) self.assertEqual(site.canonical_company, company) self.assertIn("Posting", company.enabled_access) self.assertIn(site, package.sites.all()) self.assertTrue( LoginBlock.objects.filter( name="Posting Company Login Block").exists()) def test_enable_marketplace(self): """ Attempting to automatically enable posting for a company should result in a valid site package owned by that company. """ AppAccessFactory(name='MarketPlace') company = CompanyFactory(name='Marketplace Company') site = SeoSite.objects.create(domain='somewhereelse.jobs') # sanity checks self.assertIsNone(site.canonical_company) self.assertEqual(company.enabled_access, []) package = enable_marketplace(company, site) # Django caches model instances company = Company.objects.get(pk=company.pk) site = SeoSite.objects.get(pk=site.pk) self.assertEqual(site.canonical_company, company) self.assertIn("MarketPlace", company.enabled_access) self.assertIn(site, package.sites.all()) self.assertTrue( LoginBlock.objects.filter( name="Marketplace Company Login Block").exists()) def test_create_login_block(self): """ Ensures that a login block is createed with the correct associations and a valid template. """ company = CompanyFactory(name='Marketplace Company') site = SeoSite.objects.create(domain='somewhereelse.jobs') template = raw_base_template(LoginBlock) response = self.client.get("/login", follow=True) login_block = create_login_block(company, site) # validate that the correct template was assigned to the login block self.assertEqual(login_block.template, template)
class ModelTests(MyJobsBase): def setUp(self): super(ModelTests, self).setUp() self.user = User.objects.create(email='*****@*****.**') self.company = CompanyFactory() CompanyProfile.objects.create(company=self.company) self.site = SeoSiteFactory()#domain='buckconsoltants.jobs') self.bu = BusinessUnitFactory() self.site.business_units.add(self.bu) self.site.save() self.company.job_source_ids.add(self.bu) self.company.save() # Use the newly created site for testing instead of secure.my.jobs. settings.SITE = self.site self.request_data = { 'title': 'title', 'company': self.company.id, 'reqid': '1', 'description': 'sadfljasdfljasdflasdfj', 'link': 'www.google.com', 'on_sites': '0', 'apply_info': '', } self.request_location = { 'city': 'Indianapolis', 'state': 'Indiana', 'state_short': 'IN', 'country': 'United States', 'country_short': 'USA', 'zipcode': '46268', } self.site_package_data = { 'name': 'Test Site Package', } def test_job_creation_and_deletion(self): locations = JobLocationFactory.create_batch(5) job = JobFactory(owner=self.company, created_by=self.user) job.locations = locations job.save() self.assertEqual(Job.objects.all().count(), 1) self.assertEqual(JobLocation.objects.all().count(), 5) job.delete() self.assertEqual(Job.objects.all().count(), 0) self.assertEqual(JobLocation.objects.all().count(), 0) def test_add_remove_job_locations(self): """ Regression test, job locations that were removed from a job weren't being removed from Solr Tests that adding or removing locations results in Solr updates after a job save """ locations = JobLocationFactory.create_batch(2) job = JobFactory( owner=self.company, created_by=self.user, locations=locations) self.assertEqual(self.ms_solr.search('*:*').hits, 2) guid = locations[0].guid job.locations.remove(locations[0]) self.assertEqual(self.ms_solr.search('guid:%s' % guid).hits, 0) self.assertEqual(self.ms_solr.search('guid:%s' % locations[1].guid).hits, 1) job.locations.remove(locations[1]) self.assertEqual(self.ms_solr.search('*:*').hits, 0) def test_job_add_to_solr(self): job = JobFactory(owner=self.company, created_by=self.user) job.locations.add(JobLocationFactory()) job.add_to_solr() guids = " OR ".join(job.guids()) query = "guid:%s" % guids self.assertEqual(self.ms_solr.search(query).hits, 1) def test_job_remove_from_solr(self): job = JobFactory(owner=self.company, created_by=self.user) locations = JobLocationFactory.create_batch(2) job.locations = locations job.save() job.remove_from_solr() guids = " OR ".join(job.guids()) query = "guid:%s" % guids self.assertEqual(self.ms_solr.search(query).hits, 0) def test_job_generate_guid(self): guid = '1'*32 # Confirm that pre-assigned guids are not being overwritten. location = JobLocationFactory(guid=guid) self.assertEqual(guid, location.guid) location.delete() # Confirm that if a guid isn't assigned one is getting assigned # to it properly. location = JobLocationFactory() self.assertIsNotNone(location.guid) self.assertNotEqual(location.guid, guid) def test_site_package_make_unique_for_site(self): package = SitePackage.objects.create(**self.site_package_data) package.make_unique_for_site(self.site) self.assertEqual(self.site.site_package, package) package.delete() package = SitePackage.objects.create(**self.site_package_data) for x in range(100, 110): site = SeoSiteFactory(id=x, domain="%s.jobs" % x) package.sites.add(site) # Site packages with existing sites associated with it should still # only end up with one associated site. site = SeoSiteFactory(id=4000, domain="4000.jobs") package.make_unique_for_site(site) self.assertEqual(site.site_package, package) def test_site_package_make_unique_for_company(self): package = SitePackage.objects.create(**self.site_package_data) package.make_unique_for_company(self.company) self.assertEqual(self.company.site_package, package) package.delete() package = SitePackage.objects.create(**self.site_package_data) first = [] for x in range(1000, 1003): site = SeoSiteFactory(id=x, domain="%s.jobs" % x) package.sites.add(site) first.append(site.pk) self.assertItemsEqual(package.sites.all().values_list('id', flat=True), first) second = [self.site.pk] for x in range(100, 103): site = SeoSiteFactory(id=x, domain="%s.jobs" % x) site.business_units.add(self.bu) site.save() second.append(site.pk) # Site packages with existing sites associated with it should # only end up with the sites for a company. package.make_unique_for_company(self.company) self.assertEqual(self.company.site_package, package) self.assertItemsEqual(package.sites.all().values_list('id', flat=True), second) def create_purchased_job(self, pk=None): if not hasattr(self, 'package'): self.package = SitePackageFactory(owner=self.company) if not hasattr(self, 'product'): self.product = ProductFactory( package=self.package, owner=self.company) if not hasattr(self, 'purchased_product'): self.purchased_product = PurchasedProductFactory( product=self.product, owner=self.company) exp_date = date.today() + timedelta(self.product.posting_window_length) self.assertEqual(self.purchased_product.expiration_date, exp_date) return PurchasedJobFactory( owner=self.company, created_by=self.user, purchased_product=self.purchased_product, pk=pk) def test_purchased_job_add(self): self.create_purchased_job() self.assertEqual(PurchasedJob.objects.all().count(), 1) self.assertEqual(SitePackage.objects.all().count(), 1) self.assertEqual(Request.objects.all().count(), 1) package = SitePackage.objects.get() job = PurchasedJob.objects.get() self.assertItemsEqual(job.site_packages.all(), [package]) def test_purchased_job_add_to_solr(self): job = self.create_purchased_job() job.locations.add(JobLocationFactory()) job.save() # Add to solr and delete from solr shouldn't be called until # the job is approved. guids = " OR ".join(job.guids()) query = "guid:%s" % guids self.assertEqual(self.ms_solr.search(query).hits, 0) job.is_approved = True # Jobs won't be added/deleted until it's confirmed that the # purchased product is paid for as well. job.purchased_product.paid = True job.purchased_product.save() job.save() # Now that the job is approved, it should've been sent to solr. guids = " OR ".join(job.guids()) query = "guid:%s" % guids self.assertEqual(self.ms_solr.search(query).hits, 1) def test_purchased_product_jobs_remaining(self): num_jobs_allowed = Product._meta.get_field_by_name('num_jobs_allowed') expected_num_jobs = num_jobs_allowed[0].default for x in range(50, 50 + expected_num_jobs): if hasattr(self, 'purchased_product'): self.assertTrue(self.purchased_product.can_post_more()) self.create_purchased_job() expected_num_jobs -= 1 self.assertEqual(self.purchased_product.jobs_remaining, expected_num_jobs) self.assertFalse(self.purchased_product.can_post_more()) def test_invoice_sent_to_specific_email(self): """ When `other_recipients` is specified, `send_invoice_email` should send invoices to those email addresses. """ self.create_purchased_job() group, _ = Group.objects.get_or_create(name=Product.ADMIN_GROUP_NAME) # No one to receive the email. self.purchased_product.invoice.send_invoice_email() self.assertEqual(len(mail.outbox), 0) # Only recipient is specified recipient. other_recipients = ['*****@*****.**', '*****@*****.**'] self.purchased_product.invoice.send_invoice_email( other_recipients=other_recipients) self.assertItemsEqual(mail.outbox[0].to, other_recipients) def test_invoice_sent_to_admins(self): """ When a company has postajob admins, invoices should be sent to each of them, unless the behavior is disabled by passing `send_to_admins=False`. """ self.create_purchased_job() group, _ = Group.objects.get_or_create(name=Product.ADMIN_GROUP_NAME) # No one to receive the email. self.purchased_product.invoice.send_invoice_email() self.assertEqual(len(mail.outbox), 0) # Only recipients are admins. role = RoleFactory(company=self.company) self.user.roles.add(role) self.user.groups.add(group) self.purchased_product.invoice.send_invoice_email() self.assertItemsEqual(mail.outbox[0].to, [u'*****@*****.**']) self.assertItemsEqual(mail.outbox[0].from_email, '*****@*****.**') self.purchased_product.invoice.send_invoice_email( send_to_admins=False, other_recipients=['*****@*****.**']) # Only one in .to should be [email protected] self.assertEquals(len(mail.outbox[1].to), 1) self.assertItemsEqual(mail.outbox[1].to, ['*****@*****.**']) def test_invoice_sent_to_admins_and_others(self): """ If a company has postajob admins and `other_recipients` is specified, emails should be sent to both groups. """ self.create_purchased_job() group, _ = Group.objects.get_or_create(name=Product.ADMIN_GROUP_NAME) # No one to receive the email. self.purchased_product.invoice.send_invoice_email() self.assertEqual(len(mail.outbox), 0) role = RoleFactory(company=self.company) self.user.roles.add(role) self.user.groups.add(group) self.site.email_domain = 'test.domain' self.site.save() # Recipients are admins + specified recipients. self.purchased_product.invoice.send_invoice_email( other_recipients=['*****@*****.**']) self.assertItemsEqual(mail.outbox[0].to, ['*****@*****.**', u'*****@*****.**']) self.assertEqual(mail.outbox[0].from_email, '*****@*****.**') def test_invoice_unchanged_after_purchased_product_deletion(self): self.create_purchased_job() invoice = self.purchased_product.invoice invoice.send_invoice_email(other_recipients=['*****@*****.**']) old_email = mail.outbox.pop() self.purchased_product.delete() invoice.send_invoice_email(other_recipients=['*****@*****.**']) new_email = mail.outbox.pop() self.assertEqual(old_email.body, new_email.body) def test_invoice_unchanged_after_product_changed(self): self.create_purchased_job() invoice = self.purchased_product.invoice invoice.send_invoice_email(other_recipients=['*****@*****.**']) old_email = mail.outbox.pop() self.purchased_product.product.name = 'new name' self.purchased_product.product.save() invoice.send_invoice_email(other_recipients=['*****@*****.**']) new_email = mail.outbox.pop() self.assertEqual(old_email.body, new_email.body) def test_productgrouping_add_delete(self): self.create_purchased_job() ProductGrouping.objects.create(display_title='Test Grouping', explanation='Test Grouping', name='Test Grouping', owner=self.company) self.assertEqual(ProductGrouping.objects.all().count(), 1) grouping = ProductGrouping.objects.get() order = ProductOrder.objects.create(product=self.product, group=grouping) grouping_products = grouping.products.all().values_list('pk', flat=True) self.assertItemsEqual(grouping_products, [order.product.pk]) grouping.delete() self.assertEqual(ProductGrouping.objects.all().count(), 0) self.assertEqual(ProductOrder.objects.all().count(), 0) def test_request_generation(self): role = RoleFactory(company=self.company) self.user.roles.add(role) self.user.make_purchased_microsite_admin() self.create_purchased_job() self.assertEqual(PurchasedJob.objects.all().count(), 1) self.assertEqual(Request.objects.all().count(), 1) self.assertEqual(len(mail.outbox), 1) self.assertItemsEqual(mail.outbox[0].from_email, '*****@*****.**') mail.outbox = [] # Already approved jobs should not generate an additional request. PurchasedJobFactory( owner=self.company, created_by=self.user, purchased_product=self.purchased_product, is_approved=True) self.assertEqual(PurchasedJob.objects.all().count(), 2) self.assertEqual(Request.objects.all().count(), 1) self.assertEqual(len(mail.outbox), 0) def test_offlinepurchase_create_purchased_products(self): role = RoleFactory(company=self.company) self.user.roles.add(role) offline_purchase = OfflinePurchaseFactory( owner=self.company, created_by=self.user) package = SitePackageFactory(owner=self.company) product = ProductFactory(package=package, owner=self.company) for x in range(1, 15): PurchasedProduct.objects.all().delete() OfflineProduct.objects.all().delete() OfflineProductFactory( product=product, offline_purchase=offline_purchase, product_quantity=x) offline_purchase.create_purchased_products(self.company) self.assertEqual(PurchasedProduct.objects.all().count(), x) product_two = ProductFactory(package=package, owner=self.company) for x in range(1, 15): PurchasedProduct.objects.all().delete() OfflineProduct.objects.all().delete() OfflineProductFactory(product=product, offline_purchase=offline_purchase, product_quantity=x) OfflineProductFactory(product=product_two, offline_purchase=offline_purchase, product_quantity=x) offline_purchase.create_purchased_products(self.company) self.assertEqual(PurchasedProduct.objects.all().count(), x*2) def test_offlinepurchase_filter_by_sites(self): role = RoleFactory(company=self.company) self.user.roles.add(role) for x in range(8800, 8815): domain = 'testsite-%s.jobs' % x site = SeoSiteFactory(id=x, domain=domain, name=domain) site_package = SitePackageFactory(owner=self.company) site_package.make_unique_for_site(site) product = ProductFactory(package=site_package, owner=self.company) for y in range(1, 5): purchase = OfflinePurchaseFactory(owner=self.company, created_by=self.user) OfflineProduct.objects.create(product=product, offline_purchase=purchase) count = OfflinePurchase.objects.filter_by_sites([site]).count() self.assertEqual(count, 4) self.assertEqual(OfflinePurchase.objects.all().count(), 60) def test_invoice_filter_by_sites(self): role = RoleFactory(company=self.company) self.user.roles.add(role) for x in range(8800, 8815): domain = 'testsite-%s.jobs' % x site = SeoSiteFactory(id=x, domain=domain, name=domain) site_package = SitePackageFactory(owner=self.company) site_package.make_unique_for_site(site) product = ProductFactory(package=site_package, owner=self.company) for y in range(1, 5): # OfflinePurchaseFactory() automatically creates the invoice. purchase = OfflinePurchaseFactory(owner=self.company, created_by=self.user) OfflineProduct.objects.create(product=product, offline_purchase=purchase) # Confirm it correctly picks up Invoices associated with # OfflinePurchases. self.assertEqual(Invoice.objects.filter_by_sites([site]).count(), 4) for y in range(1, 5): # PurchasedProductFactory() also automatically creates # the invoice. PurchasedProductFactory(product=product, owner=self.company) # Confirm it correctly picks up Invoices associated with # PurchasedProducts. self.assertEqual(Invoice.objects.filter_by_sites([site]).count(), 8) self.assertEqual(Invoice.objects.all().count(), 120) def test_product_filter_by_sites(self): for x in range(8800, 8815): domain = 'testsite-%s.jobs' % x site = SeoSiteFactory(id=x, domain=domain, name=domain) site_package = SitePackageFactory(owner=self.company) site_package.make_unique_for_site(site) for y in range(1, 5): ProductFactory(package=site_package, owner=self.company) self.assertEqual(Product.objects.filter_by_sites([site]).count(), 4) self.assertEqual(Product.objects.all().count(), 60) def test_request_filter_by_sites(self): for x in range(8800, 8815): domain = 'testsite-%s.jobs' % x site = SeoSiteFactory(id=x, domain=domain, name=domain) site_package = SitePackageFactory(owner=self.company) site_package.make_unique_for_site(site) product = ProductFactory(package=site_package, owner=self.company) purchased_product = PurchasedProductFactory(product=product, owner=self.company) for y in range(1, 5): # Unapproved purchased jobs should create Requests. PurchasedJobFactory(owner=self.company, created_by=self.user, purchased_product=purchased_product) self.assertEqual(Request.objects.filter_by_sites([site]).count(), 4) self.assertEqual(Request.objects.all().count(), 60) def test_purchasedproduct_filter_by_sites(self): for x in range(8800, 8815): domain = 'testsite-%s.jobs' % x site = SeoSiteFactory(id=x, domain=domain, name=domain) site_package = SitePackageFactory(owner=self.company) site_package.make_unique_for_site(site) for y in range(1, 5): product = ProductFactory(package=site_package, owner=self.company) PurchasedProductFactory(product=product, owner=self.company) count = PurchasedProduct.objects.filter_by_sites([site]).count() self.assertEqual(count, 4) self.assertEqual(PurchasedProduct.objects.all().count(), 60) def test_job_filter_by_sites(self): for x in range(8800, 8815): domain = 'testsite-%s.jobs' % x site = SeoSiteFactory(id=x, domain=domain, name=domain) site_package = SitePackageFactory(owner=self.company) site_package.make_unique_for_site(site) for y in range(1, 5): job = JobFactory(owner=self.company, created_by=self.user) job.site_packages.add(site_package) job.save() self.assertEqual(Job.objects.filter_by_sites([site]).count(), 4) self.assertEqual(Job.objects.all().count(), 60) def test_purchasedjob_filter_by_sites(self): for x in range(8800, 8815): domain = 'testsite-%s.jobs' % x site = SeoSiteFactory(id=x, domain=domain, name=domain) site_package = SitePackageFactory(owner=self.company) site_package.make_unique_for_site(site) product = ProductFactory(package=site_package, owner=self.company) purchased_product = PurchasedProductFactory(product=product, owner=self.company) for y in range(1, 5): job = PurchasedJobFactory(owner=self.company, created_by=self.user, purchased_product=purchased_product) job.site_packages.add(site_package) job.save() count = PurchasedJob.objects.filter_by_sites([site]).count() self.assertEqual(count, 4) self.assertEqual(PurchasedJob.objects.all().count(), 60) def test_productgrouping_filter_by_sites(self): for x in range(8800, 8815): domain = 'testsite-%s.jobs' % x site = SeoSiteFactory(id=x, domain=domain, name=domain) site_package = SitePackageFactory(owner=self.company) site_package.make_unique_for_site(site) product = ProductFactory(package=site_package, owner=self.company) for y in range(1, 5): grouping = ProductGroupingFactory(owner=self.company, display_order=y) ProductOrder.objects.create(product=product, group=grouping, display_order=y) count = ProductGrouping.objects.filter_by_sites([site]).count() self.assertEqual(count, 4) self.assertEqual(ProductGrouping.objects.all().count(), 60) def test_sitepackage_filter_by_sites(self): for x in range(8800, 8815): domain = 'testsite-%s.jobs' % x site = SeoSiteFactory(id=x, domain=domain, name=domain) for y in range(1, 5): site_package = SitePackageFactory(owner=self.company) site_package.sites.add(site) site_package.save() count = SitePackage.objects.filter_by_sites([site]).count() self.assertEqual(count, 4) count = Package.objects.filter_by_sites([site]).count() self.assertEqual(count, 4) self.assertEqual(SitePackage.objects.all().count(), 60) self.assertEqual(Package.objects.all().count(), 60) def test_product_filter_by_site_multiple_sites(self): site_in_both_packages = SeoSiteFactory(domain='secondsite.jobs', id=7) single_site_package = SitePackageFactory(owner=self.company) single_site_package.make_unique_for_site(site_in_both_packages) both_sites_package = SitePackageFactory(owner=self.company) both_sites_package.sites.add(site_in_both_packages) both_sites_package.sites.add(self.site) both_sites_package.save() ProductFactory(package=single_site_package, owner=self.company) ProductFactory(package=both_sites_package, owner=self.company) self.assertEqual(Product.objects.all().count(), 2) # Confirm that filtering by both sites gets both jobs. both_sites = [site_in_both_packages, self.site] count = Product.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) # Confirm that filtering by the site that only has one job only # gets one job. count = Product.objects.filter_by_sites([self.site]).count() self.assertEqual(count, 1) # Confirm that filtering by the site the site that has both jobs gets # both jobs. count = (Product .objects .filter_by_sites([site_in_both_packages]) .count()) self.assertEqual(count, 2) def test_job_filter_by_site_multiple_sites(self): site_in_both_packages = SeoSiteFactory(domain='secondsite.jobs', id=7) single_site_package = SitePackageFactory(owner=self.company) single_site_package.make_unique_for_site(site_in_both_packages) both_sites_package = SitePackageFactory(owner=self.company) both_sites_package.sites.add(site_in_both_packages) both_sites_package.sites.add(self.site) both_sites_package.save() job_on_both = JobFactory(owner=self.company, created_by=self.user) job_on_both.site_packages.add(both_sites_package) job_on_both.save() job_on_new_site = JobFactory(owner=self.company, created_by=self.user) job_on_new_site.site_packages.add(single_site_package) job_on_new_site.save() self.assertEqual(Job.objects.all().count(), 2) # Confirm that filtering by both sites gets both jobs. both_sites = [site_in_both_packages, self.site] count = Job.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) # Confirm that filtering by the site that only has one job only # gets one job. self.assertEqual(Job.objects.filter_by_sites([self.site]).count(), 1) # Confirm that filtering by the site the site that has both jobs gets # both jobs. count = Job.objects.filter_by_sites([site_in_both_packages]).count() self.assertEqual(count, 2) def test_purchasedproduct_filter_by_site_multiple_sites(self): site_in_both_packages = SeoSiteFactory(domain='secondsite.jobs', id=7) single_site_package = SitePackageFactory(owner=self.company) single_site_package.make_unique_for_site(site_in_both_packages) both_sites_package = SitePackageFactory(owner=self.company) both_sites_package.sites.add(site_in_both_packages) both_sites_package.sites.add(self.site) both_sites_package.save() single_site_product = ProductFactory(package=single_site_package, owner=self.company) PurchasedProductFactory(product=single_site_product, owner=self.company) both_sites_product = ProductFactory(package=both_sites_package, owner=self.company) PurchasedProductFactory(product=both_sites_product, owner=self.company) self.assertEqual(PurchasedProduct.objects.all().count(), 2) # Confirm that filtering by both sites gets both products. both_sites = [site_in_both_packages, self.site] count = PurchasedProduct.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) # Confirm that filtering by the site that only has one product only # gets one product. count = PurchasedProduct.objects.filter_by_sites([self.site]).count() self.assertEqual(count, 1) # Confirm that filtering by the site the site that has both products # gets both jobs. objs = (PurchasedProduct .objects .filter_by_sites([site_in_both_packages])) count = objs.count() self.assertEqual(count, 2) def test_purchasedjob_filter_by_site_multiple_sites(self): site_in_both_packages = SeoSiteFactory(domain='secondsite.jobs', id=7) single_site_package = SitePackageFactory(owner=self.company) single_site_package.make_unique_for_site(site_in_both_packages) both_sites_package = SitePackageFactory(owner=self.company) both_sites_package.sites.add(site_in_both_packages) both_sites_package.sites.add(self.site) both_sites_package.save() single_site_product = ProductFactory(package=single_site_package, owner=self.company) single_site_purchasedproduct = PurchasedProductFactory( product=single_site_product, owner=self.company) both_sites_product = ProductFactory(package=both_sites_package, owner=self.company) both_sites_purchasedproduct = PurchasedProductFactory( product=both_sites_product, owner=self.company ) PurchasedJobFactory(owner=self.company, created_by=self.user, purchased_product=single_site_purchasedproduct) PurchasedJobFactory(owner=self.company, created_by=self.user, purchased_product=both_sites_purchasedproduct) self.assertEqual(PurchasedJob.objects.all().count(), 2) # Confirm that filtering by both sites gets both jobs. both_sites = [site_in_both_packages, self.site] count = PurchasedJob.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) # Confirm that filtering by the site that only has one job only # gets one job. count = PurchasedJob.objects.filter_by_sites([self.site]).count() self.assertEqual(count, 1) # Confirm that filtering by the site the site that has both jobs gets # both jobs. objs = PurchasedJob.objects.filter_by_sites([site_in_both_packages]) count = objs.count() self.assertEqual(count, 2) def test_sitepackage_filter_by_site_multiple_sites(self): site_in_both_packages = SeoSiteFactory(domain='secondsite.jobs', id=7) single_site_package = SitePackageFactory(owner=self.company) single_site_package.make_unique_for_site(site_in_both_packages) both_sites_package = SitePackageFactory(owner=self.company) both_sites_package.sites.add(site_in_both_packages) both_sites_package.sites.add(self.site) both_sites_package.save() self.assertEqual(SitePackage.objects.all().count(), 2) self.assertEqual(Package.objects.all().count(), 2) # Confirm that filtering by both sites gets both packages. both_sites = [site_in_both_packages, self.site] count = SitePackage.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) count = Package.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) # Confirm that filtering by the site that only has one package only # gets one package. count = SitePackage.objects.filter_by_sites([self.site]).count() self.assertEqual(count, 1) count = Package.objects.filter_by_sites([self.site]).count() self.assertEqual(count, 1) # Confirm that filtering by the site the site that has both packages # gets both packages. objs = SitePackage.objects.filter_by_sites([site_in_both_packages]) count = objs.count() self.assertEqual(count, 2) count = (Package .objects .filter_by_sites([site_in_both_packages]) .count()) self.assertEqual(count, 2) def test_productgrouping_filter_by_site_multiple_sites(self): site_in_both_packages = SeoSiteFactory(domain='secondsite.jobs', id=7) single_site_package = SitePackageFactory(owner=self.company) single_site_package.make_unique_for_site(site_in_both_packages) both_sites_package = SitePackageFactory(owner=self.company) both_sites_package.sites.add(site_in_both_packages) both_sites_package.sites.add(self.site) both_sites_package.save() single_site_product = ProductFactory(package=single_site_package, owner=self.company) single_site_grouping = ProductGroupingFactory(owner=self.company) ProductOrder.objects.create(product=single_site_product, group=single_site_grouping) both_sites_product = ProductFactory(package=both_sites_package, owner=self.company) both_sites_grouping = ProductGroupingFactory(owner=self.company) ProductOrder.objects.create(product=both_sites_product, group=both_sites_grouping) # Confirm that filtering by both sites gets both groupings. both_sites = [site_in_both_packages, self.site] count = ProductGrouping.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) # Confirm that filtering by the site that only has one grouping only # gets one grouping. count = ProductGrouping.objects.filter_by_sites([self.site]).count() self.assertEqual(count, 1) # Confirm that filtering by the site the site that has both jobs gets # both jobs. objs = ProductGrouping.objects.filter_by_sites([site_in_both_packages]) count = objs.count() self.assertEqual(count, 2) def test_offlinepurchase_filter_by_site_multiple_sites(self): role = RoleFactory(company=self.company) self.user.roles.add(role) site_in_both_packages = SeoSiteFactory(domain='secondsite.jobs', id=7) single_site_package = SitePackageFactory(owner=self.company) single_site_package.make_unique_for_site(site_in_both_packages) both_sites_package = SitePackageFactory(owner=self.company) both_sites_package.sites.add(site_in_both_packages) both_sites_package.sites.add(self.site) both_sites_package.save() single_site_product = ProductFactory(package=single_site_package, owner=self.company) single_site_purchase = OfflinePurchaseFactory(owner=self.company, created_by=self.user) OfflineProduct.objects.create(product=single_site_product, offline_purchase=single_site_purchase) both_sites_product = ProductFactory(package=both_sites_package, owner=self.company) both_sites_purchase = OfflinePurchaseFactory(owner=self.company, created_by=self.user) OfflineProduct.objects.create(product=both_sites_product, offline_purchase=both_sites_purchase) # Confirm that filtering by both sites gets both groupings. both_sites = [site_in_both_packages, self.site] count = OfflinePurchase.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) # Confirm that filtering by the site that only has one grouping only # gets one grouping. count = OfflinePurchase.objects.filter_by_sites([self.site]).count() self.assertEqual(count, 1) # Confirm that filtering by the site the site that has both jobs gets # both jobs. objs = OfflinePurchase.objects.filter_by_sites([site_in_both_packages]) count = objs.count() self.assertEqual(count, 2) def test_invoice_from_offlinepurchase_filter_by_site_multiple_sites(self): role = RoleFactory(company=self.company) self.user.roles.add(role) site_in_both_packages = SeoSiteFactory(domain='secondsite.jobs', id=7) single_site_package = SitePackageFactory(owner=self.company) single_site_package.make_unique_for_site(site_in_both_packages) both_sites_package = SitePackageFactory(owner=self.company) both_sites_package.sites.add(site_in_both_packages) both_sites_package.sites.add(self.site) both_sites_package.save() single_site_product = ProductFactory(package=single_site_package, owner=self.company) single_site_purchase = OfflinePurchaseFactory(owner=self.company, created_by=self.user) OfflineProduct.objects.create(product=single_site_product, offline_purchase=single_site_purchase) both_sites_product = ProductFactory(package=both_sites_package, owner=self.company) both_sites_purchase = OfflinePurchaseFactory(owner=self.company, created_by=self.user) OfflineProduct.objects.create(product=both_sites_product, offline_purchase=both_sites_purchase) # Confirm that filtering by both sites gets both groupings. both_sites = [site_in_both_packages, self.site] count = Invoice.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) # Confirm that filtering by the site that only has one grouping only # gets one grouping. count = Invoice.objects.filter_by_sites([self.site]).count() self.assertEqual(count, 1) # Confirm that filtering by the site the site that has both jobs gets # both jobs. count = (Invoice .objects .filter_by_sites([site_in_both_packages]) .count()) self.assertEqual(count, 2) def test_invoice_from_purchasedproduct_filter_by_site_multiple_sites(self): site_in_both_packages = SeoSiteFactory(domain='secondsite.jobs', id=7) single_site_package = SitePackageFactory(owner=self.company) single_site_package.make_unique_for_site(site_in_both_packages) both_sites_package = SitePackageFactory(owner=self.company) both_sites_package.sites.add(site_in_both_packages) both_sites_package.sites.add(self.site) both_sites_package.save() single_site_product = ProductFactory(package=single_site_package, owner=self.company) PurchasedProductFactory(product=single_site_product, owner=self.company) both_sites_product = ProductFactory(package=both_sites_package, owner=self.company) PurchasedProductFactory(product=both_sites_product, owner=self.company) self.assertEqual(Invoice.objects.all().count(), 2) # Confirm that filtering by both sites gets both products. both_sites = [site_in_both_packages, self.site] count = Invoice.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) # Confirm that filtering by the site that only has one product only # gets one product. count = Invoice.objects.filter_by_sites([self.site]).count() self.assertEqual(count, 1) # Confirm that filtering by the site the site that has both products # gets both jobs. count = (Invoice .objects .filter_by_sites([site_in_both_packages]) .count()) self.assertEqual(count, 2) def test_request_filter_by_site_multiple_sites(self): site_in_both_packages = SeoSiteFactory(domain='secondsite.jobs', id=7) single_site_package = SitePackageFactory(owner=self.company) single_site_package.make_unique_for_site(site_in_both_packages) both_sites_package = SitePackageFactory(owner=self.company) both_sites_package.sites.add(site_in_both_packages) both_sites_package.sites.add(self.site) both_sites_package.save() single_site_product = ProductFactory(package=single_site_package, owner=self.company) single_site_purchasedproduct = PurchasedProductFactory( product=single_site_product, owner=self.company) # Unapproved PurchasedJobs generate Requests. PurchasedJobFactory(owner=self.company, created_by=self.user, purchased_product=single_site_purchasedproduct) both_sites_product = ProductFactory(package=both_sites_package, owner=self.company) both_sites_purchasedproduct = PurchasedProductFactory( product=both_sites_product, owner=self.company) PurchasedJobFactory(owner=self.company, created_by=self.user, purchased_product=both_sites_purchasedproduct) self.assertEqual(Request.objects.all().count(), 2) # Confirm that filtering by both sites gets both products. both_sites = [site_in_both_packages, self.site] count = Request.objects.filter_by_sites(both_sites).count() self.assertEqual(count, 2) # Confirm that filtering by the site that only has one product only # gets one product. count = Request.objects.filter_by_sites([self.site]).count() self.assertEqual(count, 1) # Confirm that filtering by the site the site that has both products # gets both jobs. count = (Request .objects .filter_by_sites([site_in_both_packages]) .count()) self.assertEqual(count, 2) def test_request_on_purchasedjob_update(self): # Creating a PurchasedJob for a Product that requires approval # creates a Request job = PurchasedJobFactory(owner=self.company, created_by=self.user) self.assertEqual(Request.objects.count(), 1) # Re-saving that job should not create a new request. job.save() self.assertEqual(Request.objects.count(), 1) # Approving the job should also not create a new request. job.is_approved = True job.save() self.assertEqual(Request.objects.count(), 1) # Marking the job as not approved and saving it should not create # a new request if no action was taken on the old request. job.is_approved = False job.save() self.assertEqual(Request.objects.count(), 1) # Marking the job as not approved and saving it should create # a new request if there is no pending request. request = Request.objects.get() request.action_taken = True request.save() job.save() self.assertEqual(Request.objects.count(), 2) def test_enable_posting(self): """ Attempting to automatically enable posting for a company should result in a valid site package owned by that company. """ AppAccessFactory(name='Posting') company = CompanyFactory(name='Posting Company') site = SeoSite.objects.create(domain='somewhere.jobs') # sanity checks self.assertIsNone(site.canonical_company) self.assertEqual(company.enabled_access, []) package = enable_posting(company, site) # Django caches model instances company = Company.objects.get(pk=company.pk) site = SeoSite.objects.get(pk=site.pk) self.assertEqual(site.canonical_company, company) self.assertIn("Posting", company.enabled_access) self.assertIn(site, package.sites.all()) self.assertTrue(LoginBlock.objects.filter( name="Posting Company Login Block").exists()) def test_enable_marketplace(self): """ Attempting to automatically enable posting for a company should result in a valid site package owned by that company. """ AppAccessFactory(name='MarketPlace') company = CompanyFactory(name='Marketplace Company') site = SeoSite.objects.create(domain='somewhereelse.jobs') # sanity checks self.assertIsNone(site.canonical_company) self.assertEqual(company.enabled_access, []) package = enable_marketplace(company, site) # Django caches model instances company = Company.objects.get(pk=company.pk) site = SeoSite.objects.get(pk=site.pk) self.assertEqual(site.canonical_company, company) self.assertIn("MarketPlace", company.enabled_access) self.assertIn(site, package.sites.all()) self.assertTrue(LoginBlock.objects.filter( name="Marketplace Company Login Block").exists()) def test_create_login_block(self): """ Ensures that a login block is createed with the correct associations and a valid template. """ company = CompanyFactory(name='Marketplace Company') site = SeoSite.objects.create(domain='somewhereelse.jobs') template = raw_base_template(LoginBlock) response = self.client.get("/login", follow=True) login_block = create_login_block(company, site) # validate that the correct template was assigned to the login block self.assertEqual(login_block.template, template)