예제 #1
0
def test_upload_buyers_csv_to_s3(mocked_upload_file_object_to_s3):
    BuyerFactory.create_batch(5)
    call_command('generate_buyers_csv_dump')
    assert mocked_upload_file_object_to_s3.called
    assert mocked_upload_file_object_to_s3.call_args == mock.call(
        file_object=mock.ANY,
        key=settings.BUYERS_CSV_FILE_NAME,
        bucket=settings.CSV_DUMP_BUCKET_NAME,
    )
예제 #2
0
def test_new_companies_in_sector_exclude_suppliers_without_companies(
        mock_task, settings):
    settings.NEW_COMPANIES_IN_SECTOR_FREQUENCY_DAYS = 3
    settings.NEW_COMPANIES_IN_SECTOR_SUBJECT = 'test subject'

    BuyerFactory.create(sector='AEROSPACE')

    notifications.new_companies_in_sector()

    assert mock_task.delay.called is False
예제 #3
0
def test_new_companies_in_sector_exclude_suppliers_without_companies(settings):
    settings.NEW_COMPANIES_IN_SECTOR_FREQUENCY_DAYS = 3
    settings.NEW_COMPANIES_IN_SECTOR_SUBJECT = 'test subject'

    BuyerFactory.create(sector='AEROSPACE')

    mail.outbox = []  # reset after emails sent by signals
    notifications.new_companies_in_sector()

    assert len(mail.outbox) == 0
예제 #4
0
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']
예제 #5
0
    def test_download_csv(self):
        buyer = BuyerFactory()

        data = {
            'action': 'download_csv',
            '_selected_action': Buyer.objects.all().values_list('pk',
                                                                flat=True)
        }
        response = self.client.post(reverse('admin:buyer_buyer_changelist'),
                                    data,
                                    follow=True)

        expected_data = OrderedDict([
            ('company_name', str(buyer.company_name)),
            ('country', str(buyer.country)),
            ('created', str(buyer.created)),
            ('email', buyer.email),
            ('id', str(buyer.id)),
            ('modified', str(buyer.modified)),
            ('name', buyer.name),
            ('sector', buyer.sector),
        ])
        actual = str(response.content, 'utf-8').split('\r\n')

        assert actual[0] == ','.join(expected_data.keys())
        assert actual[1] == ','.join(expected_data.values())
