11from enum import Enum
22
33import pytest
4+ from bs4 import BeautifulSoup
45from django .contrib .auth .models import Group , User
56
67from django_sql_dashboard .models import Dashboard
@@ -314,11 +315,21 @@ def test_user_can_edit(
314315 slug = "owned_by_other_superuser" , owned_by = other , edit_policy = "superuser"
315316 )
316317 dashboard_obj = Dashboard .objects .get (slug = dashboard )
318+ dashboard_obj .queries .create (sql = "select 1 + 1" )
317319 assert dashboard_obj .user_can_edit (user ) == expected
318320 if dashboard != "owned_by_other_staff" :
319321 # This test doesn't make sense for the 'staff' one, they cannot access admin
320322 # 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+
322333 user .is_staff = True
323334 user .save ()
324335 assert dashboard_obj .user_can_edit (user ) == expected_if_staff
@@ -329,15 +340,23 @@ def test_user_can_edit(
329340 assert can_user_edit_using_admin (client , user , dashboard_obj )
330341
331342
332- def can_user_edit_using_admin (client , user , dashboard ):
343+ def get_admin_change_form_html (client , user , dashboard ):
333344 # Only staff can access the admin:
345+ original_is_staff = user .is_staff
334346 user .is_staff = True
335347 user .save ()
336348 client .force_login (user )
337349 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 ):
338357 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 )
341360 )
342361
343362
0 commit comments