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

Refactor user_scores endpoint to add marshmallow schema validation #365

Open
3 tasks
sentry-io bot opened this issue Jul 20, 2022 · 0 comments
Open
3 tasks

Refactor user_scores endpoint to add marshmallow schema validation #365

sentry-io bot opened this issue Jul 20, 2022 · 0 comments

Comments

@sentry-io
Copy link

sentry-io bot commented Jul 20, 2022

Modules and functions to refactor

  • app/scoring/routes.py::user_scores

Refactor reason

The refactoring is needed to filter issues like Sentry Issue: BACKEND-G caused by an invalid request body. (the problem is what frontend sent us SetOne object with questionId > 10)

Details:
ValueError: 15 is not a valid PersonalValue
(10 additional frame(s) were not displayed)


...
  File "app/scoring/process_scores.py", line 43, in calculate_scores
    value_type = PersonalValue(question_id)
  File "enum.py", line 309, in __call__
    return cls.__new__(cls, value)
  File "enum.py", line 600, in __new__
    raise exc
  File "enum.py", line 584, in __new__
    result = cls._missing_(value)
  File "enum.py", line 613, in _missing_
    raise ValueError("%r is not a valid %s" % (value, cls.__name__))

request body

{
questionResponses: {
SetOne: [
{"answerId":"2","questionId":"15"}, 
{"answerId":"4","questionId":"9"}, 
{"answerId":"2","questionId":"4"}, 
{"answerId":"2","questionId":"8"}, 
{"answerId":"4","questionId":"10"}, 
{"answerId":"3","questionId":"6"}, 
{"answerId":"3","questionId":"1"}, 
{"answerId":"4","questionId":"3"}, 
{"answerId":"3","questionId":"2"}, 
{"answerId":"2","questionId":"7"}
], 
SetTwo: [
{"answerId":"4","questionId":"15"}, 
{"answerId":"3","questionId":"19"}, 
{"answerId":"5","questionId":"14"}, 
{"answerId":"4","questionId":"18"}, 
{"answerId":"3","questionId":"20"}, 
{"answerId":"4","questionId":"16"}, 
{"answerId":"5","questionId":"11"}, 
{"answerId":"3","questionId":"13"}, 
{"answerId":"2","questionId":"12"}, 
{"answerId":"3","questionId":"17"}
]
}
}

Suggested solution

  • new marshmallow schema created to validate the request body
  • documentation (Climate-Mind_bundled.yml) updated to show 422 HTTP error

Tests

  • integration test for the endpoint OR/AND unit test for a schema
@danmash danmash changed the title ValueError: 15 is not a valid PersonalValue app/scoring/routes.py::user_scores refactoring (ValueError: 15 is not a valid PersonalValue) Jul 20, 2022
@danmash danmash changed the title app/scoring/routes.py::user_scores refactoring (ValueError: 15 is not a valid PersonalValue) app/scoring/routes.py user_scores refactoring (ValueError: 15 is not a valid PersonalValue) Jul 20, 2022
@danmash danmash changed the title app/scoring/routes.py user_scores refactoring (ValueError: 15 is not a valid PersonalValue) app/scoring/routes.py user_scores refactoring Jul 20, 2022
@danmash danmash added refactoring documentation Improvements or additions to documentation bug Something isn't working help wanted labels Jul 20, 2022
@danmash danmash removed the documentation Improvements or additions to documentation label Oct 12, 2022
@danmash danmash changed the title app/scoring/routes.py user_scores refactoring Refactor user_scores endpoint to add marshmallow schema validation Oct 12, 2022
@danmash danmash removed the bug Something isn't working label Oct 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 📋 Backlog
Development

No branches or pull requests

1 participant