1717from official_documents .tests .paths import (
1818 EXAMPLE_DOCX_FILENAME ,
1919 EXAMPLE_HTML_FILENAME ,
20+ EXAMPLE_PDF_FILENAME ,
2021)
2122from sopn_parsing .tests import should_skip_conversion_tests
2223from webtest import Upload
@@ -37,6 +38,7 @@ class TestModels(TestUserMixin, WebTest):
3738 example_image_filename = EXAMPLE_IMAGE_FILENAME
3839 example_docx_filename = EXAMPLE_DOCX_FILENAME
3940 example_html_filename = EXAMPLE_HTML_FILENAME
41+ example_pdf_filename = EXAMPLE_PDF_FILENAME
4042
4143 def setUp (self ):
4244 gb_parties = PartySetFactory .create (slug = "gb" , name = "Great Britain" )
@@ -229,3 +231,124 @@ def test_jpg_form_validation(self):
229231 "File extension “jpg” is not allowed. Allowed extensions are: pdf, docx." ,
230232 response .text ,
231233 )
234+
235+ def test_filename_updated (self ):
236+ # # When a SoPN has already been uploaded, test the filename
237+ # # is updated when a new SoPN is uploaded.
238+ self .assertFalse (LoggedAction .objects .exists ())
239+ request = self .app .get (
240+ self .ballot .get_absolute_url (),
241+ user = self .user_who_can_upload_documents ,
242+ )
243+
244+ self .assertInHTML ("Upload SOPN" , request .text )
245+
246+ response = self .app .get (
247+ reverse (
248+ "upload_document_view" ,
249+ kwargs = {"ballot_paper_id" : self .ballot .ballot_paper_id },
250+ ),
251+ user = self .user_who_can_upload_documents ,
252+ )
253+ form = response .forms ["document-upload-form" ]
254+ form ["source_url" ] = "http://example.org/foo"
255+ with open (self .example_image_filename , "rb" ) as f :
256+ form ["uploaded_file" ] = Upload ("initial.pdf" , f .read ())
257+
258+ with patch (
259+ "official_documents.views.extract_pages_for_ballot"
260+ ) as extract_pages , patch (
261+ "official_documents.views.extract_ballot_table"
262+ ) as extract_tables , patch (
263+ "official_documents.views.parse_raw_data_for_ballot"
264+ ) as parse_tables :
265+ response = form .submit ()
266+ self .assertEqual (response .status_code , 302 )
267+ extract_pages .assert_called_once ()
268+ extract_tables .assert_called_once ()
269+ parse_tables .assert_called_once ()
270+
271+ ods = OfficialDocument .objects .all ()
272+ self .assertEqual (ods .count (), 1 )
273+ od = ods [0 ]
274+ self .assertEqual (od .source_url , "http://example.org/foo" )
275+ self .assertEqual (
276+ od .uploaded_file .name ,
277+ "official_documents/parl.dulwich-and-west-norwood.2015-05-07/initial.pdf" ,
278+ )
279+ self .assertEqual (
280+ od .ballot .ballot_paper_id ,
281+ "parl.dulwich-and-west-norwood.2015-05-07" ,
282+ )
283+
284+ # repeat the above with another file and check the filename is updated
285+ # again
286+ request = self .app .get (
287+ self .ballot .get_absolute_url (),
288+ user = self .user_who_can_upload_documents ,
289+ )
290+
291+ self .assertInHTML ('"SOPN" uploaded.' , request .text )
292+ # at this stage we know the sopn has been uploaded. Now we need to
293+ # check the filename is updated when a new SoPN is uploaded.
294+
295+ response = self .app .get (
296+ reverse (
297+ "ballot_paper_sopn" ,
298+ kwargs = {"ballot_id" : self .ballot .ballot_paper_id },
299+ ),
300+ user = self .user_who_can_upload_documents ,
301+ )
302+
303+ self .assertInHTML ("Update SOPN" , response .text )
304+
305+ response = self .app .get (
306+ reverse (
307+ "upload_document_view" ,
308+ kwargs = {"ballot_paper_id" : self .ballot .ballot_paper_id },
309+ ),
310+ user = self .user_who_can_upload_documents ,
311+ )
312+
313+ form = response .forms ["document-upload-form" ]
314+ form ["source_url" ] = "http://example.org/bar"
315+ with open (self .example_image_filename , "rb" ) as f :
316+ form ["uploaded_file" ] = Upload ("replacement.pdf" , f .read ())
317+
318+ with patch (
319+ "official_documents.views.extract_pages_for_ballot"
320+ ) as extract_pages , patch (
321+ "official_documents.views.extract_ballot_table"
322+ ) as extract_tables , patch (
323+ "official_documents.views.parse_raw_data_for_ballot"
324+ ) as parse_tables :
325+ response = form .submit ()
326+ self .assertEqual (response .status_code , 302 )
327+ extract_pages .assert_called_once ()
328+ extract_tables .assert_called_once ()
329+ parse_tables .assert_called_once ()
330+
331+ ods = OfficialDocument .objects .all ()
332+ self .assertEqual (ods .count (), 2 )
333+ od = ods [1 ]
334+ self .assertEqual (od .source_url , "http://example.org/bar" )
335+ self .assertEqual (
336+ od .uploaded_file .name ,
337+ "official_documents/parl.dulwich-and-west-norwood.2015-05-07/replacement.pdf" ,
338+ )
339+
340+ self .assertEqual (
341+ od .ballot .ballot_paper_id ,
342+ "parl.dulwich-and-west-norwood.2015-05-07" ,
343+ )
344+ response = self .app .get (
345+ reverse (
346+ "ballot_paper_sopn" ,
347+ kwargs = {"ballot_id" : self .ballot .ballot_paper_id },
348+ ),
349+ user = self .user_who_can_upload_documents ,
350+ )
351+ self .assertInHTML (
352+ '<div class="pdf_container" id="sopn-parl.dulwich-and-west-norwood.2015-05-07">' ,
353+ response .text ,
354+ )
0 commit comments