Skip to content

Commit

Permalink
[Backend] Add method to check if a user is staff (#4045)
Browse files Browse the repository at this point in the history
* Adeed function to check a user staff

* Added test for the function

* Fix user being same in test

* Name Change

---------

Co-authored-by: Gunjan Chhablani <[email protected]>
  • Loading branch information
Suryansh5545 and gchhablani authored Jul 28, 2023
1 parent 7d1cd20 commit 74d7396
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
13 changes: 13 additions & 0 deletions apps/base/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,16 @@ def is_model_field_changed(model_obj, field_name):
if prev != curr:
return True
return False


def is_user_a_staff(user):
"""
Function to check if a user is staff or not
Args:
user ([User Class Object]): User model class object
Return:
{bool} : True/False if the user is staff or not
"""
return user.is_staff
25 changes: 24 additions & 1 deletion tests/unit/base/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from rest_framework import status
from rest_framework.test import APITestCase, APIClient

from base.utils import RandomFileName, send_slack_notification
from base.utils import RandomFileName, send_slack_notification, is_user_a_staff
from challenges.models import Challenge, ChallengePhase
from hosts.models import ChallengeHostTeam
from jobs.models import Submission
Expand Down Expand Up @@ -134,3 +134,26 @@ def test_if_slack_notification_works(self):
response = send_slack_notification(message=message)
self.assertEqual(type(response), requests.models.Response)
self.assertEqual(response.status_code, status.HTTP_200_OK)


class TestUserIsStaff(BaseAPITestClass):

def test_if_user_is_staff(self):
self.user = User.objects.create(
username="someuser1",
email="[email protected]",
password="secret_password",
is_staff=True,
)
self.assertTrue(is_user_a_staff(self.user))

def test_if_user_is_not_staff(self):
self.user = User.objects.create(
username="someuser2",
email="[email protected]",
password="secret_password",
is_staff=False,
)
self.user.is_staff = False
self.user.save()
self.assertFalse(is_user_a_staff(self.user))

0 comments on commit 74d7396

Please sign in to comment.