def test_get_editor_interaction_mode_participation_not_none(self): page_context = mock.MagicMock() page_context.flow_session = mock.MagicMock() page_context.flow_session.participation = mock.MagicMock() page_context.flow_session.participation.user = mock.MagicMock() page_context.flow_session.participation.user.editor_mode = "some_mode" self.assertEqual(get_editor_interaction_mode(page_context), "some_mode")
def test_get_data_bad_download_on_unsupported_dataset_address_scheme_error( self): """If downloader doesn't return the downloaded file path, get_data must return an empty array""" # noqa product = EOProduct(self.provider, self.eoproduct_props, productType=self.product_type) product.driver = mock.MagicMock(spec_set=NoDriver()) product.driver.get_data_address.side_effect = UnsupportedDatasetAddressScheme mock_downloader = mock.MagicMock(spec_set=Download( provider=self.provider, config=config.PluginConfig.from_mapping({"extract": False}), )) mock_downloader.download.return_value = None mock_authenticator = mock.MagicMock(spec_set=Authentication( provider=self.provider, config=config.PluginConfig.from_mapping({}))) product.register_downloader(mock_downloader, mock_authenticator) self.assertRaises(DownloadError, product.download) data, band = self.execute_get_data(product, give_back=("band", )) self.assertEqual(product.driver.get_data_address.call_count, 1) product.driver.get_data_address.assert_called_with(product, band) self.assertIsInstance(data, xr.DataArray) self.assertEqual(data.values.size, 0)
def setUp(self): super(RunCourseUpdateCommandTest, self).setUp() self.course = factories.CourseFactory( active_git_commit_sha=self.default_old_sha) user = factories.UserFactory() instructor_role = factories.ParticipationRoleFactory( course=self.course, identifier="instructor" ) self.participation = factories.ParticipationFactory( course=self.course, preview_git_commit_sha=None, user=user) self.participation.roles.set([instructor_role]) self.request = mock.MagicMock() self.request.user = user self.pctx = mock.MagicMock() self.pctx.course = self.course self.pctx.participation = self.participation self.repo = mock.MagicMock() self.content_repo = self.repo fake_get_dulwich_client_and_remote_path_from_course = mock.patch( "course.versioning.get_dulwich_client_and_remote_path_from_course") self.mock_get_dulwich_client_and_remote_path_from_course = ( fake_get_dulwich_client_and_remote_path_from_course.start() ) self.mock_client = mock.MagicMock() remote_path = "/remote/path" self.mock_get_dulwich_client_and_remote_path_from_course.return_value = ( self.mock_client, remote_path ) self.mock_client.fetch.return_value = { b"HEAD": self.default_switch_to_sha.encode()} self.addCleanup(fake_get_dulwich_client_and_remote_path_from_course.stop) fake_transfer_remote_refs = mock.patch( "course.versioning.transfer_remote_refs") self.mock_transfer_remote_refs = fake_transfer_remote_refs.start() self.addCleanup(fake_transfer_remote_refs.stop) fake_is_parent_commit = mock.patch("course.versioning.is_parent_commit") self.mock_is_parent_commit = fake_is_parent_commit.start() self.mock_is_parent_commit.return_value = False self.addCleanup(fake_is_parent_commit.stop) fake_validate_course_content = mock.patch( "course.validation.validate_course_content") self.mock_validate_course_content = fake_validate_course_content.start() self.mock_validate_course_content.return_value = [] self.addCleanup(fake_validate_course_content.stop)
def test_escape_html(self): commit_sha_1 = b"a_commit" commit_sha_2 = b"another_commit" commit_sha_3 = b"yet_another_commit" repo_dict = { commit_sha_1: FakeCommit("a_commit", message=b"test a > b "), commit_sha_2: FakeCommit("another_commit", message=b" <p>test</p>"), commit_sha_3: FakeCommit("another_commit", message=b"abc\uDC80"), } repo = mock.MagicMock() repo.__getitem__.side_effect = repo_dict.__getitem__ repo.__setitem__.side_effect = repo_dict.__setitem__ expected_msg = "test a > b" self.assertEqual( versioning._get_commit_message_as_html(repo, commit_sha_1), expected_msg) expected_msg = "<p>test</p>" self.assertEqual( versioning._get_commit_message_as_html(repo, commit_sha_2), expected_msg) expected_msg = "abc\\uDC80" self.assertEqual( versioning._get_commit_message_as_html(repo, commit_sha_3), expected_msg)
def setUp(self): self.requset = mock.MagicMock() self.requset.relate_facilities = ["fa1", "fa2"] fake_get_facilities_config = mock.patch( "course.utils.get_facilities_config") self.mock_get_facilities_config = fake_get_facilities_config.start() self.addCleanup(fake_get_facilities_config.stop)
def setUp(self): super(GetLoginExamTicketTest, self).setUp() self.ticket = factories.ExamTicketFactory( exam=self.exam, participation=self.student_participation, state=constants.exam_ticket_states.valid) self.request = mock.MagicMock()
def setUp(self): super(FlowRuleExceptionTest, self).setUp() user = factories.UserFactory() self.participation = factories.ParticipationFactory(course=self.course, user=user) fake_get_course_repo = mock.patch("course.content.get_course_repo") self.mock_get_course_repo = fake_get_course_repo.start() self.mock_get_course_repo.return_value = mock.MagicMock() self.addCleanup(fake_get_course_repo.stop) fake_get_flow_desc = mock.patch("course.content.get_flow_desc") self.mock_get_flow_desc = fake_get_flow_desc.start() self.addCleanup(fake_get_flow_desc.stop) fake_validate_session_start_rule = mock.patch( "course.validation.validate_session_start_rule") self.mock_validate_session_start_rule = ( fake_validate_session_start_rule.start()) self.addCleanup(fake_validate_session_start_rule.stop) fake_validate_session_access_rule = mock.patch( "course.validation.validate_session_access_rule") self.mock_validate_session_access_rule = ( fake_validate_session_access_rule.start()) self.addCleanup(fake_validate_session_access_rule.stop) fake_validate_session_grading_rule = mock.patch( "course.validation.validate_session_grading_rule") self.mock_validate_session_grading_rule = ( fake_validate_session_grading_rule.start()) self.addCleanup(fake_validate_session_grading_rule.stop)
def test_eoproduct_get_quicklook_ok_existing(self): """EOProduct.get_quicklook must return the path to an already downloaded quicklook""" # noqa quicklook_dir = os.path.join(tempfile.gettempdir(), "quicklooks") quicklook_basename = "the_quicklook.png" existing_quicklook_file_path = os.path.join(quicklook_dir, quicklook_basename) if not os.path.exists(quicklook_dir): os.mkdir(quicklook_dir) with open(existing_quicklook_file_path, "wb") as fh: fh.write(b"content") product = EOProduct(self.provider, self.eoproduct_props, productType=self.product_type) product.properties["quicklook"] = "https://fake.url.to/quicklook" mock_downloader = mock.MagicMock( spec_set=Download(provider=self.provider, config=None)) mock_downloader.config = config.PluginConfig.from_mapping( {"outputs_prefix": tempfile.gettempdir()}) product.register_downloader(mock_downloader, None) quicklook_file_path = product.get_quicklook( filename=quicklook_basename) self.assertEqual(self.requests_http_get.call_count, 0) self.assertEqual(quicklook_file_path, existing_quicklook_file_path) os.remove(existing_quicklook_file_path) os.rmdir(quicklook_dir)
def test_post_form_not_valid(self): with mock.patch( "course.sandbox.SandboxForm.is_valid") as mock_form_valid: mock_form_valid.return_value = False resp = self.post_markup_sandbox_view( markup_content=mock.MagicMock()) self.assertEqual(resp.status_code, 200) self.assertResponseContextEqual(resp, "preview_text", "")
def test_float_matcher_neither_atol_nor_rtol_present_warning(self): mock_vctx = mock.MagicMock() expected_warning = ("Float match should have either rtol or atol--" "otherwise it will match any number") FloatMatcher(mock_vctx, "some where", dict_to_struct( {"type": "float", "value": "1"})) self.assertIn(expected_warning, mock_vctx.add_warning.call_args[0])
def test_float_matcher_value_zero_atol_not_present_warning(self): mock_vctx = mock.MagicMock() expected_warning = ("Float match for 'value' zero should have " "atol--otherwise it will match any number") FloatMatcher(mock_vctx, "some where", dict_to_struct( {"type": "float", "value": "0"})) self.assertIn(expected_warning, mock_vctx.add_warning.call_args[0])
def test_remote_ref_none(self): repo_dict = {} repo_dict[b"refs/remotes/origin/1"] = b"some_bytes" repo_dict[b'HEAD'] = b"some_head" repo = mock.MagicMock() repo.__getitem__.side_effect = repo_dict.__getitem__ repo.get_refs.return_value = { b"refs/remotes/origin/1": "some_text1", b"refs/remotes/other/1": "some_text2"} versioning.transfer_remote_refs(repo, None)
def test_symbolic_expression_matcher_pymbolic_import_error(self): with mock.patch.dict(sys.modules, {'pymbolic': None}): expected_warning = ("some_where: unable to check symbolic " "expression") mock_vctx = mock.MagicMock() SymbolicExpressionMatcher(mock_vctx, "some_where", "abcd") self.assertEqual(mock_vctx.add_warning.call_count, 1) self.assertIn(expected_warning, mock_vctx.add_warning.call_args[0][1]) # no validation context SymbolicExpressionMatcher(None, "", "abcd")
def test_set_up_ensure_get_transport_called(self): with mock.patch( "course.versioning.paramiko.SSHClient.connect" ) as mock_connect, mock.patch( "course.versioning.paramiko.SSHClient.get_transport" ) as mock_get_transport: mock_channel = mock.MagicMock() mock_get_transport.return_value.open_session.return_value = mock_channel mock_channel.exec_command = mock.MagicMock() mock_channel.recv.side_effect = [b"10"] mock_channel.recv_stderr.side_effect = [b"my custom error", "", ""] try: self.ssh_vendor.run_command( host="github.com", command="git-upload-pack '/bar/baz'") except StopIteration: pass self.assertEqual(mock_connect.call_count, 1) from paramiko.rsakey import RSAKey used_rsa_key = None # make sure rsa_key is used when connect for v in mock_connect.call_args[1].values(): if isinstance(v, RSAKey): used_rsa_key = v self.assertIsNotNone(used_rsa_key) # make sure get_transport is called self.assertEqual(mock_get_transport.call_count, 1) # make sure exec_command is called self.assertEqual(mock_channel.exec_command.call_count, 1) self.assertIn( "git-upload-pack '/bar/baz'", mock_channel.exec_command.call_args[0])
def test_get_data_download_on_unsupported_dataset_address_scheme_error( self): """If a product is not on the local filesystem, it must download itself before returning the data""" # noqa product = EOProduct(self.provider, self.eoproduct_props, productType=self.product_type) def get_data_address(*args, **kwargs): eo_product = args[0] if eo_product.location.startswith("https"): raise UnsupportedDatasetAddressScheme return self.local_band_file product.driver = mock.MagicMock(spec_set=NoDriver()) product.driver.get_data_address.side_effect = get_data_address mock_downloader = mock.MagicMock(spec_set=Download( provider=self.provider, config=config.PluginConfig.from_mapping({ "extract": False, "archive_depth": 1 }), )) mock_downloader.download.return_value = self.local_product_as_archive_path # mock_downloader.config = {'extract': False, 'archive_depth': 1} mock_authenticator = mock.MagicMock(spec_set=Authentication( provider=self.provider, config=config.PluginConfig.from_mapping({}))) product.register_downloader(mock_downloader, mock_authenticator.authenticate()) data, band = self.execute_get_data(product, give_back=("band", )) self.assertEqual(product.driver.get_data_address.call_count, 2) product.driver.get_data_address.assert_called_with(product, band) self.assertIsInstance(data, xr.DataArray) self.assertNotEqual(data.values.size, 0)
def test_get_data_local_product_ok(self): """A call to get_data on a product present in the local filesystem must succeed""" # noqa self.eoproduct_props.update( {"downloadLink": "file://{}".format(self.local_product_abspath)}) product = EOProduct(self.provider, self.eoproduct_props, productType=self.product_type) product.driver = mock.MagicMock(spec_set=NoDriver()) product.driver.get_data_address.return_value = self.local_band_file data, band = self.execute_get_data(product, give_back=("band", )) self.assertEqual(product.driver.get_data_address.call_count, 1) product.driver.get_data_address.assert_called_with(product, band) self.assertIsInstance(data, xr.DataArray) self.assertNotEqual(data.values.size, 0)
def test_get_data_dl_on_unsupported_ds_address_scheme_error_wo_downloader( self): """If a product is not on filesystem and a downloader isn't registered, get_data must return an empty array""" # noqa product = EOProduct(self.provider, self.eoproduct_props, productType=self.product_type) product.driver = mock.MagicMock(spec_set=NoDriver()) product.driver.get_data_address.side_effect = UnsupportedDatasetAddressScheme self.assertRaises(RuntimeError, product.download) data = self.execute_get_data(product) self.assertEqual(product.driver.get_data_address.call_count, 1) self.assertIsInstance(data, xr.DataArray) self.assertEqual(data.values.size, 0)
def test_parse_matcher_string_deprecated(self): s = "plain:half" with mock.patch("course.validation.ValidationContext.add_warning", autospec=True) as mock_vctx_add_warning: mock_vctx_add_warning.return_value = None result = parse_matcher_string(None, "", s) self.assertEqual(mock_vctx_add_warning.call_count, 0) self.assertTrue(isinstance(result, PlainMatcher)) self.assertEqual(result.correct_answer_text(), "half") mock_vctx = mock.MagicMock() expected_warning = "uses deprecated 'matcher:answer' style" result = parse_matcher_string(mock_vctx, "some_where", s) self.assertIn(expected_warning, mock_vctx.add_warning.call_args[0]) self.assertTrue(isinstance(result, PlainMatcher)) self.assertEqual(result.correct_answer_text(), "half")
def test_eoproduct_get_quicklook_http_error(self): """EOProduct.get_quicklook must return an empty string if there was an error during retrieval""" # noqa product = EOProduct(self.provider, self.eoproduct_props, productType=self.product_type) product.properties["quicklook"] = "https://fake.url.to/quicklook" self.requests_http_get.return_value.__enter__.return_value.raise_for_status.side_effect = ( # noqa requests.HTTPError) mock_downloader = mock.MagicMock( spec_set=Download(provider=self.provider, config=None)) mock_downloader.config = config.PluginConfig.from_mapping( {"outputs_prefix": tempfile.gettempdir()}) product.register_downloader(mock_downloader, None) quicklook_file_path = product.get_quicklook() self.requests_http_get.assert_called_with( "https://fake.url.to/quicklook", stream=True, auth=None) self.assertEqual(quicklook_file_path, "")
def test_eodag_search_storage_arg(self, dag): """Calling eodag search with specified result filename without .geojson extension""" # noqa with self.user_conf() as conf_file: api_obj = dag.return_value api_obj.search.return_value = (mock.MagicMock(), ) * 2 self.runner.invoke( eodag, [ "search", "--conf", conf_file, "-p", "whatever", "--storage", "results", ], ) api_obj.serialize.assert_called_with( api_obj.search.return_value[0], filename="results.geojson")
def test_eoproduct_get_quicklook_ok(self): """EOProduct.get_quicklook must return the path to the successfully downloaded quicklook""" # noqa product = EOProduct(self.provider, self.eoproduct_props, productType=self.product_type) product.properties["quicklook"] = "https://fake.url.to/quicklook" self.requests_http_get.return_value = self._quicklook_response() mock_downloader = mock.MagicMock( spec_set=Download(provider=self.provider, config=None)) mock_downloader.config = config.PluginConfig.from_mapping( {"outputs_prefix": tempfile.gettempdir()}) product.register_downloader(mock_downloader, None) quicklook_file_path = product.get_quicklook() self.requests_http_get.assert_called_with( "https://fake.url.to/quicklook", stream=True, auth=None) self.assertEqual(os.path.basename(quicklook_file_path), product.properties["id"]) self.assertEqual( os.path.dirname(quicklook_file_path), os.path.join(tempfile.gettempdir(), "quicklooks"), ) os.remove(quicklook_file_path) # Test the same thing as above but with an explicit name given to the downloaded File quicklook_file_path = product.get_quicklook( filename="the_quicklook.png") self.requests_http_get.assert_called_with( "https://fake.url.to/quicklook", stream=True, auth=None) self.assertEqual(self.requests_http_get.call_count, 2) self.assertEqual(os.path.basename(quicklook_file_path), "the_quicklook.png") self.assertEqual( os.path.dirname(quicklook_file_path), os.path.join(tempfile.gettempdir(), "quicklooks"), ) os.remove(quicklook_file_path) # Overall teardown os.rmdir(os.path.dirname(quicklook_file_path))
def test_eodag_search_with_cruncher(self, dag): """Calling eodag search with --cruncher arg should call crunch method of search result""" # noqa with self.user_conf() as conf_file: api_obj = dag.return_value api_obj.search.return_value = (mock.MagicMock(), ) * 2 product_type = "whatever" cruncher = "FilterLatestIntersect" criteria = dict( startTimeFromAscendingNode=None, completionTimeFromAscendingNode=None, geometry=None, cloudCover=None, instrument=None, platform=None, platformSerialIdentifier=None, processingLevel=None, sensorType=None, productType=product_type, id=None, locations=None, ) self.runner.invoke( eodag, [ "search", "-f", conf_file, "-p", product_type, "--cruncher", cruncher ], ) search_results = api_obj.search.return_value[0] crunch_results = api_obj.crunch.return_value # Assertions dag.assert_called_once_with(user_conf_file_path=conf_file, locations_conf_path=None) api_obj.search.assert_called_once_with(items_per_page=20, page=1, **criteria) api_obj.crunch.assert_called_once_with(search_results, search_criteria=criteria, **{cruncher: {}}) api_obj.serialize.assert_called_with( crunch_results, filename="search_results.geojson") # Call with a cruncher taking arguments cruncher = "FilterOverlap" self.runner.invoke( eodag, [ "search", "-f", conf_file, "-p", product_type, "--cruncher", cruncher, "--cruncher-args", cruncher, "minimum_overlap", 10, ], ) api_obj.crunch.assert_called_with( search_results, search_criteria=criteria, **{cruncher: { "minimum_overlap": 10 }})
What's a half? answers: - type: float value: 0.5 rtol: 1e-4 - <plain>half - <plain>a half """ events_file = "events.yml" my_events_file = "my_events_file.yml" course_file = "test_course_file" course_desc = mock.MagicMock() validate_sha = "test_validate_sha" staticpage1_path = "staticpages/spage1.yml" staticpage1_location = "spage1.yml" staticpage1_id = "spage1" staticpage1_desc = mock.MagicMock() staticpage2_path = "staticpages/spage2.yml" staticpage2_location = "spage2.yml" staticpage2_id = "spage2" staticpage2_desc = mock.MagicMock() flow1_path = "flows/flow1.yml" flow1_location = "flow1.yml" flow1_id = "flow1"
def setUp(self): self.repo = mock.MagicMock() self.course = factories.CourseFactory() fake_get_yaml_from_repo_safely = mock.patch( "course.validation.get_yaml_from_repo_safely") self.mock_get_yaml_from_repo_safely = fake_get_yaml_from_repo_safely.start( ) self.mock_get_yaml_from_repo_safely.side_effect = ( get_yaml_from_repo_safely_side_effect) self.addCleanup(fake_get_yaml_from_repo_safely.stop) fake_validate_staticpage_desc = mock.patch( "course.validation.validate_staticpage_desc") self.mock_validate_staticpage_desc = fake_validate_staticpage_desc.start( ) self.addCleanup(fake_validate_staticpage_desc.stop) fake_get_yaml_from_repo = mock.patch( "course.content.get_yaml_from_repo") self.mock_get_yaml_from_repo = fake_get_yaml_from_repo.start() self.mock_get_yaml_from_repo.side_effect = get_yaml_from_repo_side_effect self.addCleanup(fake_get_yaml_from_repo.stop) fake_validate_calendar_desc_struct = mock.patch( "course.validation.validate_calendar_desc_struct") self.mock_validate_calendar_desc_struct = ( fake_validate_calendar_desc_struct.start()) self.addCleanup(fake_validate_calendar_desc_struct.stop) fake_check_attributes_yml = ( mock.patch("course.validation.check_attributes_yml")) self.mock_check_attributes_yml = fake_check_attributes_yml.start() self.addCleanup(fake_check_attributes_yml.stop) fake_validate_flow_id = ( mock.patch("course.validation.validate_flow_id")) self.mock_validate_flow_id = fake_validate_flow_id.start() self.addCleanup(fake_validate_flow_id.stop) fake_validate_flow_desc = ( mock.patch("course.validation.validate_flow_desc")) self.mock_validate_flow_desc = fake_validate_flow_desc.start() self.addCleanup(fake_validate_flow_desc.stop) fake_check_for_page_type_changes = ( mock.patch("course.validation.check_for_page_type_changes")) self.mock_check_for_page_type_changes = ( fake_check_for_page_type_changes.start()) self.addCleanup(fake_check_for_page_type_changes.stop) fake_check_grade_identifier_link = ( mock.patch("course.validation.check_grade_identifier_link")) self.mock_check_grade_identifier_link = ( fake_check_grade_identifier_link.start()) self.addCleanup(fake_check_grade_identifier_link.stop) fake_get_repo_blob = (mock.patch("course.validation.get_repo_blob")) self.mock_get_repo_blob = fake_get_repo_blob.start() self.mock_get_repo_blob.side_effect = get_repo_blob_side_effect self.addCleanup(fake_get_repo_blob.stop) fake_validate_static_page_name = ( mock.patch("course.validation.validate_static_page_name")) self.mock_validate_static_page_name = fake_validate_static_page_name.start( ) self.addCleanup(fake_validate_static_page_name.stop) fake_vctx_add_warning = ( mock.patch("course.validation.ValidationContext.add_warning")) self.mock_vctx_add_warning = fake_vctx_add_warning.start() self.addCleanup(fake_vctx_add_warning.stop)
def test_get_editor_interaction_mode_participation_none(self): page_context = mock.MagicMock() page_context.flow_session = mock.MagicMock() page_context.flow_session.participation = None self.assertEqual(get_editor_interaction_mode(page_context), "default")
def setUp(self): repo_dict = {} repo = mock.MagicMock() repo.__getitem__.side_effect = repo_dict.__getitem__ repo.__setitem__.side_effect = repo_dict.__setitem__ self.repo = repo