conftest.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import pytest
  2. from pytest_mock import MockerFixture
  3. from flask_login import login_user, logout_user
  4. from flexmeasures.api.tests.utils import UserContext
  5. @pytest.fixture
  6. def requesting_user(request):
  7. """Use this fixture to log in a user for the scope of a test.
  8. Sets the user by passing it an email address (see usage examples below), or pass None to get the AnonymousUser.
  9. Passes the user object to the test.
  10. Logs the user out after the test ran.
  11. Usage:
  12. >>> @pytest.mark.parametrize("requesting_user", ["test_prosumer_user_2@seita.nl", None], indirect=True)
  13. Or in combination with other parameters:
  14. @pytest.mark.parametrize(
  15. "requesting_user, status_code",
  16. [
  17. (None, 401),
  18. ("test_prosumer_user_2@seita.nl", 200),
  19. ],
  20. indirect=["requesting_user"],
  21. )
  22. """
  23. email = request.param
  24. if email is not None:
  25. with UserContext(email) as user:
  26. login_user(user)
  27. yield user
  28. logout_user()
  29. else:
  30. yield
  31. @pytest.fixture
  32. def mock_get_statuses(mocker: MockerFixture):
  33. return mocker.patch(
  34. "flexmeasures.data.services.sensors.get_statuses", autospec=True
  35. )