From 5c2c1cad70739bb5dabc88e99de174596e9bffa2 Mon Sep 17 00:00:00 2001 From: Joe Cohen Date: Fri, 16 Aug 2024 13:12:17 -0700 Subject: [PATCH] Fix remaining errors in this branch - Update stub of iNat API request (conforms param order and format to those in this branch) - Stub jwt request --- .../inat_imports_controller_test.rb | 47 ++++++++++++++----- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/test/controllers/observations/inat_imports_controller_test.rb b/test/controllers/observations/inat_imports_controller_test.rb index bb5a5a7025..c92df1b267 100644 --- a/test/controllers/observations/inat_imports_controller_test.rb +++ b/test/controllers/observations/inat_imports_controller_test.rb @@ -111,6 +111,7 @@ def test_import_authorized inat_authorization_callback_params = { code: "MockCode" } stub_access_token_request + stub_jwt_request login(user.login) get(:authenticate, params: inat_authorization_callback_params) @@ -300,6 +301,7 @@ def test_import_plant stub_inat_api_request(inat_import_ids, mock_search_result) simulate_authorization(user: user, inat_import_ids: inat_import_ids) stub_access_token_request + stub_jwt_request params = { inat_ids: inat_import_ids, code: "MockCode" } login(user.login) @@ -322,6 +324,7 @@ def test_import_zero_results stub_inat_api_request(inat_import_ids, mock_search_result) simulate_authorization(user: user, inat_import_ids: inat_import_ids) stub_access_token_request + stub_jwt_request params = { inat_ids: inat_import_ids, code: "MockCode" } login(user.login) @@ -419,6 +422,7 @@ def import_mock_observation(filename) stub_access_token_request stub_inat_api_request(inat_import_ids, mock_search_result, inat_user_login: inat_obs.inat_user_login) + stub_jwt_request params = { inat_ids: inat_import_ids, code: "MockCode" } login(user.login) @@ -437,23 +441,25 @@ def import_mock_observation(filename) def stub_inat_api_request(inat_obs_ids, mock_inat_response, id_above: 0, inat_user_login: nil) - # mirror param order of iNat example/test page - # https://api.inaturalist.org/v1/docs/#!/Observations/get_observations - # for convenience in creating mock responses from that page - params = <<~PARAMS.delete("\n") - ?id=#{inat_obs_ids} - &user_login=#{inat_user_login} - &iconic_taxa=#{Observations::InatImportsController::ICONIC_TAXA} + # params must be in same order as in the controller + # omit trailing "=" since the controller omits it (via `merge`) + params = <<~PARAMS.delete("\n").chomp("=") + ?iconic_taxa=#{Observations::InatImportsController::ICONIC_TAXA} + &id=#{inat_obs_ids} &id_above=#{id_above} &per_page=200 - &order=asc&order_by=id &only_id=false + &order=asc&order_by=id + &user_login=#{inat_user_login} PARAMS - - WebMock.stub_request( - :get, - "#{INAT_OBS_REQUEST_PREFIX}#{params}" - ).to_return(body: mock_inat_response) + stub_request(:get, "#{INAT_OBS_REQUEST_PREFIX}#{params}"). + with(headers: + { "Accept" => "application/json", + "Accept-Encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3", + "Authorization" => "Bearer", + "Host" => "api.inaturalist.org", + "User-Agent" => "rest-client/2.1.0 (darwin23 x86_64) ruby/3.3.0p0" }). + to_return(body: mock_inat_response) end def mock_photo_importer(inat_obs) @@ -529,6 +535,21 @@ def stub_access_token_request headers: {}) end + def stub_jwt_request + stub_request(:get, "https://www.inaturalist.org/users/api_token"). + with( + headers: { + "Accept" => "application/json", + "Accept-Encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3", + "Authorization" => "Bearer MockAccessToken", + "Host" => "www.inaturalist.org" + } + ). + to_return(status: 200, + body: { access_token: "MockJWT" }.to_json, + headers: {}) + end + def result_without_photos(mock_search_result) ms_hash = JSON.parse(mock_search_result) ms_hash["results"].each do |result|