~ubuntu-branches/ubuntu/trusty/piston-mini-client/trusty-proposed

« back to all changes in this revision

Viewing changes to piston_mini_client/tests/test_validators.py

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2011-08-11 17:26:42 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20110811172642-pwbja8hb8u0lvy16
Tags: 0.5+bzr41-0ubuntu1
new bzr snapshot that supports overriding the httplib2
signature checks

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# Copyright 2010-2011 Canonical Ltd.  This software is licensed under the
 
3
# GNU Lesser General Public License version 3 (see the file LICENSE).
 
4
 
 
5
from piston_mini_client.validators import (validate_pattern, validate,
 
6
    validate_integer, oauth_protected, basic_protected, ValidationException)
 
7
from piston_mini_client.auth import OAuthAuthorizer, BasicAuthorizer
 
8
from unittest import TestCase
 
9
 
 
10
class ValidatePatternTestCase(TestCase):
 
11
    def setUp(self):
 
12
        self.called = False
 
13
 
 
14
    def test_raises_if_arg_omitted(self):
 
15
        @validate_pattern('arg', 'foo')
 
16
        def func(arg):
 
17
            self.called = True
 
18
        self.assertRaises(ValidationException, func)
 
19
        self.assertFalse(self.called)
 
20
 
 
21
    def test_raises_if_arg_not_named(self):
 
22
        @validate_pattern('arg', 'foo')
 
23
        def func(arg):
 
24
            self.called = True
 
25
        self.assertRaises(ValidationException, func, 'foo')
 
26
        self.assertFalse(self.called)
 
27
 
 
28
    def test_raises_if_arg_not_a_string(self):
 
29
        @validate_pattern('arg', r'\d+')
 
30
        def func(arg):
 
31
            self.called = True
 
32
        self.assertRaises(ValidationException, func, arg=42)
 
33
        self.assertFalse(self.called)
 
34
        
 
35
    def test_raises_if_arg_doesnt_match(self):
 
36
        @validate_pattern('arg', 'foo')
 
37
        def func(arg):
 
38
            self.called = True
 
39
        self.assertRaises(ValidationException, func, 'bar')
 
40
        self.assertFalse(self.called)
 
41
 
 
42
    def test_match_must_be_complete(self):
 
43
        @validate_pattern('arg', 'foo')
 
44
        def func(arg):
 
45
            self.called = True
 
46
        self.assertRaises(ValidationException, func, arg='foobar')
 
47
        self.assertFalse(self.called)
 
48
 
 
49
    def test_match_success(self):
 
50
        @validate_pattern('arg', '\w{4,7}')
 
51
        def func(arg):
 
52
            self.called = True
 
53
        func(arg='foobar')
 
54
        self.assertTrue(self.called)
 
55
 
 
56
    def test_optional_can_be_omitted(self):
 
57
        @validate_pattern('arg', 'foo', required=False)
 
58
        def func(arg=None):
 
59
            self.called = True
 
60
        func()
 
61
        self.assertTrue(self.called)
 
62
 
 
63
    def test_optional_must_match_if_provided(self):
 
64
        @validate_pattern('arg', 'foo', required=False)
 
65
        def func(arg=None):
 
66
            self.called = True
 
67
        self.assertRaises(ValidationException, func, arg='bar')
 
68
        self.assertFalse(self.called)
 
69
 
 
70
class ValidateIntegerTestCase(TestCase):
 
71
    def setUp(self):
 
72
        self.called = False
 
73
 
 
74
    def test_raises_if_arg_omitted(self):
 
75
        @validate_integer('arg')
 
76
        def func(arg):
 
77
            self.called = True
 
78
        self.assertRaises(ValidationException, func)
 
79
        self.assertFalse(self.called)
 
80
 
 
81
    def test_raises_if_arg_not_named(self):
 
82
        @validate_integer('arg')
 
83
        def func(arg):
 
84
            self.called = True
 
