mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-10-30 03:56:23 -05:00 
			
		
		
		
	tests for pre and post consume script
This commit is contained in:
		| @@ -71,6 +71,11 @@ class Consumer(LoggingMixin): | ||||
|         if not settings.PRE_CONSUME_SCRIPT: | ||||
|             return | ||||
|  | ||||
|         if not os.path.isfile(settings.PRE_CONSUME_SCRIPT): | ||||
|             raise ConsumerError( | ||||
|                 f"Configured pre-consume script " | ||||
|                 f"{settings.PRE_CONSUME_SCRIPT} does not exist.") | ||||
|  | ||||
|         try: | ||||
|             Popen((settings.PRE_CONSUME_SCRIPT, self.path)).wait() | ||||
|         except Exception as e: | ||||
| @@ -82,6 +87,11 @@ class Consumer(LoggingMixin): | ||||
|         if not settings.POST_CONSUME_SCRIPT: | ||||
|             return | ||||
|  | ||||
|         if not os.path.isfile(settings.POST_CONSUME_SCRIPT): | ||||
|             raise ConsumerError( | ||||
|                 f"Configured post-consume script " | ||||
|                 f"{settings.POST_CONSUME_SCRIPT} does not exist.") | ||||
|  | ||||
|         try: | ||||
|             Popen(( | ||||
|                 settings.POST_CONSUME_SCRIPT, | ||||
|   | ||||
| @@ -468,6 +468,42 @@ class TestConsumer(DirectoriesMixin, TestCase): | ||||
|         self.assertTrue(os.path.isfile(dst)) | ||||
|  | ||||
|  | ||||
| class PreConsumeTestCase(TestCase): | ||||
|  | ||||
|     @mock.patch("documents.consumer.Popen") | ||||
|     @override_settings(PRE_CONSUME_SCRIPT=None) | ||||
|     def test_no_pre_consume_script(self, m): | ||||
|         c = Consumer() | ||||
|         c.path = "path-to-file" | ||||
|         c.run_pre_consume_script() | ||||
|         m.assert_not_called() | ||||
|  | ||||
|     @mock.patch("documents.consumer.Popen") | ||||
|     @override_settings(PRE_CONSUME_SCRIPT="does-not-exist") | ||||
|     def test_pre_consume_script_not_found(self, m): | ||||
|         c = Consumer() | ||||
|         c.path = "path-to-file" | ||||
|         self.assertRaises(ConsumerError, c.run_pre_consume_script) | ||||
|  | ||||
|     @mock.patch("documents.consumer.Popen") | ||||
|     def test_pre_consume_script(self, m): | ||||
|         with tempfile.NamedTemporaryFile() as script: | ||||
|             with override_settings(PRE_CONSUME_SCRIPT=script.name): | ||||
|                 c = Consumer() | ||||
|                 c.path = "path-to-file" | ||||
|                 c.run_pre_consume_script() | ||||
|  | ||||
|                 m.assert_called_once() | ||||
|  | ||||
|                 args, kwargs = m.call_args | ||||
|  | ||||
|                 command = args[0] | ||||
|  | ||||
|                 self.assertEqual(command[0], script.name) | ||||
|                 self.assertEqual(command[1], "path-to-file") | ||||
|  | ||||
|  | ||||
|  | ||||
| class PostConsumeTestCase(TestCase): | ||||
|  | ||||
|     @mock.patch("documents.consumer.Popen") | ||||
| @@ -483,9 +519,17 @@ class PostConsumeTestCase(TestCase): | ||||
|  | ||||
|         m.assert_not_called() | ||||
|  | ||||
|  | ||||
|     @override_settings(POST_CONSUME_SCRIPT="does-not-exist") | ||||
|     def test_post_consume_script_not_found(self): | ||||
|         doc = Document.objects.create(title="Test", mime_type="application/pdf") | ||||
|  | ||||
|         self.assertRaises(ConsumerError, Consumer().run_post_consume_script, doc) | ||||
|  | ||||
|     @mock.patch("documents.consumer.Popen") | ||||
|     @override_settings(POST_CONSUME_SCRIPT="script") | ||||
|     def test_post_consume_script_simple(self, m): | ||||
|         with tempfile.NamedTemporaryFile() as script: | ||||
|             with override_settings(POST_CONSUME_SCRIPT=script.name): | ||||
|                 doc = Document.objects.create(title="Test", mime_type="application/pdf") | ||||
|  | ||||
|                 Consumer().run_post_consume_script(doc) | ||||
| @@ -493,8 +537,9 @@ class PostConsumeTestCase(TestCase): | ||||
|                 m.assert_called_once() | ||||
|  | ||||
|     @mock.patch("documents.consumer.Popen") | ||||
|     @override_settings(POST_CONSUME_SCRIPT="script") | ||||
|     def test_post_consume_script_with_correspondent(self, m): | ||||
|         with tempfile.NamedTemporaryFile() as script: | ||||
|             with override_settings(POST_CONSUME_SCRIPT=script.name): | ||||
|                 c = Correspondent.objects.create(name="my_bank") | ||||
|                 doc = Document.objects.create(title="Test", mime_type="application/pdf", correspondent=c) | ||||
|                 tag1 = Tag.objects.create(name="a") | ||||
| @@ -510,7 +555,7 @@ class PostConsumeTestCase(TestCase): | ||||
|  | ||||
|                 command = args[0] | ||||
|  | ||||
|         self.assertEqual(command[0], "script") | ||||
|                 self.assertEqual(command[0], script.name) | ||||
|                 self.assertEqual(command[1], str(doc.pk)) | ||||
|                 self.assertEqual(command[5], f"/api/documents/{doc.pk}/download/") | ||||
|                 self.assertEqual(command[6], f"/api/documents/{doc.pk}/thumb/") | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 jonaswinkler
					jonaswinkler