Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed delimiter validation in resources endpoint #1998

Merged
merged 1 commit into from
Jan 26, 2021

Conversation

liavyona
Copy link
Contributor

@liavyona liavyona commented Jan 19, 2021

What does this PR do?

  • _What's changed? Added delimiter validation to resources endpoint
  • Are there relevant screenshots you can add to the PR description?
    image

image

What ticket does this PR close?

Resolves #1997

Checklists

Change log

  • The CHANGELOG has been updated, or
  • This PR does not include user-facing changes and doesn't require a CHANGELOG update

Test coverage

  • This PR includes new unit and integration tests to go with the code changes, or
  • The changes in this PR do not require tests

Documentation

  • Docs (e.g. READMEs) were updated in this PR, and/or there is a follow-on issue to update docs, or
  • This PR does not require updating any documentation

@@ -105,6 +105,9 @@ def search account: nil, kind: nil, owner: nil, offset: nil, limit: nil, search:
scope = scope.textsearch(search) if search

if offset || limit
if (offset && !numeric?(offset)) || (limit && !numeric?(limit))
raise ApplicationController::UnprocessableEntity, "Delimiter must be an integer if given"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shulifink can you please review this message?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand. What do you mean "if given"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liavyona Isn't it enough to just say "If you provide a value for Delimiter, it must be an integer greater than or equal to 0".

or simply: "Delimiter must be an integer greater than or equal to 0".

or "... must be an integer", or "must be a positive integer " (depending on what the integer type)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would go with "Delimiter offset or limit must be an integer greater than or equal to 0"

orenbm
orenbm previously approved these changes Jan 19, 2021
Copy link
Member

@orenbm orenbm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM @liavyona , thanks!
please get a review from @shulifink on the message.

@@ -212,6 +216,11 @@ def bad_request e
head :bad_request
end

def unprocessable_entity e
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ApplicationController#unprocessable_entity has the parameter name 'e'

@@ -131,6 +134,10 @@ def textsearch input
def visible_to role
from Sequel.function(:visible_resources, role.id).as(:resources)
end

def numeric? val
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resource#numeric? doesn't depend on instance state (maybe move it to another class?)

@izgeri
Copy link
Contributor

izgeri commented Jan 19, 2021

@liavyona please make sure to add a change log message for this! it's definitely a user-impacting change, and we should let people know we've fixed this.

@orenbm
Copy link
Member

orenbm commented Jan 19, 2021

@liavyona the code is fine by me but i would like to see a better commit message. please read this post. To be specific, please change the tense to Fix instead of Fixed and add a description that will explain this change a bit better for the future devs who will see it.

CHANGELOG.md Outdated Show resolved Hide resolved
app/models/resource.rb Outdated Show resolved Hide resolved
case kind
when "variable"
response["secrets"] = secrets_dataset.order(:version).as_json
.map { |h| h.except 'resource' }
.map { |h| h.except 'resource' }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 2 (not 37) spaces for indenting an expression in an assignment spanning multiple lines.

app/models/resource.rb Outdated Show resolved Hide resolved
sigalsax
sigalsax previously approved these changes Jan 20, 2021
Copy link
Contributor

@sigalsax sigalsax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Thanks for jumping on this bug
A couple of things 1. as Oren mentioned please check with Shuli that these logs are ok before merging 2. I tagged you in a codeclimate suggestion so check that out :)

app/models/resource.rb Outdated Show resolved Hide resolved
app/models/resource.rb Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
sigalsax
sigalsax previously approved these changes Jan 21, 2021
Copy link
Contributor

@sigalsax sigalsax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for this improvement!

# 'offset' must be an integer greater than or equal to 0 if given
if offset && (!numeric?(offset) || offset.to_i.negative?)
raise ApplicationController::UnprocessableEntity, "'offset' contains an invalid value. 'offset' must be an integer greater than or equal to 0."
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UnprocessableEntity is an http level concern. That is, we should only be talking about that error inside of controllers, and specifically in code that is mapping an actual domain error into an http status code.

Inside of the model we should be raising a more specific validation error, with a name that reflects what went wrong. InvalidQueryParameter or something like that.

Separate comment: There's a lot of formatting / clean changes here (which is awesome), but please put those into a separate commit with a subject like "Cleanup formatting" -- otherwise the "meat" of this PR (which is actually pretty small and simple) isn't clear at a glance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonahx fixed

jonahx
jonahx previously approved these changes Jan 21, 2021
Copy link
Contributor

@InbalZilberman InbalZilberman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great @liavyona
Thank you for taking this on.

Comment on lines 110 to 114
raise "'limit' contains an invalid value. 'limit' must be a positive integer."
end
# 'offset' must be an integer greater than or equal to 0 if given
if offset && (!numeric?(offset) || offset.to_i.negative?)
raise "'offset' contains an invalid value. 'offset' must be an integer greater than or equal to 0."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liavyona is it possible to move these errors to errors.rb?
we prefer to have all the user-facing messages there

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will be a problem to use a custom exception inside a model class. Ads you can see there is no usage of any error which is defined at errors.rb under app/model. Other model classes use simple raise or ArgumentError. I changed it to use ArgumentError

orenbm
orenbm previously approved these changes Jan 26, 2021
`GET /resources` endpoints takes 2 optionals numeric delimiters as requests parameters: `limit` & `offset`.
Previously, no input validation has been made when receiving these optional parameters.
Now, we verify that their value is valid if given.
@codeclimate
Copy link

codeclimate bot commented Jan 26, 2021

Code Climate has analyzed commit f8679e1 and detected 8 issues on this pull request.

Here's the issue category breakdown:

Category Count
Complexity 7
Style 1

The test coverage on the diff in this pull request is 90.9% (50% is the threshold).

This pull request will bring the total coverage in the repository to 89.3% (0.0% change).

View more on Code Climate.

@liavyona liavyona merged commit 0c4dabd into master Jan 26, 2021
@liavyona liavyona deleted the 1997-delimiter-validation branch January 26, 2021 10:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

Offset accepts string as valid input
7 participants