def test_valid_postcode_returns_multiple_areas(self, mock_requests): mock_requests.get.side_effect = fake_requests_for_every_election self._setup_extra_posts() response = self.app.get('/') form = response.forms['form-postcode'] form['q'] = 'SE24 0AG' response = form.submit() self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual(split_location.path, '/') self.assertEqual(split_location.query, 'q=SE24%200AG') response = self.app.get(response.location) split_location = urlsplit(response.location) self.assertEqual( split_location.path, '/postcode/SE24%200AG/', ) response = response.follow() self.assertEqual(len(response.context['pees']), 3) self.assertContains(response, "2016 London Assembly Election (Additional)") self.assertContains(response, "2016 London Assembly Election (Constituencies)") self.assertContains(response, "2015 General Election")
def test_valid_postcode_redirects_to_multiple_areas(self, mock_requests): mock_requests.get.side_effect = fake_requests_for_every_election # Create some extra posts and areas: london_assembly = ParliamentaryChamberExtraFactory.create( slug='london-assembly', base__name='London Assembly') lac_area_type = AreaTypeFactory.create(name='LAC') gla_area_type = AreaTypeFactory.create(name='GLA') area_extra_lac = AreaExtraFactory.create( base__identifier='gss:E32000010', base__name="Dulwich and West Norwood", type=lac_area_type, ) area_extra_gla = AreaExtraFactory.create( base__identifier='unit_id:41441', base__name='Greater London Authority', type=gla_area_type, ) election_lac = ElectionFactory.create( slug='gb-gla-2016-05-05-c', organization=london_assembly.base, name='2016 London Assembly Election (Constituencies)', area_types=(lac_area_type, ), ) election_gla = ElectionFactory.create( slug='gb-gla-2016-05-05-a', organization=london_assembly.base, name='2016 London Assembly Election (Additional)', area_types=(gla_area_type, ), ) PostExtraFactory.create( elections=(election_lac, ), base__area=area_extra_lac.base, base__organization=london_assembly.base, slug='11822', base__label='Assembly Member for Lambeth and Southwark', ) PostExtraFactory.create( elections=(election_gla, ), base__area=area_extra_gla.base, base__organization=london_assembly.base, slug='2247', base__label='2016 London Assembly Election (Additional)', ) # ---------------------------- response = self.app.get('/') form = response.forms['form-postcode'] form['q'] = 'SE24 0AG' response = form.submit() self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual(split_location.path, '/') self.assertEqual(split_location.query, 'q=SE24%200AG') response = self.app.get(response.location) split_location = urlsplit(response.location) self.assertEqual( split_location.path, '/areas/GLA--unit_id:41441,LAC--gss:E32000010,WMC--gss:E14000673', )
def test_fields_are_saved_when_creating(self): response = self.app.get('/election/2015/person/create/', user=self.user) form = response.forms['new-candidate-form'] form['name'] = 'Naomi Newperson' form['cv'] = 'http://example.org/another-cv' form['profession'] = 'Longshoreman' form['standing_2015'] = 'not-standing' form['reelection'] = 'yes' form['source'] = 'Test creating someone with additional fields' submission_response = form.submit() self.assertEqual(submission_response.status_code, 302) split_location = urlsplit(submission_response.location) m = re.search(r'^/person/(.*)', split_location.path) self.assertTrue(m) person = Person.objects.get(id=m.group(1)) self.assertEqual( PersonExtraFieldValue.objects.get(person=person, field__key='cv').value, 'http://example.org/another-cv') self.assertEqual( PersonExtraFieldValue.objects.get(person=person, field__key='profession').value, 'Longshoreman') self.assertEqual( PersonExtraFieldValue.objects.get(person=person, field__key='reelection').value, 'yes')
def test_photo_review_upload_rejected_privileged_no_uploading_user( self, mock_send_mail ): with self.settings(SITE_ID=1): review_url = reverse( 'photo-review', kwargs={'queued_image_id': self.q_no_uploading_user.id} ) review_page_response = self.app.get( review_url, user=self.test_reviewer ) form = review_page_response.forms['photo-review-form'] form['decision'] = 'rejected' response = form.submit(user=self.test_reviewer) self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual('/moderation/photo/review', split_location.path) las = LoggedAction.objects.all() self.assertEqual(1, len(las)) la = las[0] self.assertEqual(la.user.username, 'jane') self.assertEqual(la.action_type, 'photo-reject') self.assertEqual(la.person.id, 2009) self.assertEqual(la.source, 'Rejected a photo upload from a script') self.assertFalse(mock_send_mail.called) self.assertEqual( QueuedImage.objects.get( pk=self.q_no_uploading_user.id).decision, 'rejected')
def test_photo_review_upload_ignore_privileged( self, mock_send_mail ): review_url = reverse( 'photo-review', kwargs={'queued_image_id': self.q1.id} ) review_page_response = self.app.get( review_url, user=self.test_reviewer ) form = review_page_response.forms['photo-review-form'] form['decision'] = 'ignore' response = form.submit(user=self.test_reviewer) self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual('/moderation/photo/review', split_location.path) self.assertEqual(mock_send_mail.call_count, 0) self.assertEqual(QueuedImage.objects.get(pk=self.q1.id).decision, 'ignore') las = LoggedAction.objects.all() self.assertEqual(1, len(las)) la = las[0] self.assertEqual(la.user.username, 'jane') self.assertEqual(la.action_type, 'photo-ignore') self.assertEqual(la.person.id, 2009)
def test_all_steps_with_party_list_position_successful(self): self.election.party_lists_in_use = True self.election.save() # Now go through all the steps, but set a party list position # too: # Select the election: response = self.app.get(self.wizard_url, user=self.user) election_form = response.forms['add-candidacy-wizard'] election_form['election-election'].select(text='2015 General Election') response = election_form.submit() # Select the post: post_form = response.forms['add-candidacy-wizard'] post_form['post-post'].select(text='Dulwich and West Norwood') response = post_form.submit() # Select the party: party_form = response.forms['add-candidacy-wizard'] party_form['party-party'].select(text='Labour Party') party_form['party-party_list_position'] = 3 response = party_form.submit() # Explain the source: source_form = response.forms['add-candidacy-wizard'] source_form['source-source'] = 'Testing adding a candidacy' response = source_form.submit() # Now check we're redirected back to the person edit page: self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual(split_location.path, '/person/1234/update') # Now check that that person has a new candidacy: m = Membership.objects.select_related('extra', 'on_behalf_of').get( person=self.person, role='Candidate') self.assertEqual(m.extra.party_list_position, 3)
def test_photo_upload(self): queued_images = QueuedImage.objects.all() initial_count = queued_images.count() upload_form_url = reverse('photo-upload', kwargs={'person_id': '2009'}) form_page_response = self.app.get(upload_form_url, user=self.test_upload_user) form = form_page_response.forms['person-upload-photo'] with open(self.example_image_filename, 'rb') as f: form['image'] = Upload('pilot.jpg', f.read()) form['why_allowed'] = 'copyright-assigned' form['justification_for_use'] = 'I took this photo' upload_response = form.submit() self.assertEqual(upload_response.status_code, 302) split_location = urlsplit(upload_response.location) self.assertEqual('/moderation/photo/upload/2009/success', split_location.path) queued_images = QueuedImage.objects.all() self.assertEqual(initial_count + 1, queued_images.count()) queued_image = queued_images.last() self.assertEqual(queued_image.decision, 'undecided') self.assertEqual(queued_image.why_allowed, 'copyright-assigned') self.assertEqual(queued_image.justification_for_use, 'I took this photo') self.assertEqual(queued_image.person.id, 2009) self.assertEqual(queued_image.user, self.test_upload_user)
def test_photo_review_queue_view_not_logged_in(self): queue_url = reverse('photo-review-list') response = self.app.get(queue_url) self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual('/accounts/login/', split_location.path) self.assertEqual('next=/moderation/photo/review', split_location.query)
def test_update_person_submission(self): memberships_before = membership_id_set(Person.objects.get(pk=2009)) response = self.app.get( "/person/2009/update", user=self.user_who_can_lock ) form = response.forms["person-details"] form[ "tmp_person_identifiers-0-value" ] = "http://en.wikipedia.org/wiki/Tessa_Jowell" form["tmp_person_identifiers-0-value_type"] = "wikipedia_url" form["party_GB_parl.2015-05-07"] = self.labour_party.ec_id form["source"] = "Some source of this information" submission_response = form.submit() person = Person.objects.get(id="2009") party = person.memberships.all() self.assertEqual(party.count(), 1) self.assertEqual(party[0].party.legacy_slug, "party:53") self.assertEqual(party[0].party.ec_id, "PP53") person_identifiers = person.tmp_person_identifiers.all() self.assertEqual(person_identifiers.count(), 1) self.assertEqual( person_identifiers[0].value, "http://en.wikipedia.org/wiki/Tessa_Jowell", ) # It should redirect back to the same person's page: split_location = urlsplit(submission_response.location) self.assertEqual("/person/2009", split_location.path) self.assertEqual(memberships_before, membership_id_set(person))
def test_photo_review_upload_rejected_privileged(self, mock_send_mail): with self.settings(SITE_ID=self.site.id): review_url = reverse('photo-review', kwargs={'queued_image_id': self.q1.id}) review_page_response = self.app.get(review_url, user=self.test_reviewer) form = review_page_response.forms['photo-review-form'] form['decision'] = 'rejected' form[ 'rejection_reason'] = 'There\'s no clear source or copyright statement' response = form.submit(user=self.test_reviewer) self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual('/moderation/photo/review', split_location.path) las = LoggedAction.objects.all() self.assertEqual(1, len(las)) la = las[0] self.assertEqual(la.user.username, 'jane') self.assertEqual(la.action_type, 'photo-reject') self.assertEqual(la.person.id, 2009) self.assertEqual(la.source, 'Rejected a photo upload from john') mock_send_mail.assert_called_once_with( 'YNR image moderation results', "Thank-you for uploading a photo of Tessa Jowell to YNR, but\nunfortunately we can't use that image because:\n\n There\'s no clear source or copyright statement\n\nYou can just reply to this email if you want to discuss that\nfurther, or you can try uploading a photo with a different\nreason or justification for its use using this link:\n\n http://localhost:80/moderation/photo/upload/2009\n\nMany thanks from the YNR volunteers\n\n-- \nFor administrators' use: http://localhost:80/moderation/photo/review/{0}\n" .format(self.q1.id), '*****@*****.**', ['*****@*****.**', '*****@*****.**'], fail_silently=False) self.assertEqual( QueuedImage.objects.get(pk=self.q1.id).decision, 'rejected')
def test_update_person_submission(self): response = self.app.get( '/person/2009/update', user=self.user_who_can_lock, ) form = response.forms['person-details'] form['wikipedia_url'] = 'http://en.wikipedia.org/wiki/Tessa_Jowell' form['party_gb_2015'] = self.parties['party:53'].base_id form['source'] = "Some source of this information" submission_response = form.submit() person = Person.objects.get(id='2009') party = person.memberships.filter(role='Candidate') self.assertEqual(party.count(), 1) self.assertEqual(party[0].on_behalf_of.extra.slug, 'party:53') links = person.links.all() self.assertEqual(links.count(), 1) self.assertEqual(links[0].url, 'http://en.wikipedia.org/wiki/Tessa_Jowell') # It should redirect back to the same person's page: split_location = urlsplit(submission_response.location) self.assertEqual('/person/2009', split_location.path)
def test_all_steps_successful(self): # Select the election: response = self.app.get(self.wizard_url, user=self.user) election_form = response.forms['add-candidacy-wizard'] election_form['election-election'].select(text='2015 General Election') response = election_form.submit() # Select the post: post_form = response.forms['add-candidacy-wizard'] post_form['post-post'].select(text='Dulwich and West Norwood') response = post_form.submit() # Select the party: party_form = response.forms['add-candidacy-wizard'] party_form['party-party'].select(text='Labour Party') response = party_form.submit() # Explain the source: source_form = response.forms['add-candidacy-wizard'] source_form['source-source'] = 'Testing adding a candidacy' response = source_form.submit() # Now check we're redirected back to the person edit page: self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual(split_location.path, '/person/1234/update') # And check that all the changes we expect have been made. # The action must have been logged: la = LoggedAction.objects.order_by('updated').last() self.assertEqual(la.user, self.user) self.assertEqual(la.note, None) self.assertTrue(la.popit_person_new_version) self.assertEqual(la.person, self.person) self.assertEqual(la.source, 'Testing adding a candidacy') # Now check that that person has a new candidacy: m = Membership.objects.select_related('extra', 'on_behalf_of').get( person=self.person, role='Candidate') self.assertEqual(m.on_behalf_of.name, 'Labour Party') self.assertEqual(m.post, self.dulwich_post_extra.base)
def test_photo_review_upload_undecided_privileged( self, mock_send_mail ): settings = get_current_usersettings() settings.DEFAULT_FROM_EMAIL = '*****@*****.**' settings.save() review_url = reverse( 'photo-review', kwargs={'queued_image_id': self.q1.id} ) review_page_response = self.app.get( review_url, user=self.test_reviewer ) form = review_page_response.forms['photo-review-form'] form['decision'] = 'undecided' form['rejection_reason'] = 'No clear source or copyright statement' response = form.submit(user=self.test_reviewer) self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual('/moderation/photo/review', split_location.path) self.assertEqual(mock_send_mail.call_count, 0) self.assertEqual(QueuedImage.objects.get(pk=self.q1.id).decision, 'undecided')
def test_photo_review_upload_rejected_privileged(self, mock_send_mail): with self.settings(SITE_ID=1): review_url = reverse("photo-review", kwargs={"queued_image_id": self.q1.id}) review_page_response = self.app.get(review_url, user=self.test_reviewer) form = review_page_response.forms["photo-review-form"] form["decision"] = "rejected" form[ "rejection_reason"] = "There's no clear source or copyright statement" response = form.submit(user=self.test_reviewer) self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual("/moderation/photo/review", split_location.path) las = LoggedAction.objects.all() self.assertEqual(1, len(las)) la = las[0] self.assertEqual(la.user.username, "jane") self.assertEqual(la.action_type, "photo-reject") self.assertEqual(la.person.id, 2009) self.assertEqual(la.source, "Rejected a photo upload from john") mock_send_mail.assert_called_once_with( "example.com image moderation results", "Thank-you for uploading a photo of Tessa Jowell to example.com,\nbut unfortunately we can't use that image because:\n\n There's no clear source or copyright statement\n\nYou can just reply to this email if you want to discuss that\nfurther, or you can try uploading a photo with a different\nreason or justification for its use using this link:\n\n http://testserver/moderation/photo/upload/2009\n\nMany thanks from the example.com volunteers\n\n--\nFor administrators' use: http://testserver/moderation/photo/review/{}\n" .format(self.q1.id), "*****@*****.**", ["*****@*****.**", "*****@*****.**"], fail_silently=False, ) self.assertEqual( QueuedImage.objects.get(pk=self.q1.id).decision, "rejected")
def test_photo_review_queue_view_not_logged_in(self): queue_url = reverse('photo-review-list') response = self.app.get(queue_url) self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual('/accounts/login/', split_location.path) self.assertEqual('next=/moderation/photo/review', split_location.query)
def test_photo_upload(self): queued_images = QueuedImage.objects.all() initial_count = queued_images.count() upload_form_url = reverse( 'photo-upload', kwargs={'person_id': '2009'} ) form_page_response = self.app.get( upload_form_url, user=self.test_upload_user ) form = form_page_response.forms['person-upload-photo'] with open(self.example_image_filename, 'rb') as f: form['image'] = Upload('pilot.jpg', f.read()) form['why_allowed'] = 'copyright-assigned' form['justification_for_use'] = 'I took this photo' upload_response = form.submit() self.assertEqual(upload_response.status_code, 302) split_location = urlsplit(upload_response.location) self.assertEqual('/moderation/photo/upload/2009/success', split_location.path) queued_images = QueuedImage.objects.all() self.assertEqual(initial_count + 1, queued_images.count()) queued_image = queued_images.last() self.assertEqual(queued_image.decision, 'undecided') self.assertEqual(queued_image.why_allowed, 'copyright-assigned') self.assertEqual( queued_image.justification_for_use, 'I took this photo' ) self.assertEqual(queued_image.person.id, 2009) self.assertEqual(queued_image.user, self.test_upload_user)
def test_fields_are_saved_when_creating(self): response = self.app.get("/election/2015/person/create/", user=self.user) form = response.forms["new-candidate-form"] form["name"] = "Naomi Newperson" form["cv"] = "http://example.org/another-cv" form["profession"] = "Longshoreman" form["standing_2015"] = "not-standing" form["reelection"] = "yes" form["source"] = "Test creating someone with additional fields" submission_response = form.submit() self.assertEqual(submission_response.status_code, 302) split_location = urlsplit(submission_response.location) m = re.search(r"^/person/(.*)", split_location.path) self.assertTrue(m) person = Person.objects.get(id=m.group(1)) self.assertEqual( PersonExtraFieldValue.objects.get( person=person, field__key="cv" ).value, "http://example.org/another-cv", ) self.assertEqual( PersonExtraFieldValue.objects.get( person=person, field__key="profession" ).value, "Longshoreman", ) self.assertEqual( PersonExtraFieldValue.objects.get( person=person, field__key="reelection" ).value, "yes", )
def test_photo_review_upload_ignore_privileged( self, mock_send_mail ): review_url = reverse( 'photo-review', kwargs={'queued_image_id': self.q1.id} ) review_page_response = self.app.get( review_url, user=self.test_reviewer ) form = review_page_response.forms['photo-review-form'] form['decision'] = 'ignore' response = form.submit(user=self.test_reviewer) self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual('/moderation/photo/review', split_location.path) self.assertEqual(mock_send_mail.call_count, 0) self.assertEqual(QueuedImage.objects.get(pk=self.q1.id).decision, 'ignore') las = LoggedAction.objects.all() self.assertEqual(1, len(las)) la = las[0] self.assertEqual(la.user.username, 'jane') self.assertEqual(la.action_type, 'photo-ignore') self.assertEqual(la.person.id, 2009)
def test_update_person_extra_fields(self): memberships_before = membership_id_set(Person.objects.get(pk=2009)) ExtraField.objects.create(type="url", key="cv", label="CV or Resumé") ExtraField.objects.create(type="longer-text", key="notes", label="Notes") response = self.app.get("/person/2009/update", user=self.user_who_can_lock) form = response.forms["person-details"] form["birth_date"] = "1/4/1875" form["source"] = "An update for testing purposes" form["cv"] = "http://example.org/cv.pdf" response = form.submit() self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual("/person/2009", split_location.path) person = Person.objects.get(id="2009") self.assertEqual(person.birth_date, "1875-04-01") versions_data = json.loads(person.versions) self.assertEqual( versions_data[0]["data"]["extra_fields"], { "cv": "http://example.org/cv.pdf", "notes": "", "favourite_biscuits": "", }, ) self.assertEqual(memberships_before, membership_id_set(person))
def test_update_person_add_new_candidacy(self): memberships_before = membership_id_set(Person.objects.get(pk=2009)) response = self.app.get('/person/2009/update', user=self.user) # Now fake the addition of elements to the form as would # happen with the Javascript addition of a new candidacy. form = response.forms['person-details'] for name, value in [ ('extra_election_id', 'local.maidstone.2016-05-05'), ('party_gb_local.maidstone.2016-05-05', self.labour_party_extra.base.id), ('constituency_local.maidstone.2016-05-05', 'DIW:E05005004'), ('standing_local.maidstone.2016-05-05', 'standing'), ('source', 'Testing dynamic election addition'), ]: field = Text(form, 'input', None, None, value) form.fields[name] = [field] form.field_order.append((name, field)) response = form.submit() self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual('/person/2009', split_location.path) person = Person.objects.get(pk=2009) memberships_afterwards = membership_id_set(person) extra_membership_ids = memberships_afterwards - memberships_before self.assertEqual(len(extra_membership_ids), 1) new_candidacy = Membership.objects.get( pk=list(extra_membership_ids)[0]) self.assertEqual(new_candidacy.post.label, 'Shepway South Ward') self.assertEqual(new_candidacy.extra.election.slug, 'local.maidstone.2016-05-05') self.assertEqual(new_candidacy.on_behalf_of.name, 'Labour Party') same_before_and_after = memberships_before & memberships_afterwards self.assertEqual(len(same_before_and_after), 1)
def test_only_logged_in_users(self): response = self.app.get(self.wizard_url) self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual(split_location.path, '/accounts/login/') self.assertEqual(split_location.query, 'next=/person/1234/update/add-candidacy')
def test_update_person_extra_fields(self): ExtraField.objects.create( type='url', key='cv', label='CV or Resumé', ) ExtraField.objects.create( type='longer-text', key='notes', label='Notes', ) response = self.app.get( '/person/2009/update', user=self.user_who_can_lock, ) form = response.forms['person-details'] form['birth_date'] = '1/4/1875' form['source'] = "An update for testing purposes" form['cv'] = 'http://example.org/cv.pdf' response = form.submit() self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual('/person/2009', split_location.path) person = Person.objects.get(id='2009') self.assertEqual(person.birth_date, '1875-04-01') versions_data = json.loads(person.extra.versions) self.assertEqual( versions_data[0]['data']['extra_fields'], { 'cv': 'http://example.org/cv.pdf', 'notes': '', } )
def test_fields_are_saved_when_editing(self): response = self.app.get( '/person/{person_id}/update'.format(person_id=self.person.id), user=self.user, ) form = response.forms['person-details'] form['cv'] = 'http://homepage.example.org/john-the-described' form['profession'] = 'Soda Jerk' form['reelection'] = 'no' form['source'] = 'Testing setting additional fields' submission_response = form.submit() self.assertEqual(submission_response.status_code, 302) split_location = urlsplit(submission_response.location) self.assertEqual( '/person/{person_id}'.format(person_id=self.person.id), split_location.path) person = Person.objects.get(id=self.person.id) self.assertEqual( PersonExtraFieldValue.objects.get(person=person, field__key='cv').value, 'http://homepage.example.org/john-the-described') self.assertEqual( PersonExtraFieldValue.objects.get(person=person, field__key='profession').value, 'Soda Jerk') self.assertEqual( PersonExtraFieldValue.objects.get(person=person, field__key='reelection').value, 'no')
def test_update_person_add_new_candidacy_unsure_if_standing(self): memberships_before = membership_id_set(Person.objects.get(pk=2009)) response = self.app.get("/person/2009/update", user=self.user) # Now fake the addition of elements to the form as would # happen with the Javascript addition of a new candidacy. form = response.forms["person-details"] extra_fields = [ ("extra_election_id", "local.maidstone.2016-05-05"), ("party_GB_local.maidstone.2016-05-05", self.labour_party.ec_id), ("constituency_local.maidstone.2016-05-05", "DIW:E05005004"), ("standing_local.maidstone.2016-05-05", "not-sure"), ("source", "Testing dynamic election addition"), ] starting_pos = len(form.field_order) for pos, data in enumerate(extra_fields): name, value = data field = Text(form, "input", None, starting_pos + pos, value) form.fields[name] = [field] form.field_order.append((name, field)) response = form.submit() self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual("/person/2009", split_location.path) person = Person.objects.get(pk=2009) memberships_afterwards = membership_id_set(person) membership_ids = memberships_afterwards - memberships_before self.assertEqual(len(membership_ids), 0)
def test_update_person_submission(self): response = self.app.get( '/person/2009/update', user=self.user_who_can_lock, ) form = response.forms['person-details'] form['wikipedia_url'] = 'http://en.wikipedia.org/wiki/Tessa_Jowell' form['party_gb_2015'] = self.labour_party_extra.base_id form['source'] = "Some source of this information" submission_response = form.submit() person = Person.objects.get(id='2009') party = person.memberships.filter(role='Candidate') self.assertEqual(party.count(), 1) self.assertEqual(party[0].on_behalf_of.extra.slug, 'party:53') links = person.links.all() self.assertEqual(links.count(), 1) self.assertEqual(links[0].url, 'http://en.wikipedia.org/wiki/Tessa_Jowell') # It should redirect back to the same person's page: split_location = urlsplit(submission_response.location) self.assertEqual( '/person/2009', split_location.path )
def test_update_person_add_new_candidacy(self): memberships_before = membership_id_set(Person.objects.get(pk=2009)) response = self.app.get("/person/2009/update", user=self.user) # Now fake the addition of elements to the form as would # happen with the Javascript addition of a new candidacy. form = response.forms["person-details"] for name, value in [ ("extra_election_id", "local.maidstone.2016-05-05"), ("party_GB_local.maidstone.2016-05-05", self.labour_party.ec_id), ("constituency_local.maidstone.2016-05-05", "DIW:E05005004"), ("standing_local.maidstone.2016-05-05", "standing"), ("source", "Testing dynamic election addition"), ]: field = Text(form, "input", None, None, value) form.fields[name] = [field] form.field_order.append((name, field)) response = form.submit() self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual("/person/2009", split_location.path) person = Person.objects.get(pk=2009) memberships_afterwards = membership_id_set(person) extra_membership_ids = memberships_afterwards - memberships_before self.assertEqual(len(extra_membership_ids), 1) new_candidacy = Membership.objects.get( pk=list(extra_membership_ids)[0]) self.assertEqual(new_candidacy.post.label, "Shepway South Ward") self.assertEqual( new_candidacy.post_election.election.slug, "local.maidstone.2016-05-05", ) self.assertEqual(new_candidacy.party.name, "Labour Party") same_before_and_after = memberships_before & memberships_afterwards self.assertEqual(len(same_before_and_after), 1)
def test_update_person_extra_fields(self): ExtraField.objects.create( type='url', key='cv', label='CV or Resumé', ) ExtraField.objects.create( type='longer-text', key='notes', label='Notes', ) response = self.app.get( '/person/2009/update', user=self.user_who_can_lock, ) form = response.forms['person-details'] form['birth_date'] = '1/4/1875' form['source'] = "An update for testing purposes" form['cv'] = 'http://example.org/cv.pdf' response = form.submit() self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual('/person/2009', split_location.path) person = Person.objects.get(id='2009') self.assertEqual(person.birth_date, '1875-04-01') versions_data = json.loads(person.extra.versions) self.assertEqual(versions_data[0]['data']['extra_fields'], { 'cv': 'http://example.org/cv.pdf', 'notes': '', })
def test_valid_postcode_redirects_to_constituency(self, mock_requests): mock_requests.get.side_effect = fake_requests_for_every_election response = self.app.get('/') form = response.forms['form-postcode'] form['q'] = 'SE24 0AG' response = form.submit() self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual(split_location.path, '/') self.assertEqual(split_location.query, 'q=SE24%200AG') response = self.app.get(response.location) split_location = urlsplit(response.location) self.assertEqual( split_location.path, '/areas/WMC--gss:E14000673', )
def test_photo_upload_through_image_field(self): queued_images = QueuedImage.objects.all() initial_count = queued_images.count() upload_form_url = reverse("photo-upload", kwargs={"person_id": "2009"}) form_page_response = self.app.get(upload_form_url, user=self.test_upload_user) form = form_page_response.forms["person-upload-photo-image"] with open(self.example_image_filename, "rb") as f: form["image"] = Upload("pilot.jpg", f.read()) form["why_allowed"] = "copyright-assigned" form["justification_for_use"] = "I took this photo" upload_response = form.submit() self.assertEqual(upload_response.status_code, 302) split_location = urlsplit(upload_response.location) self.assertEqual("/moderation/photo/upload/2009/success", split_location.path) queued_images = QueuedImage.objects.all() self.assertEqual(initial_count + 1, queued_images.count()) queued_image = queued_images.last() self.assertEqual(queued_image.decision, "undecided") self.assertEqual(queued_image.why_allowed, "copyright-assigned") self.assertEqual(queued_image.justification_for_use, "I took this photo") self.assertEqual(queued_image.person.id, 2009) self.assertEqual(queued_image.user, self.test_upload_user)
def test_valid_postcode_redirects_to_constituency(self, mock_requests): mock_requests.get.side_effect = fake_requests_for_every_election response = self.app.get("/") form = response.forms["form-postcode"] form["q"] = "SE24 0AG" response = form.submit() self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual(split_location.path, "/") self.assertEqual(split_location.query, "q=SE24%200AG") response = self.app.get(response.location) split_location = urlsplit(response.location) self.assertEqual(split_location.path, "/postcode/SE24%200AG/") # follow the redirect response = response.follow() self.assertEqual(len(response.context["ballots"]), 1) self.assertContains(response, "2015 General Election")
def test_loads_success_page_if_upload_was_successful( self, *all_mock_requests): self.successful_get_image(*all_mock_requests) upload_response = self.valid_form().submit() self.assertEqual(upload_response.status_code, 302) split_location = urlsplit(upload_response.location) self.assertEqual(split_location.path, "/moderation/photo/upload/2009/success")
def test_new_person_submission_refused_copyright(self): # Just a smoke test for the moment: response = self.app.get('/constituency/65808/dulwich-and-west-norwood', user=self.user_refused) split_location = urlsplit(response.location) self.assertEqual('/copyright-question', split_location.path) self.assertEqual('next=/constituency/65808/dulwich-and-west-norwood', split_location.query)
def test_only_logged_in_users(self): response = self.app.get(self.wizard_url) self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual( split_location.path, '/accounts/login/') self.assertEqual( split_location.query, 'next=/person/1234/update/add-candidacy')
def test_valid_postcode_redirects_to_constituency(self, mock_requests): mock_requests.get.side_effect = fake_requests_for_mapit response = self.app.get("/") form = response.forms["form-postcode"] form["postcode"] = "SE24 0AG" response = form.submit() self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual(split_location.path, "/areas/WMC-gss:E14000673")
def test_photo_review_upload_approved_privileged( self, mock_send_mail ): settings = get_current_usersettings() settings.DEFAULT_FROM_EMAIL = '*****@*****.**' settings.site_id = self.site.id settings.save() with self.settings(SITE_ID=self.site.id): review_url = reverse( 'photo-review', kwargs={'queued_image_id': self.q1.id} ) review_page_response = self.app.get( review_url, user=self.test_reviewer ) form = review_page_response.forms['photo-review-form'] form['decision'] = 'approved' form['moderator_why_allowed'] = 'profile-photo' response = form.submit(user=self.test_reviewer) # FIXME: check that mocked_person_put got the right calls self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual('/moderation/photo/review', split_location.path) mock_send_mail.assert_called_once_with( 'YNR image upload approved', "Thank-you for submitting a photo to YNR; that's been uploaded\nnow for the candidate page here:\n\n http://localhost:80/person/2009/tessa-jowell\n\nMany thanks from the YNR volunteers\n", '*****@*****.**', ['*****@*****.**'], fail_silently=False ) person = Person.objects.get(id=2009) image = person.extra.images.last() self.assertTrue(image.is_primary) self.assertEqual( 'Uploaded by john: Approved from photo moderation queue', image.source ) self.assertEqual(427, image.image.width) self.assertEqual(639, image.image.height) self.q1.refresh_from_db() self.assertEqual('public-domain', self.q1.why_allowed) self.assertEqual('approved', self.q1.decision) las = LoggedAction.objects.all() self.assertEqual(1, len(las)) la = las[0] self.assertEqual(la.user.username, 'jane') self.assertEqual(la.action_type, 'photo-approve') self.assertEqual(la.person.id, 2009) self.assertEqual(QueuedImage.objects.get(pk=self.q1.id).decision, 'approved')
def test_update_person_submission_copyright_refused(self): response = self.app.get("/person/2009/update", user=self.user) form = response.forms["person-details"] form["wikipedia_url"] = "http://en.wikipedia.org/wiki/Tessa_Jowell" form["party_GB_2015"] = self.labour_party.ec_id form["source"] = "Some source of this information" submission_response = form.submit(user=self.user_refused) split_location = urlsplit(submission_response.location) self.assertEqual("/copyright-question", split_location.path) self.assertEqual("next=/person/2009/update", split_location.query)
def test_update_person_submission_copyright_refused(self): response = self.app.get('/person/2009/update', user=self.user) form = response.forms['person-details'] form['wikipedia_url'] = 'http://en.wikipedia.org/wiki/Tessa_Jowell' form['party_gb_2015'] = self.labour_party_extra.base_id form['source'] = "Some source of this information" submission_response = form.submit(user=self.user_refused) split_location = urlsplit(submission_response.location) self.assertEqual('/copyright-question', split_location.path) self.assertEqual('next=/person/2009/update', split_location.query)
def test_update_person_submission_copyright_refused(self): response = self.app.get('/person/2009/update', user=self.user) form = response.forms['person-details'] form['wikipedia_url'] = 'http://en.wikipedia.org/wiki/Tessa_Jowell' form['party_gb_2015'] = self.parties['party:53'].base_id form['source'] = "Some source of this information" submission_response = form.submit(user=self.user_refused) split_location = urlsplit(submission_response.location) self.assertEqual('/copyright-question', split_location.path) self.assertEqual('next=/person/2009/update', split_location.query)
def test_valid_postcode_redirects_to_multiple_areas(self, mock_requests): mock_requests.get.side_effect = fake_requests_for_mapit # Create some extra posts and areas: london_assembly = ParliamentaryChamberExtraFactory.create( slug='london-assembly', base__name='London Assembly' ) lac_area_type = AreaTypeFactory.create(name='LAC') gla_area_type = AreaTypeFactory.create(name='GLA') area_extra_lac = AreaExtraFactory.create( base__identifier='gss:E32000010', base__name="Dulwich and West Norwood", type=lac_area_type, ) area_extra_gla = AreaExtraFactory.create( base__identifier='unit_id:41441', base__name='Greater London Authority', type=gla_area_type, ) election_lac = ElectionFactory.create( slug='gb-gla-2016-05-05-c', organization=london_assembly.base, name='2016 London Assembly Election (Constituencies)', area_types=(lac_area_type,), ) election_gla = ElectionFactory.create( slug='gb-gla-2016-05-05-a', organization=london_assembly.base, name='2016 London Assembly Election (Additional)', area_types=(gla_area_type,), ) PostExtraFactory.create( elections=(election_lac,), base__area=area_extra_lac.base, base__organization=london_assembly.base, slug='11822', base__label='Assembly Member for Lambeth and Southwark', ) PostExtraFactory.create( elections=(election_gla,), base__area=area_extra_gla.base, base__organization=london_assembly.base, slug='2247', base__label='2016 London Assembly Election (Additional)', ) # ---------------------------- response = self.app.get('/') form = response.forms['form-postcode'] form['postcode'] = 'SE24 0AG' response = form.submit() self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual( split_location.path, '/areas/GLA-unit_id:41441,LAC-gss:E32000010,WMC-gss:E14000673', )
def test_new_person_submission_refused_copyright(self): response = self.app.get( "/constituency/65808/dulwich-and-west-norwood", user=self.user_refused, ) split_location = urlsplit(response.location) self.assertEqual("/copyright-question", split_location.path) self.assertEqual( "next=/constituency/65808/dulwich-and-west-norwood", split_location.query, )
def test_valid_postcode_redirects_to_constituency(self, mock_requests): mock_requests.get.side_effect = fake_requests_for_every_election response = self.app.get('/') form = response.forms['form-postcode'] form['q'] = 'SE24 0AG' response = form.submit() self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual(split_location.path, '/') self.assertEqual(split_location.query, 'q=SE24%200AG') response = self.app.get(response.location) split_location = urlsplit(response.location) self.assertEqual( split_location.path, '/postcode/SE24%200AG/', ) # follow the redirect response = response.follow() self.assertEqual(len(response.context['pees']), 1) self.assertContains(response, '2015 General Election')
def test_front_page_good_address_lookup(self, patched_requests, patched_geocode): patched_requests.get.side_effect = fake_represent_boundaries response = self.app.get('/') form = response.forms['form-address'] form['address'] = '631 Snelling Ave S, Saint Paul, MN' response = form.submit() self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual( split_location.path, '/areas/ocd-division,country:us,state:mn,place:st_paul;ocd-division,country:us,state:mn,place:st_paul,ward:3', )
def test_new_person_submission_refused_copyright(self): response = self.app.get( '/constituency/65808/dulwich-and-west-norwood', user=self.user_refused, ) split_location = urlsplit(response.location) self.assertEqual( '/copyright-question', split_location.path ) self.assertEqual( 'next=/constituency/65808/dulwich-and-west-norwood', split_location.query )
def test_copyright_assigned(self): response = self.app.get( "/constituency/65808/dulwich-and-west-norwood", user=self.user_refused, auto_follow=True ) form = response.forms["copyright_assignment"] form["assigned_to_dc"] = True form_response = form.submit() split_location = urlsplit(form_response.location) self.assertEqual("/constituency/65808/dulwich-and-west-norwood", split_location.path) agreement = self.user_refused.terms_agreement agreement.refresh_from_db() self.assertTrue(agreement.assigned_to_dc)
def handle(self, **options): with transaction.atomic(): split_url = urlsplit(options['SITE-URL']) if (split_url.path not in ('', '/') \ or split_url.query or split_url.fragment): raise CommandError('You must only supply the base URL of the site') # Then form the base API URL: new_url_parts = list(split_url) new_url_parts[2] = '' self.base_url = urlunsplit(new_url_parts) new_url_parts[2] = '/api/v0.9/' self.base_api_url = urlunsplit(new_url_parts) self.check_database_is_empty() self.mirror_from_api()
def test_update_dd_mm_yyyy_birth_date(self): response = self.app.get( '/person/2009/update', user=self.user_who_can_lock, ) form = response.forms['person-details'] form['birth_date'] = '1/4/1875' form['source'] = "An update for testing purposes" response = form.submit() self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual('/person/2009', split_location.path) person = Person.objects.get(id='2009') self.assertEqual(person.birth_date, '1875-04-01')
def test_front_page_good_address_outside(self, patched_requests, patched_geocode): patched_requests.get.side_effect = fake_represent_boundaries response = self.app.get('/') form = response.forms['form-address'] form['address'] = 'Empire State Building, New York' response = form.submit() # FIXME: I'm not sure this is what's really intended, since it # would be better to stay on the front page with a validation # error saying the area is outside the twin cities, but this # is what the current lookup code from Datamade does: self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual( split_location.path, '/areas/ocd-division,country:us,state:ny,place:new_york,council_district:4', )
def test_valid_postcode_redirects_to_multiple_areas(self, mock_requests): mock_requests.get.side_effect = fake_requests_for_mapit # Create some extra posts and areas: london_assembly = ParliamentaryChamberExtraFactory.create(slug="london-assembly", base__name="London Assembly") lac_area_type = AreaTypeFactory.create(name="LAC") gla_area_type = AreaTypeFactory.create(name="GLA") area_extra_lac = AreaExtraFactory.create( base__identifier="gss:E32000010", base__name="Dulwich and West Norwood", type=lac_area_type ) area_extra_gla = AreaExtraFactory.create( base__identifier="unit_id:41441", base__name="Greater London Authority", type=gla_area_type ) election_lac = ElectionFactory.create( slug="gb-gla-2016-05-05-c", organization=london_assembly.base, name="2016 London Assembly Election (Constituencies)", area_types=(lac_area_type,), ) election_gla = ElectionFactory.create( slug="gb-gla-2016-05-05-a", organization=london_assembly.base, name="2016 London Assembly Election (Additional)", area_types=(gla_area_type,), ) PostExtraFactory.create( elections=(election_lac,), base__area=area_extra_lac.base, base__organization=london_assembly.base, slug="11822", base__label="Assembly Member for Lambeth and Southwark", ) PostExtraFactory.create( elections=(election_gla,), base__area=area_extra_gla.base, base__organization=london_assembly.base, slug="2247", base__label="2016 London Assembly Election (Additional)", ) # ---------------------------- response = self.app.get("/") form = response.forms["form-postcode"] form["postcode"] = "SE24 0AG" response = form.submit() self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual(split_location.path, "/areas/GLA-unit_id:41441,LAC-gss:E32000010,WMC-gss:E14000673")
def test_photo_review_upload_rejected_privileged( self, mock_send_mail ): settings = get_current_usersettings() settings.DEFAULT_FROM_EMAIL = '*****@*****.**' settings.SUPPORT_EMAIL = '*****@*****.**' settings.site_id = self.site.id settings.save() with self.settings(SITE_ID=self.site.id): review_url = reverse( 'photo-review', kwargs={'queued_image_id': self.q1.id} ) review_page_response = self.app.get( review_url, user=self.test_reviewer ) form = review_page_response.forms['photo-review-form'] form['decision'] = 'rejected' form['rejection_reason'] = 'There\'s no clear source or copyright statement' response = form.submit(user=self.test_reviewer) self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual('/moderation/photo/review', split_location.path) las = LoggedAction.objects.all() self.assertEqual(1, len(las)) la = las[0] self.assertEqual(la.user.username, 'jane') self.assertEqual(la.action_type, 'photo-reject') self.assertEqual(la.person.id, 2009) self.assertEqual(la.source, 'Rejected a photo upload from john') self.assertEqual(la.note, 'There\'s no clear source or copyright statement') mock_send_mail.assert_called_once_with( 'YNR image moderation results', "Thank-you for uploading a photo of Tessa Jowell to YNR, but\nunfortunately we can't use that image because:\n\n There\'s no clear source or copyright statement\n\nYou can just reply to this email if you want to discuss that\nfurther, or you can try uploading a photo with a different\nreason or justification for its use using this link:\n\n http://localhost:80/moderation/photo/upload/2009\n\nMany thanks from the YNR volunteers\n\n-- \nFor administrators' use: http://localhost:80/moderation/photo/review/{0}\n".format(self.q1.id), '*****@*****.**', ['*****@*****.**', '*****@*****.**'], fail_silently=False ) self.assertEqual(QueuedImage.objects.get(pk=self.q1.id).decision, 'rejected')
def test_fields_are_saved_when_creating(self): response = self.app.get( '/election/2015/person/create/', user=self.user ) form = response.forms['new-candidate-form'] form['name'] = 'Naomi Newperson' form['additional_link'] = 'http://example.com/morelink' form['standing_2015'] = 'not-standing' form['source'] = 'Test creating someone with simple fields' submission_response = form.submit() self.assertEqual(submission_response.status_code, 302) split_location = urlsplit(submission_response.location) m = re.search(r'^/person/(.*)', split_location.path) self.assertTrue(m) person = Person.objects.get(id=m.group(1)) self.assertEqual(person.extra.additional_link, 'http://example.com/morelink')
def test_fields_are_saved_when_editing(self): response = self.app.get( '/person/{person_id}/update'.format(person_id=self.person.id), user=self.user, ) form = response.forms['person-details'] form['additional_link'] = 'http://example.com/anotherlink' form['source'] = 'Testing setting complex fields' submission_response = form.submit() self.assertEqual(submission_response.status_code, 302) split_location = urlsplit(submission_response.location) self.assertEqual( '/person/{person_id}'.format(person_id=self.person.id), split_location.path ) person = Person.objects.get(id=self.person.id) self.assertEqual(person.extra.additional_link, 'http://example.com/anotherlink')
def test_fields_are_saved_when_editing(self): response = self.app.get("/person/{person_id}/update".format(person_id=self.person.id), user=self.user) form = response.forms["person-details"] form["cv"] = "http://homepage.example.org/john-the-described" form["profession"] = "Soda Jerk" form["reelection"] = "no" form["source"] = "Testing setting additional fields" submission_response = form.submit() self.assertEqual(submission_response.status_code, 302) split_location = urlsplit(submission_response.location) self.assertEqual("/person/{person_id}".format(person_id=self.person.id), split_location.path) person = Person.objects.get(id=self.person.id) self.assertEqual( PersonExtraFieldValue.objects.get(person=person, field__key="cv").value, "http://homepage.example.org/john-the-described", ) self.assertEqual(PersonExtraFieldValue.objects.get(person=person, field__key="profession").value, "Soda Jerk") self.assertEqual(PersonExtraFieldValue.objects.get(person=person, field__key="reelection").value, "no")
def test_all_steps_successful(self): # Select the election: response = self.app.get(self.wizard_url, user=self.user) election_form = response.forms['add-candidacy-wizard'] election_form['election-election'].select( text='2015 General Election') response = election_form.submit() # Select the post: post_form = response.forms['add-candidacy-wizard'] post_form['post-post'].select(text='Dulwich and West Norwood') response = post_form.submit() # Select the party: party_form = response.forms['add-candidacy-wizard'] party_form['party-party'].select(text='Labour Party') response = party_form.submit() # Explain the source: source_form = response.forms['add-candidacy-wizard'] source_form['source-source'] = 'Testing adding a candidacy' response = source_form.submit() # Now check we're redirected back to the person edit page: self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual( split_location.path, '/person/1234/update' ) # And check that all the changes we expect have been made. # The action must have been logged: la = LoggedAction.objects.order_by('updated').last() self.assertEqual(la.user, self.user) self.assertEqual(la.note, None) self.assertTrue(la.popit_person_new_version) self.assertEqual(la.person, self.person) self.assertEqual(la.source, 'Testing adding a candidacy') # Now check that that person has a new candidacy: m = Membership.objects.select_related('extra', 'on_behalf_of').get( person=self.person, role='Candidate') self.assertEqual(m.on_behalf_of.name, 'Labour Party') self.assertEqual(m.post, self.dulwich_post_extra.base)
def test_fields_are_saved_when_editing(self): response = self.app.get( '/person/{person_id}/update'.format(person_id=self.person.id), user=self.user, ) form = response.forms['person-details'] form['cv'] = 'http://homepage.example.org/john-the-described' form['profession'] = 'Soda Jerk' form['reelection'] = 'no' form['source'] = 'Testing setting additional fields' submission_response = form.submit() self.assertEqual(submission_response.status_code, 302) split_location = urlsplit(submission_response.location) self.assertEqual( '/person/{person_id}'.format(person_id=self.person.id), split_location.path ) person = Person.objects.get(id=self.person.id) self.assertEqual( PersonExtraFieldValue.objects.get( person=person, field__key='cv' ).value, 'http://homepage.example.org/john-the-described' ) self.assertEqual( PersonExtraFieldValue.objects.get( person=person, field__key='profession' ).value, 'Soda Jerk' ) self.assertEqual( PersonExtraFieldValue.objects.get( person=person, field__key='reelection' ).value, 'no' )
def test_fields_are_saved_when_creating(self): response = self.app.get( '/election/2015/person/create/', user=self.user ) form = response.forms['person-details'] form['name'] = 'Naomi Newperson' form['cv'] = 'http://example.org/another-cv' form['profession'] = 'Longshoreman' form['standing_2015'] = 'not-standing' form['reelection'] = 'yes' form['source'] = 'Test creating someone with additional fields' submission_response = form.submit() self.assertEqual(submission_response.status_code, 302) split_location = urlsplit(submission_response.location) m = re.search(r'^/person/(.*)', split_location.path) self.assertTrue(m) person = Person.objects.get(id=m.group(1)) self.assertEqual( PersonExtraFieldValue.objects.get( person=person, field__key='cv' ).value, 'http://example.org/another-cv' ) self.assertEqual( PersonExtraFieldValue.objects.get( person=person, field__key='profession' ).value, 'Longshoreman' ) self.assertEqual( PersonExtraFieldValue.objects.get( person=person, field__key='reelection' ).value, 'yes' )
def test_fields_are_saved_when_creating(self): response = self.app.get("/election/2015/person/create/", user=self.user) form = response.forms["new-candidate-form"] form["name"] = "Naomi Newperson" form["cv"] = "http://example.org/another-cv" form["profession"] = "Longshoreman" form["standing_2015"] = "not-standing" form["reelection"] = "yes" form["source"] = "Test creating someone with additional fields" submission_response = form.submit() self.assertEqual(submission_response.status_code, 302) split_location = urlsplit(submission_response.location) m = re.search(r"^/person/(.*)", split_location.path) self.assertTrue(m) person = Person.objects.get(id=m.group(1)) self.assertEqual( PersonExtraFieldValue.objects.get(person=person, field__key="cv").value, "http://example.org/another-cv" ) self.assertEqual( PersonExtraFieldValue.objects.get(person=person, field__key="profession").value, "Longshoreman" ) self.assertEqual(PersonExtraFieldValue.objects.get(person=person, field__key="reelection").value, "yes")
def test_all_steps_with_party_list_position_successful(self): self.election.party_lists_in_use = True self.election.save() # Now go through all the steps, but set a party list position # too: # Select the election: response = self.app.get(self.wizard_url, user=self.user) election_form = response.forms['add-candidacy-wizard'] election_form['election-election'].select( text='2015 General Election') response = election_form.submit() # Select the post: post_form = response.forms['add-candidacy-wizard'] post_form['post-post'].select(text='Dulwich and West Norwood') response = post_form.submit() # Select the party: party_form = response.forms['add-candidacy-wizard'] party_form['party-party'].select(text='Labour Party') party_form['party-party_list_position'] = 3 response = party_form.submit() # Explain the source: source_form = response.forms['add-candidacy-wizard'] source_form['source-source'] = 'Testing adding a candidacy' response = source_form.submit() # Now check we're redirected back to the person edit page: self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual( split_location.path, '/person/1234/update' ) # Now check that that person has a new candidacy: m = Membership.objects.select_related('extra', 'on_behalf_of').get( person=self.person, role='Candidate') self.assertEqual(m.extra.party_list_position, 3)
def test_update_person_view_get_refused_copyright(self): response = self.app.get('/person/2009/update', user=self.user_refused) self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual('/copyright-question', split_location.path) self.assertEqual('next=/person/2009/update', split_location.query)
def update_area(self, area_data): new_area_data = area_data.copy() if settings.ELECTION_APP == 'uk_general_election_2015' or \ settings.ELECTION_APP == 'ar_elections_2015' or \ settings.ELECTION_APP == 'bf_elections_2015': identifier = new_area_data['identifier'] split_url = urlsplit(identifier) if not (split_url.netloc.endswith('mapit.mysociety.org') or split_url.netloc.endswith('mapit.staging.mysociety.org')): raise Exception("Area identifers are expected to be MapIt area URLs") mapit_area_url = identifier m = re.search(r'^/area/(\d+)$', split_url.path) if not m: message = "The format of the MapIt URL was unexpected: {0}" raise Exception(message.format(mapit_area_url)) mapit_area_id = m.group(1) # Make the Area.identifier for UK areas just the integer # MapIt Area ID to make it easy to keep area URLs the same: new_area_data['identifier'] = mapit_area_id elif settings.ELECTION_APP == 'st_paul_municipal_2015': old_identifier = new_area_data['identifier'] if old_identifier == '/area/0': new_area_data['identifier'] = 'ocd-division/country:us/state:mn/place:st_paul' else: m = re.search(r'^/area/([1-7])', old_identifier) if m: new_area_data['identifier'] = \ 'ocd-division/country:us/state:mn/place:st_paul/ward:' + m.group(1) else: message = "The format of the St Paul area ID was unexpected: {0}" raise Exception(message.format(old_identifier)) area_id, area = super(YNRPopItImporter, self).update_area(new_area_data) if settings.ELECTION_APP == 'uk_general_election_2015': ContentType = self.get_model_class('contenttypes', 'ContentType') area_content_type = ContentType.objects.get_for_model(area) Identifier = self.get_model_class('popolo', 'Identifier') # For the UK, we need to add the GSS code for each area: mapit_filename = get_url_cached(mapit_area_url) with open(mapit_filename) as f: mapit_area_data = json.load(f) self.uk_mapit_data[str(mapit_area_data['id'])] = mapit_area_data gss_code = mapit_area_data.get('codes', {}).get('gss') if gss_code: Identifier.objects.create( scheme='gss', identifier=gss_code, object_id=area.id, content_type_id=area_content_type.id, ) # Also preserve the complete MapIt URL: Identifier.objects.create( scheme='mapit-area-url', identifier=mapit_area_url, object_id=area.id, content_type_id=area_content_type.id ) # Create the extra area object: AreaExtra = self.get_model_class('candidates', 'AreaExtra') AreaExtra.objects.get_or_create(base=area) return area_id, area
def test_update_person_view_get_without_login(self): response = self.app.get('/person/2009/update') self.assertEqual(response.status_code, 302) split_location = urlsplit(response.location) self.assertEqual('/accounts/login/', split_location.path) self.assertEqual('next=/person/2009/update', split_location.query)