def test_filter_company_hashtags(self): h_1 = factories.HashtagFactory(slug="a") h_2 = factories.HashtagFactory(slug="b") _ = factories.CompanyFactory(slug="a", current_revision__hashtags=[h_1]) company_2 = factories.CompanyFactory(slug="b", current_revision__hashtags=[h_2]) company_3 = factories.CompanyFactory( slug="c", status=choices.ModerationStatus.REJECTED.name) qs = selectors.get_companies() rv = selectors.filter_companies(qs, None, ["b"], None) assert rv.count() == 1 assert rv.first().slug == company_2.slug rv = selectors.filter_companies(qs, company_2.current_revision.name, None, None) assert rv.count() == 1 assert rv.first().slug == company_2.slug rv = selectors.filter_companies(qs, None, None, choices.ModerationStatus.REJECTED.name) assert rv.count() == 1 assert rv.first().slug == company_3.slug
def test_annotated_company_bug(self): """ sometimes annotate causes query to return duplicates """ profile = ProfileFactory() profile_2 = ProfileFactory() company = factories.CompanyFactory() _ = factories.CompanyFactory() services.company_clap(company=company, profile=profile) services.company_clap(company=company, profile=profile_2) n_annotated = annotations.annotate_comment_claps( selectors.get_companies(), profile_id=profile.id).count() n_companies = selectors.get_companies().count() assert n_annotated == n_companies
def test_company_list_sorting(self, client): for name, location in zip("cab", "yxz"): f.CompanyFactory( status=choices.ModerationStatus.APPROVED.name, current_revision__name=name, current_revision__location=location, ) url = "/community/companies/?sort=name" resp = client.get(url) assert resp.status_code == 200 assert ( resp.json()["results"][0]["currentRevision"]["name"] == "a" ) # lowest company name url = "/community/companies/?sort=name&reverse=1" assert client.get(url).json()["results"][0]["currentRevision"]["name"] == "c" url = "/community/companies/?sort=location" assert ( client.get(url).json()["results"][0]["currentRevision"]["location"] == "x" ) url = "/community/companies/?sort=location&reverse=1" assert ( client.get(url).json()["results"][0]["currentRevision"]["location"] == "z" )
def test_company_clap(self, auth_client): company = f.CompanyFactory() url = f"/community/companies/{company.slug}/clap" resp = auth_client.post(url) assert resp.status_code == 200 assert resp.content == b"1"
def test_get_companies_filter(self, auth_client): a = f.HashtagFactory(slug="a") b = f.HashtagFactory(slug="b") c = f.HashtagFactory(slug="c") status = choices.ModerationStatus.APPROVED.name f.CompanyFactory(status=status, current_revision__hashtags=[a]) f.CompanyFactory(status=status, current_revision__hashtags=[a, b]) f.CompanyFactory(status=status, current_revision__hashtags=[a, b, c]) resp = auth_client.get("/community/companies/?hashtags=a") assert resp.json()["count"] == 3 resp = auth_client.get("/community/companies/?hashtags=a,b") assert resp.json()["count"] == 2 resp = auth_client.get("/community/companies/?hashtags=a,b,c") assert resp.json()["count"] == 1
def test_create_revision(self): profile = ProfileFactory() company = factories.CompanyFactory() revision = services.create_revision( company=company, created_by=profile, name="X" ) assert revision.company == company assert revision.name == "X"
def test_company_clap(self): profile = ProfileFactory() company = factories.CompanyFactory() assert company.clappers.count() == 0 rv = services.company_clap(company=company, profile=profile) assert rv == 1 rv2 = services.company_clap(company=company, profile=profile) assert rv2 == 1 assert company.clappers.count() == 1
def test_annotated_company(self): profile_1 = ProfileFactory() profile_2 = ProfileFactory() company_1 = factories.CompanyFactory() company_2 = factories.CompanyFactory() assert company_1.clappers.count() == company_2.clappers.count() == 0 assert services.company_clap(company=company_1, profile=profile_1) == 1 assert services.company_clap(company=company_2, profile=profile_2) == 1 qs_anno_1 = annotations.annotate_company_claps( selectors.get_companies(), profile_id=profile_1.id) assert qs_anno_1.get(id=company_1.id).user_did_clap is True assert qs_anno_1.get(id=company_2.id).user_did_clap is False qs_anno_2 = annotations.annotate_company_claps( selectors.get_companies(), profile_id=profile_2.id) assert qs_anno_2.get(id=company_1.id).user_did_clap is False assert qs_anno_2.get(id=company_2.id).user_did_clap is True
def test_resolve_company(): c = f.CompanyFactory(slug="aec", current_revision__twitter="aec_works", current_revision__name="AEC") assert resolve_company("add http://x/a to @aec_works", "aec_works") == c assert resolve_company("add http://x/a to @Aec_Works", "Aec_Works") == c assert resolve_company("add http://x/a to @.aec", "") == c assert resolve_company("add http://x/a to @.Aec", "") == c assert not resolve_company("add http://x/a to @xxx", "") assert not resolve_company("add http://x/a to @xxx", "xxx")
def test_company_revision_apply(self, auth_client): company = f.CompanyFactory() rev = f.CompanyRevisionFactory(company=company) assert company.current_revision != rev url = f"/community/revisions/{rev.id}/apply" resp = auth_client.post(url) company.refresh_from_db() assert resp.status_code == 200 assert company.current_revision == rev
def test_company_revision_history(self, auth_client): company = f.CompanyFactory(current_revision__status="UNMODERATED") rev = f.CompanyRevisionFactory(company=company) f.CompanyRevisionHistoryFactory(revision=rev) url = f"/community/companies/{company.slug}/revision-history/" resp = auth_client.get(url) assert resp.status_code == 200 assert resp.json()[0]["revisionId"] == rev.id
def test_company_revision(self, auth_client): company = f.CompanyFactory() url = f"/community/companies/{company.slug}/revisions/" payload = { "name": "Name", "description": "x", "hashtags": ["x"], "website": "https://www.x.com", } resp = auth_client.post(url, payload, format="json") assert resp.status_code == 200
def test_company_moderate(self, auth_client): company = f.CompanyFactory(status="UNMODERATED") url = f"/community/companies/{company.slug}/moderation/" payload = {"status": "REJECTED"} resp_post = auth_client.post(url, payload, format="json") company.refresh_from_db() assert resp_post.status_code == 200 assert company.status == "REJECTED" resp_get = auth_client.get(url) assert resp_get.status_code == 200 assert resp_get.json()[0]["status"] == "REJECTED"
def test_can_create_company(self): user = UserFactory(groups=["editors"]) profile = ProfileFactory(user=user) assert services.can_create_company(profile) user2 = UserFactory(groups=[]) profile2 = ProfileFactory(user=user2) assert services.can_create_company(profile2) for _ in range(services.MAX_UNMODERATED_CHANGES): factories.CompanyFactory( created_by=profile2, status=choices.ModerationStatus.UNMODERATED.name ) assert not services.can_create_company(profile2)
def test_company_revision_moderate(self, auth_client): company = f.CompanyFactory(current_revision__status="UNMODERATED") rev = company.current_revision assert rev.status == "UNMODERATED" url = f"/community/revisions/{rev.id}/moderation/" TARGET_STATUS = "REJECTED" payload = {"status": TARGET_STATUS} resp_post = auth_client.post(url, payload, format="json") rev.refresh_from_db() assert resp_post.status_code == 200 assert rev.status == TARGET_STATUS resp_get = auth_client.get(url) assert resp_get.status_code == 200 assert resp_get.json()[0]["status"] == TARGET_STATUS
def test_post_webhook(m_get_og_data, auth_client, token_auth_client): company = f.CompanyFactory(current_revision__twitter="ABC") assert company.articles.count() == 0 m_get_og_data.return_value = {"key": "value"} URL = "https://www.latimes.com/entertainment-arts/story/2021-03-05/new-city-program-brings-high-design-concepts-to-granny-flat" payload = { "mentioned": "ABC", "text": "Add https://fake.com to @ABC https://fake.com", "url": URL, } resp = token_auth_client.post("/webhooks/twitter/", payload, format="json") assert resp.status_code == 201 company.refresh_from_db() assert company.articles.count() == 1 assert m_get_og_data.called assert company.articles.first().url == URL assert company.articles.first().opengraph_data == {"key": "value"}
class TestCommunityViews: @pytest.mark.parametrize( "path,param_factory", [ ["comments/{0}/", lambda: f.ThreadFactory().id], ["companies/", lambda: [f.CompanyFactory() for _ in range(3)]], ["companies/{0}/", lambda: f.CompanyFactory().slug], ["hashtags/", lambda: [f.HashtagFactory() for _ in range(3)]], ], ) def test_get_views_annonymous( self, auth_client, django_assert_max_num_queries, path, param_factory ): if param_factory: path = path.format(param_factory()) url = f"/community/{path}" with django_assert_max_num_queries(10): resp = auth_client.get(url) assert resp.status_code == 200 def test_get_companies_filter(self, auth_client): a = f.HashtagFactory(slug="a") b = f.HashtagFactory(slug="b") c = f.HashtagFactory(slug="c") status = choices.ModerationStatus.APPROVED.name f.CompanyFactory(status=status, current_revision__hashtags=[a]) f.CompanyFactory(status=status, current_revision__hashtags=[a, b]) f.CompanyFactory(status=status, current_revision__hashtags=[a, b, c]) resp = auth_client.get("/community/companies/?hashtags=a") assert resp.json()["count"] == 3 resp = auth_client.get("/community/companies/?hashtags=a,b") assert resp.json()["count"] == 2 resp = auth_client.get("/community/companies/?hashtags=a,b,c") assert resp.json()["count"] == 1 def test_company_create(self, auth_client): logo = ImageAssetFactory() url = "/community/companies/" payload = { "name": "Test 1", "description": "asdasd", "location": "São Paulo, Brazil", "website": "https://aec.works", "twitter": "xxx", "crunchbaseId": "zzz", "logo": logo.id, "cover": logo.id, "hashtags": ["A", "B"], } resp = auth_client.post(url, payload, format="json") assert resp.status_code == 200 company = resp.json() rev = company["currentRevision"] assert "A" in rev["hashtags"] assert "B" in rev["hashtags"] assert payload["website"] == rev["website"] assert payload["description"] == rev["description"] assert payload["twitter"] == rev["twitter"] assert payload["crunchbaseId"] == rev["crunchbaseId"] assert payload["logo"] == logo.id assert payload["cover"] == logo.id assert rev["logoUrl"] assert rev["coverUrl"] def test_company_revision(self, auth_client): company = f.CompanyFactory() url = f"/community/companies/{company.slug}/revisions/" payload = { "name": "Name", "description": "x", "hashtags": ["x"], "website": "https://www.x.com", } resp = auth_client.post(url, payload, format="json") assert resp.status_code == 200 def test_company_revision_apply(self, auth_client): company = f.CompanyFactory() rev = f.CompanyRevisionFactory(company=company) assert company.current_revision != rev url = f"/community/revisions/{rev.id}/apply" resp = auth_client.post(url) company.refresh_from_db() assert resp.status_code == 200 assert company.current_revision == rev def test_company_moderate(self, auth_client): company = f.CompanyFactory(status="UNMODERATED") url = f"/community/companies/{company.slug}/moderation/" payload = {"status": "REJECTED"} resp_post = auth_client.post(url, payload, format="json") company.refresh_from_db() assert resp_post.status_code == 200 assert company.status == "REJECTED" resp_get = auth_client.get(url) assert resp_get.status_code == 200 assert resp_get.json()[0]["status"] == "REJECTED" def test_company_revision_moderate(self, auth_client): company = f.CompanyFactory(current_revision__status="UNMODERATED") rev = company.current_revision assert rev.status == "UNMODERATED" url = f"/community/revisions/{rev.id}/moderation/" TARGET_STATUS = "REJECTED" payload = {"status": TARGET_STATUS} resp_post = auth_client.post(url, payload, format="json") rev.refresh_from_db() assert resp_post.status_code == 200 assert rev.status == TARGET_STATUS resp_get = auth_client.get(url) assert resp_get.status_code == 200 assert resp_get.json()[0]["status"] == TARGET_STATUS def test_company_revision_history(self, auth_client): company = f.CompanyFactory(current_revision__status="UNMODERATED") rev = f.CompanyRevisionFactory(company=company) f.CompanyRevisionHistoryFactory(revision=rev) url = f"/community/companies/{company.slug}/revision-history/" resp = auth_client.get(url) assert resp.status_code == 200 assert resp.json()[0]["revisionId"] == rev.id def test_company_clap(self, auth_client): company = f.CompanyFactory() url = f"/community/companies/{company.slug}/clap" resp = auth_client.post(url) assert resp.status_code == 200 assert resp.content == b"1" def test_comment_clap(self, auth_client): comment = f.CommentFactory() url = f"/community/comments/{comment.id}/clap" resp = auth_client.post(url) assert resp.status_code == 200 assert resp.content == b"1" def test_company_list_sorting(self, client): for name, location in zip("cab", "yxz"): f.CompanyFactory( status=choices.ModerationStatus.APPROVED.name, current_revision__name=name, current_revision__location=location, ) url = "/community/companies/?sort=name" resp = client.get(url) assert resp.status_code == 200 assert ( resp.json()["results"][0]["currentRevision"]["name"] == "a" ) # lowest company name url = "/community/companies/?sort=name&reverse=1" assert client.get(url).json()["results"][0]["currentRevision"]["name"] == "c" url = "/community/companies/?sort=location" assert ( client.get(url).json()["results"][0]["currentRevision"]["location"] == "x" ) url = "/community/companies/?sort=location&reverse=1" assert ( client.get(url).json()["results"][0]["currentRevision"]["location"] == "z" ) def test_profile_company_claps(self, auth_client): profile = ProfileFactory() co = f.CompanyFactory(current_revision__name="X") services.company_clap(company=co, profile=profile) resp = auth_client.get(f"/community/companies/claps/{profile.slug}/") assert resp.status_code == 200
def test_profile_company_claps(self, auth_client): profile = ProfileFactory() co = f.CompanyFactory(current_revision__name="X") services.company_clap(company=co, profile=profile) resp = auth_client.get(f"/community/companies/claps/{profile.slug}/") assert resp.status_code == 200