fix bug for ETSI Catalog Manager needs to raise 303 exception for the same callback_u...
[modeling/etsicatalog.git] / catalog / packages / tests / test_nsdm_subscription.py
index e5e2b83..4e14ca1 100644 (file)
@@ -29,12 +29,11 @@ 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
 
 
 class TestNsdmSubscription(TestCase):
-
     def setUp(self):
         self.client = APIClient()
         NsdmSubscriptionModel.objects.all().delete()
@@ -149,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",
@@ -157,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):
@@ -466,7 +483,7 @@ class TestNsdmSubscription(TestCase):
         expected_data = {
             "status": 404,
             "detail": "Subscription(" + self.subscription_id + ") "
-            "doesn't exist"
+                                                               "doesn't exist"
         }
         response = self.client.get('/api/nsd/v1/'
                                    'subscriptions/' + self.subscription_id,
@@ -625,7 +642,10 @@ class TestNsdmSubscription(TestCase):
         }
         mock_requests_post.assert_called_with(expect_callbackuri, data=expect_notification,
                                               auth=HTTPBasicAuth("username", "password"),
-                                              headers={'Connection': 'close'})
+                                              headers={'Connection': 'close',
+                                                       'content-type': 'application/json',
+                                                       'accept': 'application/json'},
+                                              verify=False)
 
 
 class NotificationTest(TestCase):
@@ -676,7 +696,13 @@ class NotificationTest(TestCase):
                 }
             }
         }
-        mock_requests_post.assert_called_with(expect_callbackuri, data=expect_notification, headers={'Connection': 'close'})
+        mock_requests_post.assert_called_with(expect_callbackuri,
+                                              data=expect_notification,
+                                              headers={'Connection': 'close',
+                                                       'content-type': 'application/json',
+                                                       'accept': 'application/json'},
+                                              verify=False
+                                              )
 
     @mock.patch("requests.post")
     @mock.patch("uuid.uuid4")
@@ -712,4 +738,7 @@ class NotificationTest(TestCase):
             }
         }
         mock_requests_post.assert_called_with(expect_callbackuri, data=expect_notification,
-                                              headers={'Connection': 'close'})
+                                              headers={'Connection': 'close',
+                                                       'content-type': 'application/json',
+                                                       'accept': 'application/json'},
+                                              verify=False)