Skip to content
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
6 changes: 5 additions & 1 deletion FusionIIIT/applications/globals/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ def active_designation(user):
office outranks the basic role, which is the rule the IAM applies too — so
somebody who has never touched the role switcher is not locked out of the
post they hold.

Falling through to the basic role matters: most people hold nothing else,
and treating "no office" as "no role" refuses every student their own
screens.
"""
held = held_designations(user)
chosen = getattr(getattr(user, 'extrainfo', None), 'last_selected_role', None)
if chosen and chosen.lower() in held:
return chosen.lower()
return next(iter(sorted(held - BASIC_ROLES)), None)
return next(iter(sorted(held - BASIC_ROLES)), None) or next(iter(sorted(held)), None)


def role_required(allowed_roles):
Expand Down
22 changes: 20 additions & 2 deletions FusionIIIT/applications/globals/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ def test_never_having_switched_does_not_lock_the_office_out(self):
"""The column is null for anyone who has not used the switcher."""
self.assertEqual(active_designation(self.user), 'acadadmin')

def test_a_role_that_is_no_longer_held_is_ignored(self):
def test_a_role_that_is_no_longer_held_falls_back_to_one_that_is(self):
self._act_as('acadadmin')
HoldsDesignation.objects.filter(
designation__name='acadadmin').delete()
self.assertIsNone(active_designation(self.user))
self.assertEqual(active_designation(self.user), 'student')

def test_a_stand_in_is_authorised_and_the_absent_holder_is_not(self):
"""`working` is the occupant; `user` is the substantive holder."""
Expand Down Expand Up @@ -151,3 +151,21 @@ def test_an_old_token_is_refused_and_destroyed(self):
with self.assertRaises(AuthenticationFailed):
self.auth.authenticate_credentials(self.token.key)
self.assertFalse(Token.objects.filter(pk=self.token.pk).exists())


class PlainStudentTests(TestCase):
"""Most people hold no office at all. They must keep their own screens."""

def setUp(self):
self.user = User.objects.create_user(username='student1')
ExtraInfo.objects.create(id='student1', user=self.user, user_type='student')
HoldsDesignation.objects.create(
user=self.user, working=self.user,
designation=Designation.objects.create(name='student'))

def test_a_student_holding_only_the_basic_role_still_acts_in_it(self):
self.assertEqual(active_designation(self.user), 'student')

def test_somebody_holding_nothing_acts_in_nothing(self):
bare = User.objects.create_user(username='nobody')
self.assertIsNone(active_designation(bare))
Loading