1 # Copyright (c) 2019, CMCC Technologies Co., Ltd.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 from django.test import TestCase
18 from rest_framework.test import APIClient
20 from lcm.ns.tests import SUBSCRIPTION_NS_DELETION_DICT, SUBSCRIPTION_NS_OPERATION_DICT
23 class TestSubscription(TestCase):
25 self.client = APIClient()
30 @mock.patch("requests.get")
31 @mock.patch.object(uuid, 'uuid4')
32 def test_subscribe_notification_simple(self, mock_uuid4, mock_requests):
33 temp_uuid = "99442b18-a5c7-11e8-998c-bf1755941f13"
34 dummy_subscription = {
35 "callbackUri": "http://test.com"
37 mock_requests.return_value.status_code = 204
38 mock_requests.get.status_code = 204
39 mock_uuid4.return_value = temp_uuid
40 response = self.client.post("/api/nslcm/v1/subscriptions", data=dummy_subscription, format='json')
41 self.assertEqual(201, response.status_code)
42 self.assertEqual(dummy_subscription["callbackUri"], response.data["callbackUri"])
43 self.assertEqual(temp_uuid, response.data["id"])
45 @mock.patch("requests.get")
46 @mock.patch.object(uuid, 'uuid4')
47 def test_subscribe_notification(self, mock_uuid4, mock_requests):
48 temp_uuid = "99442b18-a5c7-11e8-998c-bf1755941f13"
49 mock_requests.return_value.status_code = 204
50 mock_requests.get.return_value.status_code = 204
51 mock_uuid4.return_value = temp_uuid
52 response = self.client.post("/api/nslcm/v1/subscriptions", data=SUBSCRIPTION_NS_OPERATION_DICT, format='json')
53 self.assertEqual(201, response.status_code)
54 self.assertEqual(SUBSCRIPTION_NS_OPERATION_DICT["callbackUri"], response.data["callbackUri"])
55 self.assertEqual(temp_uuid, response.data["id"])
57 @mock.patch("requests.get")
58 def test_subscription_notification_invalide_auth(self, mock_requests):
59 mock_requests.return_value.status_code = 204
60 mock_requests.get.return_value.status_code = 204
63 'detail': 'Auth type should be BASIC'
65 subscription = copy.deepcopy(SUBSCRIPTION_NS_OPERATION_DICT)
66 subscription["authentication"]["authType"] = ["OAUTH2_CLIENT_CREDENTIALS"]
67 response = self.client.post("/api/nslcm/v1/subscriptions", data=subscription, format='json')
68 self.assertEqual(500, response.status_code)
69 self.assertEqual(expected_data, response.data)
71 @mock.patch("requests.get")
72 def test_invalid_notification_type(self, mock_requests):
73 mock_requests.return_value.status_code = 204
74 mock_requests.get.return_value.status_code = 204
77 'detail': 'If you are setting operationTypes, notificationTypes must be '
78 'NsLcmOperationOccurrenceNotification'
80 response = self.client.post("/api/nslcm/v1/subscriptions", data=SUBSCRIPTION_NS_DELETION_DICT, format='json')
81 self.assertEqual(500, response.status_code)
82 self.assertEqual(expected_data, response.data)
84 @mock.patch("requests.get")
85 @mock.patch.object(uuid, 'uuid4')
86 def test_duplicate_subscription(self, mock_uuid4, mock_requests):
87 temp_uuid = str(uuid.uuid4())
88 mock_requests.return_value.status_code = 204
89 mock_requests.get.return_value.status_code = 204
90 mock_uuid4.return_value = temp_uuid
91 response = self.client.post("/api/nslcm/v1/subscriptions", data=SUBSCRIPTION_NS_OPERATION_DICT, format='json')
92 self.assertEqual(201, response.status_code)
93 self.assertEqual(SUBSCRIPTION_NS_OPERATION_DICT["callbackUri"], response.data["callbackUri"])
94 self.assertEqual(temp_uuid, response.data["id"])
95 response = self.client.post("/api/nslcm/v1/subscriptions", data=SUBSCRIPTION_NS_OPERATION_DICT, format='json')
96 self.assertEqual(303, response.status_code)
99 'detail': 'Already Subscription exists with the same callbackUri and filter'
101 self.assertEqual(expected_data, response.data)