summaryrefslogtreecommitdiff
path: root/barbicanclient/tests/v1/test_orders.py
blob: 0923aa8015101f496da86fb1c658aea5b5c9feb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# Copyright (c) 2013 Rackspace, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json

from oslo_utils import timeutils
import uuid

from barbicanclient import base
from barbicanclient.tests import test_client
from barbicanclient.v1 import orders


class OrdersTestCase(test_client.BaseEntityResource):
    def setUp(self):
        self._setUp('orders', entity_id='d0460cc4-2876-4493-b7de-fc5c812883cc')

        self.secret_ref = (self.endpoint +
                           '/secrets/a2292306-6da0-4f60-bd8a-84fc8d692716')

        self.key_order_data = """{{
            "status": "ACTIVE",
            "secret_ref": "{0}",
            "updated": "2014-10-21T17:15:50.871596",
            "meta": {{
                "name": "secretname",
                "algorithm": "aes",
                "payload_content_type": "application/octet-stream",
                "mode": "cbc",
                "bit_length": 256,
                "expiration": "2015-02-28T19:14:44.180394"
            }},
            "created": "2014-10-21T17:15:50.824202",
            "type": "key",
            "order_ref": "{1}"
        }}""".format(self.secret_ref, self.entity_href)

        self.key_order_invalid_data = """{{
            "status": "ACTIVE",
            "secret_ref": "{0}",
            "updated": "2014-10-21T17:15:50.871596",
            "meta": {{
                "name": "secretname",
                "algorithm": "aes",
                "request_type":"invalid",
                "payload_content_type": "application/octet-stream",
                "mode": "cbc",
                "bit_length": 256,
                "expiration": "2015-02-28T19:14:44.180394"
            }},
            "created": "2014-10-21T17:15:50.824202",
            "type": "key",
            "order_ref": "{1}"
        }}""".format(self.secret_ref, self.entity_href)

        self.container_ref = (
            self.endpoint + '/containers/a2292306-6da0-4f60-bd8a-84fc8d692716')
        self.source_container_ref = (
            self.endpoint + '/containers/c6f20480-c1e5-442b-94a0-cb3b5e0cf179')

        self.cert_order_data = """{{
            "status": "ACTIVE",
            "container_ref": "{0}",
            "updated": "2014-10-21T17:15:50.871596",
            "meta": {{
                "name": "secretname",
                "subject_dn": "cn=server.example.com,o=example.com",
                "request_type": "stored-key",
                "container_ref": "{1}"
            }},
            "created": "2014-10-21T17:15:50.824202",
            "type": "certificate",
            "order_ref": "{2}"
        }}""".format(self.container_ref, self.source_container_ref,
                     self.entity_href)

        self.manager = self.client.orders

    def _get_order_args(self, order_data):
        order_args = json.loads(order_data)
        order_args.update(order_args.pop('meta'))
        order_args.pop('type')
        return order_args


