def test_custom_field_in_form_for_matching_managing_group(self): group = GroupFactory() self.user.groups.add(group) self.custom_field_str.managing_group = group self.user.save() self.custom_field_str.save() response = self.client.get(self.sm1.get_absolute_url()) self.assertEqual(1, len(response.context_data['custom_fields_all'])) self.assertEqual( 'sample_value', response.context_data['custom_fields_all'][0]['value']) filled_in_custom_field_forms = [ form for form in response.context_data['inline_admin_formsets'] [0].formset.forms # noqa: E501 if form.fields['id'].initial is not None ] self.assertEqual(1, len(filled_in_custom_field_forms)) form = filled_in_custom_field_forms[0] self.assertEqual(self.cfv1.id, form.fields['id'].initial)
def add_user_and_group(username, password, group_name, permissions): group = GroupFactory(name=group_name) for codename in permissions: group.permissions.add( Permission.objects.get(codename=codename)) user = UserFactory(username=username, is_staff=True) user.regions.add(RegionFactory()) user.set_password(password) user.groups.add(group) user.save()
def add_user_and_group(username, password, group_name, permission_models): group = GroupFactory(name=group_name) for permision_model in permission_models: for perm in Permission.objects.filter( content_type=ContentType.objects.get_by_natural_key( *permision_model.split('.'))): group.permissions.add(perm) user = UserFactory(username=username, is_staff=True) user.regions.add(RegionFactory()) user.set_password(password) user.groups.add(group) user.save()
def test_delete_custom_field_value_with_unmatching_managing_group_should_fail( self): # noqa: E501 self.custom_field_str.managing_group = GroupFactory() self.custom_field_str.save() url = reverse(self.detail_view_name, kwargs={ 'pk': self.cfv1.pk, 'object_pk': self.cfv1.object_id }) response = self.client.delete(url, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertEqual( CustomFieldValue.objects.filter(pk=self.cfv1.pk).count(), 1)
def test_update_customfield_value_with_unmatching_managing_group_should_fail( self): # noqa: E501 self.custom_field_str.managing_group = GroupFactory() self.custom_field_str.save() url = reverse(self.detail_view_name, kwargs={ 'pk': self.cfv1.pk, 'object_pk': self.cfv1.object_id }) data = { 'value': 'NEW-VALUE', 'custom_field': self.custom_field_str.id, } response = self.client.put(url, data=data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_add_new_customfield_value_with_unmatching_managing_group_should_fail( self): # noqa" E501 self.custom_field_str.managing_group = GroupFactory() self.custom_field_str.save() some_object = SomeModel.objects.create(name='DEADBEEF') url = reverse(self.list_view_name, args=(some_object.id, )) data = { 'value': 'qwerty', 'custom_field': self.custom_field_str.id, } response = self.client.post(url, data=data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_delete_custom_field_value_with_matching_managing_group_should_pass( self): # noqa: E501 group = GroupFactory() self.user.groups.add(group) self.custom_field_str.managing_group = group self.custom_field_str.save() self.user.save() url = reverse(self.detail_view_name, kwargs={ 'pk': self.cfv1.pk, 'object_pk': self.cfv1.object_id }) response = self.client.delete(url, format='json') self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual( CustomFieldValue.objects.filter(pk=self.cfv1.pk).count(), 0)
def test_add_new_custom_field_value_fails_with_nonmatching_managing_group( self): # noqa: E501 group = GroupFactory() self.custom_field_choices.managing_group = group self.custom_field_choices.save() data = { 'id': self.sm1.id, 'name': self.sm1.name, } data_custom_fields = { 'TOTAL_FORMS': 3, 'INITIAL_FORMS': 1, '0-id': self.cfv1.id, '0-custom_field': self.custom_field_str.id, '0-value': 'sample_value', '1-id': '', '1-custom_field': self.custom_field_choices.id, '1-value': 'qwerty', } data.update(self._prepare_inline_data(data_custom_fields)) response = self.client.post(self.sm1.get_absolute_url(), data, follow=True) self.assertEqual(response.status_code, HTTP_200_OK) expected_error_message = ( '<ul class="errorlist"><li>' 'Only users from {} group can set this custom field.</li></ul>' ).format(group.name) self.assertIn('errors', response.context_data) self.assertIn(expected_error_message, response.context_data['errors']) custom_field_qs = CustomFieldValue.objects.filter( object_id=self.sm1.id, content_type=ContentType.objects.get_for_model(SomeModel), custom_field=self.custom_field_choices, value='qwerty') self.assertFalse(custom_field_qs.exists())
def test_add_new_custom_field_value_succeeds_with_matching_managing_group( self): # noqa: E501 group = GroupFactory() self.user.groups.add(group) self.custom_field_choices.managing_group = group self.user.save() self.custom_field_choices.save() data = { 'id': self.sm1.id, 'name': self.sm1.name, } data_custom_fields = { 'TOTAL_FORMS': 3, 'INITIAL_FORMS': 1, '0-id': self.cfv1.id, '0-custom_field': self.custom_field_str.id, '0-value': 'sample_value', '1-id': '', '1-custom_field': self.custom_field_choices.id, '1-value': 'qwerty', } data.update(self._prepare_inline_data(data_custom_fields)) response = self.client.post(self.sm1.get_absolute_url(), data, follow=True) self.assertEqual(response.status_code, HTTP_200_OK) custom_field_qs = CustomFieldValue.objects.filter( object_id=self.sm1.id, content_type=ContentType.objects.get_for_model(SomeModel), custom_field=self.custom_field_choices, value='qwerty') self.assertTrue(custom_field_qs.exists())
def test_update_customfield_value_with_matching_managing_group_should_pass( self): # noqa: E501 group = GroupFactory() self.user.groups.add(group) self.custom_field_str.managing_group = group self.custom_field_str.save() self.user.save() url = reverse(self.detail_view_name, kwargs={ 'pk': self.cfv1.pk, 'object_pk': self.cfv1.object_id }) data = { 'value': 'NEW-VALUE', 'custom_field': self.custom_field_str.id, } response = self.client.put(url, data=data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.cfv1.refresh_from_db() self.assertEqual(self.cfv1.object, self.sm1) self.assertEqual(self.cfv1.custom_field, self.custom_field_str) self.assertEqual(self.cfv1.value, 'NEW-VALUE')