Skip to content

Commit

Permalink
Fix remaining errors in this branch
Browse files Browse the repository at this point in the history
- Update stub of iNat API request
(conforms param order and format to those in this branch)
- Stub jwt request
  • Loading branch information
JoeCohen committed Aug 16, 2024
1 parent 361d45b commit 5c2c1ca
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions test/controllers/observations/inat_imports_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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|
Expand Down

0 comments on commit 5c2c1ca

Please sign in to comment.