def test_project_get_set_whitelist(self): path = generate_path() env_name = "banana" kernel_name = "conda-kernel-myname" self.notebook.kernel_spec_manager.whitelist = {"tic", "tac"} with mock.patch( "jupyter_project.handlers.ProjectTemplate.get_configuration" ) as mock_configuration: mock_configuration.return_value = {"environment": env_name} with mock.patch( "jupyter_project.handlers.ProjectsHandler.kernel_spec_manager" ) as mocked_specs: mocked_specs.configure_mock( **{ "get_all_specs.return_value": { kernel_name: { "spec": {"metadata": {"conda_env_name": env_name}} } } } ) answer = self.api_tester.get(["projects", path]) assert answer.status_code == 200 mocked_specs.get_all_specs.assert_called_once() assert mocked_specs.whitelist == { kernel_name, } mock_configuration.assert_called_once_with(Path(self.notebook_dir) / path)
def test_project_delete_no_configuration(self): path = generate_path() with mock.patch( "jupyter_project.project.ProjectTemplate.get_configuration" ) as mock_configuration: mock_configuration.side_effect = ValueError with assert_http_error(404): self.api_tester.delete(["projects", path]) mock_configuration.assert_called_once_with(Path(self.notebook_dir) / path)
def test_project_delete(self): path = generate_path() with mock.patch("jupyter_project.project.ProjectTemplate.get_configuration"): with mock.patch("jupyter_project.handlers.rmtree") as mock_rmtree: answer = self.api_tester.delete(["projects", path]) assert answer.status_code == 204 assert answer.text == "" mock_rmtree.assert_called_once_with( Path(self.notebook_dir) / path, ignore_errors=True )
def test_fail_template_rendering(self, renderer, default_name): instance = default_name.return_value name = str(uuid.uuid4()) instance.render.return_value = name renderer.side_effect = jinja2.TemplateError path = generate_path() body = dict(dummy="hello", smart="world") with assert_http_error(500): self.api_tester.post( ["files", quote("template1/file1", safe=""), path], body=body )
def detection(test_dir): resultsDir,logDir = generate_path(test_dir) params = parameters() ex = extractor(params) X,original_dataset=ex.generate_feature(test_dir,'test') classifier = attack_detection(params.model_dir) print('Start evaluation...') classifier.fit_predict(X) classifier.filter_attckSample(original_dataset,resultsDir) classifier.reset() print('evaluation finish...') print('results save in:'+resultsDir) print('logs save in:'+logDir)
def test_project_post_invalid_configuration(self): path = generate_path() body = dict(dummy="hello", smart="world") with mock.patch( "jupyter_project.project.ProjectTemplate.render" ) as mock_render: mock_render.side_effect = jsonschema.ValidationError(message="failure") with assert_http_error(500): self.api_tester.post(["projects", path], body=body) mock_render.assert_called_once_with(body, Path(self.notebook_dir) / path)
def test_project_post_cookiecutter_failure(self): path = generate_path() body = dict(dummy="hello", smart="world") with mock.patch( "jupyter_project.handlers.ProjectTemplate.render" ) as mock_render: mock_render.side_effect = CookiecutterException with assert_http_error(500): self.api_tester.post(["projects", path], body=body) mock_render.assert_called_once_with(body, Path(self.notebook_dir) / path)
def attack_detection(test_dir,option): resultsDir,logDir = generate_path(test_dir) if option == 'prediction': cmd = 'python detection.py ' + test_dir + ' &> ' + logDir code = os.system(cmd) if code != 0: error = 'bad request: ' + request.url + \ '--> Program running error...' return error res = {"results":resultsDir,"logs":logDir} responses = jsonify(res) responses.status_code = 200 return responses
def test_project_delete_invalid_configuration(self): path = generate_path() with mock.patch( "jupyter_project.project.ProjectTemplate.get_configuration" ) as mock_configuration: mock_configuration.side_effect = jsonschema.ValidationError( message="failure" ) with assert_http_error(404): self.api_tester.delete(["projects", path]) mock_configuration.assert_called_once_with(Path(self.notebook_dir) / path)
def test_project_get(self): path = generate_path() configuration = dict(key1=22, key2="hello darling") with mock.patch( "jupyter_project.handlers.ProjectTemplate.get_configuration" ) as mock_configuration: mock_configuration.return_value = configuration answer = self.api_tester.get(["projects", path]) assert answer.status_code == 200 conf = answer.json() assert conf == {"project": configuration} mock_configuration.assert_called_once_with(Path(self.notebook_dir) / path)
def test_project_post(self): path = generate_path() body = dict(dummy="hello", smart="world", name="Project Name") configuration = dict(key1=22, key2="hello darling") with mock.patch( "jupyter_project.handlers.ProjectTemplate.render" ) as mock_render: mock_render.return_value = ("project_name", configuration) answer = self.api_tester.post(["projects", path], body=body) assert answer.status_code == 201 config = answer.json() assert config == {"project": configuration} mock_render.assert_called_once_with(body, Path(self.notebook_dir) / path)
def test_template1_file1(self, renderer, default_name): instance = default_name.return_value name = str(uuid.uuid4()) instance.render.return_value = name renderer.return_value = "dummy content" path = generate_path() body = dict(dummy="hello", smart="world") answer = self.api_tester.post( ["files", quote("template1/file1", safe=""), path], body=body ) assert answer.status_code == 201 instance.render.assert_called_with(**body) renderer.assert_called_with(**body) model = answer.json() assert model["content"] is None assert model["name"] == name + ".py" assert model["path"] == url_path_join(path, name + ".py")
def test_fail_name_rendering(self, renderer, default_name): instance = default_name.return_value instance.render.side_effect = jinja2.TemplateError renderer.return_value = "dummy content" path = generate_path() body = dict(dummy="hello", smart="world") answer = self.api_tester.post( ["files", quote("template1/file1", safe=""), path], body=body ) assert answer.status_code == 201 instance.render.assert_called_with(**body) renderer.assert_called_with(**body) model = answer.json() assert model["content"] is None print(model["name"]) assert re.match(r"untitled\d*\.py", model["name"]) is not None assert re.match(path + r"/untitled\d*\.py", model["path"]) is not None
def test_project_get_no_configuration_environment(self): path = generate_path() self.notebook.kernel_spec_manager.whitelist = {"tic", "tac"} with mock.patch( "jupyter_project.handlers.ProjectTemplate.get_configuration" ) as mock_configuration: mock_configuration.return_value = dict() answer = self.api_tester.get(["projects", path]) assert answer.status_code == 200 conf = answer.json() assert self.notebook.kernel_spec_manager.whitelist == {"tic", "tac"} mock_configuration.assert_called_once_with(Path(self.notebook_dir) / path) answer = self.api_tester.get(["projects",]) assert answer.status_code == 200 conf = answer.json() assert conf == {"project": None} assert self.notebook.kernel_spec_manager.whitelist == set()