Skip to content

Commit

Permalink
Rename form to reflect action name
Browse files Browse the repository at this point in the history
I changed the action a while back but forgot about the form.f
  • Loading branch information
JoeCohen committed Jul 22, 2024
1 parent 9637994 commit 70a5239
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 23 deletions.
2 changes: 1 addition & 1 deletion app/controllers/observations/inat_imports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def import_one_observation(inat_obs_id)

def inat_search_observations(ids)
operation = "/observations?id=#{ids}" \
"&order=desc&order_by=created_at&only_id=false"
"&order=asc&order_by=id&only_id=false"
::Inat.new(operation: operation, token: inat_import.token).body
end

Expand Down
65 changes: 43 additions & 22 deletions test/controllers/observations/inat_imports_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def initialize(errors: [], results: [])

class InatImportsControllerTest < FunctionalTestCase
INAT_OBS_REQUEST_PREFIX = "https://api.inaturalist.org/v1/observations?"
INAT_OBS_REQUEST_POSTFIX = "&order=desc&order_by=created_at&only_id=false"
INAT_OBS_REQUEST_POSTFIX = "&order=asc&order_by=id&only_id=false"
# Where iNat will send the code once authorized
REDIRECT_URI =
"http://localhost:3000/observations/inat_imports/authenticate"
Expand Down Expand Up @@ -58,11 +58,11 @@ def test_create_inat_import_authorization_request
def test_create_inat_import_no_consent
mock_inat_response =
File.read("test/inat/evernia_no_photos.txt")
inat_obs_id = InatObs.new(mock_inat_response).inat_id
inat_import_ids = InatObs.new(mock_inat_response).inat_id
# TODO: remove consent key after creating model with default consent
params = { inat_ids: inat_obs_id, consent: 0 }
params = { inat_ids: inat_import_ids, consent: 0 }

stub_inat_api_request(inat_obs_id, mock_inat_response)
stub_inat_api_request(inat_import_ids, mock_inat_response)

login

Expand Down Expand Up @@ -130,7 +130,7 @@ def test_create_import_evernia_no_photos
user = rolf
filename = "evernia_no_photos"
mock_inat_response = File.read("test/inat/#{filename}.txt")
inat_obs_id = InatObs.new(mock_inat_response).inat_id
inat_import_ids = InatObs.new(mock_inat_response).inat_id

loc = Location.create(
user: users(:rolf),
Expand All @@ -141,11 +141,11 @@ def test_create_import_evernia_no_photos
west: -122.431
)

stub_inat_api_request(inat_obs_id, mock_inat_response)
simulate_authorization(user: user, inat_obs_id: inat_obs_id)
stub_inat_api_request(inat_import_ids, mock_inat_response)
simulate_authorization(user: user, inat_import_ids: inat_import_ids)
stub_token_request

params = { inat_ids: inat_obs_id, code: "MockCode" }
params = { inat_ids: inat_import_ids, code: "MockCode" }
login(user.login)

assert_difference("Observation.count", 1, "Failed to create Obs") do
Expand All @@ -157,7 +157,7 @@ def test_create_import_evernia_no_photos
assert_redirected_to(observations_path)

assert_equal("mo_inat_import", obs.source)
assert_equal(inat_obs_id, obs.inat_id)
assert_equal(inat_import_ids, obs.inat_id)

assert_equal(0, obs.images.length, "Obs should not have 0 images")

Expand Down Expand Up @@ -213,13 +213,13 @@ def test_create_import_plant
user = rolf
filename = "ceanothus_cordulatus"
mock_inat_response = File.read("test/inat/#{filename}.txt")
inat_obs_id = InatObs.new(mock_inat_response).inat_id
inat_import_ids = InatObs.new(mock_inat_response).inat_id

stub_inat_api_request(inat_obs_id, mock_inat_response)
simulate_authorization(user: user, inat_obs_id: inat_obs_id)
stub_inat_api_request(inat_import_ids, mock_inat_response)
simulate_authorization(user: user, inat_import_ids: inat_import_ids)
stub_token_request

params = { inat_ids: inat_obs_id, code: "MockCode" }
params = { inat_ids: inat_import_ids, code: "MockCode" }
login(user.login)

