summaryrefslogtreecommitdiff
path: root/nova/tests/api/test_validator.py
blob: eec09c3a064442e13e3e67c66a3c35f8f5518b59 (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
# Copyright 2011 Cloudscaling, Inc.
# Author: Matthew Hooker <matt@cloudscaling.com>
# All Rights Reserved.
#
#    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 base64

from nova.api import validator
from nova import test


class ValidatorTestCase(test.NoDBTestCase):

    def test_validate(self):
        fixture = {
            'foo': lambda val: val is True
        }

        self.assertTrue(
            validator.validate({'foo': True}, fixture))
        self.assertFalse(
            validator.validate({'foo': False}, fixture))

    def test_only_tests_intersect(self):
        """Test that validator.validate only tests the intersect of keys
        from args and validator.
        """

        fixture = {
            'foo': lambda val: True,
            'bar': lambda val: True
        }

        self.assertTrue(
            validator.validate({'foo': True}, fixture))
        self.assertTrue(
            validator.validate({'foo': True, 'bar': True}, fixture))
        self.assertTrue(
            validator.validate({'foo': True, 'bar': True, 'baz': True},
                               fixture))

    def test_validate_str(self):
        self.assertTrue(validator.validate_str()('foo'))
        self.assertFalse(validator.validate_str()(1))
        self.assertTrue(validator.validate_str(4)('foo'))
        self.assertFalse(validator.validate_str(2)('foo'))
        self.assertFalse(validator.validate_str()(None))
        self.assertTrue(validator.validate_str()(u'foo'))

    def test_validate_int(self):
        self.assertTrue(validator.validate_int()(1))
        self.assertFalse(validator.validate_int()('foo'))
        self.assertTrue(validator.validate_int(100)(1))
        self.assertFalse(validator.validate_int(4)(5))
        self.assertFalse(validator.validate_int()(None))

    def test_validate_ec2_id(self):
        self.assertFalse(validator.validate_ec2_id('foobar'))
        self.assertFalse(validator.validate_ec2_id(''))
        self.assertFalse(validator.validate_ec2_id(1234))
        self.assertTrue(validator.validate_ec2_id('i-284f3a41'))

    def test_validate_url_path(self):
        self.assertTrue(validator.validate_url_path('/path/to/file'))
        self.assertFalse(validator.validate_url_path('path/to/file'))
        self.assertFalse(
            validator.validate_url_path('#this is not a path!@#$%^&*()')
        )
        self.assertFalse(validator.validate_url_path(None))
        self.assertFalse(validator.validate_url_path(123))

    def test_validate_image_path(self):
        self.assertTrue(validator.validate_image_path('path/to/file'))
        self.assertFalse(validator.validate_image_path('/path/to/file'))
        self.assertFalse(validator.validate_image_path('path'))

    def test_validate_user_data(self):
        fixture = base64.b64encode('foo')
        self.assertTrue(validator.validate_user_data(fixture))
        self.assertFalse(validator.validate_user_data(False))
        self.assertFalse(validator.validate_user_data('hello, world!'))

    def test_default_validator(self):
        expect_pass = {
            'attribute': 'foobar'
        }
        self.assertTrue(validator.validate(expect_pass,
                                           validator.DEFAULT_VALIDATOR))
        expect_fail = {
            'attribute': 0
        }
        self.assertFalse(validator.validate(expect_fail,
                                            validator.DEFAULT_VALIDATOR))