I have a Flask app running that kept producing a weird bug in production. For a route, I check a user's role and their ID against the requested resource before returning a result.
Locally, I had the following line which works on the dev server as well as passes tests:
# user_id is a param passed to the Flask route
if current_user.usertype_id == 1 or current_user.id is user_id:
# do something
else:
abort(401)
In production, this would throw a 401 error every time, even with a logged in user. So, I changed it to check for equality only:
# user_id is a param passed to the Flask route
if current_user.usertype_id == 1 or current_user.id == user_id:
# do something
else:
abort(401)
...and that solved the unauthorized error.
I understand the difference between is
and ==
, so my question is why doesn't this throw an error locally? Nothing has changed, so I would have expected tests to catch this, but nothing failed in the unit tests or when I was testing manually in the browser.