def test_I_can_update_a_popit_instance(self): ''' By posting I can update a popit instance and relate their persons with a WriteItInstance ''' popit_load_data() api_instance = ApiInstance.objects.create(url=settings.TEST_POPIT_API_URL) writeitinstance = WriteItInstance.objects.create( name='instance 1', slug='instance-1', owner=self.user) record = WriteitInstancePopitInstanceRecord.objects.create( writeitinstance=writeitinstance, popitapiinstance=api_instance ) url = reverse('rerelate-writeit-popit', kwargs={'pk':record.pk}) c = Client() c.login(username="******", password="******") response = c.post(url) self.assertEquals(response.status_code, 200) api_instance = ApiInstance.objects.get(id=api_instance.id) self.assertTrue(writeitinstance.persons.all()) self.assertTrue(api_instance.person_set.all())
def test_if_memberships_are_no_longer_active(self): ''' If all memberships are no longer active then the contacts should be disabled. Related to #419. ''' with patch('nuntium.popit_api_instance.datetime') as mock_datetime: mock_datetime.today.return_value = datetime(2015, 1, 1) mock_datetime.strptime = lambda *args, **kw: datetime.strptime(*args, **kw) popit_load_data(fixture_name='persons_with_memberships') self.instance.load_persons_from_a_popit_api( settings.TEST_POPIT_API_URL ) # Benito was the boss between 1987-03-21 # and 2007-03-20 benito = self.instance.persons.get(name="Benito") contacts = Contact.objects.filter(person=benito) self.assertFalse(contacts.filter(enabled=True)) # Fiera has been the boss since 2011-03-21 # And she was the boss of another NGO before between 2005-03-21 # and 2009-03-21 fiera = self.instance.persons.get(name="Fiera Feroz") contacts = Contact.objects.filter(person=fiera) self.assertTrue(contacts.filter(enabled=True)) # raton has been the noboss between 1987-03-21 and # will keep on being until 2045-03-20 raton = self.instance.persons.get(name="Ratón Inteligente") contacts = Contact.objects.filter(person=raton) self.assertTrue(contacts.filter(enabled=True))
def test_it_should_update_the_date_every_time_it_is_updated(self): '''As described in http://github.com/ciudadanointeligente/write-it/issues/412 the updated date is not updated''' popit_load_data() # loading data into the popit-api writeitinstance = WriteItInstance.objects.create( name='instance 1', slug='instance-1', owner=self.owner, ) writeitinstance.load_persons_from_a_popit_api(settings.TEST_POPIT_API_URL) popit_instance = ApiInstance.objects.get(url=settings.TEST_POPIT_API_URL) record = WriteitInstancePopitInstanceRecord.objects.get( writeitinstance=writeitinstance, popitapiinstance=popit_instance, ) created_and_updated = timezone.now() - timedelta(days=2) record.updated = created_and_updated record.created = created_and_updated record.save() writeitinstance.load_persons_from_a_popit_api(settings.TEST_POPIT_API_URL) record_again = WriteitInstancePopitInstanceRecord.objects.get(id=record.id) self.assertNotEqual(record_again.updated, created_and_updated) self.assertEquals(record_again.created, created_and_updated)
def test_save_the_instance_with_the_form(self): popit_load_data() form = WriteItInstanceCreateForm(data=self.data, owner=self.user) instance = form.save() self.assertTrue(instance) self.assertEquals(instance.name, "instance") self.assertEquals(instance.owner, self.user) self.assertTrue(instance.persons.all())
def setUp(self): self.owner = User.objects.create_user(username="******", password="******") self.writeitinstance = WriteItInstance.objects.create( name='instance 1', slug='instance-1', owner=self.owner, ) self.data = {"popit_url": settings.TEST_POPIT_API_URL} popit_load_data()
def test_it_returns_a_tuple(self): '''Returns a tuple''' popit_load_data() popit_api_instance, created = PopitApiInstance.objects.get_or_create(url=settings.TEST_POPIT_API_URL) writeitinstance = WriteItInstance.objects.create(name='instance 1', slug='instance-1', owner=self.owner) result = writeitinstance.relate_with_persons_from_popit_api_instance(popit_api_instance) self.assertIsInstance(result, tuple) self.assertTrue(result[0]) self.assertIsNone(result[1])
def test_update_existing_(self): '''Update existing instance with new information coming from popit''' popit_load_data("other_persons") # This means that if I run the task then it should update the persons # I'm running the weekly job by default update_all_popits.delay() persons_after_updating = list(self.writeitinstance.persons.all()) self.assertNotEquals(self.previously_created_persons, persons_after_updating)
def test_post_to_create_an_instance(self): popit_load_data() your_instances_url = reverse('your-instances') c = Client() c.login(username=self.user.username, password='******') url = reverse('create_writeit_instance') self.assertTrue(url) response = c.post(url, data=self.data) self.assertRedirects(response, your_instances_url) instance = WriteItInstance.objects.get(Q(name='instance'), Q(owner=self.user)) self.assertTrue(instance.persons.all())
def test_set_status_in_called_success(self): '''In progress and success status called''' popit_load_data() popit_api_instance, created = PopitApiInstance.objects.get_or_create(url=settings.TEST_POPIT_API_URL) writeitinstance = WriteItInstance.objects.create(name='instance 1', slug='instance-1', owner=self.owner) with patch.object(WriteitInstancePopitInstanceRecord, 'set_status', return_value=None) as set_status: writeitinstance.load_persons_from_a_popit_api(settings.TEST_POPIT_API_URL) calls = [call('inprogress'), call('success')] set_status.assert_has_calls(calls)
def test_get_twice_from_popit_does_not_repeat_the_email(self): '''Ít does not duplicate emails if they are comming in the field preferred email''' popit_load_data(fixture_name='other_people_with_popolo_emails') self.instance.load_persons_from_a_popit_api( settings.TEST_POPIT_API_URL ) self.instance.load_persons_from_a_popit_api( settings.TEST_POPIT_API_URL ) fiera = self.instance.persons.filter(name="Fiera Feroz") contacts = Contact.objects.filter(person=fiera) self.assertEquals(contacts.count(), 1)
def test_load_persons_from_a_popit_api(self): '''Loading persons from a popit api''' popit_load_data() popit_api_instance, created = PopitApiInstance.objects.get_or_create(url=settings.TEST_POPIT_API_URL) writeitinstance = WriteItInstance.objects.create(name='instance 1', slug='instance-1', owner=self.owner) writeitinstance.relate_with_persons_from_popit_api_instance(popit_api_instance) self.assertEquals(writeitinstance.persons.all().count(), 2) raton = Person.objects.get(name='Ratón Inteligente') fiera = Person.objects.get(name="Fiera Feroz") self.assertIn(raton, [r for r in writeitinstance.persons.all()]) self.assertIn(fiera, [r for r in writeitinstance.persons.all()])
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_load_data('other_persons') # This json contains a different person named Benito 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_it_does_not_replicate_contacts(self): '''It does not replicate a contact several times''' popit_load_data(fixture_name='persons_with_emails') self.instance.load_persons_from_a_popit_api( settings.TEST_POPIT_API_URL ) self.instance.load_persons_from_a_popit_api( settings.TEST_POPIT_API_URL ) User.objects.create_user(username="******", password="******") fiera = self.instance.persons.filter(name="Fiera Feroz") # fiera should have at least one contact commig from popit contacts = Contact.objects.filter(person=fiera) self.assertEquals(contacts.count(), 1)
def test_it_does_not_try_to_replicate_the_memberships(self): '''This is related to issue #429''' popit_load_data() popit_api_instance, created = PopitApiInstance.objects.get_or_create(url=settings.TEST_POPIT_API_URL) writeitinstance = WriteItInstance.objects.create(name='instance 1', slug='instance-1', owner=self.owner) # Doing it twice so I can replicate the bug writeitinstance.relate_with_persons_from_popit_api_instance(popit_api_instance) writeitinstance.relate_with_persons_from_popit_api_instance(popit_api_instance) amount_of_memberships = Membership.objects.filter(writeitinstance=writeitinstance).count() # There are only 2 self.assertEquals(amount_of_memberships, 2) self.assertEquals(amount_of_memberships, writeitinstance.persons.count())
def setUp(self): super(PeriodicallyPullFromPopitClass, self).setUp() popit_load_data() self.owner = User.objects.get(id=1) #this is the popit_api_instance created based on the previous load self.writeitinstance = WriteItInstance.objects.create(name='instance 1', slug='instance-1', owner=self.owner) self.popit_api_instance, created = PopitApiInstance.objects.get_or_create(url=settings.TEST_POPIT_API_URL) WriteitInstancePopitInstanceRecord.objects.create( writeitinstance=self.writeitinstance, popitapiinstance=self.popit_api_instance ) #loading data from popit in a sync way self.writeitinstance._load_persons_from_a_popit_api(self.popit_api_instance) self.previously_created_persons = list(self.writeitinstance.persons.all())
def test_bug_506(self): '''If the same email is in preferred email and in the list of contact_details it creates a single one''' popit_load_data(fixture_name='person_with_preferred_email_and_contact_detail') self.instance.load_persons_from_a_popit_api( settings.TEST_POPIT_API_URL ) fiera = self.instance.persons.filter(name="Fiera Feroz") contacts = Contact.objects.filter(person=fiera) self.assertEquals(contacts.count(), 1) # I'm prefering the one with popit_id and stuff the_contact = contacts[0] self.assertTrue(the_contact.popit_identifier)
def test_not_replicate_contact_even_if_value_changes(self): '''The value of an email has changed in popit but in writeit it should just update the value''' # Creating and loading the data with popit_load_data(fixture_name='persons_with_emails'): self.instance.load_persons_from_a_popit_api( settings.TEST_POPIT_API_URL) # Updating the data and loading again with popit_load_data(fixture_name='persons_with_emails2'): self.instance.load_persons_from_a_popit_api( settings.TEST_POPIT_API_URL) fiera = self.instance.persons.filter(name="Fiera Feroz") contacts = Contact.objects.filter(person=fiera) self.assertEquals(contacts.count(), 1)
def test_get_emails_in_the_popolo_format(self): '''Get emails contact if it comes in the popolo format''' popit_load_data(fixture_name='other_people_with_popolo_emails') self.instance.load_persons_from_a_popit_api( settings.TEST_POPIT_API_URL ) fiera = self.instance.persons.filter(name="Fiera Feroz") # fiera should have at least one contact commig from popit contacts = Contact.objects.filter(person=fiera) self.assertTrue(contacts) contact = contacts[0] contact_type = ContactType.objects.get(name="e-mail") self.assertEquals(contact.contact_type, contact_type) self.assertEquals(contact.value, "*****@*****.**")
def test_creating_an_instance(self): '''Create an instance of writeit using a form that contains a popit_url''' # We have a popit running locally using the # start_local_popit_api.bash script popit_load_data() #loading data into the popit-api data = { 'owner' : self.user.id , 'popit_url' : settings.TEST_POPIT_API_URL, 'name' : "instance", "rate_limiter": 0 } form = WriteItInstanceCreateFormPopitUrl(data) instance = form.save() self.assertTrue(instance.persons.all())
def test_create_an_instance_and_load_persons_from_an_api(self): # We have a popit running locally using the # start_local_popit_api.bash script popit_load_data() #loading data into the popit-api writeitinstance = WriteItInstance.objects.create(name='instance 1', slug='instance-1', owner=self.owner) writeitinstance.load_persons_from_a_popit_api(settings.TEST_POPIT_API_URL) self.assertEquals(writeitinstance.persons.all().count(), 2) raton = Person.objects.get(name='Ratón Inteligente') fiera = Person.objects.get(name="Fiera Feroz") self.assertIn(raton, [r for r in writeitinstance.persons.all()]) self.assertIn(fiera, [r for r in writeitinstance.persons.all()])
def test_clean_memberships(self): '''As part of bug #429 there can be several Membership between one writeitinstance and a person''' popit_api_instance, created = PopitApiInstance.objects.get_or_create( url=settings.TEST_POPIT_API_URL) writeitinstance = WriteItInstance.objects.create(name='instance 1', slug='instance-1', owner=self.owner) # there should be an amount of memberships writeitinstance.relate_with_persons_from_popit_api_instance( popit_api_instance) amount_of_memberships = Membership.objects.filter( writeitinstance=writeitinstance).count() previous_memberships = list( Membership.objects.filter(writeitinstance=writeitinstance)) person = writeitinstance.persons.all()[0] # Creating a new one Membership.objects.create(writeitinstance=writeitinstance, person=person) try: with popit_load_data(): writeitinstance.relate_with_persons_from_popit_api_instance( popit_api_instance) except Membership.MultipleObjectsReturned, e: self.fail("There are more than one Membership " + e)
def test_it_does_not_autosync_if_disabled(self): '''If the instance has autosync disabled then it does not sync''' writeitinstance_popit_record = WriteitInstancePopitInstanceRecord.objects.get( writeitinstance=self.writeitinstance, popitapiinstance=self.popit_api_instance ) # Periodicity = 0 means that it is never going to send anything writeitinstance_popit_record.periodicity = 0 writeitinstance_popit_record.save() # The record has been set to autosync False popit_load_data("other_persons") update_all_popits.delay() # Loading new data persons_after_updating = list(self.writeitinstance.persons.all()) # It should not have updated our writeit instance self.assertEquals(self.previously_created_persons, persons_after_updating)
def test_not_replicate_contact_even_if_value_changes(self): '''The value of an email has changed in popit but in writeit it should just update the value''' # Creating and loading the data with popit_load_data(fixture_name='persons_with_emails'): self.instance.load_persons_from_a_popit_api( settings.TEST_POPIT_API_URL ) # Updating the data and loading again with popit_load_data(fixture_name='persons_with_emails2'): self.instance.load_persons_from_a_popit_api( settings.TEST_POPIT_API_URL ) fiera = self.instance.persons.filter(name="Fiera Feroz") contacts = Contact.objects.filter(person=fiera) self.assertEquals(contacts.count(), 1)
def test_it_pulls_and_creates_contacts(self): '''When pulling from popit it also creates emails''' popit_load_data(fixture_name='persons_with_emails') self.instance.load_persons_from_a_popit_api( settings.TEST_POPIT_API_URL ) User.objects.create_user(username="******", password="******") fiera = self.instance.persons.filter(name="Fiera Feroz") # fiera should have at least one contact commig from popit contacts = Contact.objects.filter(person=fiera) self.assertTrue(contacts) contact = contacts[0] contact_type = ContactType.objects.get(name="e-mail") self.assertEquals(contact.contact_type, contact_type) self.assertEquals(contact.value, "*****@*****.**") self.assertEquals(contact.writeitinstance, self.instance)
def test_clean_memberships(self): '''As part of bug #429 there can be several Membership between one writeitinstance and a person''' popit_load_data() popit_api_instance, created = PopitApiInstance.objects.get_or_create(url=settings.TEST_POPIT_API_URL) writeitinstance = WriteItInstance.objects.create(name='instance 1', slug='instance-1', owner=self.owner) # there should be an amount of memberships writeitinstance.relate_with_persons_from_popit_api_instance(popit_api_instance) amount_of_memberships = Membership.objects.filter(writeitinstance=writeitinstance).count() previous_memberships = list(Membership.objects.filter(writeitinstance=writeitinstance)) person = writeitinstance.persons.all()[0] # Creating a new one Membership.objects.create(writeitinstance=writeitinstance, person=person) try: writeitinstance.relate_with_persons_from_popit_api_instance(popit_api_instance) except Membership.MultipleObjectsReturned, e: self.fail("There are more than one Membership " + e)
def test_autosyncs_receiving_a_parameter_with_the_periodicity(self): '''It can receive a parameter refering to the periodicity''' writeitinstance_popit_record = WriteitInstancePopitInstanceRecord.objects.get( writeitinstance=self.writeitinstance, popitapiinstance=self.popit_api_instance ) writeitinstance_popit_record.periodicity = '1D' # Daily writeitinstance_popit_record.save() popit_load_data("other_persons") # Now because it is receiving the default value = '1W' # it should not pull anyone update_all_popits.delay() persons_after_updating = list(self.writeitinstance.persons.all()) self.assertEquals(self.previously_created_persons, persons_after_updating) # But If I tell the runner that I'm running the daily # process then it should change it update_all_popits.delay(periodicity="1D") persons_after_updating = list(self.writeitinstance.persons.all()) self.assertNotEquals(self.previously_created_persons, persons_after_updating)
def test_it_should_be_able_to_update_twice(self): '''It should be able to update all data twice''' popit_load_data() #loading data into the popit-api writeitinstance = WriteItInstance.objects.create(\ name='instance 1', \ slug='instance-1', \ owner=self.owner) writeitinstance.load_persons_from_a_popit_api(settings.TEST_POPIT_API_URL) popit_instance = ApiInstance.objects.get(url=settings.TEST_POPIT_API_URL) writeitinstance.load_persons_from_a_popit_api(settings.TEST_POPIT_API_URL) record = WriteitInstancePopitInstanceRecord.objects.get(\ writeitinstance=writeitinstance, popitapiinstance=popit_instance ) self.assertNotEqual(record.created, record.updated)
def test_it_is_created_automatically_when_fetching_a_popit_instance(self): '''create automatically a record when fetching a popit instance''' popit_load_data() #loading data into the popit-api writeitinstance = WriteItInstance.objects.create(\ name='instance 1', \ slug='instance-1', \ owner=self.owner) writeitinstance.load_persons_from_a_popit_api(settings.TEST_POPIT_API_URL) popit_instance = ApiInstance.objects.get(url=settings.TEST_POPIT_API_URL) record = WriteitInstancePopitInstanceRecord.objects.get(\ writeitinstance=writeitinstance, popitapiinstance=popit_instance ) self.assertTrue(record) self.assertTrue(record.updated) self.assertTrue(record.created)
def test_create_and_pull_people_from_a_popit_api(self): # loading data into the popit-api popit_load_data() instance_data = { 'name': 'The instance', 'slug': 'the-instance', 'popit-api': settings.TEST_POPIT_API_URL, } url = '/api/v1/instance/' response = self.api_client.post(url, data=instance_data, format='json', authentication=self.get_credentials()) self.assertHttpCreated(response) match_id = re.match(r'^http://testserver/api/v1/instance/(?P<id>\d+)/?', response['Location']) instance = WriteItInstance.objects.get(id=match_id.group('id')) self.assertEquals(instance.persons.count(), 2) #this should not break raton = Person.objects.get(name=u'Ratón Inteligente') fiera = Person.objects.get(name=u"Fiera Feroz") self.assertIn(raton, [r for r in instance.persons.all()]) self.assertIn(fiera, [r for r in instance.persons.all()])
def setUp(self): super(PeriodicallyPullFromPopitClass, self).setUp() self.owner = User.objects.get(id=1) #this is the popit_api_instance created based on the previous load self.writeitinstance = WriteItInstance.objects.create( name='instance 1', slug='instance-1', owner=self.owner) self.popit_api_instance, created = PopitApiInstance.objects.get_or_create( url=settings.TEST_POPIT_API_URL) WriteitInstancePopitInstanceRecord.objects.create( writeitinstance=self.writeitinstance, popitapiinstance=self.popit_api_instance) #loading data from popit in a sync way with popit_load_data(): self.writeitinstance._load_persons_from_a_popit_api( self.popit_api_instance) self.previously_created_persons = list( self.writeitinstance.persons.all())