1
1
from enum import Enum
2
2
3
3
import pytest
4
+ from bs4 import BeautifulSoup
4
5
from django .contrib .auth .models import Group , User
5
6
6
7
from django_sql_dashboard .models import Dashboard
@@ -314,11 +315,21 @@ def test_user_can_edit(
314
315
slug = "owned_by_other_superuser" , owned_by = other , edit_policy = "superuser"
315
316
)
316
317
dashboard_obj = Dashboard .objects .get (slug = dashboard )
318
+ dashboard_obj .queries .create (sql = "select 1 + 1" )
317
319
assert dashboard_obj .user_can_edit (user ) == expected
318
320
if dashboard != "owned_by_other_staff" :
319
321
# This test doesn't make sense for the 'staff' one, they cannot access admin
320
322
# https://github.com/simonw/django-sql-dashboard/issues/44#issuecomment-835653787
321
- assert can_user_edit_using_admin (client , user , dashboard_obj ) == expected
323
+ can_edit_using_admin = can_user_edit_using_admin (client , user , dashboard_obj )
324
+ assert can_edit_using_admin == expected
325
+ if can_edit_using_admin :
326
+ # Check that they cannot edit the SQL queries, because they do not
327
+ # have the execute_sql permisssion
328
+ assert not user .has_perm ("django_sql_dashboard.execute_sql" )
329
+ html = get_admin_change_form_html (client , user , dashboard_obj )
330
+ soup = BeautifulSoup (html , "html5lib" )
331
+ assert soup .select ("td.field-sql p" )[0 ].text == "select 1 + 1"
332
+
322
333
user .is_staff = True
323
334
user .save ()
324
335
assert dashboard_obj .user_can_edit (user ) == expected_if_staff
@@ -329,15 +340,23 @@ def test_user_can_edit(
329
340
assert can_user_edit_using_admin (client , user , dashboard_obj )
330
341
331
342
332
- def can_user_edit_using_admin (client , user , dashboard ):
343
+ def get_admin_change_form_html (client , user , dashboard ):
333
344
# Only staff can access the admin:
345
+ original_is_staff = user .is_staff
334
346
user .is_staff = True
335
347
user .save ()
336
348
client .force_login (user )
337
349
response = client .get (dashboard .get_edit_url ())
350
+ if not original_is_staff :
351
+ user .is_staff = False
352
+ user .save ()
353
+ return response .content .decode ("utf-8" )
354
+
355
+
356
+ def can_user_edit_using_admin (client , user , dashboard ):
338
357
return (
339
- b '<input type="text" name="title" class="vTextField" maxlength="128" id="id_title">'
340
- in response . content
358
+ '<input type="text" name="title" class="vTextField" maxlength="128" id="id_title">'
359
+ in get_admin_change_form_html ( client , user , dashboard )
341
360
)
342
361
343
362
0 commit comments