Skip to content

Commit

Permalink
Fix test_create_no_consent
Browse files Browse the repository at this point in the history
- 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`
  • Loading branch information
JoeCohen committed Jul 25, 2024
1 parent 34f6891 commit de65e94
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
11 changes: 4 additions & 7 deletions app/controllers/observations/inat_imports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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?
Expand Down
33 changes: 22 additions & 11 deletions test/controllers/observations/inat_imports_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit de65e94

Please sign in to comment.