|
3 | 3 | from cms.models import PlaceholderRelationField |
4 | 4 | from cms.test_utils.testcases import CMSTestCase |
5 | 5 | from cms.toolbar.items import TemplateItem |
6 | | -from cms.toolbar.utils import get_object_preview_url |
| 6 | +from cms.toolbar.utils import get_object_edit_url, get_object_preview_url |
7 | 7 | from cms.utils import get_current_site |
8 | 8 | from django.contrib import admin |
9 | 9 | from django.contrib.auth.models import Permission |
@@ -130,6 +130,131 @@ def test_user_does_not_have_change_permission(self): |
130 | 130 | self.assertIsNotNone(version.locked_by) # Was locked |
131 | 131 | self.assertEqual(response.status_code, 403) |
132 | 132 |
|
| 133 | + def test_editor_without_delete_versionlock_permission_can_edit_unlocked_content(self): |
| 134 | + """ |
| 135 | + Non-superuser editors with delete_versionlock permission can access edit mode |
| 136 | + of unlocked content. This test verifies the fix for the issue where editors |
| 137 | + without delete_versionlock permission were unable to access edit mode even |
| 138 | + with appropriate change permissions. |
| 139 | + """ |
| 140 | + # Create an editor user with change permissions and no delete_versionlock permission |
| 141 | + editor = self._create_user( |
| 142 | + "editor_without_unlock", |
| 143 | + is_staff=True, |
| 144 | + is_superuser=False, |
| 145 | + permissions=["change_page"], |
| 146 | + ) |
| 147 | + |
| 148 | + # Create a version without a lock (unlocked) |
| 149 | + version = factories.PageVersionFactory(state=DRAFT, locked_by=None) |
| 150 | + |
| 151 | + # Editor should be able to access the edit view |
| 152 | + url = get_object_edit_url(version.content) |
| 153 | + |
| 154 | + with self.login_user_context(editor): |
| 155 | + response = self.client.get(url) |
| 156 | + |
| 157 | + # Should succeed with 200 status |
| 158 | + self.assertEqual(response.status_code, 200) |
| 159 | + |
| 160 | + def test_editor_without_delete_versionlock_permission_can_edit_their_locked_content(self): |
| 161 | + """ |
| 162 | + Non-superuser editors with delete_versionlock permission can access edit mode |
| 163 | + of unlocked content. This test verifies the fix for the issue where editors |
| 164 | + without delete_versionlock permission were unable to access edit mode even |
| 165 | + with appropriate change permissions. |
| 166 | + """ |
| 167 | + # Create an editor user with change permissions and no delete_versionlock permission |
| 168 | + editor = self._create_user( |
| 169 | + "editor_without_unlock", |
| 170 | + is_staff=True, |
| 171 | + is_superuser=False, |
| 172 | + permissions=["change_page"], |
| 173 | + ) |
| 174 | + |
| 175 | + # Create a version without a lock (unlocked) |
| 176 | + version = factories.PageVersionFactory(state=DRAFT, locked_by=editor) |
| 177 | + |
| 178 | + # Editor should be able to access the edit view |
| 179 | + url = get_object_edit_url(version.content) |
| 180 | + |
| 181 | + with self.login_user_context(editor): |
| 182 | + response = self.client.get(url) |
| 183 | + |
| 184 | + # Should succeed with 200 status - this is the key test |
| 185 | + # Without delete_versionlock permission, this would return 403 |
| 186 | + self.assertEqual(response.status_code, 200) |
| 187 | + |
| 188 | + def test_editor_without_delete_versionlock_permission_cannot_edit_others_content(self): |
| 189 | + """ |
| 190 | + Non-superuser editors without delete_versionlock permission cannot access |
| 191 | + edit mode of content locked by another user. This is the expected behavior |
| 192 | + that was causing issues when users didn't have the delete_versionlock permission. |
| 193 | + """ |
| 194 | + # Create an editor user without delete_versionlock permission |
| 195 | + editor_without_unlock = self._create_user( |
| 196 | + "editor_without_unlock", |
| 197 | + is_staff=True, |
| 198 | + is_superuser=False, |
| 199 | + permissions=["change_page"], |
| 200 | + ) |
| 201 | + |
| 202 | + # Create a version locked by another user |
| 203 | + author = factories.UserFactory(is_staff=True) |
| 204 | + version = factories.PageVersionFactory( |
| 205 | + state=DRAFT, |
| 206 | + created_by=author, |
| 207 | + locked_by=author |
| 208 | + ) |
| 209 | + |
| 210 | + # Editor without unlock permission should not be able to access edit view |
| 211 | + url = get_object_edit_url(version.content) |
| 212 | + |
| 213 | + with self.login_user_context(editor_without_unlock): |
| 214 | + response = self.client.get(url) |
| 215 | + |
| 216 | + # Should be denied with 302 status -> redirect to preview |
| 217 | + self.assertEqual(response.status_code, 302) |
| 218 | + |
| 219 | + # Version should still be locked by the original author |
| 220 | + updated_version = Version.objects.get(pk=version.pk) |
| 221 | + self.assertEqual(updated_version.locked_by, author) |
| 222 | + |
| 223 | + def test_editor_with_delete_versionlock_permission_can_edit_others_content(self): |
| 224 | + """ |
| 225 | + Non-superuser editors without delete_versionlock permission cannot access |
| 226 | + edit mode of content locked by another user. This is the expected behavior |
| 227 | + that was causing issues when users didn't have the delete_versionlock permission. |
| 228 | + """ |
| 229 | + # Create an editor user without delete_versionlock permission |
| 230 | + editor_with_unlock = self._create_user( |
| 231 | + "editor_with_unlock", |
| 232 | + is_staff=True, |
| 233 | + is_superuser=False, |
| 234 | + permissions=["delete_versionlock", "change_page"], |
| 235 | + ) |
| 236 | + |
| 237 | + # Create a version locked by another user |
| 238 | + author = factories.UserFactory(is_staff=True) |
| 239 | + version = factories.PageVersionFactory( |
| 240 | + state=DRAFT, |
| 241 | + created_by=author, |
| 242 | + locked_by=author |
| 243 | + ) |
| 244 | + |
| 245 | + # Editor without unlock permission should not be able to access edit view |
| 246 | + url = get_object_edit_url(version.content) |
| 247 | + |
| 248 | + with self.login_user_context(editor_with_unlock): |
| 249 | + response = self.client.get(url) |
| 250 | + |
| 251 | + # Should be redirected with 302 status since user first must unlock |
| 252 | + self.assertEqual(response.status_code, 302) |
| 253 | + |
| 254 | + # Version should still be locked by the original author |
| 255 | + updated_version = Version.objects.get(pk=version.pk) |
| 256 | + self.assertEqual(updated_version.locked_by, author) |
| 257 | + |
133 | 258 |
|
134 | 259 | @override_settings(DJANGOCMS_VERSIONING_LOCK_VERSIONS=True) |
135 | 260 | class VersionLockUnlockTestCase(CMSTestCase): |
|
0 commit comments