def test_stream(self): # create a message export message_export_task = ExportMessagesTask.create( self.org, self.admin, SystemLabel.TYPE_INBOX) # try as anon response = self.client.get( reverse("assets.stream", kwargs=dict(type="message_export", pk=message_export_task.pk))) self.assertLoginRedirect(response) self.login(self.admin) # try with invalid object id response = self.client.get( reverse("assets.stream", kwargs=dict(type="message_export", pk=1234567890))) self.assertEqual(response.status_code, 404) # try before asset is generated response = self.client.get( reverse("assets.stream", kwargs=dict(type="message_export", pk=message_export_task.pk))) self.assertEqual(response.status_code, 404) # create asset and request again message_export_task.perform() response = self.client.get( reverse("assets.stream", kwargs=dict(type="message_export", pk=message_export_task.pk))) self.assertEqual(response.status_code, 200)
def test_message_export_finished(self): export = ExportMessagesTask.create(self.org, self.editor, system_label="I") export.perform() Notification.export_finished(export) self.assertFalse( self.editor.notifications.get(message_export=export).is_seen) # we only notify the user that started the export self.assert_notifications( after=export.created_on, expected_json={ "type": "export:finished", "created_on": matchers.ISODate(), "target_url": f"/assets/download/message_export/{export.id}/", "is_seen": False, "export": { "type": "message" }, }, expected_users={self.editor}, email=True, ) send_notification_emails() self.assertEqual(1, len(mail.outbox)) self.assertEqual("[Temba] Your message export is ready", mail.outbox[0].subject) self.assertEqual(["*****@*****.**"], mail.outbox[0].recipients())
def test_stream(self): # create a message export message_export_task = ExportMessagesTask.create(self.org, self.admin, SystemLabel.TYPE_INBOX) # try as anon response = self.client.get( reverse("assets.stream", kwargs=dict(type="message_export", pk=message_export_task.pk)) ) self.assertLoginRedirect(response) self.login(self.admin) # try with invalid object id response = self.client.get(reverse("assets.stream", kwargs=dict(type="message_export", pk=1234567890))) self.assertEqual(response.status_code, 404) # try before asset is generated response = self.client.get( reverse("assets.stream", kwargs=dict(type="message_export", pk=message_export_task.pk)) ) self.assertEqual(response.status_code, 404) # create asset and request again message_export_task.perform() response = self.client.get( reverse("assets.stream", kwargs=dict(type="message_export", pk=message_export_task.pk)) ) self.assertEqual(response.status_code, 200)
def handle(self, *args, **kwargs): unfinished_tasks = { "contact-export": ExportContactsTask.get_unfinished(), "result-export": ExportFlowResultsTask.get_unfinished(), "message-export": ExportMessagesTask.get_unfinished(), } for name, qs in unfinished_tasks.items(): count = qs.count() if count: last = qs.order_by("created_on").last() self.stdout.write( f"WARNING: there are {count} unfinished tasks of type {name}. " f"Last one started {timesince(last.created_on)} ago.")
def test_download(self): # create a message export message_export_task = ExportMessagesTask.create( self.org, self.admin, SystemLabel.TYPE_INBOX) response = self.client.get( reverse("assets.download", kwargs=dict(type="message_export", pk=message_export_task.pk))) self.assertLoginRedirect(response) self.login(self.admin) # asset doesn't exist yet response = self.client.get( reverse("assets.download", kwargs=dict(type="message_export", pk=message_export_task.pk))) self.assertContains(response, "File not found", status_code=200) # specify wrong asset type so db object won't exist response = self.client.get( reverse("assets.download", kwargs=dict(type="contact_export", pk=message_export_task.pk))) self.assertContains(response, "File not found", status_code=200) # create asset and request again with correct type message_export_task.perform() response = self.client.get( reverse("assets.download", kwargs=dict(type="message_export", pk=message_export_task.pk))) self.assertContains(response, "Your download should start automatically", status_code=200) # check direct download stream response = self.client.get( reverse("assets.stream", kwargs=dict(type="message_export", pk=message_export_task.pk))) self.assertEqual(response.status_code, 200) # create contact export and check that we can access it contact_export_task = ExportContactsTask.create(self.org, self.admin) contact_export_task.perform() response = self.client.get( reverse("assets.download", kwargs=dict(type="contact_export", pk=contact_export_task.pk))) self.assertContains(response, "Your download should start automatically", status_code=200) # create flow results export and check that we can access it flow = self.create_flow() results_export_task = ExportFlowResultsTask.objects.create( org=self.org, created_by=self.admin, modified_by=self.admin) results_export_task.flows.add(flow) results_export_task.perform() response = self.client.get( reverse("assets.download", kwargs=dict(type="results_export", pk=results_export_task.pk))) self.assertContains(response, "Your download should start automatically", status_code=200) # add our admin to another org self.setUpSecondaryOrg() self.org2.administrators.add(self.admin) self.admin.set_org(self.org2) s = self.client.session s["org_id"] = self.org2.pk s.save() # as this asset belongs to org #1, request will have that context response = self.client.get( reverse("assets.download", kwargs=dict(type="message_export", pk=message_export_task.pk))) self.assertEqual(200, response.status_code) user = response.context_data["view"].request.user self.assertEqual(user, self.admin) self.assertEqual(user.get_org(), self.org)
def test_download(self): # create a message export message_export_task = ExportMessagesTask.create(self.org, self.admin, SystemLabel.TYPE_INBOX) response = self.client.get( reverse("assets.download", kwargs=dict(type="message_export", pk=message_export_task.pk)) ) self.assertLoginRedirect(response) self.login(self.admin) # asset doesn't exist yet response = self.client.get( reverse("assets.download", kwargs=dict(type="message_export", pk=message_export_task.pk)) ) self.assertContains(response, "File not found", status_code=200) # specify wrong asset type so db object won't exist response = self.client.get( reverse("assets.download", kwargs=dict(type="contact_export", pk=message_export_task.pk)) ) self.assertContains(response, "File not found", status_code=200) # create asset and request again with correct type message_export_task.perform() response = self.client.get( reverse("assets.download", kwargs=dict(type="message_export", pk=message_export_task.pk)) ) self.assertContains(response, "Your download should start automatically", status_code=200) # check direct download stream response = self.client.get( reverse("assets.stream", kwargs=dict(type="message_export", pk=message_export_task.pk)) ) self.assertEqual(response.status_code, 200) # create contact export and check that we can access it contact_export_task = ExportContactsTask.create(self.org, self.admin) contact_export_task.perform() response = self.client.get( reverse("assets.download", kwargs=dict(type="contact_export", pk=contact_export_task.pk)) ) self.assertContains(response, "Your download should start automatically", status_code=200) # create flow results export and check that we can access it flow = self.create_flow() results_export_task = ExportFlowResultsTask.objects.create( org=self.org, created_by=self.admin, modified_by=self.admin ) results_export_task.flows.add(flow) results_export_task.perform() response = self.client.get( reverse("assets.download", kwargs=dict(type="results_export", pk=results_export_task.pk)) ) self.assertContains(response, "Your download should start automatically", status_code=200) # add our admin to another org self.create_secondary_org() self.org2.administrators.add(self.admin) self.admin.set_org(self.org2) s = self.client.session s["org_id"] = self.org2.pk s.save() # as this asset belongs to org #1, request will have that context response = self.client.get( reverse("assets.download", kwargs=dict(type="message_export", pk=message_export_task.pk)) ) self.assertEqual(200, response.status_code) user = response.context_data["view"].request.user self.assertEqual(user, self.admin) self.assertEqual(user.get_org(), self.org)