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

Fix 500 when visiting /note with no noteid (#248) #1454

Closed
wants to merge 2 commits into from

Conversation

huntertur
Copy link
Contributor

Fixes #248

I took the opportunity to practice writing tests for Weasyl by adding weasyl.test.web.test_interaction.

@@ -98,6 +98,7 @@
'noGuests': no_guest_access,
"noCover": "No cover exists for that submission.",
"noImageSource": "No image exists from which to create a thumbnail.",
"noteidInvalid": "The specified note is invalid.",
Copy link
Contributor

Choose a reason for hiding this comment

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

I think a new error code for every invalid parameter is overkill. (It’s also not a 403.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would you prefer a switch to the existing noteRecordMissing ("That note doesn't seem to be in our database."), and if so, would you want this to be a 404 or remain the default 422?

weasyl/forms.py Outdated
@@ -6,15 +6,16 @@
_T = TypeVar("T")


def expect_id(s: str) -> int:
def expect_id(s: str, error_message="Unexpected") -> int:
Copy link
Contributor

Choose a reason for hiding this comment

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

No changes to this function, please. Keep it simple. isinstance(s, str) shouldn’t be checked on s: str.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will revert this.

'/note?',
'/note?noteid',
'/note?noteid=',
'/note?noteid=a',
Copy link
Contributor

Choose a reason for hiding this comment

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

These tests are also overkill for something that’s more about integer parsing than notes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK if I move these to a separate test_forms.py that just tests expect_id directly, or would you want most of these gone entirely?

Copy link
Contributor

@charmander charmander left a comment

Choose a reason for hiding this comment

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

I’d still recommend just not fixing or testing this particular endpoint standalone – there’s nothing about /note that merits this special treatment as far as I’m aware. But if you could separate out test_forms and test_get_note_permissions into their own PRs, those would be great.



@pytest.mark.usefixtures('db')
def test_get_note_permissions(app):
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for adding this test! This is a good test.

'content': 'Content 2',
}, headers={'Cookie': cookie2})

resp = app.get('/note?noteid=1', headers={'Cookie': cookie1})
Copy link
Contributor

Choose a reason for hiding this comment

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

Can’t rely on these notes having a particular id. As far as how to find that id… exercising /notes might not be a bad thing?


INVALID_PATHS = (
'/note',
'/note?noteid=1&noteid=2',
Copy link
Contributor

Choose a reason for hiding this comment

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

Again, not really a thing that’s specific to notes. Regression tests are good in general, but extrapolating from this one, we will be testing basic parameter handling on every route.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is fair. I will remove the INVALID_PATHS-related tests and keep test_get_note_permissions.

Comment on lines +100 to +102
try:
noteid = forms.expect_id(request.GET.getone("noteid"))
except (KeyError, WeasylError) as e:
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice try – this is about is good as it can be.

@huntertur
Copy link
Contributor Author

Closing this PR to split out the new tests into their own PRs.

@huntertur huntertur closed this Nov 1, 2024
charmander added a commit that referenced this pull request Nov 1, 2024
Split from the now-closed #1454.

Reviewed-by: Charmander <~@charmander.me>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AttributeError: 'noteid' when visiting /note route without form data
2 participants