rename package name
[modeling/etsicatalog.git] / catalog / pub / Dmaap_lib / test / test_consumer.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 base64
16 import datetime
17 import hmac
18 import unittest
19 from _sha1 import sha1
20
21 from ..dmaap.consumer import ConsumerClient
22
23
24 class CreateApiKeyTest(unittest.TestCase):
25     def setUp(self):
26         self.apiKey = "7TuwzpLJ4QfQs4O"
27         self.apiSecret = "7TuwzpLJ4QfQs4O"
28         self.host = '127.0.0.1'
29         self.topic = 'abc'
30         self.group = 'def'
31         self.comsumer_id = '123'
32         self.timeout_ms = 3
33         self.limit = 3
34         self.filter = 'test'
35
36     def tearDown(self):
37         self.ret_url = ""
38
39     def test_create_url(self):
40         exp_url = 'http://127.0.0.1/events/abc/def/123'
41         consumer = ConsumerClient(self.host, self.topic, self.group, self.comsumer_id)
42         ret_url = consumer.create_url()
43         self.assertEqual(exp_url, ret_url)
44
45     def test_create_timeout_url(self):
46         exp_url = 'http://127.0.0.1/events/abc/def/123?timeout=3'
47         consumer = ConsumerClient(self.host, self.topic, self.group, self.comsumer_id, self.timeout_ms)
48         ret_url = consumer.create_url()
49         self.assertEqual(exp_url, ret_url)
50
51     def test_create_limit_url(self):
52
53         exp_url = 'http://127.0.0.1/events/abc/def/123?timeout=3&limit=3'
54         consumer = ConsumerClient(self.host, self.topic, self.group, self.comsumer_id,
55                                   self.timeout_ms, self.limit)
56         ret_url = consumer.create_url()
57         self.assertEqual(exp_url, ret_url)
58
59     def test_create_filter_url(self):
60
61         exp_url = "http://127.0.0.1/events/abc/def/123?timeout=3&limit=3&filter=b'test'"
62         consumer = ConsumerClient(self.host, self.topic, self.group, self.comsumer_id,
63                                   self.timeout_ms, self.limit, self.filter)
64         ret_url = consumer.create_url()
65         self.assertEqual(exp_url, ret_url)
66
67     def test_create_headers(self):
68         data = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S') + '-04:00'
69         hmac_code = hmac.new(self.apiSecret.encode(), data.encode(), sha1).digest()
70         signature = base64.b64encode(hmac_code).decode()
71         auth = self.apiKey + ':' + signature
72         exp_headers = {
73             'X-CambriaDate': data,
74             'X-CambriaAuth': auth
75         }
76
77         consumer = ConsumerClient(self.host, self.topic, self.group, self.comsumer_id,
78                                   self.timeout_ms, self.limit, self.filter, self.apiKey, self.apiSecret)
79         consumer.set_api_credentials(self.apiKey, self.apiSecret)
80         rea_headers = consumer.create_headers()
81         self.assertEqual(exp_headers, rea_headers)