@@ -91,6 +91,36 @@ class ImportsControllerTest < ActionController::TestCase
9191 documents : [ ]
9292 }
9393 }
94+
95+ @encrypted_with_document_text_data = {
96+ schema_version : 1 ,
97+ exported_at : Time . current . iso8601 ,
98+ user : { name : 'Encrypted User' , email : 'encrypted@example.com' } ,
99+ data : {
100+ folders : [
101+ { name : 'Encrypted Folder' , parent_folder_name : nil , created_at : 1 . day . ago . iso8601 , updated_at : 1 . day . ago . iso8601 }
102+ ] ,
103+ tags : [ ] ,
104+ people : [ ] ,
105+ states : [ ] ,
106+ documents : [
107+ {
108+ title : 'Encrypted With Text' ,
109+ description : 'Should be rejected' ,
110+ document_date : Date . today . iso8601 ,
111+ version : '1.0' ,
112+ encrypted_flag : true ,
113+ folder_name : 'Encrypted Folder' ,
114+ state_name : nil ,
115+ person_name : nil ,
116+ tag_names : [ ] ,
117+ document_text : 'This should not be here for encrypted docs' ,
118+ created_at : 1 . day . ago . iso8601 ,
119+ updated_at : 1 . day . ago . iso8601
120+ }
121+ ]
122+ }
123+ }
94124 end
95125
96126 test 'should preview import data without modifying database' do
@@ -299,4 +329,193 @@ class ImportsControllerTest < ActionController::TestCase
299329
300330 assert_equal 1 , json_response [ 'preview' ] [ 'encrypted_documents' ]
301331 end
332+
333+ test 'should reject encrypted document with document_text' do
334+ original_folder_count = @user . folders . count
335+ original_doc_count = @user . documents . count
336+
337+ post :create , params : { import : @encrypted_with_document_text_data } , format : :json
338+
339+ assert_response :unprocessable_entity
340+ json_response = JSON . parse ( response . body )
341+
342+ assert_not json_response [ 'success' ]
343+ assert_not_empty json_response [ 'errors' ]
344+ assert_includes json_response [ 'errors' ] . first , '加密文档禁止导入正文检索字段'
345+ assert_includes json_response [ 'errors' ] . first , 'Encrypted With Text'
346+
347+ assert_equal original_folder_count , @user . folders . count
348+ assert_equal original_doc_count , @user . documents . count
349+ end
350+
351+ test 'should reject encrypted document with document_text in preview' do
352+ post :preview , params : { import : @encrypted_with_document_text_data } , format : :json
353+
354+ assert_response :success
355+ json_response = JSON . parse ( response . body )
356+
357+ assert_not_empty json_response [ 'errors' ]
358+ assert_includes json_response [ 'errors' ] . first , '加密文档禁止导入正文检索字段'
359+ end
360+
361+ test 'conflict: existing folder should be skipped with warning' do
362+ existing_folder = @user . folders . create! ( name : 'Imported Work' )
363+
364+ original_folder_count = @user . folders . count
365+
366+ post :create , params : { import : @valid_export_data } , format : :json
367+
368+ assert_response :success
369+ json_response = JSON . parse ( response . body )
370+
371+ assert json_response [ 'success' ]
372+ assert_not_empty json_response [ 'warnings' ]
373+ assert_includes json_response [ 'warnings' ] . first , 'Folders already exist'
374+ assert_includes json_response [ 'warnings' ] . first , 'Imported Work'
375+
376+ assert_equal original_folder_count + 1 , @user . folders . count
377+ assert @user . folders . exists? ( id : existing_folder . id )
378+ end
379+
380+ test 'conflict: existing tag should be skipped with warning' do
381+ existing_tag = @user . tags . create! ( name : 'Imported Tag' , color : '#00ff00' )
382+
383+ original_tag_count = @user . tags . count
384+
385+ post :create , params : { import : @valid_export_data } , format : :json
386+
387+ assert_response :success
388+ json_response = JSON . parse ( response . body )
389+
390+ assert json_response [ 'success' ]
391+ assert_not_empty json_response [ 'warnings' ]
392+ assert_includes json_response [ 'warnings' ] . first , 'Tags already exist'
393+ assert_includes json_response [ 'warnings' ] . first , 'Imported Tag'
394+
395+ assert_equal original_tag_count , @user . tags . count
396+ assert @user . tags . exists? ( id : existing_tag . id )
397+ assert_equal '#00ff00' , existing_tag . reload . color
398+ end
399+
400+ test 'conflict: existing person should be skipped with warning' do
401+ existing_person = @user . people . create! ( name : 'Imported Person' )
402+
403+ original_person_count = @user . people . count
404+
405+ post :create , params : { import : @valid_export_data } , format : :json
406+
407+ assert_response :success
408+ json_response = JSON . parse ( response . body )
409+
410+ assert json_response [ 'success' ]
411+ assert_not_empty json_response [ 'warnings' ]
412+ assert_includes json_response [ 'warnings' ] . to_s , 'People already exist'
413+ assert_includes json_response [ 'warnings' ] . to_s , 'Imported Person'
414+
415+ assert_equal original_person_count , @user . people . count
416+ assert @user . people . exists? ( id : existing_person . id )
417+ end
418+
419+ test 'conflict: existing document (same title + date) should be skipped with warning' do
420+ test_date = Date . today
421+ existing_doc = @user . documents . create! (
422+ title : 'Imported Document' ,
423+ document_date : test_date ,
424+ document_url : 'existing.pdf' ,
425+ document_text : 'existing text'
426+ )
427+
428+ original_doc_count = @user . documents . count
429+
430+ post :create , params : { import : @valid_export_data } , format : :json
431+
432+ assert_response :success
433+ json_response = JSON . parse ( response . body )
434+
435+ assert json_response [ 'success' ]
436+ assert_not_empty json_response [ 'warnings' ]
437+ assert_includes json_response [ 'warnings' ] . to_s , 'Documents already exist'
438+ assert_includes json_response [ 'warnings' ] . to_s , 'Imported Document'
439+
440+ assert_equal 0 , json_response [ 'imported_data' ] [ 'documents' ]
441+ assert_equal 1 , json_response [ 'imported_data' ] [ 'documents_skipped' ]
442+
443+ assert_equal original_doc_count , @user . documents . count
444+ assert @user . documents . exists? ( id : existing_doc . id )
445+ assert_equal 'existing text' , existing_doc . reload . document_text
446+ end
447+
448+ test 'should reject import with duplicate documents in same data (same title + date)' do
449+ duplicate_data = @valid_export_data . deep_dup
450+ duplicate_data [ :data ] [ :documents ] << {
451+ title : 'Imported Document' ,
452+ description : 'Duplicate' ,
453+ document_date : Date . today . iso8601 ,
454+ version : '2.0' ,
455+ encrypted_flag : false ,
456+ folder_name : nil ,
457+ state_name : nil ,
458+ person_name : nil ,
459+ tag_names : [ ] ,
460+ document_text : 'duplicate text' ,
461+ created_at : 1 . day . ago . iso8601 ,
462+ updated_at : 1 . day . ago . iso8601
463+ }
464+
465+ original_doc_count = @user . documents . count
466+
467+ post :create , params : { import : duplicate_data } , format : :json
468+
469+ assert_response :unprocessable_entity
470+ json_response = JSON . parse ( response . body )
471+
472+ assert_not json_response [ 'success' ]
473+ assert_not_empty json_response [ 'errors' ]
474+ assert_includes json_response [ 'errors' ] . first , 'Duplicate documents in import'
475+ assert_includes json_response [ 'errors' ] . first , 'Imported Document'
476+
477+ assert_equal original_doc_count , @user . documents . count
478+ end
479+
480+ test 'conflict: mixed existing and new documents should import new ones' do
481+ test_date = Date . today
482+ @user . documents . create! (
483+ title : 'Imported Document' ,
484+ document_date : test_date ,
485+ document_url : 'existing.pdf'
486+ )
487+
488+ data_with_new_doc = @valid_export_data . deep_dup
489+ data_with_new_doc [ :data ] [ :documents ] << {
490+ title : 'New Document' ,
491+ description : 'Brand new' ,
492+ document_date : 1 . day . ago . iso8601 ,
493+ version : '1.0' ,
494+ encrypted_flag : false ,
495+ folder_name : 'Imported Work' ,
496+ state_name : nil ,
497+ person_name : nil ,
498+ tag_names : [ ] ,
499+ document_text : 'new document text' ,
500+ created_at : 1 . day . ago . iso8601 ,
501+ updated_at : 1 . day . ago . iso8601
502+ }
503+
504+ original_doc_count = @user . documents . count
505+
506+ assert_difference -> { @user . documents . count } , 1 do
507+ post :create , params : { import : data_with_new_doc } , format : :json
508+ end
509+
510+ assert_response :success
511+ json_response = JSON . parse ( response . body )
512+
513+ assert json_response [ 'success' ]
514+ assert_equal 1 , json_response [ 'imported_data' ] [ 'documents' ]
515+ assert_equal 1 , json_response [ 'imported_data' ] [ 'documents_skipped' ]
516+ assert_not_empty json_response [ 'warnings' ]
517+
518+ assert @user . documents . find_by ( title : 'Imported Document' ) . present?
519+ assert @user . documents . find_by ( title : 'New Document' ) . present?
520+ end
302521end
0 commit comments