class WhenTestingKeyOrders(OrdersTestCase):

    def test_should_include_errors_in_str(self):
        order_args = self._get_order_args(self.key_order_data)
        error_code = 500
        error_reason = 'Something is broken'
        order_obj = orders.KeyOrder(api=None, error_status_code=error_code,
                                    error_reason=error_reason, **order_args)
        self.assertIn(str(error_code), str(order_obj))
        self.assertIn(error_reason, str(order_obj))

    def test_should_include_order_ref_in_repr(self):
        order_args = self._get_order_args(self.key_order_data)
        order_obj = orders.KeyOrder(api=None, **order_args)
        self.assertIn('order_ref=' + self.entity_href, repr(order_obj))

    def test_should_be_immutable_after_submit(self):
        data = {'order_ref': self.entity_href}
        self.responses.post(self.entity_base + '/', json=data)

        order = self.manager.create_key(
            name='name',
            algorithm='algorithm',
            payload_content_type='payload_content_type'
        )
        order_href = order.submit()

        self.assertEqual(self.entity_href, order_href)
        # Verify that attributes are immutable after store.
        attributes = [
            "name", "expiration", "algorithm", "bit_length", "mode",
            "payload_content_type"
        ]
        for attr in attributes:
            try:
                setattr(order, attr, "test")
                self.fail("didn't raise an ImmutableException exception")
            except base.ImmutableException:
                pass

    def test_should_submit_via_constructor(self):
        data = {'order_ref': self.entity_href}
        self.responses.post(self.entity_base + '/', json=data)

        order = self.manager.create_key(
            name='name',
            algorithm='algorithm',
            payload_content_type='payload_content_type'
        )
        order_href = order.submit()

        self.assertEqual(self.entity_href, order_href)

        # Verify the correct URL was used to make the call.
        self.assertEqual(self.entity_base + '/',
                         self.responses.last_request.url)

        # Verify that correct information was sent in the call.
        order_req = json.loads(self.responses.last_request.text)
        self.assertEqual('name', order_req['meta']['name'])
        self.assertEqual('algorithm',
                         order_req['meta']['algorithm'])
        self.assertEqual('payload_content_type',
                         order_req['meta']['payload_content_type'])

    def test_should_submit_via_attributes(self):
        data = {'order_ref': self.entity_href}
        self.responses.post(self.entity_base + '/', json=data)

        order = self.manager.create_key()
        order.name = 'name'
        order.algorithm = 'algorithm'
        order.payload_content_type = 'payload_content_type'
        order_href = order.submit()

        self.assertEqual(self.entity_href, order_href)

        # Verify the correct URL was used to make the call.
        self.assertEqual(self.entity_base + '/',
                         self.responses.last_request.url)

        # Verify that correct information was sent in the call.
        order_req = json.loads(self.responses.last_request.text)
        self.assertEqual('name', order_req['meta']['name'])
        self.assertEqual('algorithm',
                         order_req['meta']['algorithm'])
        self.assertEqual('payload_content_type',
                         order_req['meta']['payload_content_type'])

    def test_should_not_be_able_to_set_generated_attributes(self):
        order = self.manager.create_key()

        # Verify that generated attributes cannot be set.
        attributes = [
            "order_ref", "secret_ref", "created", "updated", "status",
            "error_status_code", "error_reason"
        ]
        for attr in attributes:
            try:
                setattr(order, attr, "test")
                self.fail("didn't raise an AttributeError exception")
            except AttributeError:
                pass


class WhenTestingAsymmetricOrders(OrdersTestCase):

    def test_should_be_immutable_after_submit(self):
        data = {'order_ref': self.entity_href}
        self.responses.post(self.entity_base + '/', json=data)

        order = self.manager.create_asymmetric(
            name='name',
            algorithm='algorithm',
            payload_content_type='payload_content_type'
        )
        order_href = order.submit()

        self.assertEqual(self.entity_href, order_href)

        # Verify that attributes are immutable after store.
        attributes = [
            "name", "expiration", "algorithm", "bit_length", "pass_phrase",
            "payload_content_type"
        ]
        for attr in attributes:
            try:
                setattr(order, attr, "test")
                self.fail(
                    "{0} didn't raise an ImmutableException exception".format(
                        attr
                    )
                )
            except base.ImmutableException:
                pass

    def test_create_asymmetric_order_w_passphrase(self):
        data = {'order_ref': self.entity_href}
        self.responses.post(self.entity_base + '/', json=data)

        passphrase = str(uuid.uuid4())
        order = orders.AsymmetricOrder(
            api=self.manager._api,
            name='name',
            algorithm='algorithm',
            payload_content_type='payload_content_type',
            passphrase=passphrase,
        )
        order_href = order.submit()
        self.assertEqual(self.entity_href, order_href)
        self.assertEqual(passphrase, order.pass_phrase)

    def test_create_asymmetric_order_w_legacy_pass_phrase_param(self):
        data = {'order_ref': self.entity_href}
        self.responses.post(self.entity_base + '/', json=data)

        passphrase = str(uuid.uuid4())
        order = orders.AsymmetricOrder(
            api=self.manager._api,
            name='name',
            algorithm='algorithm',
            payload_content_type='payload_content_type',
            pass_phrase=passphrase,
        )
        order_href = order.submit()
        self.assertEqual(self.entity_href, order_href)
        self.assertEqual(passphrase, order.pass_phrase)


