From de65e94f6d28ab7f4e7b2eae0c4337b253acfd72 Mon Sep 17 00:00:00 2001 From: "Joseph D. Cohen" Date: Thu, 25 Jul 2024 14:13:45 -0700 Subject: [PATCH] Fix test_create_no_consent - Assert specific flash text (vs just any old warning) - Includes username in params - Removes unnecessary code. (If there's no consent, it never calls the iNat API.) - Trvial refacor of `create` --- .../observations/inat_imports_controller.rb | 11 +++---- .../inat_imports_controller_test.rb | 33 ++++++++++++------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/app/controllers/observations/inat_imports_controller.rb b/app/controllers/observations/inat_imports_controller.rb index da09ce961f..6ba4890487 100644 --- a/app/controllers/observations/inat_imports_controller.rb +++ b/app/controllers/observations/inat_imports_controller.rb @@ -49,13 +49,13 @@ def create def reload_form @inat_ids = params[:inat_ids] + @inat_username = params[:inat_username] render(:new) end def designation_required flash_warning(:inat_no_imports_designated.t) - @inat_username = params[:inat_username] - render(:new) + reload_form end def imports_designated? @@ -64,15 +64,12 @@ def imports_designated? def consent_required flash_warning(:inat_consent_required.t) - @inat_ids = params[:inat_ids] - @inat_username = params[:inat_username] - render(:new) + reload_form end def username_required flash_warning(:inat_missing_username.l) - @inat_ids = params[:inat_ids] - render(:new) + reload_form end def bad_inat_ids_param? diff --git a/test/controllers/observations/inat_imports_controller_test.rb b/test/controllers/observations/inat_imports_controller_test.rb index 70c8af7229..7b3b1cfebe 100644 --- a/test/controllers/observations/inat_imports_controller_test.rb +++ b/test/controllers/observations/inat_imports_controller_test.rb @@ -77,23 +77,15 @@ def test_create_missing_username end def test_create_no_consent - mock_search = File.read("test/inat/evernia.txt") - inat_obs = InatObs.new( - JSON.generate(JSON.parse(mock_search)["results"].first) - ) - inat_import_ids = inat_obs.inat_id - stub_inat_api_request(inat_import_ids, mock_search) - - # TODO: remove consent key after creating model with default consent - params = { inat_ids: inat_import_ids, consent: 0 } + params = { inat_username: "anything", inat_ids: 123, + consent: 0 } login - assert_no_difference("Observation.count", "iNat obss imported without consent") do post(:create, params: params) end - assert_flash_warning + assert_flash_text(:inat_consent_required.l) end def test_import_authorization_denied @@ -264,6 +256,25 @@ def test_import_multiple end end + def test_import_all + skip("under construction") + user = users(:rolf) + inat_import_ids = "" + + simulate_authorization(user: user, inat_import_ids: inat_import_ids) + stub_token_request + stub_inat_api_request(inat_import_ids, mock_inat_response) + + params = { inat_ids: inat_import_ids, code: "MockCode" } + login(user.login) + + assert_difference( + "Observation.count", 2, "Failed to create multiple observations" + ) do + post(:authenticate, params: params) + end + end + def import_mock_observation(filename) user = users(:rolf) mock_search = File.read("test/inat/#{filename}.txt")