85
        self.assertRaises(ValidationException, func, 42)
 
86
        self.assertFalse(self.called)
 
87
 
 
88
    def test_raises_if_arg_not_an_int(self):
 
89
        @validate_integer('arg')
 
90
        def func(arg):
 
91
            self.called = True
 
92
        self.assertRaises(ValidationException, func, arg='foo')
 
93
        self.assertRaises(ValidationException, func, arg=7.5)
 
94
        self.assertFalse(self.called)
 
95
 
 
96
    def test_raises_if_arg_below_min(self):
 
97
        @validate_integer('arg', min=4)
 
98
        def func(arg):
 
99
            self.called = True
 
100
        self.assertRaises(ValidationException, func, arg=2)
 
101
        self.assertFalse(self.called)
 
102
 
 
103
    def test_raises_if_arg_above_max(self):
 
104
        @validate_integer('arg', max=4)
 
105
        def func(arg):
 
106
            self.called = True
 
107
        self.assertRaises(ValidationException, func, arg=6)
 
108
        self.assertFalse(self.called)
 
109
 
 
110
    def test_optional_can_be_omitted(self):
 
111
        @validate_integer('arg', required=False)
 
112
        def func(arg=None):
 
113
            self.called = True
 
114
        func()
 
115
        self.assertTrue(self.called)
 
116
 
 
117
    def test_optional_must_validate_if_provided(self):
 
118
        @validate_integer('arg', required=False)
 
119
        def func(arg=None):
 
120
            self.called = True
 
121
        self.assertRaises(ValidationException, func, arg='bar')
 
122
        self.assertFalse(self.called)
 
123
 
 
124
    def test_validate_success(self):
 
125
        @validate_integer('arg', min=4, max=10)
 
126
        def func(arg):
 
127
            self.called = True
 
128
        func(arg=7)
 
129
        self.assertTrue(self.called)
 
130
 
 
131
 
 
132
 
 
133
 
 
134
class ValidateTestCase(TestCase):
 
135
    class SomeClass(object):
 
136
        pass
 
137
 
 
138
    def setUp(self):
 
139
        self.called = False
 
140
 
 
141
    def test_raises_if_arg_omitted(self):
 
142
        @validate('arg', self.SomeClass)
 
143
        def func(arg):
 
144
            self.called = True
 
145
        self.assertRaises(ValidationException, func)
 
146
        self.assertFalse(self.called)
 
147
 
 
148
    def test_raises_if_arg_not_named(self):
 
149
        @validate('arg', self.SomeClass)
 
150
        def func(arg):
 
151
            self.called = True
 
152
        self.assertRaises(ValidationException, func, True)
 
153
        self.assertFalse(self.called)
 
154
 
 
155
    def test_raises_if_arg_not_isinstance(self):
 
156
        @validate('arg', self.SomeClass)
 
157
        def func(arg):
 
158
            self.called = True
 
159
        self.assertRaises(ValidationException, func, arg='foo')
 
160
        self.assertRaises(ValidationException, func, arg=7.5)
 
161
        self.assertRaises(ValidationException, func, arg=1)
 
162
        self.assertFalse(self.called)
 
163
 
 
164
    def test_optional_can_be_omitted(self):
 
165
        @validate('arg', self.SomeClass, required=False)
 
166
        def func(arg=None):
 
167
            self.called = True
 
168
        func()
 
169
        self.assertTrue(self.called)
 
170
 
 
171
    def test_optional_must_validate_if_provided(self):
 
172
        @validate('arg', self.SomeClass, required=False)
 
173
        def func(arg=None):
 
174
            self.called = True
 
175
        self.assertRaises(ValidationException, func, arg='bar')
 
176
        self.assertFalse(self.called)
 
177
 
 
178
    def test_validate_success(self):
 
179
        @validate('arg', self.SomeClass)
 
180
        def func(arg):
 
181
            self.called = True
 
