def test_is_not_valid_location(self): schema = load_schema_from_params('test', 'is_skipping_to_end') current_location = Location('not-in-path', 0, 'not-in-path') routing_path = [ Location('test-skipping-group', 0, 'test-skipping-forced'), Location('test-skipping-section-summary-group', 0, 'test-skipping-section-summary'), Location('summary-group', 0, 'summary') ] completed_blocks = [ Location('test-skipping-group', 0, 'test-skipping-forced') ] completeness = Completeness(schema, answer_store=MagicMock(), completed_blocks=completed_blocks, routing_path=routing_path, metadata={}) router = Router(schema, routing_path, completeness, current_location) self.assertFalse(router.can_access_location()) router = Router(schema, routing_path, completeness) self.assertFalse(router.can_access_location()) # Currently, section summary is not added to completed_blocks without POST, ie when using nav bar. self.assertEqual(routing_path[2], router.get_next_location())
def get_block( routing_path, schema, metadata, answer_store, eq_id, form_type, collection_id, group_id, # pylint: disable=too-many-locals group_instance, block_id): current_location = Location(group_id, group_instance, block_id) completeness = get_completeness(current_user) router = Router(schema, routing_path, completeness, current_location) if not router.can_access_location(): next_location = router.get_next_location() return _redirect_to_location(collection_id, eq_id, form_type, next_location) block = _get_block_json(current_location, schema, answer_store, metadata) context = _get_context(routing_path, block, current_location, schema) return _render_page(block['type'], context, current_location, schema, answer_store, metadata, routing_path)
def test_cant_access_location_not_on_allowable_path(self): schema = load_schema_from_name("test_unit_patterns") router = Router( schema, self.answer_store, self.list_store, self.progress_store, self.metadata, ) current_location = Location(section_id="default-section", block_id="set-duration-units-block") routing_path = RoutingPath( [ "set-length-units-block", "set-duration-units-block", "set-area-units-block", "set-volume-units-block", "summary", ], section_id="default-section", ) can_access_location = router.can_access_location( current_location, routing_path) self.assertFalse(can_access_location)
def test_section_summary_accessible_when_section_complete(self): schema = load_schema_from_params('test', 'is_skipping_to_end') current_location = Location('test-skipping-section-summary-group-2', 0, 'test-skipping-section-summary-2') routing_path = [ Location('test-skipping-group', 0, 'test-skipping-forced'), Location('test-skipping-group', 0, 'test-skipping-optional'), Location('test-skipping-section-summary-group', 0, 'test-skipping-section-summary'), Location('test-skipping-group-2', 0, 'test-skipping-forced-2'), Location('test-skipping-group-2', 0, 'test-skipping-optional-2'), Location('test-skipping-section-summary-group-2', 0, 'test-skipping-section-summary-2'), Location('summary-group', 0, 'summary') ] completed_blocks = [ Location('test-skipping-group', 0, 'test-skipping-forced'), Location('test-skipping-group-2', 0, 'test-skipping-forced-2'), Location('test-skipping-group-2', 0, 'test-skipping-optional-2'), Location('test-skipping-section-summary-group-2', 0, 'test-skipping-section-summary-2') ] completeness = Completeness(schema, answer_store=MagicMock(), completed_blocks=completed_blocks, routing_path=routing_path, metadata={}) router = Router(schema, routing_path, completeness, current_location) self.assertTrue(router.can_access_location()) self.assertEqual(routing_path[1], router.get_next_location())
def post_block( routing_path, schema, metadata, collection_metadata, answer_store, eq_id, form_type, collection_id, group_id, # pylint: disable=too-many-locals group_instance, block_id): current_location = Location(group_id, group_instance, block_id) completeness = get_completeness(current_user) router = Router(schema, routing_path, completeness, current_location) if not router.can_access_location(): next_location = router.get_next_location() return _redirect_to_location(collection_id, eq_id, form_type, next_location) block = _get_block_json(current_location, schema, answer_store, metadata) schema_context = _get_schema_context(routing_path, current_location, metadata, collection_metadata, answer_store, schema) rendered_block = renderer.render(block, **schema_context) form = _generate_wtf_form(request.form, rendered_block, current_location, schema) if 'action[save_sign_out]' in request.form: return _save_sign_out(routing_path, current_location, form, schema, answer_store, metadata) if form.validate(): _set_started_at_metadata_if_required(form, collection_metadata) _update_questionnaire_store(current_location, form, schema) next_location = path_finder.get_next_location( current_location=current_location) if _is_end_of_questionnaire(block, next_location): return submit_answers(routing_path, eq_id, form_type, schema) return redirect(_next_location_url(next_location)) context = build_view_context(block['type'], metadata, schema, answer_store, schema_context, rendered_block, current_location, form) return _render_page(block['type'], context, current_location, schema, answer_store, metadata, routing_path)
def test_cant_access_location_invalid_list_item_id(self): schema = load_schema_from_name("test_textfield") router = Router( schema, self.answer_store, self.list_store, self.progress_store, self.metadata, ) current_location = Location(section_id="default-section", block_id="name-block") routing_path = [] can_access_location = router.can_access_location( current_location, routing_path) self.assertFalse(can_access_location)
def test_cant_access_location_section_disabled(self): schema = load_schema_from_name("test_section_enabled_checkbox") router = Router( schema, self.answer_store, self.list_store, self.progress_store, self.metadata, ) current_location = Location(section_id="section-2", block_id="section-2-block", list_item_id=None) can_access_location = router.can_access_location(current_location, routing_path=[]) self.assertFalse(can_access_location)
def test_can_access_location(self): schema = load_schema_from_name("test_textfield") router = Router( schema, self.answer_store, self.list_store, self.progress_store, self.metadata, ) current_location = Location(section_id="default-section", block_id="name-block") routing_path = RoutingPath(["name-block", "summary"], section_id="default-section") can_access_location = router.can_access_location( current_location, routing_path) self.assertTrue(can_access_location)
def test_skipping_to_end_of_survey_no_completed_blocks(self): schema = load_schema_from_params('test', 'is_skipping_to_end') current_location = Location('summary-group', 0, 'summary') routing_path = [ Location('test-skipping-group', 0, 'test-skipping-forced'), Location('test-skipping-section-summary-group', 0, 'test-skipping-section-summary'), Location('summary-group', 0, 'summary') ] completeness = Completeness(schema, answer_store=MagicMock(), completed_blocks=[], routing_path=routing_path, metadata={}) router = Router(schema, routing_path, completeness, current_location) self.assertFalse(router.can_access_location())
def test_cant_access_location(self): schema = load_schema_from_name( "test_repeating_sections_with_hub_and_spoke") list_store = ListStore([{ "items": ["abc123", "123abc"], "name": "people", "primary_person": "abc123", }]) router = Router(schema, self.answer_store, list_store, self.progress_store, self.metadata) current_location = Location( section_id="personal-details-section", block_id="proxy", list_item_id="invalid-list-item-id", ) routing_path = [] can_access_location = router.can_access_location( current_location, routing_path) self.assertFalse(can_access_location)