def login_as_user(self, user=None, password='******', *args, **kwargs): if not user: user = UserFactory(*args, **kwargs) user.is_superuser = True user.is_staff = True user.save() self.user = user return self.client.login(username=user.username, password=password)
class BackOfficeAssetFormTest(TransitionTestCase, ClientMixin): def setUp(self): super().setUp() self.asset = BackOfficeAssetFactory() self.user = UserFactory() self.user.is_superuser = False self.user.is_staff = True self.passwd = 'ralph' self.user.set_password(self.passwd) # Grant all permissions to the user permissions = Permission.objects.exclude(codename__in=[ 'view_backofficeasset_hostname_field', 'view_backofficeasset_service_env_field', 'change_backofficeasset_hostname_field', 'change_backofficeasset_service_env_field', ]).all() self.user.user_permissions.add(*permissions) self.user.regions.add(self.asset.region) self.user.save() self.login_as_user(user=self.user, password=self.passwd) self.data = { 'hostname': self.asset.hostname, 'model': self.asset.model.pk, 'status': self.asset.status, 'warehouse': self.asset.warehouse.pk, 'region': self.asset.region.pk, 'barcode': self.asset.barcode, 'depreciation_rate': 0, 'custom_fields-customfieldvalue-content_type-object_id-INITIAL_FORMS': '0', # noqa: E501 'custom_fields-customfieldvalue-content_type-object_id-MAX_NUM_FORMS': '1000', # noqa: E501 'custom_fields-customfieldvalue-content_type-object_id-MIN_NUM_FORMS': '0', # noqa: E501 'custom_fields-customfieldvalue-content_type-object_id-TOTAL_FORMS': '3', # noqa: E501fhtml } def test_bo_admin_form_wo_access_to_service_env_and_hostname(self): url = reverse('admin:back_office_backofficeasset_change', args=(self.asset.pk, )) resp = self.client.get(url, follow=True) self.assertEqual(resp.status_code, 200) self.assertIn('model', resp.context['adminform'].form.fields) self.assertNotIn('hostname', resp.context['adminform'].form.fields) self.assertNotIn('service_env', resp.context['adminform'].form.fields) @override_settings(BACKOFFICE_HOSTNAME_FIELD_READONLY=1) def test_bo_admin_form_with_readonly_hostname(self): self.assertTrue(self.login_as_user()) asset = BackOfficeAssetFactory() url = reverse('admin:back_office_backofficeasset_change', args=(asset.pk, )) resp = self.client.get(url, follow=True) self.assertEqual(resp.status_code, 200) self.assertIn('hostname', resp.context['adminform'].form.fields) self.assertTrue( resp.context['adminform'].form.fields['hostname'].widget.attrs.get( 'readonly')) def test_model_asset_type_back_office_shall_pass(self): back_office_model = DataCenterAssetModelFactory( type=ObjectModelType.from_name('back_office')) self.data.update({'model': back_office_model.pk}) response = self.client.post(self.asset.get_absolute_url(), self.data) self.asset.refresh_from_db() self.assertEqual(response.status_code, 302) self.assertEqual(self.asset.model, back_office_model) def test_model_asset_type_data_center_asset_shall_not_pass(self): back_office_model = DataCenterAssetModelFactory( type=ObjectModelType.from_name('data_center')) self.data.update({'model': back_office_model.pk}) response = self.client.post(self.asset.get_absolute_url(), self.data) self.asset.refresh_from_db() self.assertIn('Model must be of', response.content.decode('utf-8')) self.assertNotEqual(self.asset.model, back_office_model) self.assertEqual(response.status_code, 200)