<commit_before>from urlparse import urlsplit

from mock import patch

from django_webtest import WebTest

from .auth import TestUserMixin
from .helpers import equal_call_args
from .fake_popit import (
    FakeOrganizationCollection, FakePersonCollection, FakePostCollection
)
from ..models import LoggedAction

example_timestamp = '2014-09-29T10:11:59.216159'
example_version_id = '5aa6418325c1a0bb'

def fill_form(form, form_dict):
    for key, value in form_dict.items():
        form[key] = value

@patch('candidates.popit.PopIt')
class TestNewPersonView(TestUserMixin, WebTest):

    @patch('candidates.views.version_data.get_current_timestamp')
    @patch('candidates.views.version_data.create_version_id')
    @patch('candidates.models.popit.PopItPerson.create_new_person_in_popit')
    @patch('candidates.models.popit.PopItPerson.update_person_in_popit')
    def test_new_person_submission_refused_copyright(
            self,
            mock_update_person_in_popit,
            mock_create_new_person_in_popit,
            mock_create_version_id,
            mock_get_current_timestamp,
            mock_popit):
        # Get the constituency page:
        mock_popit.return_value.organizations = FakeOrganizationCollection
        mock_popit.return_value.persons = FakePersonCollection
        # 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
        )
        self.assertFalse(mock_create_new_person_in_popit.called)
        self.assertFalse(mock_update_person_in_popit.called)

    @patch('candidates.views.version_data.get_current_timestamp')
    @patch('candidates.views.version_data.create_version_id')
    @patch('candidates.models.popit.PopItPerson.update_person_in_popit')
    @patch.object(FakePersonCollection, 'post')
    def test_new_person_submission(
            self,
            mock_person_post,
            mock_update_person_in_popit,
            mock_create_version_id,
            mock_get_current_timestamp,
            mock_popit):
        # Get the constituency page:
        mock_popit.return_value.organizations = FakeOrganizationCollection
        mock_popit.return_value.persons = FakePersonCollection
        mock_popit.return_value.posts = FakePostCollection
        # Just a smoke test for the moment:
        response = self.app.get(
            '/election/2015/post/65808/dulwich-and-west-norwood',
            user=self.user
        )
        mock_get_current_timestamp.return_value = example_timestamp
        mock_create_version_id.return_value = example_version_id
        # Get the add candidate form and fill in some values:
        form = response.forms['new-candidate-form']
        form_dict = {
            'name': '<NAME>',
            # Make <NAME> be standing for the Monster Raving Loony
            # Party in Dulwich and West Norwood:
            'party_gb_2015': 'party:66',
            'constituency_2015': '65808',
            'email': '<EMAIL>',<commit_msg>Make another test work without a network connection<commit_after>from urlparse import urlsplit

from mock import patch

from django_webtest import WebTest

from .auth import TestUserMixin
from .helpers import equal_call_args
from .fake_popit import (
    FakeOrganizationCollection, FakePersonCollection, FakePostCollection,
    fake_mp_post_search_results
)
from ..models import LoggedAction

example_timestamp = '2014-09-29T10:11:59.216159'
example_version_id = '5aa6418325c1a0bb'

def fill_form(form, form_dict):
    for key, value in form_dict.items():
        form[key] = value

@patch('candidates.popit.PopIt')
class TestNewPersonView(TestUserMixin, WebTest):

    @patch('candidates.views.version_data.get_current_timestamp')
    @patch('candidates.views.version_data.create_version_id')
    @patch('candidates.models.popit.PopItPerson.create_new_person_in_popit')
    @patch('candidates.models.popit.PopItPerson.update_person_in_popit')
    def test_new_person_submission_refused_copyright(
            self,
            mock_update_person_in_popit,
            mock_create_new_person_in_popit,
            mock_create_version_id,
            mock_get_current_timestamp,
            mock_popit):
        # Get the constituency page:
        mock_popit.return_value.organizations = FakeOrganizationCollection
        mock_popit.return_value.persons = FakePersonCollection
        # 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
        )
        self.assertFalse(mock_create_new_person_in_popit.called)
        self.assertFalse(mock_update_person_in_popit.called)

    @patch('candidates.popit.requests')
    @patch('candidates.views.version_data.get_current_timestamp')
    @patch('candidates.views.version_data.create_version_id')
    @patch('candidates.models.popit.PopItPerson.update_person_in_popit')
    @patch.object(FakePersonCollection, 'post')
    def test_new_person_submission(
            self,
            mock_person_post,
            mock_update_person_in_popit,
            mock_create_version_id,
            mock_get_current_timestamp,
            mock_requests,
            mock_popit):
        # Get the constituency page:
        mock_popit.return_value.organizations = FakeOrganizationCollection
        mock_popit.return_value.persons = FakePersonCollection
        mock_popit.return_value.posts = FakePostCollection
        mock_requests.get.side_effect = fake_mp_post_search_results
        # Just a smoke test for the moment:
        response = self.app.get(
            '/election/2015/post/65808/dulwich-and-west-norwood',
            user=self.user
        )
        mock_get_current_timestamp.return_value = example_timestamp
        mock_create_version_id.return_value = example_version_id
        # Get the add candidate form and fill in some values:
        form = response.forms['new-candidate-form']
        form_dict = {
            'name': '<NAME>',
            # Make <NAME> be standing for the Monster Raving Loony
            # Party in Dulwich and West Norwood:
            'party_gb_2015': 'party:66',
            'constituency_2015': '65808',
            'email': '<EMAIL>',
{'fraction_non_alphanumeric': 0.08051341890315053, 'fraction_numerical': 0.01866977829638273, 'mean_word_length': 2.904897494305239, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5887012', 'n_tokens_mistral': 2129, 'n_tokens_neox': 2035, 'n_words': 337}