From 998084b441ab90845d129f9ad4ad989af890bd3c Mon Sep 17 00:00:00 2001 From: mprahl Date: Tue, 19 Mar 2019 08:31:33 -0400 Subject: [PATCH] Don't default the module name to "unnamed" on a direct modulemd submission The module name should either be set in the modulemd file or provided by the `module_name` parameter. --- module_build_service/utils/submit.py | 7 ++++++- module_build_service/views.py | 4 +--- tests/test_views/test_views.py | 29 ++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/module_build_service/utils/submit.py b/module_build_service/utils/submit.py index b5dedba4..05fcfffd 100644 --- a/module_build_service/utils/submit.py +++ b/module_build_service/utils/submit.py @@ -473,7 +473,12 @@ def submit_module_build_from_yaml(username, handle, params, stream=None, skiptes yaml_file = to_text_type(handle.read()) mmd = load_mmd(yaml_file) dt = datetime.utcfromtimestamp(int(time.time())) - def_name = str(os.path.splitext(os.path.basename(handle.filename))[0]) + if hasattr(handle, 'filename'): + def_name = str(os.path.splitext(os.path.basename(handle.filename))[0]) + elif not mmd.get_name(): + raise ValidationError( + "The module's name was not present in the modulemd file. Please use the " + "\"module_name\" parameter") def_version = int(dt.strftime("%Y%m%d%H%M%S")) mmd.set_name(mmd.get_name() or def_name) mmd.set_stream(stream or mmd.get_stream() or "master") diff --git a/module_build_service/views.py b/module_build_service/views.py index d897fad0..ac82908b 100644 --- a/module_build_service/views.py +++ b/module_build_service/views.py @@ -436,10 +436,8 @@ class YAMLFileHandler(BaseHandler): def post(self): if "modulemd" in self.data: handle = BytesIO(self.data["modulemd"].encode("utf-8")) - if "module_name" in self.data and self.data["module_name"]: + if self.data.get("module_name"): handle.filename = self.data["module_name"] - else: - handle.filename = "unnamed" else: handle = request.files["yaml"] return submit_module_build_from_yaml(self.username, handle, self.data) diff --git a/tests/test_views/test_views.py b/tests/test_views/test_views.py index cc5260ee..7ed653dd 100644 --- a/tests/test_views/test_views.py +++ b/tests/test_views/test_views.py @@ -1843,6 +1843,35 @@ class TestViews: assert module.buildrequires[0].context == '00000000' assert module.buildrequires[0].stream_version == 280000 + @patch('module_build_service.auth.get_user', return_value=user) + @patch('module_build_service.config.Config.modules_allow_scratch', + new_callable=PropertyMock, return_value=True) + @patch('module_build_service.config.Config.yaml_submit_allowed', + new_callable=PropertyMock, return_value=True) + def test_submit_scratch_build_with_mmd_no_module_name( + self, mocked_allow_yaml, mocked_allow_scratch, mocked_get_user): + base_dir = path.abspath(path.dirname(__file__)) + mmd_path = path.join(base_dir, '..', 'staged_data', 'testmodule.yaml') + post_url = '/module-build-service/1/module-builds/' + with open(mmd_path, 'rb') as f: + modulemd = f.read().decode('utf-8') + + post_data = { + 'branch': 'master', + 'scratch': True, + 'modulemd': modulemd, + } + rv = self.client.post(post_url, data=json.dumps(post_data)) + assert rv.status_code == 400 + data = json.loads(rv.data) + expected_error = { + 'error': 'Bad Request', + 'message': ('The module\'s name was not present in the modulemd file. Please use the ' + '"module_name" parameter'), + 'status': 400 + } + assert data == expected_error + @pytest.mark.parametrize('api_version', [1, 2]) @patch('module_build_service.auth.get_user', return_value=user) @patch('module_build_service.config.Config.modules_allow_scratch',