class TestAgencyUnit(TestCase): """Unit tests for Agencies""" def setUp(self): """Set up tests""" self.agency1 = AgencyFactory( fax__phone__number='1-987-654-3211', email__email__email='*****@*****.**', other_emails='[email protected], [email protected]') self.agency2 = AgencyFactory(fax__phone__number='987.654.3210', ) self.agency3 = AgencyFactory(email=None, ) def test_agency_url(self): """Test Agency model's get_absolute_url method""" eq_( self.agency1.get_absolute_url(), reverse('agency-detail', kwargs={ 'idx': self.agency1.pk, 'slug': self.agency1.slug, 'jurisdiction': self.agency1.jurisdiction.slug, 'jidx': self.agency1.jurisdiction.pk })) def test_agency_get_email(self): """Test the get emails method""" eq_(self.agency1.get_emails().first().email, '*****@*****.**') eq_(self.agency3.get_emails().first(), None) def test_agency_get_faxes(self): """Test the ganecy get faces method""" eq_(self.agency2.get_faxes().first().number, '19876543210') def test_agency_get_emails(self): """Test get emails method""" eq_( set(e.email for e in self.agency1.get_emails(email_type='cc')), set(['*****@*****.**', '*****@*****.**']), ) def test_agency_get_proxy_info(self): """Test an agencies get_proxy_info method""" agency_ = AgencyFactory() proxy_info = agency_.get_proxy_info() eq_(proxy_info['proxy'], False) eq_(proxy_info['missing_proxy'], False) assert_not_in('from_user', proxy_info) assert_not_in('warning', proxy_info) proxy_placeholder = UserFactory(username='******') agency_ = AgencyFactory(requires_proxy=True) proxy_info = agency_.get_proxy_info() eq_(proxy_info['proxy'], True) eq_(proxy_info['missing_proxy'], True) eq_(proxy_info['from_user'], proxy_placeholder) assert_in('warning', proxy_info) proxy = UserFactory( profile__acct_type='proxy', profile__state=agency_.jurisdiction.legal.abbrev, ) proxy_info = agency_.get_proxy_info() eq_(proxy_info['proxy'], True) eq_(proxy_info['missing_proxy'], False) eq_(proxy_info['from_user'], proxy) assert_in('warning', proxy_info)
class TestAgencyUnit(TestCase): """Unit tests for Agencies""" def setUp(self): """Set up tests""" self.agency1 = AgencyFactory( fax__phone__number="1-987-654-3211", email__email__email="*****@*****.**", other_emails="[email protected], [email protected]", ) self.agency2 = AgencyFactory(fax__phone__number="987.654.3210") self.agency3 = AgencyFactory(email=None) def test_agency_url(self): """Test Agency model's get_absolute_url method""" eq_( self.agency1.get_absolute_url(), reverse( "agency-detail", kwargs={ "idx": self.agency1.pk, "slug": self.agency1.slug, "jurisdiction": self.agency1.jurisdiction.slug, "jidx": self.agency1.jurisdiction.pk, }, ), ) def test_agency_get_email(self): """Test the get emails method""" eq_(self.agency1.get_emails().first().email, "*****@*****.**") eq_(self.agency3.get_emails().first(), None) def test_agency_get_faxes(self): """Test the ganecy get faces method""" eq_(self.agency2.get_faxes().first().number, "19876543210") def test_agency_get_emails(self): """Test get emails method""" eq_( set(e.email for e in self.agency1.get_emails(email_type="cc")), set(["*****@*****.**", "*****@*****.**"]), ) def test_agency_get_proxy_info(self): """Test an agencies get_proxy_info method""" agency_ = AgencyFactory() proxy_info = agency_.get_proxy_info() eq_(proxy_info["proxy"], False) eq_(proxy_info["missing_proxy"], False) assert_not_in("from_user", proxy_info) assert_not_in("warning", proxy_info) agency_ = AgencyFactory(requires_proxy=True) proxy_info = agency_.get_proxy_info() eq_(proxy_info["proxy"], True) eq_(proxy_info["missing_proxy"], True) assert_not_in("from_user", proxy_info) assert_in("warning", proxy_info) proxy = UserFactory( profile__proxy=True, profile__state=agency_.jurisdiction.legal.abbrev ) proxy_info = agency_.get_proxy_info() eq_(proxy_info["proxy"], True) eq_(proxy_info["missing_proxy"], False) eq_(proxy_info["from_user"], proxy) assert_in("warning", proxy_info) def test_agency_relations(self): """Pins the number of relations If we add a relation we must take into account how we want to handle it during a merge """ # Relations pointing to the Agency model eq_( len( [ f for f in Agency._meta.get_fields() if f.is_relation and f.auto_created ] ), 16, ) # Many to many relations defined on the agency model eq_( len( [ f for f in Agency._meta.get_fields() if f.many_to_many and not f.auto_created ] ), 4, ) def test_agency_merge(self): """Test agency merging""" good_agency = AgencyFactory(status="approved", email=None, fax=None) bad_agency = AgencyFactory(status="approved", email=None, fax=None) appeal_agency = AgencyFactory(appeal_agency=bad_agency) foia = FOIARequestFactory(agency=bad_agency) composer = FOIAComposerFactory(agencies=[bad_agency]) user = UserFactory() email = EmailAddressFactory() AgencyEmailFactory(agency=good_agency, email=email, email_type="to") AgencyEmailFactory(agency=bad_agency, email=email, email_type="cc") fax1 = PhoneNumberFactory() fax2 = PhoneNumberFactory() AgencyPhoneFactory(agency=good_agency, phone=fax1, request_type="primary") AgencyPhoneFactory(agency=bad_agency, phone=fax2, request_type="primary") good_agency.merge(bad_agency, user) bad_agency.refresh_from_db() appeal_agency.refresh_from_db() foia.refresh_from_db() composer.refresh_from_db() eq_(bad_agency.status, "rejected") eq_(foia.agency, good_agency) eq_(composer.agencies.first(), good_agency) eq_(appeal_agency.appeal_agency, good_agency) # email that already exists is not copied over eq_(good_agency.emails.count(), 1) eq_(good_agency.agencyemail_set.first().email_type, "to") # phone number that doesnt exist is copied over eq_(good_agency.phones.count(), 2) # existing phone number is unaffected ok_( good_agency.agencyphone_set.filter( phone=fax1, request_type="primary" ).exists() ) # its type is set to none when copied over ok_( good_agency.agencyphone_set.filter(phone=fax2, request_type="none").exists() ) assert_in(good_agency.name, bad_agency.notes) assert_in(str(good_agency.pk), bad_agency.notes) assert_in(user.username, bad_agency.notes)