def test_views_for_anonymous_users(self): """ Test that anonymous users can view XBlock's 'public_view' but not other views """ # Create an XBlock block_metadata = library_api.create_library_block( self.library.key, "html", "html1") block_usage_key = block_metadata.usage_key library_api.set_library_block_olx(block_usage_key, "<html>Hello world</html>") library_api.publish_changes(self.library.key) anon_client = APIClient() # View the public_view: public_view_result = anon_client.get( URL_BLOCK_RENDER_VIEW.format(block_key=block_usage_key, view_name='public_view'), ) assert public_view_result.status_code == 200 assert 'Hello world' in public_view_result.data['content'] # Try to view the student_view: public_view_result = anon_client.get( URL_BLOCK_RENDER_VIEW.format(block_key=block_usage_key, view_name='student_view'), ) assert public_view_result.status_code == 403
def test_i18n(self): """ Test that a block's rendered content respects the Accept-Language header and returns translated content. """ block_id = library_api.create_library_block(self.library.key, "problem", "i18n_problem").usage_key new_olx = """ <problem display_name="New Multi Choice Question" max_attempts="5"> <multiplechoiceresponse> <p>This is a normal capa problem. It has "maximum attempts" set to **5**.</p> <label>Blockstore is designed to store.</label> <choicegroup type="MultipleChoice"> <choice correct="false">XBlock metadata only</choice> <choice correct="true">XBlock data/metadata and associated static asset files</choice> <choice correct="false">Static asset files for XBlocks and courseware</choice> <choice correct="false">XModule metadata only</choice> </choicegroup> </multiplechoiceresponse> </problem> """.strip() library_api.set_library_block_olx(block_id, new_olx) library_api.publish_changes(self.library.key) # Enable the dummy language in darklang DarkLangConfig(released_languages='eo', changed_by=self.student_a, enabled=True).save() client = APIClient() # View the problem without specifying a language default_public_view = client.get( URL_BLOCK_RENDER_VIEW.format(block_key=block_id, view_name='public_view')) assert 'Submit' in default_public_view.data['content'] assert 'Süßmït' not in default_public_view.data['content'] # View the problem and request the dummy language dummy_public_view = client.get(URL_BLOCK_RENDER_VIEW.format( block_key=block_id, view_name='public_view'), HTTP_ACCEPT_LANGUAGE='eo') assert 'Süßmït' in dummy_public_view.data['content'] assert 'Submit' not in dummy_public_view.data['content']
def test_scores_persisted(self): """ Test that a block's emitted scores are cached in StudentModule In the future if there is a REST API to retrieve individual block's scores, that should be used instead of checking StudentModule directly. """ block_id = library_api.create_library_block(self.library.key, "problem", "scored_problem").usage_key new_olx = """ <problem display_name="New Multi Choice Question" max_attempts="5"> <multiplechoiceresponse> <p>This is a normal capa problem. It has "maximum attempts" set to **5**.</p> <label>Blockstore is designed to store.</label> <choicegroup type="MultipleChoice"> <choice correct="false">XBlock metadata only</choice> <choice correct="true">XBlock data/metadata and associated static asset files</choice> <choice correct="false">Static asset files for XBlocks and courseware</choice> <choice correct="false">XModule metadata only</choice> </choicegroup> </multiplechoiceresponse> </problem> """.strip() library_api.set_library_block_olx(block_id, new_olx) library_api.publish_changes(self.library.key) # Now view the problem as Alice: client = APIClient() client.login(username=self.student_a.username, password='******') student_view_result = client.get(URL_BLOCK_RENDER_VIEW.format(block_key=block_id, view_name='student_view')) problem_key = "input_{}_2_1".format(block_id) assert problem_key in student_view_result.data['content'] # And submit a wrong answer: result = client.get(URL_BLOCK_GET_HANDLER_URL.format(block_key=block_id, handler_name='xmodule_handler')) problem_check_url = result.data["handler_url"] + 'problem_check' submit_result = client.post(problem_check_url, data={problem_key: "choice_3"}) assert submit_result.status_code == 200 submit_data = json.loads(submit_result.content.decode('utf-8')) self.assertDictContainsSubset({ "current_score": 0, "total_possible": 1, "attempts_used": 1, }, submit_data) # Now test that the score is also persisted in StudentModule: # If we add a REST API to get an individual block's score, that should be checked instead of StudentModule. sm = get_score(self.student_a, block_id) assert sm.grade == 0 assert sm.max_grade == 1 # And submit a correct answer: submit_result = client.post(problem_check_url, data={problem_key: "choice_1"}) assert submit_result.status_code == 200 submit_data = json.loads(submit_result.content.decode('utf-8')) self.assertDictContainsSubset({ "current_score": 1, "total_possible": 1, "attempts_used": 2, }, submit_data) # Now test that the score is also updated in StudentModule: # If we add a REST API to get an individual block's score, that should be checked instead of StudentModule. sm = get_score(self.student_a, block_id) assert sm.grade == 1 assert sm.max_grade == 1