fix the serializer bug of vnflcm
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / tests / test_subscribe_notification.py
1 # Copyright (C) 2018 Verizon. All Rights Reserved
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 mock
16 from django.test import TestCase
17 from rest_framework.test import APIClient
18 # import uuid
19
20
21 class TestSubscription(TestCase):
22     def setUp(self):
23         self.client = APIClient()
24
25     def tearDown(self):
26         pass
27
28     # @mock.patch("requests.get")
29     # @mock.patch.object(uuid, 'uuid4')
30     # def test_subscribe_notification_simple(self, mock_uuid4, mock_requests):
31     #     temp_uuid = "99442b18-a5c7-11e8-998c-bf1755941f13"
32     #     dummy_subscription = {
33     #         "callbackUri": "http://aurl.com"
34     #     }
35     #     mock_requests.return_value.status_code = 204
36     #     mock_requests.get.status_code = 204
37     #     mock_uuid4.return_value = temp_uuid
38     #     response = self.client.post("/api/vnflcm/v1/subscriptions", data=dummy_subscription, format='json')
39     #     self.assertEqual(201, response.status_code)
40     #     self.assertEqual(dummy_subscription["callbackUri"], response.data["callbackUri"])
41     #     self.assertEqual(temp_uuid, response.data["id"])
42
43     # @mock.patch("requests.get")
44     # @mock.patch.object(uuid, 'uuid4')
45     # def test_subscribe_notification(self, mock_uuid4, mock_requests):
46     #     temp_uuid = "99442b18-a5c7-11e8-998c-bf1755941f13"
47     #     dummy_subscription = {
48     #         "callbackUri": "http://aurl.com",
49     #         "authentication": {
50     #             "authType": ["BASIC"],
51     #             "paramsBasic": {
52     #                 "username": "username",
53     #                 "password": "password"
54     #             }
55     #         },
56     #         "filter": {
57     #             "notificationTypes": ["VnfLcmOperationOccurrenceNotification"],
58     #             "operationTypes": [
59     #                 "INSTANTIATE"
60     #             ],
61     #             "operationStates": [
62     #                 "STARTING"
63     #             ],
64     #         }
65     #     }
66     #     mock_requests.return_value.status_code = 204
67     #     mock_requests.get.return_value.status_code = 204
68     #     mock_uuid4.return_value = temp_uuid
69     #     response = self.client.post("/api/vnflcm/v1/subscriptions", data=dummy_subscription, format='json')
70     #     self.assertEqual(201, response.status_code)
71     #     self.assertEqual(dummy_subscription["callbackUri"], response.data["callbackUri"])
72     #     self.assertEqual(temp_uuid, response.data["id"])
73
74     @mock.patch("requests.get")
75     def test_invalid_auth_subscription(self, mock_requests):
76         dummy_subscription = {
77             "callbackUri": "http://aurl.com",
78             "authentication": {
79                 "authType": ["OAUTH2_CLIENT_CREDENTIALS"],
80                 "paramsBasic": {
81                     "username": "username",
82                     "password": "password"
83                 }
84             },
85             "filter": {
86                 "notificationTypes": ["VnfLcmOperationOccurrenceNotification"],
87                 "operationTypes": [
88                     "INSTANTIATE"
89                 ],
90                 "operationStates": [
91                     "STARTING"
92                 ],
93             }
94         }
95         mock_requests.return_value.status_code = 204
96         mock_requests.get.return_value.status_code = 204
97         expected_data = {
98             'error': 'Auth type should be BASIC'
99         }
100         response = self.client.post("/api/vnflcm/v1/subscriptions", data=dummy_subscription, format='json')
101         self.assertEqual(500, response.status_code)
102         self.assertEqual(expected_data, response.data)
103
104     @mock.patch("requests.get")
105     def test_invalid_notification_type(self, mock_requests):
106         dummy_subscription = {
107             "callbackUri": "http://aurl.com",
108             "filter": {
109                 "notificationTypes": ["VnfIdentifierDeletionNotification"],
110                 "operationTypes": [
111                     "INSTANTIATE"
112                 ],
113                 "operationStates": [
114                     "STARTING"
115                 ],
116             }
117         }
118         mock_requests.return_value.status_code = 204
119         mock_requests.get.return_value.status_code = 204
120         expected_data = {
121             'error': 'If you are setting operationTypes,then ' +
122             'notificationTypes must be VnfLcmOperationOccurrenceNotification'
123         }
124         response = self.client.post("/api/vnflcm/v1/subscriptions", data=dummy_subscription, format='json')
125         self.assertEqual(500, response.status_code)
126         self.assertEqual(expected_data, response.data)
127
128     # @mock.patch("requests.get")
129     # @mock.patch.object(uuid, 'uuid4')
130     # def test_duplicate_subscription(self, mock_uuid4, mock_requests):
131     #     temp_uuid = str(uuid.uuid4())
132     #     dummy_subscription = {
133     #         "callbackUri": "http://aurl.com",
134     #         "filter": {
135     #             "notificationTypes": ["VnfLcmOperationOccurrenceNotification"],
136     #             "operationTypes": [
137     #                 "INSTANTIATE"
138     #             ],
139     #             "operationStates": [
140     #                 "STARTING"
141     #             ]
142     #         }
143     #     }
144     #     mock_requests.return_value.status_code = 204
145     #     mock_requests.get.return_value.status_code = 204
146     #     mock_uuid4.return_value = temp_uuid
147     #     response = self.client.post("/api/vnflcm/v1/subscriptions", data=dummy_subscription, format='json')
148     #     self.assertEqual(201, response.status_code)
149     #     self.assertEqual(dummy_subscription["callbackUri"], response.data["callbackUri"])
150     #     self.assertEqual(temp_uuid, response.data["id"])
151     #     response = self.client.post("/api/vnflcm/v1/subscriptions", data=dummy_subscription, format='json')
152     #     self.assertEqual(303, response.status_code)
153     #     expected_data = {
154     #         "error": "Already Subscription exists with the same callbackUri and filter"
155     #     }
156     #     self.assertEqual(expected_data, response.data)