|
6 | 6 | import re
|
7 | 7 |
|
8 | 8 | import ddt
|
| 9 | +from django.core.exceptions import ValidationError |
9 | 10 | from django.test.utils import override_settings
|
10 | 11 | from openedx_events.tests.utils import OpenEdxEventsTestMixin
|
| 12 | +import pytest |
11 | 13 | from xblock.core import XBlock
|
12 | 14 |
|
13 | 15 | from openedx.core.djangoapps.content_libraries.tests.base import (
|
@@ -115,8 +117,62 @@ def check_fields(display_name, setting_value, content_value):
|
115 | 117 | html = self._embed_block(block_id, version=2)
|
116 | 118 | check_fields('Field Test Block (Old, v2)', 'Old setting value 2.', 'Old content value 2.')
|
117 | 119 |
|
118 |
| - # TODO: if we requested any version other than "draft", the handlers should not allow _writing_ to authored field |
119 |
| - # data. Writing to student state is OK. |
| 120 | + @XBlock.register_temp_plugin(FieldsTestBlock, FieldsTestBlock.BLOCK_TYPE) |
| 121 | + def test_embed_vew_versions(self): |
| 122 | + """ |
| 123 | + Test that if we requested any version other than "draft", the handlers should not allow _writing_ to authored |
| 124 | + field data (because you'd be overwriting the latest draft version with changes based on an old version). |
| 125 | +
|
| 126 | + We may decide to relax this restriction in the future. Not sure how important it is. |
| 127 | +
|
| 128 | + Writing to student state is OK. |
| 129 | + """ |
| 130 | + # Create a library: |
| 131 | + lib = self._create_library(slug="test-eb-2", title="Test Library", description="") |
| 132 | + lib_id = lib["id"] |
| 133 | + # Create an XBlock. This will be the empty version 1: |
| 134 | + create_response = self._add_block_to_library(lib_id, FieldsTestBlock.BLOCK_TYPE, "block1") |
| 135 | + block_id = create_response["id"] |
| 136 | + |
| 137 | + # Now render the "embed block" view. This test only runs in CMS so it should default to the draft: |
| 138 | + html = self._embed_block(block_id) |
| 139 | + |
| 140 | + def call_update_handler(**kwargs): |
| 141 | + handler_url = re.search(r'<p>handler URL: ([^<]+)</p>', html).group(1) |
| 142 | + assert handler_url.startswith('http') |
| 143 | + handler_url = handler_url.replace('get_fields', 'update_fields') |
| 144 | + response = self.client.post(handler_url, kwargs, format='json') |
| 145 | + assert response.status_code == 200 |
| 146 | + |
| 147 | + def check_fields(display_name, setting_field, content_field): |
| 148 | + assert f'<h1>{display_name}</h1>' in html |
| 149 | + assert f'<p>SF: {setting_field}</p>' in html |
| 150 | + assert f'<p>CF: {content_field}</p>' in html |
| 151 | + |
| 152 | + # Call the update handler to change the fields on the draft: |
| 153 | + call_update_handler(display_name="DN-01", setting_field="SV-01", content_field="CV-01") |
| 154 | + |
| 155 | + # Render the block again and check that the handler was able to update the fields: |
| 156 | + html = self._embed_block(block_id) |
| 157 | + check_fields(display_name="DN-01", setting_field="SV-01", content_field="CV-01") |
| 158 | + |
| 159 | + # Publish the library: |
| 160 | + self._commit_library_changes(lib_id) |
| 161 | + |
| 162 | + # Now try changing the authored fields of the published version using a handler: |
| 163 | + html = self._embed_block(block_id, version="published") |
| 164 | + expected_msg = "Do not make changes to a component starting from the published or past versions." |
| 165 | + with pytest.raises(ValidationError, match=expected_msg) as err: |
| 166 | + call_update_handler(display_name="DN-X", setting_field="SV-X", content_field="CV-X") |
| 167 | + |
| 168 | + # Now try changing the authored fields of a specific past version using a handler: |
| 169 | + html = self._embed_block(block_id, version=2) |
| 170 | + with pytest.raises(ValidationError, match=expected_msg) as err: |
| 171 | + call_update_handler(display_name="DN-X", setting_field="SV-X", content_field="CV-X") |
| 172 | + |
| 173 | + # Make sure the fields were not updated: |
| 174 | + html = self._embed_block(block_id) |
| 175 | + check_fields(display_name="DN-01", setting_field="SV-01", content_field="CV-01") |
120 | 176 |
|
121 | 177 | # TODO: test that any static assets referenced in the student_view html are loaded as the correct version, and not
|
122 | 178 | # always loaded as "latest draft".
|
|
0 commit comments