예제 #6
0
def test_new_companies_in_sector_exclude_already_sent_recently(
        mock_task, settings):
    settings.NEW_COMPANIES_IN_SECTOR_FREQUENCY_DAYS = 3
    settings.NEW_COMPANIES_IN_SECTOR_SUBJECT = 'test subject'

    days_ago_three = datetime.utcnow() - timedelta(days=3)
    buyer_one = BuyerFactory.create(sector='AEROSPACE')
    buyer_two = BuyerFactory.create(sector='AEROSPACE')

    notification = AnonymousEmailNotificationFactory(email=buyer_two.email)
    notification.date_sent = days_ago_three
    notification.save()
    CompanyFactory(sectors=['AEROSPACE'], 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_one.email
예제 #7
0
def test_new_companies_in_sector_exclude_unsbscribed(mock_task, settings):
    settings.NEW_COMPANIES_IN_SECTOR_FREQUENCY_DAYS = 3
    settings.NEW_COMPANIES_IN_SECTOR_SUBJECT = 'test subject'

    days_ago_three = datetime.utcnow() - timedelta(days=3)
    buyer_one = BuyerFactory.create(sector='AEROSPACE')
    buyer_two = BuyerFactory.create(sector='AEROSPACE')
    AnonymousUnsubscribeFactory(email=buyer_two.email)

    CompanyFactory(sectors=['AEROSPACE'], date_published=days_ago_three)

    notifications.new_companies_in_sector()

    assert len(mock_task.delay.call_args_list) == 1
    call_args = mock_task.delay.call_args[1]
    assert call_args['recipient_email'] == buyer_one.email
    assert call_args['from_email'] == settings.FAS_FROM_EMAIL
예제 #8
0
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
예제 #9
0
def test_new_companies_in_sector_exclude_unsbscribed(settings):
    settings.NEW_COMPANIES_IN_SECTOR_FREQUENCY_DAYS = 3
    settings.NEW_COMPANIES_IN_SECTOR_SUBJECT = 'test subject'

    days_ago_three = datetime.utcnow() - timedelta(days=3)
    buyer_one = BuyerFactory.create(sector='AEROSPACE')
    buyer_two = BuyerFactory.create(sector='AEROSPACE')
    AnonymousUnsubscribeFactory(email=buyer_two.email)

    CompanyFactory(sectors=['AEROSPACE'], 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_one.email]
    assert mail.outbox[0].from_email == settings.FAS_FROM_EMAIL
예제 #10
0
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']
예제 #11
0
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
예제 #12
0
def test_new_companies_in_sector_company_multiple_sectors(mock_task, settings):
    settings.NEW_COMPANIES_IN_SECTOR_FREQUENCY_DAYS = 3

    days_ago_three = datetime.utcnow() - timedelta(days=3)
    BuyerFactory.create(sector='AEROSPACE', email='*****@*****.**')
    BuyerFactory.create(sector='AIRPORTS', email='*****@*****.**')

    company_one = CompanyFactory(sectors=['AEROSPACE', 'AIRPORTS'],
                                 date_published=days_ago_three)
    company_two = CompanyFactory(sectors=['AIRPORTS'],
                                 date_published=days_ago_three)

    notifications.new_companies_in_sector()
    unsubscribe_url = ('http://supplier.trade.great:8005/unsubscribe?email='
                       'jim%40example.com%3A2Kkc4EAEos2htrZXeLj73CSVBWA')

    assert len(mock_task.delay.call_args_list) == 1
    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']
    assert unsubscribe_url in mock_task.delay.call_args[1]['text_body']
예제 #13
0
def test_new_companies_in_sector_exclude_already_sent_recently(settings):
    settings.NEW_COMPANIES_IN_SECTOR_FREQUENCY_DAYS = 3
    settings.NEW_COMPANIES_IN_SECTOR_SUBJECT = 'test subject'

    days_ago_three = datetime.utcnow() - timedelta(days=3)
    buyer_one = BuyerFactory.create(sector='AEROSPACE')
    buyer_two = BuyerFactory.create(sector='AEROSPACE')

    notification = AnonymousEmailNotificationFactory(email=buyer_two.email)
    notification.date_sent = days_ago_three
    notification.save()

    CompanyFactory(sectors=['AEROSPACE'], 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_one.email]
예제 #14
0
def test_new_companies_in_sector_records_notification(mock_task, settings):
    settings.NEW_COMPANIES_IN_SECTOR_FREQUENCY_DAYS = 3

    days_ago_three = datetime.utcnow() - timedelta(days=3)
    buyer_one = BuyerFactory.create(sector='AEROSPACE')
    CompanyFactory(sectors=['AEROSPACE'], date_published=days_ago_three)

    notifications.new_companies_in_sector()

    assert len(mock_task.delay.call_args_list) == 1
    call_args = mock_task.delay.call_args[1]
    assert call_args['recipient_email'] == buyer_one.email
예제 #15
0
def test_new_companies_in_sector_company_multiple_sectors(settings):
    settings.NEW_COMPANIES_IN_SECTOR_FREQUENCY_DAYS = 3

    days_ago_three = datetime.utcnow() - timedelta(days=3)
    BuyerFactory.create(sector='AEROSPACE', email='*****@*****.**')
    BuyerFactory.create(sector='AIRPORTS', email='*****@*****.**')

    company_one = CompanyFactory(sectors=['AEROSPACE', 'AIRPORTS'],
                                 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()
    unsubscribe_url = (
        'http://supplier.trade.great.dev:8005/unsubscribe?email='
        'jim%40example.com%3A2Kkc4EAEos2htrZXeLj73CSVBWA')

    assert len(mail.outbox) == 1
    assert mail.outbox[0].body.count(company_one.name) == 1
    assert mail.outbox[0].body.count(company_two.name) == 1
    assert unsubscribe_url in mail.outbox[0].body
예제 #16
0
def test_new_companies_in_sector_records_notification(settings):
    settings.NEW_COMPANIES_IN_SECTOR_FREQUENCY_DAYS = 3

    days_ago_three = datetime.utcnow() - timedelta(days=3)
    buyer_one = BuyerFactory.create(sector='AEROSPACE')
    CompanyFactory(sectors=['AEROSPACE'], date_published=days_ago_three)

    mail.outbox = []  # reset after emails sent by signals
    notifications.new_companies_in_sector()

    assert len(mail.outbox) == 1

    notification_record = AnonymousEmailNotification.objects.first()
    assert AnonymousEmailNotification.objects.count() == 1
    assert notification_record.email == buyer_one.email
    assert notification_record.category == constants.NEW_COMPANIES_IN_SECTOR
예제 #17
0
def test_buyer_name():
    buyer = BuyerFactory()
    assert str(buyer) == buyer.name