11import importlib
22import io
33import os
4+ import subprocess
45import unittest
56from unittest import mock
67
8+
79from fastapi .testclient import TestClient
810
911import server
@@ -34,14 +36,29 @@ def test_health_check(self):
3436 def test_print_endpoint (self , mock_pathlib_unlink , mock_open_func , mock_popen , _ ):
3537 client = self .load_server_with_args ()
3638 test_file = io .BytesIO (b"dummy file content" )
39+
40+ mock_popen_result = mock .MagicMock ()
41+ mock_popen_result .returncode = 0
42+ mock_popen_result .stdout .read .return_value = (
43+ "request id is HP_LaserJet_p2015dn_Right-53 (1 file(s))"
44+ )
45+
46+ mock_popen .return_value = mock_popen_result
47+
3748 response = client .post (
3849 "/print" ,
3950 files = {"file" : ("test.txt" , test_file , "text/plain" )},
4051 data = {"copies" : "1" , "sides" : "one-sided" },
4152 )
4253
4354 self .assertEqual (response .status_code , 200 )
44- self .assertEqual (response .text , '"worked!"' )
55+ json_response = response .json ()
56+ self .assertEqual (
57+ json_response ,
58+ {
59+ "print_id" : "HP_LaserJet_p2015dn_Right-53" ,
60+ },
61+ )
4562
4663 mock_open_func .assert_called_once_with ("/tmp/test-id" , "wb" )
4764
@@ -57,6 +74,9 @@ def test_print_endpoint(self, mock_pathlib_unlink, mock_open_func, mock_popen, _
5774 mock .call (
5875 "lp -n 1 -o sides=one-sided -o media=na_letter_8.5x11in -d HP_P2015_DN /tmp/test-id" ,
5976 shell = True ,
77+ stdout = subprocess .PIPE ,
78+ stderr = subprocess .PIPE ,
79+ text = True ,
6080 ),
6181 )
6282
@@ -69,6 +89,14 @@ def test_print_endpoint(self, mock_pathlib_unlink, mock_open_func, mock_popen, _
6989 def test_print_endpoint_dont_delete_pdf (
7090 self , mock_pathlib_unlink , mock_open_func , mock_popen , _
7191 ):
92+
93+ mock_popen_result = mock .MagicMock ()
94+ mock_popen_result .returncode = 0
95+ mock_popen_result .stdout .read .return_value = (
96+ "request id is HP_LaserJet_p2015dn_Right-53 (1 file(s))"
97+ )
98+
99+ mock_popen .return_value = mock_popen_result
72100 client = self .load_server_with_args (["--dont-delete-pdfs" ])
73101 test_file = io .BytesIO (b"dummy file content" )
74102 response = client .post (
@@ -78,7 +106,13 @@ def test_print_endpoint_dont_delete_pdf(
78106 )
79107
80108 self .assertEqual (response .status_code , 200 )
81- self .assertEqual (response .text , '"worked!"' )
109+ json_response = response .json ()
110+ self .assertEqual (
111+ json_response ,
112+ {
113+ "print_id" : "HP_LaserJet_p2015dn_Right-53" ,
114+ },
115+ )
82116
83117 mock_open_func .assert_called_once_with ("/tmp/test-id" , "wb" )
84118
@@ -94,6 +128,9 @@ def test_print_endpoint_dont_delete_pdf(
94128 mock .call (
95129 "lp -n 1 -o sides=one-sided -o media=na_letter_8.5x11in -d HP_P2015_DN /tmp/test-id" ,
96130 shell = True ,
131+ stdout = subprocess .PIPE ,
132+ stderr = subprocess .PIPE ,
133+ text = True ,
97134 ),
98135 )
99136
@@ -102,7 +139,7 @@ def test_print_endpoint_dont_delete_pdf(
102139 @mock .patch ("server.subprocess.Popen" )
103140 @mock .patch ("builtins.open" , side_effect = FileNotFoundError ("sorry!" ))
104141 @mock .patch ("pathlib.Path.unlink" )
105- def test_print_endpoint_error (self , mock_pathlib_unlink , _ , mock_popen ):
142+ def test_print_endpoint_file_not_found (self , mock_pathlib_unlink , _ , mock_popen ):
106143 client = self .load_server_with_args ()
107144 test_file = io .BytesIO (b"dummy file content" )
108145 response = client .post (
@@ -124,6 +161,107 @@ def test_print_endpoint_error(self, mock_pathlib_unlink, _, mock_popen):
124161 mock_popen .assert_not_called ()
125162 mock_pathlib_unlink .assert_not_called ()
126163
164+ @mock .patch ("server.uuid.uuid4" , return_value = "test-id" )
165+ @mock .patch ("server.subprocess.Popen" )
166+ @mock .patch ("builtins.open" , new_callable = mock .mock_open )
167+ @mock .patch ("pathlib.Path.unlink" )
168+ def test_print_endpoint_nonzero_returncode (
169+ self , mock_pathlib_unlink , mock_open_func , mock_popen , _
170+ ):
171+ client = self .load_server_with_args ()
172+ test_file = io .BytesIO (b"dummy file content" )
173+
174+ mock_popen_result = mock .MagicMock ()
175+ mock_popen_result .returncode = 1
176+ mock_popen .return_value = mock_popen_result
177+
178+ response = client .post (
179+ "/print" ,
180+ files = {"file" : ("test.txt" , test_file , "text/plain" )},
181+ data = {"copies" : "1" , "sides" : "dark-side" },
182+ )
183+
184+ self .assertEqual (response .status_code , 200 )
185+ self .assertEqual (
186+ response .json (),
187+ {
188+ "status_code" : 500 ,
189+ "detail" : "printing failed, check logs" ,
190+ "headers" : None ,
191+ },
192+ )
193+
194+ mock_open_func .assert_called_once_with ("/tmp/test-id" , "wb" )
195+
196+ mock_open_func ().write .assert_called_once ()
197+ self .assertEqual (
198+ mock_open_func ().write .call_args_list [0 ], mock .call (b"dummy file content" )
199+ )
200+
201+ mock_popen .assert_called_once ()
202+
203+ self .assertEqual (
204+ mock_popen .call_args_list [0 ],
205+ mock .call (
206+ "lp -n 1 -o sides=dark-side -o media=na_letter_8.5x11in -d HP_P2015_DN /tmp/test-id" ,
207+ shell = True ,
208+ stdout = subprocess .PIPE ,
209+ stderr = subprocess .PIPE ,
210+ text = True ,
211+ ),
212+ )
213+
214+ mock_pathlib_unlink .assert_called_once ()
215+
216+ @mock .patch ("server.uuid.uuid4" , return_value = "test-id" )
217+ @mock .patch ("server.subprocess.Popen" )
218+ @mock .patch ("builtins.open" , new_callable = mock .mock_open )
219+ @mock .patch ("pathlib.Path.unlink" )
220+ def test_junk_print_id (self , mock_pathlib_unlink , mock_open_func , mock_popen , _ ):
221+ client = self .load_server_with_args ()
222+ test_file = io .BytesIO (b"dummy file content" )
223+
224+ mock_popen_result = mock .MagicMock ()
225+ mock_popen_result .returncode = 0
226+ mock_popen_result .stdout .read .return_value = "junk output"
227+ mock_popen .return_value = mock_popen_result
228+
229+ response = client .post (
230+ "/print" ,
231+ files = {"file" : ("test.txt" , test_file , "text/plain" )},
232+ data = {"copies" : "1" , "sides" : "one-sided" },
233+ )
234+
235+ self .assertEqual (response .status_code , 200 )
236+ self .assertEqual (
237+ response .json (),
238+ {
239+ "print_id" : "" ,
240+ },
241+ )
242+
243+ mock_open_func .assert_called_once_with ("/tmp/test-id" , "wb" )
244+
245+ mock_open_func ().write .assert_called_once ()
246+ self .assertEqual (
247+ mock_open_func ().write .call_args_list [0 ], mock .call (b"dummy file content" )
248+ )
249+
250+ mock_popen .assert_called_once ()
251+
252+ self .assertEqual (
253+ mock_popen .call_args_list [0 ],
254+ mock .call (
255+ "lp -n 1 -o sides=one-sided -o media=na_letter_8.5x11in -d HP_P2015_DN /tmp/test-id" ,
256+ shell = True ,
257+ stdout = subprocess .PIPE ,
258+ stderr = subprocess .PIPE ,
259+ text = True ,
260+ ),
261+ )
262+
263+ mock_pathlib_unlink .assert_called_once ()
264+
127265
128266if __name__ == "__main__" :
129267 unittest .main ()
0 commit comments