class WhenTestingOrderManager(OrdersTestCase):

    def test_should_get(self):
        self.responses.get(self.entity_href, text=self.key_order_data)

        order = self.manager.get(order_ref=self.entity_href)
        self.assertIsInstance(order, orders.KeyOrder)
        self.assertEqual(self.entity_href, order.order_ref)

        # Verify the correct URL was used to make the call.
        self.assertEqual(self.entity_href, self.responses.last_request.url)

    def test_should_get_invalid_meta(self):
        self.responses.get(self.entity_href, text=self.key_order_invalid_data)

        # Verify checking for invalid meta fields.
        self.assertRaises(TypeError,
                          self.manager.get,
                          self.entity_href)

    def test_should_get_list(self):
        data = {"orders": [json.loads(self.key_order_data) for _ in range(3)]}
        self.responses.get(self.entity_base, json=data)

        orders_list = self.manager.list(limit=10, offset=5)
        self.assertTrue(len(orders_list) == 3)
        self.assertIsInstance(orders_list[0], orders.KeyOrder)
        self.assertEqual(self.entity_href, orders_list[0].order_ref)

        # Verify the correct URL was used to make the call.
        self.assertEqual(self.entity_base,
                         self.responses.last_request.url.split('?')[0])

        # Verify that correct information was sent in the call.
        self.assertEqual(['10'], self.responses.last_request.qs['limit'])
        self.assertEqual(['5'], self.responses.last_request.qs['offset'])

    def test_should_delete(self):
        self.responses.delete(self.entity_href, status_code=204)

        self.manager.delete(order_ref=self.entity_href)

        # Verify the correct URL was used to make the call.
        self.assertEqual(self.entity_href, self.responses.last_request.url)

    def test_should_fail_delete_no_href(self):
        self.assertRaises(ValueError, self.manager.delete, None)

    def test_should_get_total(self):
        self.responses.get(self.entity_base, json={'total': 1})
        total = self.manager.total()
        self.assertEqual(1, total)

    def test_get_formatted_data(self):
        self.responses.get(self.entity_href, text=self.key_order_data)

        order = self.manager.get(order_ref=self.entity_href)
        data = order._get_formatted_data()

        order_args = self._get_order_args(self.key_order_data)
        self.assertEqual(timeutils.parse_isotime(
                         order_args['created']).isoformat(),
                         data[4])


class WhenTestingCertificateOrders(OrdersTestCase):

    def test_get(self):
        self.responses.get(self.entity_href, text=self.cert_order_data)

        order = self.manager.get(order_ref=self.entity_href)
        self.assertIsInstance(order, orders.CertificateOrder)
        self.assertEqual(self.entity_href, order.order_ref)

        # Verify the correct URL was used to make the call.
        self.assertEqual(self.entity_href, self.responses.last_request.url)

    def test_repr(self):
        order_args = self._get_order_args(self.cert_order_data)
        order_obj = orders.CertificateOrder(api=None, **order_args)
        self.assertIn('order_ref=' + self.entity_href, repr(order_obj))

    def test_constructor(self):
        data = {'order_ref': self.entity_href}
        self.responses.post(self.entity_base + '/', json=data)

        order = self.manager.create_certificate(
            name='name',
            subject_dn='cn=server.example.com,o=example.com',
            request_type='stored-key',
            source_container_ref=self.source_container_ref
        )
        order_href = order.submit()

        self.assertEqual(self.entity_href, order_href)

        # Verify the correct URL was used to make the call.
        self.assertEqual(self.entity_base + '/',
                         self.responses.last_request.url)

        # Verify that correct information was sent in the call.
        order_req = json.loads(self.responses.last_request.text)
        self.assertEqual('name', order_req['meta']['name'])
        self.assertEqual('cn=server.example.com,o=example.com',
                         order_req['meta']['subject_dn'])
        self.assertEqual('stored-key',
                         order_req['meta']['request_type'])
        self.assertEqual(self.source_container_ref,
                         order_req['meta']['container_ref'])

    def test_list(self):
        data = {"orders": [json.loads(self.cert_order_data) for _ in range(3)]}
        self.responses.get(self.entity_base, json=data)

        orders_list = self.manager.list(limit=10, offset=5)
        self.assertEqual(3, len(orders_list))
        self.assertIsInstance(orders_list[0], orders.CertificateOrder)
        self.assertEqual(self.entity_href, orders_list[0].order_ref)

    def test_get_formatted_data(self):
        self.responses.get(self.entity_href, text=self.cert_order_data)

        order = self.manager.get(order_ref=self.entity_href)
        data = order._get_formatted_data()

        order_args = self._get_order_args(self.cert_order_data)
        self.assertEqual(timeutils.parse_isotime(
                         order_args['created']).isoformat(),
                         data[4])