assert_no_difference(
Expand All @@ -228,20 +228,41 @@ def test_create_import_plant
post(:authenticate, params: params)
end

assert_flash_text(:inat_taxon_not_importable.l(id: inat_obs_id))
assert_flash_text(:inat_taxon_not_importable.l(id: inat_import_ids))
end

def test_create_import_multiple
inat_obss =
"216357655, 219783802" # evernia_no_photos, fuligo_septica
user = users(:rolf)
filename = "listed_ids"
mock_inat_response = File.read("test/inat/#{filename}.txt")

# test that mock was constructed properly
json = JSON.parse(mock_inat_response)
assert_equal(2, json["total_results"])
assert_equal(1, json["page"])
assert_equal(30, json["per_page"])
# mock is sorted by id, asc
assert_equal(216_357_655, json["results"].first["id"])
assert_equal(219_783_802, json["results"].second["id"])

# stub_inat_api_request(inat_import_ids, mock_inat_response)
# simulate_authorization(user: user, inat_import_ids: inat_import_ids)
stub_token_request
end

def import_mock_observation(filename)
user = users(:rolf)
mock_inat_response = File.read("test/inat/#{filename}.txt")
inat_obs = InatObs.new(mock_inat_response)
inat_obs_id = inat_obs.inat_id
inat_import_ids = inat_obs.inat_id

stub_inat_api_request(inat_obs_id, mock_inat_response)
simulate_authorization(user: user, inat_obs_id: inat_obs_id)
stub_inat_api_request(inat_import_ids, mock_inat_response)
simulate_authorization(user: user, inat_import_ids: inat_import_ids)
stub_token_request

params = { inat_ids: inat_obs_id, code: "MockCode" }
params = { inat_ids: inat_import_ids, code: "MockCode" }
login(user.login)

# NOTE: Stubs the importer's return value, but not its side-effect --
Expand All @@ -256,10 +277,10 @@ def import_mock_observation(filename)
Observation.order(created_at: :asc).last
end

def stub_inat_api_request(inat_obs_id, mock_inat_response)
def stub_inat_api_request(inat_obs_ids, mock_inat_response)
WebMock.stub_request(
:get,
"#{INAT_OBS_REQUEST_PREFIX}id=#{inat_obs_id}" \
"#{INAT_OBS_REQUEST_PREFIX}id=#{inat_obs_ids}" \
"#{INAT_OBS_REQUEST_POSTFIX}"
).to_return(body: mock_inat_response)
end
Expand Down Expand Up @@ -298,9 +319,9 @@ def expected_mo_photo_license(mock_inat_photo)
mo_license.id
end

def simulate_authorization(user: rolf, inat_obs_id: "")
def simulate_authorization(user: rolf, inat_import_ids: "")
inat_import = InatImport.find_or_create_by(user: user)
inat_import.inat_ids = inat_obs_id
inat_import.inat_ids = inat_import_ids
inat_import.state = "Authorizing"
inat_import.save
stub_request(:any, authorization_url)
Expand Down
1 change: 1 addition & 0 deletions test/inat/README_INAT_EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ All data as of the time of importing. (The corresponding iNat Observation may ha
| gyromitra_ancilis.txt | [216745568](https://www.inaturalist.org/observations/216745568) | 3 | public | **cc-by license**, **many projects**, US 20, Linn Co.|
| inocybe.txt | [222904190](https://www.inaturalist.org/observations/222904190) | 5 | public | cc-by-nc, **2 tags** |
| lentinellus_ursinus.txt | [24970904](https://inaturalist.org/observations/24970904) | 2 | obscured | **ID matches many MO names** |
| listed_ids.txt | na | na | na | response to request for 2 obs by number (evernia_no_photos, fuligo_septica) |
| lycoperdon.txt | [24970904](https://www.inaturalist.org/observations/24970904) | 2 | public | cc-by-nc, projects, Activity, many fields including **DNA**, place: E. side of Metolius River, Sisters Ranger District, Deschutes National Forest, Jefferson County, Oregon, US |
| russulaceae.txt | [216675045](https://www.inaturalist.org/observations/216675045) | 2 | public | **all rights reserved**, many projects, Activity; place: Point Defiance Park, Tacoma, WA, US |
| somion_unicolor.json | | | | Formatted version of following; facilitates viewing iNat API response key/values test/inat/somion_unicolor.json |
Expand Down
1 change: 1 addition & 0 deletions test/inat/listed_ids.txt

Large diffs are not rendered by default.

0 comments on commit 70a5239

Please sign in to comment.