def test_notify_completed_flights(self): backend = get_backend() backend.reset_messages() notify_of_completed_flights() messages = backend.retrieve_messages() # Shouldn't be any completed flight messages self.assertEqual(len(messages), 0) # "Complete" the flight self.flight.sold_clicks = 1 self.flight.total_views = 1 self.flight.total_clicks = 1 self.flight.save() get( Offer, advertisement=self.ad1, publisher=self.publisher, viewed=True, clicked=True, ) daily_update_impressions() backend.reset_messages() notify_of_completed_flights() # Should be one message for the completed flight now messages = backend.retrieve_messages() self.assertEqual(len(messages), 1) # Flight should no longer be live self.flight.refresh_from_db() self.assertFalse(self.flight.live)
def test_400_bad_request(self): backend = get_backend() backend.reset_messages() submission = ApplicationSubmissionFactory() adapter = SlackAdapter() message = '' message_status = adapter.send_message(message, '', source=submission) messages = backend.retrieve_messages() self.assertEqual(len(messages), 0) self.assertEqual(message_status, '400: Bad Request')
def test_correct_payload(self): backend = get_backend() backend.reset_messages() submission = ApplicationSubmissionFactory() adapter = SlackAdapter() message = 'my message' adapter.send_message(message, '', source=submission) messages = backend.retrieve_messages() self.assertEqual(len(messages), 1) message_payload = json.loads(messages[0]['payload']) self.assertEqual(message_payload['text'], message)
def test_send(self, send): from django_slack.celery_task import celery_task from celery.registry import tasks BACKEND = 'django_slack.backends.CeleryBackend' with override_settings(SLACK_BACKEND=BACKEND): backend = get_backend() self.assertIsInstance(backend, CeleryBackend) # make sure we auto register the celery task self.assertEqual(tasks['django_slack.tasks._sender'], celery_task) self.assertTrue(hasattr(celery_task, 'delay')) self.assertTrue(hasattr(celery_task, 'apply_async'))
def test_fund_custom_slack_channel(self): backend = get_backend() backend.reset_messages() responses.add(responses.POST, self.target_url, status=200, body='OK') submission = ApplicationSubmissionFactory( round__parent__slack_channel='dummy') adapter = SlackAdapter() message = 'my message' adapter.send_message(message, '', source=submission) messages = backend.retrieve_messages() self.assertEqual(len(messages), 1) message_payload = json.loads(messages[0]['payload']) self.assertEqual(message_payload['text'], message) self.assertEqual(message_payload['channel'], '#dummy')
def test_send(self, send, get_rq_task): BACKEND = 'django_slack.backends.RQBackend' with override_settings( SLACK_RQ_QUEUE='default', SLACK_BACKEND=BACKEND): backend = get_backend() self.assertIsInstance(backend, RQBackend) worker = get_rq_task() # check if we got the right job instance self.assertTrue(hasattr(worker, 'delay')) backend.send() self.assertTrue(send.called)
def test_fund_multiple_custom_slack_channel(self): backend = get_backend() backend.reset_messages() submission = ApplicationSubmissionFactory( round__parent__slack_channel='dummy1, dummy2') adapter = SlackAdapter() message = 'my message' adapter.send_message(message, '', source=submission) messages = backend.retrieve_messages() self.assertEqual(len(messages), 2) for index, sent_message in enumerate(messages): message_payload = json.loads(sent_message['payload']) self.assertEqual(message_payload['text'], message) self.assertEqual(message_payload['channel'], '#dummy' + str(index + 1))
def test_slack_staff_update_invoice(self): backend = get_backend() backend.reset_messages() project = self.source_factory() invoice = InvoiceFactory(project=project) staff = StaffFactory() self.adapter_process( MESSAGES.UPDATE_INVOICE, adapter=self.slack(), user=staff, source=project, related=invoice, ) messages = backend.retrieve_messages() self.assertEqual(len(messages), 1)
def test_slack_lead_change(self): backend = get_backend() backend.reset_messages() project = self.source_factory() user = UserFactory() self.adapter_process( MESSAGES.UPDATE_PROJECT_LEAD, adapter=self.slack(), user=user, source=project, related=project.submission, ) messages = backend.retrieve_messages() self.assertEqual(len(messages), 1) message_payload = json.loads(messages[0]['payload']) self.assertIn(str(user), message_payload['text']) self.assertIn(str(project), message_payload['text'])
def test_slack_applicant_update_invoice(self): backend = get_backend() backend.reset_messages() project = self.source_factory() invoice = InvoiceFactory(project=project) applicant = ApplicantFactory() self.adapter_process( MESSAGES.UPDATE_INVOICE, adapter=self.slack(), user=applicant, source=project, related=invoice, ) messages = backend.retrieve_messages() self.assertEqual(len(messages), 1) message_payload = json.loads(messages[0]['payload']) self.assertIn(str(applicant), message_payload['text']) self.assertIn(str(project), message_payload['text'])
def setUp(self): self.backend = get_backend() self.backend.reset()
def test_notify_of_publisher_changes(self): # Publisher changes only apply to paid campaigns self.publisher.allow_paid_campaigns = True self.publisher.save() backend = get_backend() messages = backend.retrieve_messages() notify_of_publisher_changes() # Shouldn't be any publisher changes yet self.assertEqual(len(messages), 0) # Add some views and clicks for _ in range(50): get(Offer, advertisement=self.ad1, publisher=self.publisher, viewed=True) for _ in range(5): offer = get( Offer, advertisement=self.ad1, publisher=self.publisher, viewed=True, clicked=True, ) # Add some impressions from a week ago eight_days_ago = offer.date - datetime.timedelta(days=8) for _ in range(100): get( Offer, advertisement=self.ad1, publisher=self.publisher, date=eight_days_ago, viewed=True, ) for _ in range(11): get( Offer, advertisement=self.ad1, publisher=self.publisher, date=eight_days_ago, viewed=True, clicked=True, ) daily_update_impressions() daily_update_impressions(eight_days_ago) # Ensure the ad impressions (used in the reports) are generated self.assertTrue(AdImpression.objects.filter(publisher=self.publisher).exists()) backend.reset_messages() notify_of_publisher_changes(min_views=100) # Should be 1 message: one for views with CTR being within the threshold messages = backend.retrieve_messages() self.assertEqual(len(messages), 1, messages) self.assertTrue( '"views" was 55 last week and 111 the previous week (-50.45%)' in messages[0]["text"] ) backend.reset_messages() # No messages because it's below the minimum views notify_of_publisher_changes(min_views=1000) messages = backend.retrieve_messages() self.assertEqual(len(messages), 0)
def test_backend_cache(self): """ Ensure that the backend is cached once it is called once. """ self.assertEqual(id(get_backend()), id(get_backend()))