@@ -35,8 +35,8 @@ def test_new_inat_import
3535
3636 assert_response ( :success )
3737 assert_form_action ( action : :create )
38- assert_select ( "input #inat_ids" , true ,
39- "Form needs a field for inputting iNat ids" )
38+ assert_select ( "textarea #inat_ids" , true ,
39+ "Form needs a textarea for inputting iNat ids" )
4040 assert_select ( "input#inat_username" , true ,
4141 "Form needs a field for inputting iNat username" )
4242 assert_select ( "input[type=checkbox][id=consent]" , true ,
@@ -152,11 +152,66 @@ def test_create_no_consent
152152 assert_flash_text ( :inat_consent_required . l )
153153 end
154154
155+ def test_allows_maximum_ids
156+ user = users ( :rolf )
157+ inat_username = "rolf" # use different inat_username to test if it's updated
158+ inat_import = inat_imports ( :rolf_inat_import )
159+ assert_equal ( "Unstarted" , inat_import . state ,
160+ "Need a Unstarted inat_import fixture" )
161+ assert_equal ( 0 , inat_import . total_imported_count . to_i ,
162+ "Test needs InatImport fixture without prior imports" )
163+
164+ id = 1_234_567_890
165+ reps = InatImportsController ::Validators ::MAX_ID_LIST_SIZE / id . to_s . length
166+ id_list = ( id . to_s * reps ) . chop
167+
168+ stub_request ( :any , authorization_url )
169+ login ( user . login )
170+
171+ assert_no_difference (
172+ "Observation.count" ,
173+ "Authorization request to iNat shouldn't create MO Observation(s)"
174+ ) do
175+ post ( :create ,
176+ params : { inat_ids : id_list , inat_username : inat_username ,
177+ consent : 1 } )
178+ end
179+
180+ assert_response ( :redirect )
181+ assert_equal ( id_list , inat_import . reload . inat_ids ,
182+ "Failed to save inat_ids at maximum length" )
183+ end
184+
185+ def test_strips_trailing_commas_and_space_chars_from_id_list
186+ inat_import = inat_imports ( :rolf_inat_import )
187+ user = inat_import . user
188+ assert_equal ( "Unstarted" , inat_import . state ,
189+ "Need a Unstarted inat_import fixture" )
190+ id_list = "123,456,789, \n "
191+ expected_saved_id_list = "123,456,789"
192+ stub_request ( :any , authorization_url )
193+ login ( user . login )
194+
195+ post ( :create ,
196+ params : { inat_ids : id_list ,
197+ inat_username : "" , # omit this to force form reload
198+ consent : 1 } )
199+
200+ assert_form_action ( action : :create )
201+ assert_select (
202+ "textarea#inat_ids" , { text : expected_saved_id_list , count : 1 } ,
203+ "inat_ids textarea should have trailing commas and whitespace stripped"
204+ )
205+ end
206+
155207 def test_create_too_many_ids_listed
156208 # generate an id list that's barely too long
157209 id_list = ""
158210 id = 1_234_567_890
159- id_list += "#{ id += 1 } ," until id_list . length > 255
211+ until id_list . length > InatImportsController ::Validators ::MAX_ID_LIST_SIZE
212+ id_list += "#{ id += 1 } ,"
213+ end
214+
160215 params = { inat_username : "anything" , inat_ids : id_list , consent : 1 }
161216
162217 login
@@ -185,44 +240,11 @@ def test_create_previously_imported
185240 post ( :create , params : params )
186241 end
187242
188- # NOTE: 2024-09-04 jdc
189- # I'd prefer that the flash include links to both obss,
190- # and that this (or another) assertion check for that.
191- # At the moment, it's taking too long to figure out how.
192- assert_flash_text ( /iNat #{ inat_id } previously imported/ )
193- end
194-
195- def test_create_previously_mirrored
196- user = users ( :rolf )
197- inat_id = "1234567"
198- mirrored_obs = Observation . create (
199- where : "North Falmouth, Massachusetts, USA" ,
200- user : user ,
201- when : "2023-09-08" ,
202- inat_id : nil ,
203- # When Pulk's `mirror`Python script copies an MO Obs to iNat,
204- # it adds a text in this form to the MO Obs notes
205- # See https://github.com/JacobPulk/mirror
206- notes : { Other : "Mirrored on iNaturalist as <a href=\" https://www.inaturalist.org/observations/#{ inat_id } \" >observation #{ inat_id } </a> on December 18, 2023" }
207- )
208- params = { inat_username : "anything" , inat_ids : inat_id , consent : 1 }
209-
210- login
211- assert_no_difference (
212- "Observation.count" ,
213- "Imported an iNat obs which had been 'mirrored' from MO"
214- ) do
215- post ( :create , params : params )
216- end
217-
218- # NOTE: 2024-09-04 jdc
219- # I'd prefer that the flash include links to both obss,
220- # and that this (or another) assertion check for that.
221- # At the moment, it's taking too long to figure out how.
222- assert_flash_text (
223- "iNat #{ inat_id } is a “mirror” of " \
224- "existing MO Observation #{ mirrored_obs . id } "
225- )
243+ assert_flash_text ( /#{ :inat_previous_import . l ( count : 1 ) } / )
244+ # It should continue even if some ids were previously imported
245+ # The job will exclude previous imports via the iNat API
246+ # `without_field: "Mushroom Observer URL"` param.
247+ assert_redirected_to ( INAT_AUTHORIZATION_URL )
226248 end
227249
228250 def test_create_strip_inat_username
@@ -280,7 +302,7 @@ def test_create_authorization_request
280302 consent : 1 } )
281303 end
282304
283- assert_response ( :redirect )
305+ assert_redirected_to ( INAT_AUTHORIZATION_URL )
284306 assert_equal ( inat_ids . split ( "," ) . length , inat_import . reload . importables ,
285307 "Failed to save InatImport.importables" )
286308 assert_equal ( "Authorizing" , inat_import . reload . state ,
0 commit comments