@@ -81,39 +81,121 @@ def test_set_default_dashboard_with_multiple_previous_defaults_should_succeed(
8181 )
8282
8383
84- def test_delete_last_dashboard_should_fail (db , client , admin_account ):
85- """Tests that the last dashboard cannot be deleted"""
86- dashboard = AccountDashboard .objects .get (
87- is_default = True ,
88- account = admin_account ,
89- )
90- url = reverse ("delete-dashboard" , args = (dashboard .pk ,))
91- response = client .post (url , follow = True )
84+ class TestDeleteDashboardView :
85+ """Tests for the delete_dashboard view which allows deleting dashboards"""
9286
93- assert response .status_code == 400
94- assert "Cannot delete last dashboard" in smart_str (response .content )
95- assert AccountDashboard .objects .filter (id = dashboard .id ).exists ()
87+ def test_given_dashboard_when_delete_unconfirmed_then_do_not_delete_dashboard (
88+ self , db , client , admin_account
89+ ):
90+ """
91+ Tests that the dashboard is not deleted when deletion is not yet confirmed
92+ """
93+ dashboard = self ._create_dashboard (
94+ admin_account ,
95+ name = "to_be_deleted" ,
96+ is_default = False ,
97+ )
98+ url = reverse ("delete-dashboard" , args = (dashboard .pk ,))
99+ response = client .post (url )
96100
101+ assert response .status_code == 200
102+ assert AccountDashboard .objects .filter (id = dashboard .id ).exists ()
97103
98- def test_delete_default_dashboard_should_fail (db , client , admin_account ):
99- """Tests that the default dashboard cannot be deleted"""
100- # Creating another dashboard, so that default is not the last one
101- AccountDashboard .objects .create (
102- name = "non_default" ,
103- is_default = False ,
104- account = admin_account ,
105- )
104+ def test_given_dashboard_id_when_delete_unconfirmed_then_render_confirmation (
105+ self , db , client , admin_account
106+ ):
107+ """
108+ Tests that the deletion confirmation is rendered when deletion is unconfirmed
109+ """
110+ dashboard = self ._create_dashboard (
111+ admin_account ,
112+ name = "to_be_deleted" ,
113+ is_default = False ,
114+ )
115+ url = reverse ("delete-dashboard" , args = (dashboard .pk ,))
116+ response = client .post (url )
106117
107- default_dashboard = AccountDashboard .objects .get (
108- is_default = True ,
109- account = admin_account ,
110- )
111- url = reverse ("delete-dashboard" , args = (default_dashboard .pk ,))
112- response = client .post (url , follow = True )
118+ assert response .status_code == 200
119+ assert 'id="delete-dashboard-confirmation"' in smart_str (response .content )
120+
121+ def test_given_dashboard_with_subscribers_then_render_subscriber_warning (
122+ self , db , client , admin_account , non_admin_account
123+ ):
124+ """Tests that the deletion confirmation shows subscriber info when applicable"""
125+ dashboard = self ._create_dashboard (
126+ admin_account ,
127+ is_shared = True ,
128+ )
129+ AccountDashboardSubscription .objects .create (
130+ account = non_admin_account ,
131+ dashboard = dashboard ,
132+ )
133+ url = reverse ("delete-dashboard" , args = (dashboard .pk ,))
134+ response = client .post (url )
135+ response_content = smart_str (response .content )
136+
137+ assert response .status_code == 200
138+ assert 'data-test-id="subscriber-warning"' in response_content
139+
140+ def test_given_dashboard_when_delete_confirmed_then_delete_dashboard (
141+ self , db , client , admin_account
142+ ):
143+ """Tests that an existing dashboard can be deleted"""
144+ dashboard = self ._create_dashboard (admin_account )
145+ url = reverse ("delete-dashboard" , args = (dashboard .pk ,))
146+ response = client .post (
147+ url ,
148+ data = {"confirm_delete" : "true" },
149+ follow = True ,
150+ )
151+
152+ assert response .status_code == 200
153+ assert not AccountDashboard .objects .filter (id = dashboard .id ).exists ()
154+
155+ def test_given_last_dashboard_of_account_then_render_error_message (
156+ self , db , client , admin_account
157+ ):
158+ """Tests that the last dashboard cannot be deleted"""
113159
114- assert response .status_code == 400
115- assert "Cannot delete default dashboard" in smart_str (response .content )
116- assert AccountDashboard .objects .filter (id = default_dashboard .id ).exists ()
160+ # Ensure only one dashboard exists for the account
161+ AccountDashboard .objects .filter (account = admin_account ).delete ()
162+ dashboard = self ._create_dashboard (admin_account , name = "last_dashboard" )
163+
164+ url = reverse ("delete-dashboard" , args = (dashboard .pk ,))
165+ response = client .post (
166+ url ,
167+ )
168+
169+ assert response .status_code == 200
170+ assert "Cannot delete last dashboard" in smart_str (response .content )
171+ assert AccountDashboard .objects .filter (id = dashboard .id ).exists ()
172+
173+ def test_given_default_dashboard_of_account_then_render_error_message (
174+ self , db , client , admin_account
175+ ):
176+ """Tests that the default dashboard cannot be deleted"""
177+ # Ensure at least one non-default dashboard exists
178+ self ._create_dashboard (admin_account , name = "non_default_dashboard" )
179+ default_dashboard = AccountDashboard .objects .get (
180+ is_default = True , account = admin_account
181+ )
182+ url = reverse ("delete-dashboard" , args = (default_dashboard .pk ,))
183+ response = client .post (url )
184+
185+ assert response .status_code == 200
186+ assert "Cannot delete default dashboard" in smart_str (response .content )
187+ assert AccountDashboard .objects .filter (id = default_dashboard .id ).exists ()
188+
189+ @staticmethod
190+ def _create_dashboard (
191+ account , name = "to_be_deleted" , is_default = False , is_shared = False
192+ ):
193+ return AccountDashboard .objects .create (
194+ name = name ,
195+ is_default = is_default ,
196+ account = account ,
197+ is_shared = is_shared ,
198+ )
117199
118200
119201def test_when_logging_in_it_should_change_the_session_id (
0 commit comments