182
        func(arg=self.SomeClass())
 
183
        self.assertTrue(self.called)
 
184
 
 
185
 
 
186
class OAuthProtectedTestCase(TestCase):
 
187
    """ The oauth_protected decorator can only be applied to methods
 
188
        (or functions that receive a first 'self' arg), as it
 
189
        needs to check for the self._auth attribute.
 
190
        So we define a small class for each test to use here.
 
191
    """
 
192
    class MyAPI(object):
 
193
        called = False
 
194
        @oauth_protected
 
195
        def method(self):
 
196
            self.called = True
 
197
 
 
198
    def test_fail_if_no_auth(self):
 
199
        api = self.MyAPI()
 
200
        self.assertRaises(ValidationException, api.method)
 
201
        self.assertFalse(api.called)
 
202
 
 
203
    def test_fail_if_auth_is_none(self):
 
204
        api = self.MyAPI()
 
205
        api._auth = None
 
206
        self.assertRaises(ValidationException, api.method)
 
207
        self.assertFalse(api.called)
 
208
 
 
209
    def test_fail_if_auth_isnt_oauth(self):
 
210
        api = self.MyAPI()
 
211
        api._auth = BasicAuthorizer('username', 'password')
 
212
        self.assertRaises(ValidationException, api.method)
 
213
        self.assertFalse(api.called)
 
214
 
 
215
    def test_success(self):
 
216
        auth = OAuthAuthorizer('tkey', 'tsecret', 'ckey', 'csecret')
 
217
        api = self.MyAPI()
 
218
        api._auth = auth
 
219
        api.method()
 
220
        self.assertTrue(api.called)
 
221
 
 
222
    def test_success_with_subclass(self):
 
223
        class MyOAuth(OAuthAuthorizer):
 
224
            pass
 
225
        auth = MyOAuth('tkey', 'tsecret', 'ckey', 'csecret')
 
226
        api = self.MyAPI()
 
227
        api._auth = auth
 
228
        api.method()
 
229
        self.assertTrue(api.called)
 
230
 
 
231
class BasicProtectedTestCase(TestCase):
 
232
    """ The basic_protected decorator can only be applied to methods
 
233
        (or functions that receive a first 'self' arg), as it
 
234
        needs to check for the self._auth attribute.
 
235
        So we define a small class for each test to use here.
 
236
    """
 
237
    class MyAPI(object):
 
238
        called = False
 
239
        @basic_protected
 
240
        def method(self):
 
241
            self.called = True
 
242
 
 
243
    def test_fail_if_no_auth(self):
 
244
        api = self.MyAPI()
 
245
        self.assertRaises(ValidationException, api.method)
 
246
        self.assertFalse(api.called)
 
247
 
 
248
    def test_fail_if_auth_is_none(self):
 
249
        api = self.MyAPI()
 
250
        api._auth = None
 
251
        self.assertRaises(ValidationException, api.method)
 
252
        self.assertFalse(api.called)
 
253
 
 
254
    def test_fail_if_auth_isnt_basic(self):
 
255
        api = self.MyAPI()
 
256
        api._auth = OAuthAuthorizer('tkey', 'tsecret', 'ckey', 'csecret')
 
257
        self.assertRaises(ValidationException, api.method)
 
258
        self.assertFalse(api.called)
 
259
 
 
260
    def test_success(self):
 
261
        auth = BasicAuthorizer('username', 'password')
 
262
        api = self.MyAPI()
 
263
        api._auth = auth
 
264
        api.method()
 
265
        self.assertTrue(api.called)
 
266
 
 
267
    def test_success_with_subclass(self):
 
268
        class MyBasic(BasicAuthorizer):
 
269
            pass
 
270
        auth = MyBasic('username', 'password')
 
271
        api = self.MyAPI()
 
272
        api._auth = auth
 
273
        api.method()
 
274
        self.assertTrue(api.called)
 
275