def test_form_relate(self):
     form = RelatePopitInstanceWithWriteItInstance(data=self.data, writeitinstance=self.writeitinstance)
     form.is_valid()
     form.relate()
     records = WriteitInstancePopitInstanceRecord.objects.filter(writeitinstance=self.writeitinstance)
     self.assertEquals(records.count(), 1)
     self.assertTrue(self.writeitinstance.persons.all())
 def test_it_parses_the_popit_api(self):
     data = {"popit_url": 'http://the-instance.popit.mysociety.org/api/'}
     form = RelatePopitInstanceWithWriteItInstance(data=data, writeitinstance=self.writeitinstance)
     popit_url = form.get_popit_url_parsed(data["popit_url"])
     expected_url = 'https://the-instance.popit.mysociety.org/api/v0.1'
     self.assertEquals(popit_url, expected_url)
     self.assertTrue(form.is_valid())
     cleaned_data = form.clean()
     self.assertEquals(cleaned_data.get('popit_url'), expected_url)
 def test_it_parses_the_popit_api(self):
     data = {"popit_url": 'http://the-instance.popit.mysociety.org/api/'}
     form = RelatePopitInstanceWithWriteItInstance(data=data, writeitinstance=self.writeitinstance)
     popit_url = form.get_popit_url_parsed(data["popit_url"])
     expected_url = 'https://the-instance.popit.mysociety.org/api/v0.1'
     self.assertEquals(popit_url, expected_url)
     self.assertTrue(form.is_valid())
     cleaned_data = form.clean()
     self.assertEquals(cleaned_data.get('popit_url'), expected_url)
    def test_get_the_url(self):
        form = RelatePopitInstanceWithWriteItInstance(data=self.data, writeitinstance=self.writeitinstance)
        form.is_valid()
        form.relate()

        self.client.login(username="******", password="******")
        url = reverse('relate-writeit-popit', subdomain=self.writeitinstance.slug)
        response = self.client.get(url)
        self.assertEquals(response.status_code, 200)
        self.assertIn('relations', response.context)
        self.assertEquals(len(response.context['relations']), self.writeitinstance.writeitinstancepopitinstancerecord_set.count())
        self.assertTemplateUsed(response, 'nuntium/profiles/writeitinstance_and_popit_relations.html')
        self.assertEquals(response.context['relations'][0], self.writeitinstance.writeitinstancepopitinstancerecord_set.all()[0])
    def test_the_form_is_not_valid_if_there_is_another_popit(self):
        '''The form is not valid if there is already another popit api instance related'''

        popit_api_instance = PopitApiInstance.objects.create(url=settings.TEST_POPIT_API_URL)
        WriteitInstancePopitInstanceRecord.objects.create(writeitinstance=self.writeitinstance,
            popitapiinstance=popit_api_instance)

        data = {"popit_url": settings.TEST_POPIT_API_URL}
        form = RelatePopitInstanceWithWriteItInstance(data=data, writeitinstance=self.writeitinstance)
        self.assertFalse(form.is_valid())
        # Ok so because we know that you are trying to update from your previously related popit
        # rather than creating a new one I now should just check that Benito is here
        self.assertTrue(self.writeitinstance.persons.filter(name="Benito"))
    def test_the_form_is_not_valid_if_there_is_another_popit(self):
        '''The form is not valid if there is already another popit api instance related'''

        popolo_source = PopoloSource.objects.create(url=settings.TEST_POPIT_API_URL)
        WriteitInstancePopitInstanceRecord.objects.create(writeitinstance=self.writeitinstance,
            popolo_source=popolo_source)

        data = {"popit_url": settings.TEST_POPIT_API_URL}
        form = RelatePopitInstanceWithWriteItInstance(data=data, writeitinstance=self.writeitinstance)
        self.assertFalse(form.is_valid())
        # Ok so because we know that you are trying to update from your previously related popit
        # rather than creating a new one I now should just check that Benito is here
        self.assertTrue(self.writeitinstance.persons.filter(name="Benito"))
 def test_form_relate(self):
     form = RelatePopitInstanceWithWriteItInstance(data=self.data, writeitinstance=self.writeitinstance)
     form.is_valid()
     form.relate()
     records = WriteitInstancePopitInstanceRecord.objects.filter(writeitinstance=self.writeitinstance)
     self.assertEquals(records.count(), 1)
     self.assertTrue(self.writeitinstance.persons.all())
    def test_get_the_url(self):
        form = RelatePopitInstanceWithWriteItInstance(data=self.data, writeitinstance=self.writeitinstance)
        form.is_valid()
        form.relate()

        self.client.login(username="******", password="******")
        url = reverse('relate-writeit-popit', subdomain=self.writeitinstance.slug)
        response = self.client.get(url)
        self.assertEquals(response.status_code, 200)
        self.assertIn('relations', response.context)
        self.assertEquals(len(response.context['relations']), self.writeitinstance.writeitinstancepopitinstancerecord_set.count())
        self.assertTemplateUsed(response, 'nuntium/profiles/writeitinstance_and_popit_relations.html')
        self.assertEquals(response.context['relations'][0], self.writeitinstance.writeitinstancepopitinstancerecord_set.all()[0])
 def test_form_fields(self):
     form = RelatePopitInstanceWithWriteItInstance(data=self.data, writeitinstance=self.writeitinstance)
     self.assertIn('popit_url', form.fields)
     self.assertIsInstance(form.fields['popit_url'], URLField)
     self.assertTrue(form.is_valid())
 def test_form_fields(self):
     form = RelatePopitInstanceWithWriteItInstance(data=self.data, writeitinstance=self.writeitinstance)
     self.assertIn('popit_url', form.fields)
     self.assertIsInstance(form.fields['popit_url'], URLField)
     self.assertTrue(form.is_valid())
    def test_create_form(self):
        form = RelatePopitInstanceWithWriteItInstance(data=self.data, writeitinstance=self.writeitinstance)

        self.assertTrue(form)
        self.assertIsInstance(form, Form)