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).
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
10
class ValidatePatternTestCase(TestCase):
14
def test_raises_if_arg_omitted(self):
15
@validate_pattern('arg', 'foo')
18
self.assertRaises(ValidationException, func)
19
self.assertFalse(self.called)
21
def test_raises_if_arg_not_named(self):
22
@validate_pattern('arg', 'foo')
25
self.assertRaises(ValidationException, func, 'foo')
26
self.assertFalse(self.called)
28
def test_raises_if_arg_not_a_string(self):
29
@validate_pattern('arg', r'\d+')
32
self.assertRaises(ValidationException, func, arg=42)
33
self.assertFalse(self.called)
35
def test_raises_if_arg_doesnt_match(self):
36
@validate_pattern('arg', 'foo')
39
self.assertRaises(ValidationException, func, 'bar')
40
self.assertFalse(self.called)
42
def test_match_must_be_complete(self):
43
@validate_pattern('arg', 'foo')
46
self.assertRaises(ValidationException, func, arg='foobar')
47
self.assertFalse(self.called)
49
def test_match_success(self):
50
@validate_pattern('arg', '\w{4,7}')
54
self.assertTrue(self.called)
56
def test_optional_can_be_omitted(self):
57
@validate_pattern('arg', 'foo', required=False)
61
self.assertTrue(self.called)
63
def test_optional_must_match_if_provided(self):
64
@validate_pattern('arg', 'foo', required=False)
67
self.assertRaises(ValidationException, func, arg='bar')
68
self.assertFalse(self.called)
70
class ValidateIntegerTestCase(TestCase):
74
def test_raises_if_arg_omitted(self):
75
@validate_integer('arg')
78
self.assertRaises(ValidationException, func)
79
self.assertFalse(self.called)
81
def test_raises_if_arg_not_named(self):
82
@validate_integer('arg')
85
self.assertRaises(ValidationException, func, 42)
86
self.assertFalse(self.called)
88
def test_raises_if_arg_not_an_int(self):
89
@validate_integer('arg')
92
self.assertRaises(ValidationException, func, arg='foo')
93
self.assertRaises(ValidationException, func, arg=7.5)
94
self.assertFalse(self.called)
96
def test_raises_if_arg_below_min(self):
97
@validate_integer('arg', min=4)
100
self.assertRaises(ValidationException, func, arg=2)
101
self.assertFalse(self.called)
103
def test_raises_if_arg_above_max(self):
104
@validate_integer('arg', max=4)
107
self.assertRaises(ValidationException, func, arg=6)
108
self.assertFalse(self.called)
110
def test_optional_can_be_omitted(self):
111
@validate_integer('arg', required=False)
115
self.assertTrue(self.called)
117
def test_optional_must_validate_if_provided(self):
118
@validate_integer('arg', required=False)
121
self.assertRaises(ValidationException, func, arg='bar')
122
self.assertFalse(self.called)
124
def test_validate_success(self):
125
@validate_integer('arg', min=4, max=10)
129
self.assertTrue(self.called)
134
class ValidateTestCase(TestCase):
135
class SomeClass(object):
141
def test_raises_if_arg_omitted(self):
142
@validate('arg', self.SomeClass)
145
self.assertRaises(ValidationException, func)
146
self.assertFalse(self.called)
148
def test_raises_if_arg_not_named(self):
149
@validate('arg', self.SomeClass)
152
self.assertRaises(ValidationException, func, True)
153
self.assertFalse(self.called)
155
def test_raises_if_arg_not_isinstance(self):
156
@validate('arg', self.SomeClass)
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)
164
def test_optional_can_be_omitted(self):
165
@validate('arg', self.SomeClass, required=False)
169
self.assertTrue(self.called)
171
def test_optional_must_validate_if_provided(self):
172
@validate('arg', self.SomeClass, required=False)
175
self.assertRaises(ValidationException, func, arg='bar')
176
self.assertFalse(self.called)
178
def test_validate_success(self):
179
@validate('arg', self.SomeClass)
182
func(arg=self.SomeClass())
183
self.assertTrue(self.called)
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.
198
def test_fail_if_no_auth(self):
200
self.assertRaises(ValidationException, api.method)
201
self.assertFalse(api.called)
203
def test_fail_if_auth_is_none(self):
206
self.assertRaises(ValidationException, api.method)
207
self.assertFalse(api.called)
209
def test_fail_if_auth_isnt_oauth(self):
211
api._auth = BasicAuthorizer('username', 'password')
212
self.assertRaises(ValidationException, api.method)
213
self.assertFalse(api.called)
215
def test_success(self):
216
auth = OAuthAuthorizer('tkey', 'tsecret', 'ckey', 'csecret')
220
self.assertTrue(api.called)
222
def test_success_with_subclass(self):
223
class MyOAuth(OAuthAuthorizer):
225
auth = MyOAuth('tkey', 'tsecret', 'ckey', 'csecret')
229
self.assertTrue(api.called)
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.
243
def test_fail_if_no_auth(self):
245
self.assertRaises(ValidationException, api.method)
246
self.assertFalse(api.called)
248
def test_fail_if_auth_is_none(self):
251
self.assertRaises(ValidationException, api.method)
252
self.assertFalse(api.called)
254
def test_fail_if_auth_isnt_basic(self):
256
api._auth = OAuthAuthorizer('tkey', 'tsecret', 'ckey', 'csecret')
257
self.assertRaises(ValidationException, api.method)
258
self.assertFalse(api.called)
260
def test_success(self):
261
auth = BasicAuthorizer('username', 'password')
265
self.assertTrue(api.called)
267
def test_success_with_subclass(self):
268
class MyBasic(BasicAuthorizer):
270
auth = MyBasic('username', 'password')
274
self.assertTrue(api.called)