update link to upper-constraints.txt
[vfc/nfvo/lcm.git] / lcm / ns / tests / test_subscribe_notification.py
1 # Copyright (c) 2019, CMCC Technologies Co., Ltd.
2
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
6
7 # http://www.apache.org/licenses/LICENSE-2.0
8
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.
14
15 import copy
16 import mock
17 from django.test import TestCase
18 from rest_framework.test import APIClient
19 import uuid
20 from lcm.ns.tests import SUBSCRIPTION_NS_DELETION_DICT, SUBSCRIPTION_NS_OPERATION_DICT
21
22
23 class TestSubscription(TestCase):
24     def setUp(self):
25         self.client = APIClient()
26
27     def tearDown(self):
28         pass
29
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"
36         }
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"])
44
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"])
56
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
61         expected_data = {
62             'status': 500,
63             'detail': 'Auth type should be BASIC'
64         }
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)
70
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
75         expected_data = {
76             'status': 500,
77             'detail': 'If you are setting operationTypes, notificationTypes must be '
78             'NsLcmOperationOccurrenceNotification'
79         }
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)
83
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)
97         expected_data = {
98             'status': 303,
99             'detail': 'Already Subscription exists with the same callbackUri and filter'
100         }
101         self.assertEqual(expected_data, response.data)