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

blocks admins from running tests #250

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/groundlight/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ def _verify_connectivity(self) -> None:
try:
# a simple query to confirm that the endpoint & API token are working
self.whoami()
if self._user_is_privileged():
logger.warning(
"WARNING: The current user has elevated permissions. Please verify such permissions are necessary"
" for your current operation"
)
except UnauthorizedException as e:
msg = (
f"Invalid API token '{self.api_token_prefix}...' connecting to endpoint "
Expand Down Expand Up @@ -201,6 +206,14 @@ def whoami(self) -> str:
obj = self.user_api.who_am_i()
return obj["username"]

def _user_is_privileged(self) -> bool:
"""
Return a boolean indicating whether the user is privileged.
Privleged users have elevated permissions, so care should be taken when using a privileged account.
"""
obj = self.user_api.who_am_i()
return obj["is_superuser"] or obj["is_staff"]

def get_detector(self, id: Union[str, Detector]) -> Detector: # pylint: disable=redefined-builtin
"""
Get a detector by id
Expand Down
9 changes: 9 additions & 0 deletions test/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
from model import Detector, ImageQuery


def pytest_configure(config):
# Run environment check before tests
gl = Groundlight()
if gl._user_is_privileged():
raise Exception(
"ERROR: You are running tests with a privileged user. Please run tests with a non-privileged user."
)


@pytest.fixture(name="gl")
def fixture_gl() -> Groundlight:
"""Creates a Groundlight client object for testing."""
Expand Down