~ubuntu-branches/ubuntu/utopic/python-barbicanclient/utopic

« back to all changes in this revision

Viewing changes to barbicanclient/test/test_client_verifications.py

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand
  • Date: 2014-09-06 17:21:41 UTC
  • Revision ID: package-import@ubuntu.com-20140906172141-w8vrditz233uqzp3
Tags: upstream-2.2.1
ImportĀ upstreamĀ versionĀ 2.2.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2013 Rackspace, Inc.
 
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
 
12
# implied.
 
13
# See the License for the specific language governing permissions and
 
14
# limitations under the License.
 
15
 
 
16
from barbicanclient import verifications as vers
 
17
from barbicanclient.openstack.common import timeutils
 
18
from barbicanclient.test import test_client
 
19
 
 
20
 
 
21
class VerificationData(object):
 
22
    def __init__(self):
 
23
        self.created = str(timeutils.utcnow())
 
24
 
 
25
        self.resource_type = 'image'
 
26
        self.resource_ref = 'http://www.image.com/v1/images/1234'
 
27
        self.resource_action = 'vm_attach'
 
28
        self.impersonation_allowed = True
 
29
 
 
30
        self.verification_dict = {'created': self.created,
 
31
                                  'resource_type': self.resource_type,
 
32
                                  'resource_ref': self.resource_ref,
 
33
                                  'resource_action': self.resource_action,
 
34
                                  'impersonation_allowed':
 
35
                                  self.impersonation_allowed}
 
36
 
 
37
    def get_dict(self, verification_ref):
 
38
        verify = self.verification_dict
 
39
        verify['verification_ref'] = verification_ref
 
40
        return verify
 
41
 
 
42
 
 
43
class WhenTestingVerifications(test_client.BaseEntityResource):
 
44
 
 
45
    def setUp(self):
 
46
        self._setUp('verifications')
 
47
 
 
48
        self.verify = VerificationData()
 
49
 
 
50
        self.manager = vers.VerificationManager(self.api)
 
51
 
 
52
    def test_should_entity_str(self):
 
53
        verif_obj = vers.Verification(self.verify.get_dict(self.entity_href))
 
54
        verif_obj.error_status_code = '500'
 
55
        verif_obj.error_reason = 'Something is broken'
 
56
        self.assertIn('resource_type: ' + self.verify.resource_type,
 
57
                      str(verif_obj))
 
58
        self.assertIn('error_status_code: 500', str(verif_obj))
 
59
 
 
60
    def test_should_entity_repr(self):
 
61
        verif = vers.Verification(self.verify.get_dict(self.entity_href))
 
62
        self.assertIn('verification_ref=' + self.entity_href,
 
63
                      repr(verif))
 
64
 
 
65
    def test_should_create(self):
 
66
        self.api.post.return_value = {'verification_ref': self.entity_href}
 
67
 
 
68
        order_href = self.manager\
 
69
            .create(resource_type=self.verify.resource_type,
 
70
                    resource_ref=self.verify.resource_ref,
 
71
                    resource_action=self.verify.resource_action)
 
72
 
 
73
        self.assertEqual(self.entity_href, order_href)
 
74
 
 
75
        # Verify the correct URL was used to make the call.
 
76
        args, kwargs = self.api.post.call_args
 
77
        entity_resp = args[0]
 
78
        self.assertEqual(self.entity, entity_resp)
 
79
 
 
80
        # Verify that correct information was sent in the call.
 
81
        verify_req = args[1]
 
82
        self.assertEqual(self.verify.resource_type,
 
83
                         verify_req['resource_type'])
 
84
        self.assertEqual(self.verify.resource_action,
 
85
                         verify_req['resource_action'])
 
86
        self.assertEqual(self.verify.resource_ref,
 
87
                         verify_req['resource_ref'])
 
88
 
 
89
    def test_should_get(self):
 
90
        self.api.get.return_value = self.verify.get_dict(self.entity_href)
 
91
 
 
92
        verify = self.manager.get(verification_ref=self.entity_href)
 
93
        self.assertIsInstance(verify, vers.Verification)
 
94
        self.assertEqual(self.entity_href, verify.verif_ref)
 
95
 
 
96
        # Verify the correct URL was used to make the call.
 
97
        args, kwargs = self.api.get.call_args
 
98
        url = args[0]
 
99
        self.assertEqual(self.entity_href, url)
 
100
 
 
101
    def test_should_delete(self):
 
102
        self.manager.delete(verification_ref=self.entity_href)
 
103
 
 
104
        # Verify the correct URL was used to make the call.
 
105
        args, kwargs = self.api.delete.call_args
 
106
        url = args[0]
 
107
        self.assertEqual(self.entity_href, url)
 
108
 
 
109
    def test_should_get_list(self):
 
110
        verify_resp = self.verify.get_dict(self.entity_href)
 
111
        self.api.get.return_value = {"verifications":
 
112
                                     [verify_resp for v in range(3)]}
 
113
 
 
114
        verifies = self.manager.list(limit=10, offset=5)
 
115
        self.assertTrue(len(verifies) == 3)
 
116
        self.assertIsInstance(verifies[0], vers.Verification)
 
117
        self.assertEqual(self.entity_href, verifies[0].verif_ref)
 
118
 
 
119
        # Verify the correct URL was used to make the call.
 
120
        args, kwargs = self.api.get.call_args
 
121
        url = args[0]
 
122
        self.assertEqual(self.entity_base[:-1], url)
 
123
 
 
124
        # Verify that correct information was sent in the call.
 
125
        params = args[1]
 
126
        self.assertEqual(10, params['limit'])
 
127
        self.assertEqual(5, params['offset'])
 
128
 
 
129
    def test_should_fail_get_no_href(self):
 
130
        self.assertRaises(ValueError, self.manager.get, None)
 
131
 
 
132
    def test_should_fail_delete_no_href(self):
 
133
        self.assertRaises(ValueError, self.manager.delete, None)