Skip to content
This repository was archived by the owner on Oct 1, 2022. It is now read-only.

Commit 1ee4aeb

Browse files
committed
Add unit test for ServiceX.get_requesting_user
1 parent c877a8b commit 1ee4aeb

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

tests/resources/test_servicex_resource.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,42 @@
2727
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828
import pkg_resources
2929

30+
from pytest import fixture
31+
32+
from servicex.resources.servicex_resource import ServiceXResource
33+
from tests.resource_test_base import ResourceTestBase
34+
35+
36+
class TestServiceXResource(ResourceTestBase):
37+
module = ServiceXResource.__module__
38+
39+
@fixture
40+
def mock_user(self, mocker):
41+
return mocker.patch(f"{self.module}.UserModel.find_by_sub").return_value
3042

31-
class TestServiceXResource:
3243
def test_get_app_version_no_servicex_app(self, mocker):
3344
mock_get_distribution = mocker.patch(
34-
"servicex.resources.servicex_resource.pkg_resources.get_distribution",
35-
side_effect=pkg_resources.DistributionNotFound(None, None))
45+
f"{self.module}.pkg_resources.get_distribution",
46+
side_effect=pkg_resources.DistributionNotFound(None, None)
47+
)
3648

37-
from servicex.resources.servicex_resource import ServiceXResource
3849
version = ServiceXResource._get_app_version()
3950

4051
mock_get_distribution.assert_called_with("servicex_app")
4152
assert version == 'develop'
53+
54+
def test_get_requesting_user_no_auth(self, client):
55+
with client.application.app_context():
56+
assert ServiceXResource.get_requesting_user() is None
57+
58+
def test_get_requesting_user_with_auth(self, mocker, mock_user):
59+
mocker.patch(f"{self.module}.get_jwt_identity").return_value = "abcd"
60+
client = self._test_client(extra_config={'ENABLE_AUTH': True})
61+
with client.application.app_context():
62+
assert ServiceXResource.get_requesting_user() == mock_user
63+
64+
def test_get_requesting_user_no_identity(self, mocker):
65+
mocker.patch(f"{self.module}.get_jwt_identity").return_value = None
66+
client = self._test_client(extra_config={'ENABLE_AUTH': True})
67+
with client.application.app_context():
68+
assert ServiceXResource.get_requesting_user() is None

0 commit comments

Comments
 (0)