@@ -23,16 +23,67 @@ def test_admin_mixin_importable(self):
2323 assert ChangeFormActionsMixin is not None
2424
2525 def test_changeform_has_actions_dropdown (self ):
26- # Create superuser and log in
26+ # Setup
2727 User .objects .create_superuser ("admin" , "admin@example.com" , "password" )
2828 client = Client ()
2929 client .login (username = "admin" , password = "password" )
3030
31- # Create a model instance
3231 instance = MyModel .objects .create (name = "Test" )
3332
34- # Get changeform page and check that the dropdown is present
35- changeform_url = reverse ("admin:tests_mymodel_change" , args = [instance .pk ])
33+ # Test: Get changeform page and check that the dropdown is present
34+ changeform_url : str = reverse ("admin:tests_mymodel_change" , args = [instance .pk ])
3635 response = client .get (changeform_url )
3736 assert response .status_code == 200
3837 assert b'<select name="action"' in response .content
38+
39+
40+ @pytest .mark .django_db
41+ class TestRunAction :
42+ def test_run_action_needs_staff_user_login (self ):
43+ # Setup
44+ User .objects .create_user ("user" , "user@example.com" , "password" )
45+ client = Client ()
46+ client .login (username = "user" , password = "password" )
47+
48+ instance = MyModel .objects .create (name = "Test" )
49+
50+ # Test: Redirected to admin login instead
51+ run_action_url : str = reverse ("changeform_actions:run_admin_changeform_action" )
52+ response = client .post (
53+ run_action_url ,
54+ data = {
55+ "app_label" : "tests" ,
56+ "model_name" : "mymodel" ,
57+ "pk" : str (instance .pk ),
58+ "action" : "add_copy_suffix" ,
59+ },
60+ HTTP_REFERER = reverse ("admin:tests_mymodel_change" , args = [instance .pk ]),
61+ )
62+ assert response .status_code == 302
63+ assert response .url .startswith (reverse ("admin:login" ))
64+
65+ def test_run_action (self ):
66+ # Setup
67+ User .objects .create_superuser ("admin" , "admin@example.com" , "password" )
68+ client = Client ()
69+ client .login (username = "admin" , password = "password" )
70+
71+ instance = MyModel .objects .create (name = "Test" )
72+
73+ # Test: Run action renames the model instance
74+ run_action_url : str = reverse ("changeform_actions:run_admin_changeform_action" )
75+ response = client .post (
76+ run_action_url ,
77+ data = {
78+ "app_label" : "tests" ,
79+ "model_name" : "mymodel" ,
80+ "pk" : str (instance .pk ),
81+ "action" : "add_copy_suffix" ,
82+ },
83+ HTTP_REFERER = reverse ("admin:tests_mymodel_change" , args = [instance .pk ]),
84+ follow = True ,
85+ )
86+ assert response .status_code == 200
87+
88+ instance .refresh_from_db ()
89+ assert instance .name == "Test - copy"
0 commit comments