-
Notifications
You must be signed in to change notification settings - Fork 79
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
Add invalid ID tests for main.views
#1124
Conversation
OpenOversight/app/main/views.py
Outdated
exception_type, value, full_traceback = sys.exc_info() | ||
error_str = " ".join([str(exception_type), str(value), format_exc()]) | ||
current_app.logger.error(f"Error finding officer: {error_str}") | ||
abort(HTTPStatus.NOT_FOUND) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little unsure of how to test this. Any advice would be appreciated, @sea-kelp @abandoned-prototype.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see what you're saying - it doesn't seem like this code is reachable since (as far as I can tell) the only other exception that could be thrown in the try block is MultipleResultsFound, but officer id is configured as a primary key. I wonder if we should just remove it.
If you do want to test it, you could patch Query.one()
with a side effect to raise MultipleResultsFound
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that we are searching by the primary key, I am comfortable removing this exception section. Thanks for the 👁️ , @sea-kelp!
main.views
try: | ||
department = Department.query.filter_by(id=department_id).one() | ||
except NoResultFound: | ||
abort(HTTPStatus.NOT_FOUND) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though it is equivalent logic, but I feel more comfortable using the id
field of the Department
object to query the Image
s downstream.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am assuming this will lead to one additional query so not exactly equivalent, but makes sense to me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, one more additional query. I think there's more value to the sureness around it than the minor performance hit.
except: # noqa: E722 | ||
exception_type, value, full_traceback = sys.exc_info() | ||
error_str = " ".join([str(exception_type), str(value), format_exc()]) | ||
current_app.logger.error(f"Error finding officer: {error_str}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic was unreachable.
OpenOversight/app/main/views.py
Outdated
@@ -896,6 +898,10 @@ def list_officer( | |||
current_job=None, | |||
require_photo: Optional[bool] = None, | |||
): | |||
department = db.session.get(Department, department_id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there isn't a valid Department
there is no need to perform any of the other queries.
OpenOversight/app/main/views.py
Outdated
if not officer: | ||
flash("Officer not found") | ||
abort(HTTPStatus.NOT_FOUND) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar logic to: https://github.com/lucyparsons/OpenOversight/pull/1124/files#r1779369649
@@ -379,15 +380,17 @@ def redirect_add_assignment(officer_id: int): | |||
@ac_or_admin_required | |||
def add_assignment(officer_id: int): | |||
form = AssignmentForm() | |||
officer = db.session.get(Officer, officer_id) | |||
try: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made object queries more uniform to the rest of the application.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Thanks!
try: | ||
department = Department.query.filter_by(id=department_id).one() | ||
except NoResultFound: | ||
abort(HTTPStatus.NOT_FOUND) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am assuming this will lead to one additional query so not exactly equivalent, but makes sense to me
# Select a random unsorted image from the database | ||
image_query = Image.query.filter_by(contains_cops=None).filter_by( | ||
department_id=department_id | ||
department_id=department.id |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would either not change this line, or also update the department_id
on line 282
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll update line 282, good 👁️!
Description of Changes
There were a handful of routes where we did not validate the initial ID parameters. Additionally, I standardized how object querying was done within the
main.views
file.Tests and Linting
develop
branch.pytest
passes on my local development environment.pre-commit
passes on my local development environment.