@@ -225,6 +225,220 @@ def test_save_slice(self):
225225 db .session .delete (slc )
226226 db .session .commit ()
227227
228+ @pytest .mark .usefixtures ("load_energy_table_with_slice" )
229+ def test_save_slice_to_forbidden_dashboard_does_not_persist (self ):
230+ """Saving a chart to a dashboard the user cannot edit must fail
231+ with 403 and persist nothing (fix for #46324).
232+
233+ The legacy explore save committed the chart before the dashboard
234+ permission check, so the 403 response still left the chart saved.
235+ The refusal message is asserted (not just the status) so a 403
236+ from an unrelated check cannot satisfy the pin.
237+ """
238+ admin = self .get_user (ADMIN_USERNAME )
239+ dashboard = self .insert_dashboard (
240+ "Forbidden Dashboard" ,
241+ "forbidden-dashboard-46324" ,
242+ [admin .id ],
243+ created_by = admin ,
244+ )
245+ try :
246+ tbl_id = self .table_ids .get ("energy_usage" )
247+ slice_name = f"Unsaved Slice { random .random ()} " # noqa: S311
248+ form_data = {
249+ "adhoc_filters" : [],
250+ "viz_type" : "sankey" ,
251+ "groupby" : ["target" ],
252+ "metric" : "sum__value" ,
253+ "row_limit" : 5000 ,
254+ }
255+ table = self .get_table ("energy_usage" )
256+ gamma = self .get_user (GAMMA_USERNAME )
257+ # Drop the admin session before impersonating gamma: logging
258+ # in as the temporary user does not replace an already
259+ # authenticated session, so the admin would otherwise leak
260+ # into these checks.
261+ self .logout ()
262+ with self .temporary_user (
263+ gamma ,
264+ extra_pvms = [("datasource_access" , table .perm )],
265+ login = True ,
266+ ):
267+ resp = self .client .post (
268+ "/explore/table/{}/?slice_name={}&action=saveas"
269+ "&save_to_dashboard_id={}" .format (tbl_id , slice_name , dashboard .id ),
270+ data = {"form_data" : json .dumps (form_data )},
271+ )
272+ assert resp .status_code == 403
273+ assert (
274+ "You don't have the rights to alter this dashboard"
275+ in resp .json ["error" ]
276+ )
277+ leaked = (
278+ db .session .query (Slice )
279+ .filter_by (slice_name = slice_name )
280+ .one_or_none ()
281+ )
282+ # Clean up the leaked chart (the bug under test) so the
283+ # temporary user's teardown can still delete the user:
284+ # the chart references the user, and leaving it behind
285+ # breaks teardown with a foreign-key error.
286+ if leaked is not None :
287+ db .session .delete (leaked )
288+ db .session .commit ()
289+ assert leaked is None
290+ finally :
291+ db .session .delete (dashboard )
292+ db .session .commit ()
293+
294+ @pytest .mark .usefixtures ("load_energy_table_with_slice" )
295+ def test_save_slice_invalid_dashboard_id_returns_400 (self ):
296+ """A non-numeric save_to_dashboard_id must fail with 400 and
297+ persist nothing (fix for #46324).
298+
299+ The id was parsed with an unguarded int() after the chart had
300+ already been committed, so the request 500ed and still saved.
301+ """
302+ self .login (ADMIN_USERNAME )
303+ tbl_id = self .table_ids .get ("energy_usage" )
304+ slice_name = f"Unsaved Slice { random .random ()} " # noqa: S311
305+ form_data = {
306+ "adhoc_filters" : [],
307+ "viz_type" : "sankey" ,
308+ "groupby" : ["target" ],
309+ "metric" : "sum__value" ,
310+ "row_limit" : 5000 ,
311+ }
312+ resp = self .client .post (
313+ "/explore/table/{}/?slice_name={}&action=saveas"
314+ "&save_to_dashboard_id=abc" .format (tbl_id , slice_name ),
315+ data = {"form_data" : json .dumps (form_data )},
316+ )
317+ assert resp .status_code == 400
318+ assert (
319+ db .session .query (Slice ).filter_by (slice_name = slice_name ).one_or_none ()
320+ is None
321+ )
322+
323+ @pytest .mark .usefixtures ("load_energy_table_with_slice" )
324+ def test_save_slice_missing_dashboard_id_returns_404 (self ):
325+ """A nonexistent save_to_dashboard_id must fail with 404 and
326+ persist nothing (fix for #46324).
327+
328+ The dashboard was loaded with an unguarded .one() after the
329+ chart had already been committed, so the request 500ed and
330+ still saved.
331+ """
332+ self .login (ADMIN_USERNAME )
333+ tbl_id = self .table_ids .get ("energy_usage" )
334+ slice_name = f"Unsaved Slice { random .random ()} " # noqa: S311
335+ form_data = {
336+ "adhoc_filters" : [],
337+ "viz_type" : "sankey" ,
338+ "groupby" : ["target" ],
339+ "metric" : "sum__value" ,
340+ "row_limit" : 5000 ,
341+ }
342+ missing_id = self .get_nonexistent_numeric_id (Dashboard )
343+ resp = self .client .post (
344+ "/explore/table/{}/?slice_name={}&action=saveas"
345+ "&save_to_dashboard_id={}" .format (tbl_id , slice_name , missing_id ),
346+ data = {"form_data" : json .dumps (form_data )},
347+ )
348+ assert resp .status_code == 404
349+ assert (
350+ db .session .query (Slice ).filter_by (slice_name = slice_name ).one_or_none ()
351+ is None
352+ )
353+
354+ @pytest .mark .usefixtures ("load_energy_table_with_slice" )
355+ def test_save_slice_to_accessible_dashboard_persists (self ):
356+ """The happy path is unchanged: saving to a dashboard the user
357+ can edit still creates the chart and attaches it (fix for
358+ #46324 must not break the success path).
359+ """
360+ admin = self .get_user (ADMIN_USERNAME )
361+ dashboard = self .insert_dashboard (
362+ "Accessible Dashboard" ,
363+ "accessible-dashboard-46324" ,
364+ [admin .id ],
365+ created_by = admin ,
366+ )
367+ try :
368+ self .login (ADMIN_USERNAME )
369+ tbl_id = self .table_ids .get ("energy_usage" )
370+ slice_name = f"Saved Slice { random .random ()} " # noqa: S311
371+ form_data = {
372+ "adhoc_filters" : [],
373+ "viz_type" : "sankey" ,
374+ "groupby" : ["target" ],
375+ "metric" : "sum__value" ,
376+ "row_limit" : 5000 ,
377+ }
378+ resp = self .client .post (
379+ "/explore/table/{}/?slice_name={}&action=saveas"
380+ "&save_to_dashboard_id={}" .format (tbl_id , slice_name , dashboard .id ),
381+ data = {"form_data" : json .dumps (form_data )},
382+ )
383+ assert resp .status_code == 200
384+ slc = db .session .query (Slice ).filter_by (slice_name = slice_name ).one_or_none ()
385+ assert slc is not None
386+ assert slc in dashboard .slices
387+ assert resp .json ["dashboard_id" ] == dashboard .id
388+ finally :
389+ slc = db .session .query (Slice ).filter_by (slice_name = slice_name ).one_or_none ()
390+ if slc is not None :
391+ db .session .delete (slc )
392+ db .session .delete (dashboard )
393+ db .session .commit ()
394+
395+ @pytest .mark .usefixtures ("load_energy_table_with_slice" )
396+ def test_save_slice_to_new_dashboard_persists (self ):
397+ """Saving to a brand new dashboard still creates the dashboard
398+ and attaches the chart (fix for #46324 moved the dashboard
399+ creation after the chart save; this pins the success path).
400+ """
401+ self .login (ADMIN_USERNAME )
402+ tbl_id = self .table_ids .get ("energy_usage" )
403+ slice_name = f"Saved Slice { random .random ()} " # noqa: S311
404+ dashboard_title = f"New Dashboard { random .random ()} " # noqa: S311
405+ form_data = {
406+ "adhoc_filters" : [],
407+ "viz_type" : "sankey" ,
408+ "groupby" : ["target" ],
409+ "metric" : "sum__value" ,
410+ "row_limit" : 5000 ,
411+ }
412+ try :
413+ resp = self .client .post (
414+ "/explore/table/{}/?slice_name={}&action=saveas"
415+ "&new_dashboard_name={}" .format (tbl_id , slice_name , dashboard_title ),
416+ data = {"form_data" : json .dumps (form_data )},
417+ )
418+ assert resp .status_code == 200
419+ slc = db .session .query (Slice ).filter_by (slice_name = slice_name ).one_or_none ()
420+ assert slc is not None
421+ dash = (
422+ db .session .query (Dashboard )
423+ .filter_by (dashboard_title = dashboard_title )
424+ .one_or_none ()
425+ )
426+ assert dash is not None
427+ assert slc in dash .slices
428+ assert resp .json ["dashboard_id" ] == dash .id
429+ finally :
430+ slc = db .session .query (Slice ).filter_by (slice_name = slice_name ).one_or_none ()
431+ if slc is not None :
432+ db .session .delete (slc )
433+ dash = (
434+ db .session .query (Dashboard )
435+ .filter_by (dashboard_title = dashboard_title )
436+ .one_or_none ()
437+ )
438+ if dash is not None :
439+ db .session .delete (dash )
440+ db .session .commit ()
441+
228442 @pytest .mark .usefixtures ("load_energy_table_with_slice" )
229443 def test_overwrite_refuses_externally_managed_slice (self ):
230444 """sc-120011: the legacy Explore overwrite is gated server-side.
0 commit comments