From 701b1ed5f30fef18cd1b8046efd6338ed594d799 Mon Sep 17 00:00:00 2001 From: Paul Van Eck Date: Tue, 18 Oct 2016 12:26:07 -0700 Subject: [PATCH] Disallow deleting the null version of a product The null version is implicitly created with the product and should only be deleted if the product is deleted. This version is used when a user doesn't want to associate a test to an explicit version but just the product itself. This patch restricts users from explicitly deleting this null version. Change-Id: Ibb29940b5702f73cd50ed2a29e5a23a4d87ed887 --- refstack/api/controllers/products.py | 9 +++++++++ refstack/tests/api/test_products.py | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/refstack/api/controllers/products.py b/refstack/api/controllers/products.py index 08194733..9d054862 100644 --- a/refstack/api/controllers/products.py +++ b/refstack/api/controllers/products.py @@ -115,6 +115,15 @@ class VersionsController(validation.BaseRestControllerWithValidation): pecan.abort(403, 'Forbidden.') try: + version = db.get_product_version(version_id, + allowed_keys=['version']) + if not version['version']: + pecan.abort(400, 'Can not delete the empty version as it is ' + 'used for basic product/test association. ' + 'This version was implicitly created with ' + 'the product, and so it cannot be deleted ' + 'explicitly.') + db.delete_product_version(version_id) except DBReferenceError: pecan.abort(400, 'Unable to delete. There are still tests ' diff --git a/refstack/tests/api/test_products.py b/refstack/tests/api/test_products.py index 1ccca172..51906776 100644 --- a/refstack/tests/api/test_products.py +++ b/refstack/tests/api/test_products.py @@ -284,3 +284,9 @@ class TestProductVersionEndpoint(api.FunctionalTest): self.delete(self.URL + version_id) self.assertRaises(webtest.app.AppError, self.get_json, self.URL + 'version_id') + + # Get the null version and ensure it can't be deleted. + versions = self.get_json(self.URL) + version_id = versions[0]['id'] + response = self.delete(self.URL + version_id, expect_errors=True) + self.assertEqual(400, response.status_code)