From: hongyuzhao Date: Wed, 8 Apr 2020 03:50:53 +0000 (+0800) Subject: fix bug for ETSI Catalog Manager needs to raise 303 exception for the same callback_u... X-Git-Tag: 1.0.6~12^2 X-Git-Url: https://gerrit.onap.org/r/gitweb?p=modeling%2Fetsicatalog.git;a=commitdiff_plain;h=3fcd9e6adf5833688b0007983aab00db312e433e fix bug for ETSI Catalog Manager needs to raise 303 exception for the same callback_uri and the same filter Change-Id: Ie577540d2c4bb25299059869ce967422610f5785 Issue-ID: MODELING-353 Signed-off-by: hongyu zhao --- diff --git a/catalog/packages/biz/nsdm_subscription.py b/catalog/packages/biz/nsdm_subscription.py index d111bc2..84bacde 100644 --- a/catalog/packages/biz/nsdm_subscription.py +++ b/catalog/packages/biz/nsdm_subscription.py @@ -26,6 +26,7 @@ from catalog.pub.database.models import NsdmSubscriptionModel from catalog.pub.exceptions import CatalogException, \ NsdmBadRequestException, NsdmDuplicateSubscriptionException, SubscriptionDoesNotExistsException from catalog.pub.utils.values import ignore_case_get +from catalog.pub.config.config import MSB_SERVICE_IP, MSB_SERVICE_PORT logger = logging.getLogger(__name__) @@ -185,13 +186,15 @@ class NsdmSubscription: subscriptions = \ NsdmSubscriptionModel.objects.filter( callback_uri=self.callback_uri) - if not subscriptions.exists(): - return + for subscription in subscriptions: if self.check_filter_exists(subscription): - raise NsdmDuplicateSubscriptionException( + links = json.loads(subscription.links) + logger.error( "Subscription has already existed with the " - "same callbackUri and filter") + "same callbackUri and filter:%s" % links) + raise NsdmDuplicateSubscriptionException( + "https://%s:%s/%s" % (MSB_SERVICE_IP, MSB_SERVICE_PORT, links["self"]["href"])) def save_db(self): logger.debug("Create Subscription --> Saving the subscription " diff --git a/catalog/packages/biz/vnf_pkg_subscription.py b/catalog/packages/biz/vnf_pkg_subscription.py index a98fe12..d059365 100644 --- a/catalog/packages/biz/vnf_pkg_subscription.py +++ b/catalog/packages/biz/vnf_pkg_subscription.py @@ -27,6 +27,7 @@ from catalog.pub.database.models import VnfPkgSubscriptionModel from catalog.pub.exceptions import VnfPkgSubscriptionException, \ VnfPkgDuplicateSubscriptionException, SubscriptionDoesNotExistsException from catalog.pub.utils.values import ignore_case_get +from catalog.pub.config.config import MSB_SERVICE_IP, MSB_SERVICE_PORT logger = logging.getLogger(__name__) @@ -44,7 +45,6 @@ def is_filter_type_equal(new_filter, existing_filter): class CreateSubscription(object): - def __init__(self, data): self.data = data self.filter = ignore_case_get(self.data, "filter", {}) @@ -104,11 +104,10 @@ class CreateSubscription(object): def check_valid_auth_info(self): logger.debug("SubscribeNotification--post::> Validating Auth " "details if provided") - if self.authentication.get("paramsBasic", {}) and \ - const.BASIC not in self.authentication.get("authType"): + if self.authentication.get("paramsBasic", {}) and const.BASIC not in self.authentication.get("authType"): raise VnfPkgSubscriptionException('Auth type should be ' + const.BASIC) - if self.authentication.get("paramsOauth2ClientCredentials", {}) and \ - const.OAUTH2_CLIENT_CREDENTIALS not in self.authentication.get("authType"): + if self.authentication.get("paramsOauth2ClientCredentials", {}) \ + and const.OAUTH2_CLIENT_CREDENTIALS not in self.authentication.get("authType"): raise VnfPkgSubscriptionException('Auth type should be ' + const.OAUTH2_CLIENT_CREDENTIALS) def check_filter_exists(self, sub): @@ -125,16 +124,17 @@ class CreateSubscription(object): return True def check_valid(self): + links = "" logger.debug("SubscribeNotification--post::> Checking DB if " "callbackUri already exists") subscriptions = VnfPkgSubscriptionModel.objects.filter(callback_uri=self.callback_uri) - if not subscriptions.exists(): - return True for subscription in subscriptions: if self.check_filter_exists(subscription): + links = json.loads(subscription.links) + logger.error("Subscriptions has already exists with the same callbackUri and filter:%s" % links) raise VnfPkgDuplicateSubscriptionException( - "Already Subscription (%s) exists with the " - "same callbackUri and filter" % subscription.subscription_id) + "https://%s:%s/%s" % (MSB_SERVICE_IP, MSB_SERVICE_PORT, links["self"]["href"])) + return True def save_db(self): @@ -160,7 +160,6 @@ class CreateSubscription(object): class QuerySubscription(object): - def query_multi_subscriptions(self, params): query_data = {} logger.debug("QuerySubscription--get--multi--subscriptions--biz::> Check " @@ -190,7 +189,6 @@ class QuerySubscription(object): class TerminateSubscription(object): - def terminate(self, subscription_id): logger.debug("TerminateSubscriptions--delete--biz::> " "ID: %s" % subscription_id) diff --git a/catalog/packages/tests/test_nsdm_subscription.py b/catalog/packages/tests/test_nsdm_subscription.py index 2d95a1c..4e14ca1 100644 --- a/catalog/packages/tests/test_nsdm_subscription.py +++ b/catalog/packages/tests/test_nsdm_subscription.py @@ -29,7 +29,7 @@ from catalog.pub.config import config as pub_config import catalog.pub.utils.timeutil from catalog.packages.tests.const import nsd_data from catalog.pub.database.models import NSPackageModel, VnfPackageModel, PnfPackageModel -from catalog.pub.config.config import CATALOG_ROOT_PATH +from catalog.pub.config.config import CATALOG_ROOT_PATH, MSB_SERVICE_IP, MSB_SERVICE_PORT from catalog.pub.utils import toscaparser @@ -148,7 +148,7 @@ class TestNsdmSubscription(TestCase): response.data["callbackUri"]) @mock.patch("requests.get") - def test_nsdm_duplicate_subscription(self, mock_requests): + def test_nsdm_duplicate_filter_or_callbackuri_subscription(self, mock_requests): mock_requests.return_value.status_code = 204 mock_requests.get.return_value.status_code = 204 response = self.client.post("/api/nsd/v1/subscriptions", @@ -156,15 +156,33 @@ class TestNsdmSubscription(TestCase): self.assertEqual(201, response.status_code) self.assertEqual(self.subscription["callbackUri"], response.data["callbackUri"]) - expected_data = { - 'status': 303, - 'detail': 'Subscription has already existed with' - ' the same callbackUri and filter' - } + newsub1 = self.subscription + newsub1["callbackUri"] = "http://newcallbackuri.com" + response = self.client.post("/api/nsd/v1/subscriptions", + data=newsub1, format='json') + self.assertEqual(201, response.status_code) + newsub2 = self.subscription + newsub2["filter"]["nsdName"] = ["aaa"] + response = self.client.post("/api/nsd/v1/subscriptions", + data=newsub2, format='json') + self.assertEqual(201, response.status_code) + + @mock.patch("requests.get") + def test_nsdm_duplicate_callbackuri_filter_subscription(self, mock_requests): + mock_requests.return_value.status_code = 204 + mock_requests.get.return_value.status_code = 204 + response = self.client.post("/api/nsd/v1/subscriptions", + data=self.subscription, format='json') + self.assertEqual(201, response.status_code) + self.assertEqual(self.subscription["callbackUri"], + response.data["callbackUri"]) + subscriptionid = response.data["id"] response = self.client.post("/api/nsd/v1/subscriptions", data=self.subscription, format='json') self.assertEqual(303, response.status_code) - self.assertEqual(expected_data, response.data) + redirect_addr = "https://%s:%s/%s" % (MSB_SERVICE_IP, MSB_SERVICE_PORT, + os.path.join(const.NSDM_SUBSCRIPTION_ROOT_URI, subscriptionid)) + self.assertEqual(redirect_addr, response["Location"]) @mock.patch("requests.get") def test_nsdm_bad_request(self, mock_requests): diff --git a/catalog/packages/tests/test_vnf_pkg_subscription.py b/catalog/packages/tests/test_vnf_pkg_subscription.py index 17de7cf..080a7c4 100644 --- a/catalog/packages/tests/test_vnf_pkg_subscription.py +++ b/catalog/packages/tests/test_vnf_pkg_subscription.py @@ -27,11 +27,12 @@ from catalog.packages import const from catalog.packages.biz.notificationsutil import PkgNotifications from catalog.packages.biz.vnf_pkg_subscription import QuerySubscription, TerminateSubscription from catalog.pub.config import config as pub_config -from catalog.pub.config.config import CATALOG_ROOT_PATH +from catalog.pub.config.config import CATALOG_ROOT_PATH, MSB_SERVICE_IP, MSB_SERVICE_PORT from catalog.pub.database.models import VnfPkgSubscriptionModel, VnfPackageModel from catalog.pub.exceptions import SubscriptionDoesNotExistsException +from .const import vnf_subscription_data from catalog.pub.utils import toscaparser -from .const import vnf_subscription_data, vnfd_data +from catalog.packages.tests.const import vnfd_data class TestNfPackageSubscription(TestCase): @@ -81,16 +82,46 @@ class TestNfPackageSubscription(TestCase): response.data["callbackUri"] ) self.assertEqual(temp_uuid, response.data["id"]) - temp_uuid = "00442b18-a5c7-11e8-998c-bf1755941f12" + temp2_uuid = "00442b18-a5c7-11e8-998c-bf1755941f12" mock_requests.return_value.status_code = 204 mock_requests.get.status_code = 204 - mock_uuid4.return_value = temp_uuid + mock_uuid4.return_value = temp2_uuid response = self.client.post( "/api/vnfpkgm/v1/subscriptions", data=self.vnf_subscription_data, format='json' ) self.assertEqual(303, response.status_code) + redirect_addr = "https://%s:%s/%s" % (MSB_SERVICE_IP, MSB_SERVICE_PORT, + os.path.join(const.VNFPKG_SUBSCRIPTION_ROOT_URI, temp_uuid)) + self.assertEqual(redirect_addr, response["Location"]) + + @mock.patch("requests.get") + def test_callbackuri_same_subscriptions(self, mock_requests): + mock_requests.return_value.status_code = 204 + mock_requests.get.status_code = 204 + response = self.client.post( + "/api/vnfpkgm/v1/subscriptions", + data=self.vnf_subscription_data, + format='json' + ) + self.assertEqual(201, response.status_code) + self.assertEqual( + self.vnf_subscription_data["callbackUri"], + response.data["callbackUri"] + ) + newsubs = self.vnf_subscription_data + newsubs["filter"]["vnfdId"] = ["ssss"] + response = self.client.post( + "/api/vnfpkgm/v1/subscriptions", + data=self.vnf_subscription_data, + format='json' + ) + self.assertEqual(201, response.status_code) + self.assertEqual( + newsubs["callbackUri"], + response.data["callbackUri"] + ) @mock.patch("requests.get") @mock.patch.object(uuid, 'uuid4') diff --git a/catalog/packages/views/common.py b/catalog/packages/views/common.py index 12840a5..e25deea 100644 --- a/catalog/packages/views/common.py +++ b/catalog/packages/views/common.py @@ -68,16 +68,14 @@ def view_safe_call_with_log(logger): return func(*args, **kwargs) except NsdmDuplicateSubscriptionException as e: logger.error(e.args[0]) - return make_error_resp( - detail=e.args[0], - status=status.HTTP_303_SEE_OTHER - ) + resp = Response(status=status.HTTP_303_SEE_OTHER, headers={'Location': e.args[0]}) + # resp["Location"] = e.args[0] + return resp except VnfPkgDuplicateSubscriptionException as e: logger.error(e.args[0]) - return make_error_resp( - detail=e.args[0], - status=status.HTTP_303_SEE_OTHER - ) + resp = Response(status=status.HTTP_303_SEE_OTHER, headers={'Location': e.args[0]}) + # resp["Location"] = e.args[0] + return resp except PackageNotFoundException as e: logger.error(e.args[0]) return make_error_resp(