def test_has_valid_address_all_values_present(): company = CompanyFactory( postal_full_name='Mr Fakingham', address_line_1='123 fake street', postal_code='EM6 6EM', ) assert company.has_valid_address() is True
def test_does_not_send_verification_letter_on_update(settings): settings.FEATURE_VERIFICATION_LETTERS_ENABLED = True with mock.patch('requests.post') as requests_mock: company = CompanyFactory(name="Original name") company.name = "Changed name" company.save() requests_mock.assert_called_once_with( 'https://dash.stannp.com/api/v1/letters/create', auth=('debug', ''), data={ 'test': True, 'recipient[company_name]': 'Original name', 'recipient[country]': company.country, 'recipient[date]': datetime.date.today().strftime('%d/%m/%Y'), 'recipient[address1]': company.address_line_1, 'recipient[full_name]': company.postal_full_name, 'recipient[city]': company.locality, 'recipient[company]': 'Original name', 'recipient[postcode]': company.postal_code, 'recipient[title]': company.postal_full_name, 'recipient[address2]': company.address_line_2, 'recipient[verification_code]': company.verification_code, 'template': 'debug' }, )
def test_companies_in_post_set_to_published(self): companies = CompanyFactory.create_batch(5, is_published=False) published_company = CompanyFactory(is_published=True) numbers = '{num1},{num2}'.format(num1=companies[0].number, num2=companies[3].number) response = self.client.post( reverse('admin:company_company_publish'), {'company_numbers': numbers}, ) assert response.status_code == http.client.FOUND assert response.url == reverse('admin:company_company_changelist') published = Company.objects.filter(is_published=True).values_list( 'number', flat=True) assert len(published) == 3 assert companies[0].number in published assert companies[3].number in published assert published_company.number in published unpublished = Company.objects.filter(is_published=False).values_list( 'number', flat=True) assert len(unpublished) == 3 assert companies[1].number in unpublished assert companies[2].number in unpublished assert companies[4].number in unpublished
def test_has_valid_address_all_values_missing(): company = CompanyFactory( postal_full_name='', address_line_1='', postal_code='', ) assert company.has_valid_address() is False
def test_send_letter(mock_stannp_client): company = CompanyFactory(verification_code='test') send_verification_letter(company) mock_stannp_client.send_letter.assert_called_with(recipient={ 'postal_full_name': company.postal_full_name, 'address_line_1': company.address_line_1, 'address_line_2': company.address_line_2, 'locality': company.locality, 'country': company.country, 'postal_code': company.postal_code, 'po_box': company.po_box, 'custom_fields': [('full_name', company.postal_full_name), ('company_name', company.name), ('verification_code', company.verification_code), ('date', datetime.date.today().strftime('%d/%m/%Y')), ('company', company.name)] }, template='debug') company.refresh_from_db() assert company.is_verification_letter_sent assert company.date_verification_letter_sent == timezone.now()
def test_does_not_overwrite_verification_code_if_already_set(settings): settings.FEATURE_VERIFICATION_LETTERS_ENABLED = True with mock.patch('requests.post'): company = CompanyFactory(verification_code='test') company.refresh_from_db() assert company.verification_code == 'test'
def test_retrieve_missing_company_date(mock_get_companies_house_profile): company = CompanyFactory(date_of_creation=None, number=123) mock_get_companies_house_profile.return_value = { 'date_of_creation': '2016-12-16' } call_command('retrieve_missing_company_details') company.refresh_from_db() assert company.date_of_creation == datetime.date(2016, 12, 16)
def test_companies_house_non_active_status(mock_get_companies_house_profile): company = CompanyFactory(date_of_creation=None, number=123) mock_get_companies_house_profile.return_value = { 'company_status': 'dissolved', } call_command('retrieve_companies_house_company_status') company.refresh_from_db() assert company.companies_house_company_status == 'dissolved'
def test_retrieve_company_status_handles_error( mock_get_companies_house_profile): company = CompanyFactory(number=123) mock_get_companies_house_profile.side_effect = Exception() call_command('retrieve_companies_house_company_status') company.refresh_from_db() assert company.companies_house_company_status == ''
def test_companies_house_missing_status_on_update( mock_get_companies_house_profile): company = CompanyFactory(date_of_creation=None, number=123, companies_house_company_status='active') mock_get_companies_house_profile.return_value = {} call_command('retrieve_companies_house_company_status') company.refresh_from_db() assert company.companies_house_company_status == ''
def setUp(self): self.client = APIClient() self.company1 = CompanyFactory() self.company1_headquarter = OfficeFactory(company=self.company1, headquarter=True) OfficeFactory(company=self.company1) self.company2 = CompanyFactory() self.company2_headquarter = OfficeFactory(company=self.company2, headquarter=True) OfficeFactory(company=self.company2)
def test_new_companies_in_sector(mock_task, settings): settings.NEW_COMPANIES_IN_SECTOR_FREQUENCY_DAYS = 3 expected_subject = email.NewCompaniesInSectorNotification.subject days_ago_three = datetime.utcnow() - timedelta(days=3) days_ago_four = datetime.utcnow() - timedelta(days=4) buyer_one = BuyerFactory.create(sector='AEROSPACE') buyer_two = BuyerFactory.create(sector='AEROSPACE') buyer_three = BuyerFactory.create(sector='CONSTRUCTION') company_one = CompanyFactory( sectors=['AEROSPACE'], date_published=days_ago_three, ) company_two = CompanyFactory( sectors=['AEROSPACE'], date_published=days_ago_four, ) company_three = CompanyFactory( sectors=['CONSTRUCTION'], date_published=days_ago_three, ) notifications.new_companies_in_sector() call_args_list = mock_task.delay.call_args_list assert len(call_args_list) == 3 email_one = list( filter(lambda x: x[1]['recipient_email'] == buyer_one.email, call_args_list))[0][1] email_two = list( filter(lambda x: x[1]['recipient_email'] == buyer_two.email, call_args_list))[0][1] email_three = list( filter(lambda x: x[1]['recipient_email'] == buyer_three.email, call_args_list))[0][1] assert email_one['recipient_email'] == buyer_one.email assert email_one['subject'] == expected_subject assert company_one.name in email_one['text_body'] assert company_two.name not in email_one['text_body'] assert email_two['recipient_email'] == buyer_two.email assert email_two['subject'] == expected_subject assert company_one.name in email_two['text_body'] assert company_two.name not in email_two['text_body'] assert company_three.name not in email_two['text_body'] assert email_three['recipient_email'] == buyer_three.email assert email_three['subject'] == expected_subject assert company_one.name not in email_three['text_body'] assert company_two.name not in email_three['text_body'] assert company_three.name in email_three['text_body']
def test_legacy_keywords(migration): app = 'company' model_name = 'Company' name = '0044_rebuild_elasticserach_index' migration.before(app, name).get_model(app, model_name) company_one = CompanyFactory.create(keywords='hello, test, foo') company_two = CompanyFactory.create(keywords='hello; test: foo') company_three = CompanyFactory.create(keywords='hello\t test\t foo') migration.apply('company', '0045_auto_20170620_1426') for company in [company_one, company_two, company_three]: company.refresh_from_db() assert company_two.keywords == 'hello, test, foo' assert company_three.keywords == 'hello, test, foo'
def test_companies_in_post_set_to_published(self): companies = CompanyFactory.create_batch( 7, is_published_investment_support_directory=False, is_published_find_a_supplier=False, ) published_company_isd = CompanyFactory( is_published_investment_support_directory=True) numbers = '{num1},{num2}'.format(num1=companies[0].number, num2=companies[3].number) response = self.client.post( reverse('admin:company_company_publish'), { 'company_numbers': numbers, 'directories': ['investment_support_directory', 'find_a_supplier'], }, ) assert response.status_code == http.client.FOUND assert response.url == reverse('admin:company_company_changelist') published_isd = Company.objects.filter( is_published_investment_support_directory=True).values_list( 'number', flat=True) assert len(published_isd) == 3 assert companies[0].number in published_isd assert companies[3].number in published_isd assert published_company_isd.number in published_isd published_fas = Company.objects.filter( is_published_find_a_supplier=True).values_list('number', flat=True) assert len(published_fas) == 2 assert companies[0].number in published_fas assert companies[3].number in published_fas unpublished = Company.objects.filter( is_published_investment_support_directory=False, is_published_find_a_supplier=False, ).values_list('number', flat=True) assert len(unpublished) == 5 assert companies[1].number in unpublished assert companies[2].number in unpublished assert companies[4].number in unpublished
def test_retrieve_missing_company_404_case(mock_get_companies_house_profile): company = CompanyFactory(date_of_creation=None, number=123) mock_get_companies_house_profile.return_value = { 'date_of_creation': '2016-12-16' } call_command('retrieve_missing_company_details') assert company.date_of_creation is None
def test_company_is_verified(code, preverfiied, oauth2, expected): company = CompanyFactory.build( verified_with_preverified_enrolment=preverfiied, verified_with_code=code, verified_with_companies_house_oauth2=oauth2, ) assert company.is_verified is expected
def test_if_never_verified_not_in_stream(api_client): """If the company never verified, then it's not in the activity stream """ with freeze_time('2012-01-14 12:00:02'): CompanyFactory() sender = _auth_sender() response = api_client.get( _url(), content_type='', HTTP_AUTHORIZATION=sender.request_header, HTTP_X_FORWARDED_FOR='1.2.3.4, 123.123.123.123', ) assert response.status_code == status.HTTP_200_OK assert response.json() == { '@context': [ 'https://www.w3.org/ns/activitystreams', { 'dit': 'https://www.trade.gov.uk/ns/activitystreams/v1', } ], 'type': 'Collection', 'orderedItems': [], 'next': 'http://testserver/activity-stream/?after=1326542402.0_3', }
def test_automatic_publish_create(): should_be_published = [ { 'description': 'description', 'email_address': '*****@*****.**', 'verified_with_code': True, }, { 'summary': 'summary', 'email_address': '*****@*****.**', 'verified_with_code': True, }, ] should_be_unpublished = [ { 'description': '', 'summary': '', 'email_address': '*****@*****.**', 'verified_with_code': True, }, { 'description': 'description', 'summary': 'summary', 'email_address': '', 'verified_with_code': True, }, { 'description': 'description', 'summary': 'summary', 'email_address': '*****@*****.**', 'verified_with_code': False, }, ] should_be_force_published = [{ **item, 'is_published': True } for item in should_be_unpublished] for kwargs in should_be_published: assert CompanyFactory(**kwargs).is_published is True for kwargs in should_be_unpublished: assert CompanyFactory(**kwargs).is_published is False for kwargs in should_be_force_published: assert CompanyFactory(**kwargs).is_published is True
def test_new_companies_in_sector(settings): settings.NEW_COMPANIES_IN_SECTOR_FREQUENCY_DAYS = 3 expected_subject = email.NewCompaniesInSectorNotification.subject days_ago_three = datetime.utcnow() - timedelta(days=3) days_ago_four = datetime.utcnow() - timedelta(days=4) buyer_one = BuyerFactory.create(sector='AEROSPACE') buyer_two = BuyerFactory.create(sector='AEROSPACE') buyer_three = BuyerFactory.create(sector='CONSTRUCTION') company_one = CompanyFactory( sectors=['AEROSPACE'], date_published=days_ago_three, ) company_two = CompanyFactory( sectors=['AEROSPACE'], date_published=days_ago_four, ) company_three = CompanyFactory( sectors=['CONSTRUCTION'], date_published=days_ago_three, ) mail.outbox = [] # reset after emails sent by signals notifications.new_companies_in_sector() assert len(mail.outbox) == 3 email_one = next(e for e in mail.outbox if buyer_one.email in e.to) email_two = next(e for e in mail.outbox if buyer_two.email in e.to) email_three = next(e for e in mail.outbox if buyer_three.email in e.to) assert email_one.to == [buyer_one.email] assert email_one.subject == expected_subject assert company_one.name in email_one.body assert company_two.name not in email_one.body assert email_two.to == [buyer_two.email] assert email_two.subject == expected_subject assert company_one.name in email_two.body assert company_two.name not in email_two.body assert company_three.name not in email_two.body assert email_three.to == [buyer_three.email] assert email_three.subject == expected_subject assert company_one.name not in email_three.body assert company_two.name not in email_three.body assert company_three.name in email_three.body
def test_companies_publish_form_handles_whitespace(): companies = CompanyFactory.create_batch(3) data = ' {num1},{num2} , {num3},'.format(num1=companies[0].number, num2=companies[1].number, num3=companies[2].number) form = PublishByCompanyHouseNumberForm(data={'company_numbers': data}) assert form.is_valid() is True
def test_does_not_send_if_letter_already_sent(mock_send_letter, settings): settings.FEATURE_VERIFICATION_LETTERS_ENABLED = True CompanyFactory( is_verification_letter_sent=True, verification_code='test', ) mock_send_letter.assert_not_called()
def test_if_verified_with_code_in_stream_in_date_then_seq_order(api_client): """If the company verified_with_code, then it's in the activity stream """ CompanyFactory(number=10000000) with freeze_time('2012-01-14 12:00:02'): company_a = CompanyFactory(number=10000003, name='a', verified_with_code=True) with freeze_time('2012-01-14 12:00:02'): company_b = CompanyFactory(number=10000002, name='b', verified_with_code=True) with freeze_time('2012-01-14 12:00:01'): company_c = CompanyFactory(number=10000001, name='c', verified_with_code=True) sender = _auth_sender() response = api_client.get( _url(), content_type='', HTTP_AUTHORIZATION=sender.request_header, HTTP_X_FORWARDED_FOR='1.2.3.4, 123.123.123.123', ) items = response.json()['orderedItems'] id_prefix = 'dit:directory:Company:' assert len(items) == 3 assert items[0]['published'] == '2012-01-14T12:00:01+00:00' assert get_companies_house_number(items[0]) == '10000001' assert get_company_name(items[0]) == 'c' assert get_company_id(items[0]) == id_prefix + str(company_c.id) assert items[1]['published'] == '2012-01-14T12:00:02+00:00' assert get_companies_house_number(items[1]) == '10000003' assert get_company_name(items[1]) == 'a' assert get_company_id(items[1]) == id_prefix + str(company_a.id) assert items[2]['published'] == '2012-01-14T12:00:02+00:00' assert get_companies_house_number(items[2]) == '10000002' assert get_company_name(items[2]) == 'b' assert get_company_id(items[2]) == id_prefix + str(company_b.id)
def test_guest_cannot_access_company_publish_view_post(self): url = reverse('admin:company_company_publish') company = CompanyFactory() response = self.client.post(url, {'company_numbers': company.number}) assert response.status_code == http.client.FOUND assert response.url == '/admin/login/?next={redirect_to}'.format( redirect_to=url)
def test_new_companies_in_sector_single_email_per_buyer(mock_task, settings): settings.NEW_COMPANIES_IN_SECTOR_FREQUENCY_DAYS = 3 days_ago_three = datetime.utcnow() - timedelta(days=3) buyer = BuyerFactory.create(sector='AEROSPACE', email='*****@*****.**') BuyerFactory.create(sector='AIRPORTS', email='*****@*****.**') company_one = CompanyFactory(sectors=['AEROSPACE'], date_published=days_ago_three) company_two = CompanyFactory(sectors=['AIRPORTS'], date_published=days_ago_three) notifications.new_companies_in_sector() assert len(mock_task.delay.call_args_list) == 1 assert mock_task.delay.call_args[1]['recipient_email'] == buyer.email assert company_one.name in mock_task.delay.call_args[1]['text_body'] assert company_two.name in mock_task.delay.call_args[1]['text_body']
def test_supplier_with_company_serializer_save(): company = CompanyFactory.create(number='01234567') data = VALID_REQUEST_DATA.copy() data['company'] = company.pk serializer = serializers.SupplierSerializer(data=data) serializer.is_valid() supplier = serializer.save() assert supplier.company == company
def test_retrieve_missing_company_details(mock_get_date_of_creation): mock_get_date_of_creation.return_value = None company_with_date = CompanyFactory(date_of_creation=date(2010, 10, 1)) companies = CompanyFactory.create_batch(2) mock_get_date_of_creation.return_value = date(2010, 10, 10) call_command('retrieve_missing_company_details') assert models.Company.objects.get( number=company_with_date.number ).date_of_creation == date(2010, 10, 1) assert models.Company.objects.get( number=companies[0].number ).date_of_creation == date(2010, 10, 10) assert models.Company.objects.get( number=companies[1].number ).date_of_creation == date(2010, 10, 10)
def test_create_companies_form_existing(self): company = CompanyFactory(number=12355434) assert company.is_uk_isd_company is False file_path = os.path.join( settings.BASE_DIR, 'company/tests/fixtures/valid-companies-upload.csv') response = self.client.post(reverse('admin:company_company_enrol'), { 'generated_for': constants.UK_ISD, 'csv_file': open(file_path, 'rb'), }) assert response.status_code == 200 assert Company.objects.count() == 2 company.refresh_from_db() assert company.is_uk_isd_company is True
def test_new_companies_in_sector_single_email_per_buyer(settings): settings.NEW_COMPANIES_IN_SECTOR_FREQUENCY_DAYS = 3 days_ago_three = datetime.utcnow() - timedelta(days=3) buyer = BuyerFactory.create(sector='AEROSPACE', email='*****@*****.**') BuyerFactory.create(sector='AIRPORTS', email='*****@*****.**') company_one = CompanyFactory(sectors=['AEROSPACE'], date_published=days_ago_three) company_two = CompanyFactory(sectors=['AIRPORTS'], date_published=days_ago_three) mail.outbox = [] # reset after emails sent by signals notifications.new_companies_in_sector() assert len(mail.outbox) == 1 assert mail.outbox[0].to == [buyer.email] assert company_one.name in mail.outbox[0].body assert company_two.name in mail.outbox[0].body
def test_can_publish(desciption, summary, email, is_verified, expected, settings): mock_verifed = mock.PropertyMock(return_value=is_verified) with mock.patch('company.models.Company.is_verified', mock_verifed): company = CompanyFactory.build( description=desciption, summary=summary, email_address=email, ) assert company.is_publishable is expected
def test_preverified_retrieve_company_succcess(authed_client): company = CompanyFactory() url = reverse( 'enrolment-preverified', kwargs={'key': signing.Signer().sign(company.number)} ) response = authed_client.get(url) assert response.status_code == 200