def post(self, request, user_id): post_body = request.POST status_code = status.HTTP_200_OK invalid = False form = PermissionBuilderForm(post_body) add_another = True if post_body.get('add_another') == "true" else False if form.is_valid(): response = self.managed_user.add_permission(form, self.user.auth_token) if response.ok: if add_another: form = PermissionBuilderForm() else: return redirect('/users/%s/manage' % self.managed_user.user_id) else: invalid = True log_api_error('permission creation', response.text) status_code = response.status_code else: invalid = True status_code = status.HTTP_400_BAD_REQUEST context = self.__set_add_permission_context(form, invalid) return render(request, self.template, context, status=status_code)
def test_location_id_returns_the_trust_id_if_its_present(self): form_content = { 'role': 'meo', 'permission_level': 'trust', 'region': None, 'trust': '2' } form = PermissionBuilderForm(form_content) self.assertEqual(form.trust, form_content['trust']) self.assertEqual(form.location_id(), form.trust)
def test_permission_level_uses_permission_level_if_multiple_values_present_and_trust_level_specified( self): form_content = { 'role': 'meo', 'permission_level': 'trust', 'region': 'region_id', 'trust': 'trust_id' } form = PermissionBuilderForm(form_content) self.assertEqual(form.location_id(), 'trust_id')
def test_is_valid_returns_false_when_no_role_given(self): form_content = { 'role': None, 'permission_level': 'national', 'region': None, 'trust': None } form = PermissionBuilderForm(form_content) result = form.is_valid() self.assertIsFalse(result) self.assertEqual(form.role_error, messages.FIELD_MISSING % "a role")
def test_location_id_returns_none_if_no_trust_or_region_present(self): form_content = { 'role': 'meo', 'permission_level': 'national', 'region': None, 'trust': None } form = PermissionBuilderForm(form_content) self.assertIsNone(form.region) self.assertIsNone(form.trust) self.assertIsNone(form.location_id())
def test_region_present_returns_true_when_a_region_is_present(self): form_content = { 'role': 'meo', 'permission_level': 'regional', 'region': '1', 'trust': None } form = PermissionBuilderForm(form_content) self.assertEqual(form.region, form_content['region']) self.assertIsTrue(form.region_present())
def test_is_valid_returns_true_when_role_at_trust_level_given_with_trust_given( self): form_content = { 'role': 'sa', 'permission_level': 'trust', 'region': None, 'trust': '1' } form = PermissionBuilderForm(form_content) result = form.is_valid() self.assertIsTrue(result) self.assertEqual(form.role_error, None) self.assertEqual(form.permission_level_error, None) self.assertEqual(form.region_error, None) self.assertEqual(form.trust_error, None)
def test_to_dict_returns_form_fields_as_a_dict(self): role = 'sa' level = 'trust' trust = '1' form_content = { 'role': role, 'permission_level': level, 'region': None, 'trust': trust } user_id = 1 form = PermissionBuilderForm(form_content) result = form.to_dict(user_id) self.assertEqual(result['userRole'], role) self.assertEqual(result['locationId'], trust) self.assertEqual(result['userId'], user_id)
def post(self, request, user_id, permission_id): post_body = request.POST form = PermissionBuilderForm(post_body) if form.is_valid(): response = self.managed_user.update_permission(form, permission_id, self.user.auth_token) if response.ok: return redirect('/users/%s/manage' % self.managed_user.user_id) else: invalid = True log_api_error('permission creation', response.text) status_code = response.status_code else: invalid = True status_code = status.HTTP_400_BAD_REQUEST permission = Permission.load_by_id(user_id, permission_id, self.user.auth_token) context = self.__set_edit_permission_context(permission, invalid, posted_form=form) return render(request, self.template, context, status=status_code)
def __set_edit_permission_context(self, permission, invalid, posted_form=None): locations = self.user.get_location_collection() form = posted_form if posted_form else PermissionBuilderForm.load_from_permission(permission, locations) return { 'session_user': self.user, 'sub_heading': 'Edit role and permission level', 'form': form, 'submit_path': 'edit_permission', 'invalid': invalid, 'locations': locations, 'managed_user': self.managed_user, 'permission': permission, }
def test_region_present_returns_false_when_a_region_is_not_present(self): form_content = { 'role': 'meo', 'permission_level': 'trust', 'region': None, 'trust': '1' } form = PermissionBuilderForm(form_content) self.assertIsNone(form.region) self.assertIsFalse(form.region_present()) form.region = '' self.assertEqual(form.region, '') self.assertIsFalse(form.region_present()) form.region = 'None' self.assertEqual(form.region, 'None') self.assertIsFalse(form.region_present())
def get(self, request, user_id): status_code = status.HTTP_200_OK context = self.__set_add_permission_context(PermissionBuilderForm(), False) return render(request, self.template, context, status=status_code)