Skip to content

Commit bcf2fce

Browse files
authored
Merge pull request #672 from jbernal0019/master
Remove compute_auth_token descriptor from a submitted Compute Resource form in the Admin site if value is a blank string
2 parents d64d80e + d3e3ec4 commit bcf2fce

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

chris_backend/plugins/admin.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,20 @@
2828
fld.name != 'compute_resources']
2929

3030

31+
class ComputeResourceAdminForm(forms.ModelForm):
32+
33+
def clean(self):
34+
"""
35+
Overriden to remove the compute_auth_token field if blank.
36+
"""
37+
if 'compute_auth_token' in self.cleaned_data:
38+
if self.cleaned_data['compute_auth_token'].strip() == '':
39+
del self.cleaned_data['compute_auth_token']
40+
return self.cleaned_data
41+
42+
3143
class ComputeResourceAdmin(admin.ModelAdmin):
44+
form = ComputeResourceAdminForm
3245
readonly_fields = ['creation_date', 'modification_date']
3346
list_display = ('name', 'compute_url', 'compute_innetwork', 'description', 'id')
3447
list_filter = ['name', 'creation_date', 'modification_date']

chris_backend/plugins/tests/test_admin.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,44 @@
2323
COMPUTE_RESOURCE_URL = settings.COMPUTE_RESOURCE_URL
2424

2525

26+
class ComputeResourceFormTests(TestCase):
27+
"""
28+
Test ComputeResourceAdminForm.
29+
"""
30+
31+
@classmethod
32+
def setUpClass(cls):
33+
super().setUpClass()
34+
# avoid cluttered console output (for instance logging all the http requests)
35+
logging.disable(logging.WARNING)
36+
37+
@classmethod
38+
def tearDownClass(cls):
39+
super().tearDownClass()
40+
# re-enable logging
41+
logging.disable(logging.NOTSET)
42+
43+
def setUp(self):
44+
# avoid cluttered console output (for instance logging all the http requests)
45+
logging.disable(logging.WARNING)
46+
47+
(self.compute_resource, tf) = pl_admin.ComputeResource.objects.get_or_create(
48+
name="host", compute_url=COMPUTE_RESOURCE_URL)
49+
50+
def test_clean_removes_compute_auth_token_if_blank(self):
51+
"""
52+
Test whether overriden clean method removes compute_auth_token descriptor from
53+
the form if it is a blank string.
54+
"""
55+
compute_resource_admin = pl_admin.ComputeResourceAdmin(pl_admin.ComputeResource,
56+
pl_admin.admin.site)
57+
form = compute_resource_admin.form
58+
form.instance = self.compute_resource
59+
form.cleaned_data = {'compute_auth_token': ''}
60+
cleaned_data = form.clean(form)
61+
self.assertNotIn('compute_auth_token', cleaned_data)
62+
63+
2664
class ComputeResourceAdminTests(TestCase):
2765
"""
2866
Test ComputeResourceAdmin.

0 commit comments

Comments
 (0)