def test_metadata_filter(self): election = ElectionFactory(group=None, poll_open_date=datetime.today()) resp = self.client.get("/api/elections/?metadata=1") data = resp.json() assert data['count'] == 0 metadata = MetaData.objects.create( description="just a test", data=""" { "2018-05-03-id-pilot": { "title": "You need to show ID to vote at this election", "url": "https://www.woking.gov.uk/voterid", "detail": " Voters in Woking will be required to show photo ID before they can vote. " } } """, ) election.metadata = metadata election.save() resp = self.client.get("/api/elections/?metadata=1") data = resp.json() assert data['count'] == 1
def test_public_private_filter_complex(self): # set up 2 ballot objects e1 = ElectionFactory(group=None) e2 = ElectionFactory(group=None) # to start off with they're both 'suggested' ModerationHistoryFactory( election=e1, status=ModerationStatusFactory(short_label="Suggested")) ModerationHistoryFactory( election=e2, status=ModerationStatusFactory(short_label="Suggested")) self.assertEqual(0, Election.public_objects.count()) self.assertEqual(2, Election.private_objects.count()) # approve one of them ModerationHistoryFactory( election=e1, status=ModerationStatusFactory(short_label="Approved")) self.assertEqual(1, Election.public_objects.count()) self.assertEqual(e1.election_id, Election.public_objects.all()[0].election_id) self.assertEqual(2, Election.private_objects.count()) # and then delete it again ModerationHistoryFactory( election=e1, status=ModerationStatusFactory(short_label="Deleted")) self.assertEqual(0, Election.public_objects.count()) self.assertEqual(2, Election.private_objects.count())
def test_election_endpoint_for_postcode_cors(self): election_id = "local.place-name.2017-03-23" ElectionFactory(group=None, election_id=election_id) ElectionFactory(group=None, division_geography=None) url = "/api/elections/?postcode=SW1A1AA" resp = self.client.get(url, HTTP_ORIGIN='foo.bar/baz') self.assertEqual(resp.get('Access-Control-Allow-Origin'), '*')
def test_current_elections_for_postcode(self): ElectionFactory(group=None, poll_open_date=datetime.today()) ElectionFactory( group=None, poll_open_date=datetime.today(), geography=None) ElectionFactory( group=None, poll_open_date=datetime.today() - timedelta(days=60) ) assert Election.objects.current().for_postcode('SW1A1AA').count() == 1
def test_election_endpoint_for_postcode_jsonp(self): election_id = "local.place-name.2017-03-23" ElectionFactory(group=None, election_id=election_id) ElectionFactory(group=None, geography=None) url = "/api/elections/?postcode=SW1A1AA" + \ "&format=jsonp&callback=a_callback_string" resp = self.client.get(url) assert resp.content.decode('utf8').startswith("a_callback_string(")
def test_election_endpoint_for_bad_postcode(self): election_id = "local.place-name.2017-03-23" ElectionFactory(group=None, election_id=election_id) ElectionFactory(group=None, geography=None) resp = self.client.get("/api/elections/?postcode=SW1A1AX") data = resp.json() assert data['detail'] == "Invalid postcode"
def test_election_endpoint_for_postcode(self): election_id = "local.place-name.2017-03-23" ElectionFactory(group=None, election_id=election_id) ElectionFactory(group=None, geography=None) resp = self.client.get("/api/elections/?postcode=SW1A1AA") data = resp.json() assert len(data['results']) == 1 assert data['results'][0]['election_id'] == election_id
def test_election_endpoint_for_lat_lng(self): election_id = "local.place-name.2017-03-23" ElectionFactory(group=None, election_id=election_id) ElectionFactory(group=None, geography=None) resp = self.client.get( "/api/elections/?coords=51.5010089365,-0.141587600123") data = resp.json() assert data['results'][0]['election_id'] == election_id assert len(data['results']) == 1
def test_election_endpoint_current(self): ElectionFactory(group=None, poll_open_date=datetime.today()) ElectionFactory(group=None, poll_open_date=datetime.today() - timedelta(days=60)) resp = self.client.get("/api/elections/?current") data = resp.json() assert len(data['results']) == 1 assert data['results'][0]['election_id'] == \ "local.place-name-1.2017-03-23" assert data['results'][0]['current'] == True
def test_election_endpoint_current(self): id_current = ElectionFactory( group=None, poll_open_date=datetime.today()).election_id id_future = ElectionFactory( # noqa group=None, poll_open_date=datetime.today() - timedelta(days=60)).election_id resp = self.client.get("/api/elections/?current") data = resp.json() assert len(data['results']) == 1 assert data['results'][0]['election_id'] == id_current assert data['results'][0]['current'] == True
def setUp(self): self.election = ElectionFactory( name="City of London Corporation local election", election_date="2017-03-23", slug="local.city-of-london.2017-03-23", ) self.post = PostFactory(ynr_id="LBW:E05009288", label="Aldersgate", elections=self.election) PostElection.objects.get_or_create( election=self.election, post=self.post, ballot_paper_id="local.city-of-london.aldersgate.2017-03-23", )
def test_election_endpoint_future(self): ElectionFactory( group=None, poll_open_date=datetime.today(), election_id="local.place-name-future-election.2017-03-23") ElectionFactory(group=None, poll_open_date=datetime.today() - timedelta(days=1)) resp = self.client.get("/api/elections/?future") data = resp.json() assert len(data['results']) == 1 assert data['results'][0]['election_id'] == \ "local.place-name-future-election.2017-03-23"
def test_more_than_one_election(self): person = PersonFactory() election1 = ElectionFactory() election2 = ElectionFactory(slug="parl.2010") post2 = PostFactory(ynr_id="WMC:E14000645", label="Southwark", elections=election2) person.elections.add(election1) person.elections.add(election2) PersonPostFactory(person=person, election=election1) PersonPostFactory(person=person, elections=election2, post=post2) assert person.posts.all().count() == 2
def test_current_elections(self): # This is implicetly current ElectionFactory(group=None, poll_open_date=datetime.today()) # This is implicetly not current ElectionFactory(group=None, poll_open_date=datetime.today() - timedelta(days=60)) # This is implicetly not current, but current manually set ElectionFactory(group=None, poll_open_date=datetime.today() - timedelta(days=60), current=True) # This is implicetly current, current manually set to False ElectionFactory(group=None, poll_open_date=datetime.today() - timedelta(days=1), current=False) assert Election.objects.current().count() == 2
def test_ical_view(self): election = ElectionFactory(slug="local.cambridgeshire.2017-05-04") post = PostFactory(ynr_id="CED:romsey", label="Romsey") PostElectionFactory(post=post, election=election) response = self.client.get("/elections/CB13HU.ics", follow=True) self.assertEqual(response.status_code, 200)
def setUp(self): self.election = ElectionFactory( name="City of London Corporation local election", election_date="2017-03-23", slug="local.city-of-london.2017-03-23", ) self.post = PostFactory(ynr_id="LBW:E05009288", label="Aldersgate")
def test_party_detail_candidate_count_view(self): # Make a 2nd candidate pe = PostElectionFactory( election=ElectionFactory(slug="2010", name="2010 GE"), post=self.post, ) PersonPostFactory( post_election=pe, party=self.party, election=pe.election, post=self.post, ) p2 = PersonFactory(name="Test 3") PersonPostFactory( post_election=self.pe, person=p2, post=self.post, election=self.election, party=self.party, ) # TODO Use reverse here response = self.client.get("/parties/{}/london".format( self.party.party_id), follow=True) self.assertEqual(response.status_code, 200) self.assertContains(response, "3 candidates") x = response.context_data["object"] assert len(x.personpost_set.all().counts_by_post()) == 2
def setUp(self): cache.clear() self.election = ElectionFactory( name="2017 General Election", election_date="2017-06-08", slug="parl.2017-06-08", any_non_by_elections=True, ) self.post = PostFactory(ynr_id="WMC:E14000639", label="Cities of London and Westminster") self.post_election = PostElectionFactory( ballot_paper_id="parl.cities-of-london-and-westminster.2017-06-08", post=self.post, election=self.election, ) PersonFactory.reset_sequence() person = PersonFactory() pe = PostElectionFactory(election=self.election, post=self.post) PersonPostFactory( post_election=pe, election=self.election, person=person, post=self.post, party=PartyFactory(), ) self.expected_response = [{ "ballot_paper_id": "parl.cities-of-london-and-westminster.2017-06-08", "absolute_url": "http://testserver/elections/parl.cities-of-london-and-westminster.2017-06-08/cities-of-london-and-westminster/", "election_date": "2017-06-08", "election_id": "parl.2017-06-08", "election_name": "2017 General Election", "post": { "post_name": "Cities of London and Westminster", "post_slug": "WMC:E14000639", }, "cancelled": False, "replaced_by": None, "ballot_locked": False, "candidates": [{ "list_position": None, "party": { "party_id": "PP01", "party_name": "Test Party", }, "person": { "absolute_url": "http://testserver/person/0/candidate-0", "ynr_id": 0, "name": "Candidate 0", }, }], }]
class ElectionViewTests(TestCase): def setUp(self): self.election = ElectionFactory( name="City of London Corporation local election", election_date="2017-03-23", slug="local.city-of-london.2017-03-23", ) self.post = PostFactory( ynr_id="LBW:E05009288", label="Aldersgate", elections=self.election ) PostElection.objects.get_or_create( election=self.election, post=self.post, ballot_paper_id="local.city-of-london.aldersgate.2017-03-23", ) def test_election_list_view(self): # TODO Use reverse here with self.assertNumQueries(2): response = self.client.get("/elections/", follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "elections/elections_view.html") self.assertContains(response, self.election.nice_election_name) def test_election_detail_view(self): response = self.client.get( self.election.get_absolute_url(), follow=True ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "elections/election_view.html") self.assertContains(response, self.election.nice_election_name)
def test_election_endpoint(self): id = ElectionFactory(group=None).election_id resp = self.client.get("/api/elections/") data = resp.json() assert len(data['results']) == 1 assert data['results'][0]['election_id'] == id
class ElectionViewTests(TestCase): def setUp(self): self.election = ElectionFactory( name="City of London Corporation local election", election_date="2017-03-23", slug="local.city-of-london.2017-03-23", ) self.post = PostFactory(ynr_id="LBW:E05009288", label="Aldersgate", elections=self.election) PostElection.objects.get_or_create( election=self.election, post=self.post, ballot_paper_id="local.city-of-london.aldersgate.2017-03-23", ) def test_election_list_view(self): # TODO Use reverse here with self.assertNumQueries(2): response = self.client.get("/elections/", follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "elections/elections_view.html") self.assertContains(response, self.election.nice_election_name) def test_election_detail_view(self): response = self.client.get(self.election.get_absolute_url(), follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "elections/election_view.html") self.assertContains(response, self.election.nice_election_name)
def test_election_in_past_listed(self): response = self.client.get(self.person_url, follow=True) election_name = "FooBar Election 2017" self.assertNotContains(response, election_name) election = ElectionFactory( name=election_name, current=False, election_date="2017-01-01", slug="local.foobar.2017-01-01", ) post = PostFactory() pe = PostElectionFactory( election=election, post=post, ballot_paper_id="local.foo.bar.2017-01-01", ) PersonPostFactory( post_election=pe, election=election, person=self.person, party=self.party, ) response = self.client.get(self.person_url, follow=True) self.assertContains(response, election_name) self.assertContains(response, "was a")
def setUp(self): self.importer = YNRBallotImporter() election = ElectionFactory(name="Adur local election") self.post = PostFactory(label="Adur local election") self.post.elections.add(election) self.orphan_post = PostFactory(label="Adur local election", ynr_id="foo")
def test_youtube(self): self.person.youtube_profile = "Mary123" self.person.save() PersonPostFactory(person=self.person, election=ElectionFactory()) response = self.client.get(self.person_url, follow=True) self.assertEqual(response.template_name, ["people/person_detail.html"]) self.assertContains(response, "YouTube")
def test_email(self): self.person.email = "*****@*****.**" self.person.save() PersonPostFactory(person=self.person, election=ElectionFactory()) response = self.client.get(self.person_url, follow=True) self.assertEqual(response.template_name, ["people/person_detail.html"]) self.assertContains(response, "Email")
def test_instagram(self): self.person.instagram_url = "https://www.instagram.com/yo" self.person.save() PersonPostFactory(person=self.person, election=ElectionFactory()) response = self.client.get(self.person_url, follow=True) self.assertEqual(response.template_name, ["people/person_detail.html"]) self.assertContains(response, "Instagram")
def test_TWFY(self): self.person.twfy_id = 123 self.person.save() PersonPostFactory(person=self.person, election=ElectionFactory()) response = self.client.get(self.person_url, follow=True) self.assertEqual(response.template_name, ["people/person_detail.html"]) self.assertContains(response, "Record in office")
def test_statement_to_voters(self): self.person.statement_to_voters = "I believe in equal rights." self.person.save() PersonPostFactory(person=self.person, election=ElectionFactory()) response = self.client.get(self.person_url, follow=True) self.assertEqual(response.template_name, ["people/person_detail.html"]) self.assertContains(response, "Statement to voters")
def test_party_page(self): self.person.party_ppc_page_url = "https://www.voteforme.com/bob" self.person.save() PersonPostFactory(person=self.person, election=ElectionFactory()) response = self.client.get(self.person_url, follow=True) self.assertEqual(response.template_name, ["people/person_detail.html"]) self.assertContains(response, "The party's candidate page for this person")
def test_current_person_view(self): self.personpost = PersonPostFactory(person=self.person, election=ElectionFactory()) response = self.client.get(self.person_url, follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "people/person_detail.html") self.assertNotContains(response, '<meta name="robots" content="noindex">')
def test_facebook(self): self.person.facebook_personal_url = "https//www.facebook.com/yo" self.person.facebook_page_url = "https//www.facebook.com/yo" self.person.save() PersonPostFactory(person=self.person, election=ElectionFactory()) response = self.client.get(self.person_url, follow=True) self.assertEqual(response.template_name, ["people/person_detail.html"]) self.assertContains(response, "yo")
def setUp(self): self.election = ElectionFactory( name="City of London Corporation local election", election_date="2017-03-23", slug="local.city-of-london.2017-03-23", ) self.post = PostFactory( ynr_id="LBW:E05009288", label="Aldersgate", elections=self.election ) PostElection.objects.get_or_create( election=self.election, post=self.post, ballot_paper_id="local.city-of-london.aldersgate.2017-03-23", )