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

get_current_account: catch exception when portal cannot be found. #757

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mauritsvanrees
Copy link
Contributor

This can happen in unit tests without test layer.
For example in a buildout with osha.oira:

$ bin/test -s euphorie --test-path=/Users/maurits/syslab/quoira/oira/src/Euphorie/src -u
...
Error in test testAccountType (euphorie.client.tests.test_model.AccountTests)
Traceback (most recent call last):
  File "/Users/maurits/.pyenv/versions/3.8.19/lib/python3.8/unittest/case.py", line 60, in testPartExecutor
    yield
  File "/Users/maurits/.pyenv/versions/3.8.19/lib/python3.8/unittest/case.py", line 676, in run
    self._callTestMethod(testMethod)
  File "/Users/maurits/.pyenv/versions/3.8.19/lib/python3.8/unittest/case.py", line 633, in _callTestMethod
    method()
  File "/Users/maurits/syslab/quoira/oira/src/Euphorie/src/euphorie/client/tests/test_model.py", line 294, in testAccountType
    (self.session, self.survey) = createSurvey()
  File "/Users/maurits/syslab/quoira/oira/src/Euphorie/src/euphorie/client/tests/test_model.py", line 18, in createSurvey
    survey = model.SurveySession(title="Session", zodb_path="survey", account=account)
  File "<string>", line 4, in __init__
  File "/Users/maurits/shared-eggs/cp38/SQLAlchemy-1.4.52-py3.8-macosx-14.4-x86_64.egg/sqlalchemy/orm/state.py", line 476, in _initialize_instance
    manager.dispatch.init(self, args, kwargs)
  File "/Users/maurits/shared-eggs/cp38/SQLAlchemy-1.4.52-py3.8-macosx-14.4-x86_64.egg/sqlalchemy/event/attr.py", line 346, in __call__
    fn(*args, **kw)
  File "/Users/maurits/shared-eggs/cp38/SQLAlchemy-1.4.52-py3.8-macosx-14.4-x86_64.egg/sqlalchemy/orm/events.py", line 236, in wrap
    return fn(target, *arg, **kw)
  File "/Users/maurits/syslab/quoira/oira/src/osha.oira/src/osha/oira/client/subscribers.py", line 39, in update_user_languages_subscriber
    account = get_current_account()
  File "/Users/maurits/syslab/quoira/oira/src/Euphorie/src/euphorie/client/model.py", line 1986, in get_current_account
    user_id = api.user.get_current().getId()
  File "/Users/maurits/shared-eggs/cp38/plone.api-2.1.0-py3.8.egg/plone/api/user.py", line 131, in get_current
    portal_membership = portal.get_tool("portal_membership")
  File "/Users/maurits/shared-eggs/cp38/decorator-5.1.1-py3.8.egg/decorator.py", line 232, in fun
    return caller(func, *(extras + args), **kw)
  File "/Users/maurits/shared-eggs/cp38/plone.api-2.1.0-py3.8.egg/plone/api/validation.py", line 73, in wrapped
    return function(*args, **kwargs)
  File "/Users/maurits/shared-eggs/cp38/plone.api-2.1.0-py3.8.egg/plone/api/portal.py", line 105, in get_tool
    return getToolByName(get(), name)
  File "/Users/maurits/shared-eggs/cp38/plone.api-2.1.0-py3.8.egg/plone/api/portal.py", line 68, in get
    raise CannotGetPortalError(
plone.api.exc.CannotGetPortalError: Unable to get the portal object. More info on https://docs.plone.org/develop/plone.api/docs/api/exceptions.html#plone.api.exc.CannotGetPortalError
...
  Ran 133 tests with 0 failures, 3 errors, 0 skipped in 2.499 seconds.

Copy link
Member

@ale-rt ale-rt left a comment

Choose a reason for hiding this comment

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

I would prefer to not import new stuff from plone.api.

Also, I would consider using a mock rather than catching that expression, e.g.:

with mock.patch("plone.api.user.get_current", return_value=DummyUser(email="...")):

or

with mock.patch("plone.api.user.get_current", return_value=None):

src/euphorie/client/model.py Outdated Show resolved Hide resolved
src/euphorie/client/model.py Outdated Show resolved Hide resolved
In these unit tests no portal is available, which trips up our test in combination with some third party code.
@mauritsvanrees
Copy link
Contributor Author

Ah, we are using mock in some of those tests already. That is better. Done.
I have force pushed, so we only have that change and not my original.

@mauritsvanrees mauritsvanrees force-pushed the maurits-cannot-get-portal-error branch from cf29e24 to 084066e Compare August 2, 2024 12:35
@ale-rt
Copy link
Member

ale-rt commented Aug 2, 2024

Ah! Now I was able to grok it...
In the end, the issue is related to: update_user_languages_subscriber:
See:
https://github.com/euphorie/osha.oira/blob/e36ebf46053519eabad4d425cf8fe6370c1756c9/src/osha/oira/client/subscribers.py#L39-L41

There it might have sense to catch the CannotGetPortalError exception.

survey3 = add_survey(zodb_path="3", group=group2)
session.add(survey3)
session.flush()
with mock.patch("plone.api.user.get_current", return_value=nobody):
Copy link
Member

Choose a reason for hiding this comment

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

I think that in this case the return_value should be sqlalchemy account, i.e. account1.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have updated the PR to do this. But then the tests actually fail again: in the osha.oira subscriber get_current_account then gets an actual account, and then the subscriber tries to get the portal and this fails.

So maybe we should abandon this, and instead catch the error in osha.oira as you suggest. I will push a PR for that.

@mauritsvanrees mauritsvanrees force-pushed the maurits-cannot-get-portal-error branch from c6542d8 to cc7e7a7 Compare August 5, 2024 13:44
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.

2 participants