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
Esempio n. 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
Esempio n. 3
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
Esempio n. 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']
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
Esempio n. 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
Esempio n. 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
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
Esempio n. 9
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']
Esempio n. 10
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
Esempio n. 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
Esempio n. 12
0
def test_new_companies_in_sector_include_already_sent_long_time_ago(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)
    days_ago_four = datetime.utcnow() - timedelta(days=4)
    buyer_one = BuyerFactory.create(sector='AEROSPACE')
    notification = AnonymousEmailNotificationFactory(email=buyer_one.email)
    notification.date_sent = days_ago_four
    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]
Esempio n. 13
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']
Esempio n. 14
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
Esempio n. 15
0
def new_companies_in_sector():
    if lock_acquired('new_companies_in_sector'):
        notifications.new_companies_in_sector()
Esempio n. 16
0
 def run_weekly(self):
     notifications.new_companies_in_sector()