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

Add invalid ID tests for main.views #1124

Merged
merged 22 commits into from
Oct 1, 2024
Merged

Conversation

michplunkett
Copy link
Collaborator

@michplunkett michplunkett commented Sep 27, 2024

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

  • This branch is up-to-date with the develop branch.
  • pytest passes on my local development environment.
  • pre-commit passes on my local development environment.

@michplunkett michplunkett self-assigned this Sep 27, 2024
Comment on lines 325 to 328
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)
Copy link
Collaborator Author

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.

Copy link
Collaborator

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

Copy link
Collaborator Author

@michplunkett michplunkett Sep 28, 2024

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!

@michplunkett michplunkett changed the title Add invalid ID tests Add invalid ID tests for main.views Sep 28, 2024
Comment on lines +266 to +269
try:
department = Department.query.filter_by(id=department_id).one()
except NoResultFound:
abort(HTTPStatus.NOT_FOUND)
Copy link
Collaborator Author

@michplunkett michplunkett Sep 28, 2024

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 Images downstream.

Copy link
Collaborator

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

Copy link
Collaborator Author

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.

Comment on lines -319 to -322
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}")
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This logic was unreachable.

@@ -896,6 +898,10 @@ def list_officer(
current_job=None,
require_photo: Optional[bool] = None,
):
department = db.session.get(Department, department_id)
Copy link
Collaborator Author

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.

Comment on lines 384 to 386
if not officer:
flash("Officer not found")
abort(HTTPStatus.NOT_FOUND)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@michplunkett michplunkett marked this pull request as ready for review September 28, 2024 05:38
@@ -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:
Copy link
Collaborator Author

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.

@michplunkett michplunkett marked this pull request as draft September 28, 2024 06:05
@michplunkett michplunkett marked this pull request as ready for review September 28, 2024 06:22
Copy link
Collaborator

@abandoned-prototype abandoned-prototype left a comment

Choose a reason for hiding this comment

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

Looks good. Thanks!

Comment on lines +266 to +269
try:
department = Department.query.filter_by(id=department_id).one()
except NoResultFound:
abort(HTTPStatus.NOT_FOUND)
Copy link
Collaborator

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
Copy link
Collaborator

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

Copy link
Collaborator Author

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 👁️!

@michplunkett michplunkett merged commit 146cc8e into develop Oct 1, 2024
3 checks passed
@michplunkett michplunkett deleted the make-pages-more-robust branch October 1, 2024 04:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants