test_url_schema_node_not_rfc3987_compliant()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
1
# -*- coding: utf-8 -*-
2
import os
3
import unittest
4
5
import colander
6
import responses
7
from cornice import Service
8
from sqlalchemy import engine_from_config
9
from sqlalchemy.orm import sessionmaker
10
11
from oe_utils.validation import sqlalchemy_validator, OEIDSchemaNode, OEEmailSchemaNode, OEBoolSchemaNode, UrlSchemaNode, UriSchemaNode, ExternalUriSchemaNode
12
from tests import init_test_db, DummyDossier, DummySchema, init_validation_responses
13
14
from zope.sqlalchemy import ZopeTransactionExtension
15
from pyramid.testing import DummyRequest
16
17
try:
18
    from unittest.mock import Mock, patch, MagicMock
19
except ImportError:
20
    from mock import Mock, patch, MagicMock
21
try:
22
    import configparser
23
except ImportError:
24
    import ConfigParser as configparser
25
26
service = Service('dummy_service', path='/test')
27
28
29
class ValdiationTests(unittest.TestCase):
30 View Code Duplication
    @classmethod
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
31
    def setUpClass(cls):
32
        config = configparser.ConfigParser()
33
        config.read(os.path.join(os.path.dirname(__file__), 'test.ini'))
34
        settings = config.items('app:oe_utils')
35
        settings = dict((s[0], s[1]) for s in settings)
36
        cls.engine = engine_from_config(settings, prefix='sqlalchemy.')
37
        cls.session_maker = sessionmaker(
38
            bind=cls.engine,
39
            extension=ZopeTransactionExtension()
40
        )
41
        init_test_db(cls.engine)
42
43
    def setUp(self):
44
        self.session = self.session_maker()
45
        self.request = DummyRequest()
46
        init_validation_responses()
47
48
        mapping_strict = colander.Mapping(unknown='raise')
49
50
        self.test_oe_id_schema = TestOEIDSchema(typ=mapping_strict).bind(request=self.request)
51
        self.test_oe_email_schema = TestOEEmailSchema(typ=mapping_strict).bind(request=self.request)
52
        self.test_oe_bool_schema = TestOEBoolSchema(typ=mapping_strict).bind(request=self.request)
53
        self.test_url_schema = TestUrlSchema(typ=mapping_strict).bind(request=self.request)
54
        self.test_uri_schema = TestUriSchema(typ=mapping_strict).bind(request=self.request)
55
        self.test_external_uri_schema = TestExternalUriSchema(typ=mapping_strict).bind(request=self.request)
56
57
    def tearDown(self):
58
        self.session.rollback()
59
        self.session.close()
60
61
    def test_sqlalchemy_validator_invalid(self):
62
        with self.assertRaises(colander.Invalid) as e:
63
            sqlalchemy_validator(DummySchema, 987654321, DummyDossier, self.session,
64
                             '%s is geen geldig dummydossier id')
65
66
            self.assertIn('987654321 is geen geldig dummydossier id', e.exception.__str__())
67
68
    def test_sqlalchemy_validator(self):
69
        result = sqlalchemy_validator(DummySchema, 1, DummyDossier, self.session,
70
                             '%s is geen geldig dummydossier id')
71
        self.assertIsInstance(result, DummyDossier)
72
73
    def test_oe_id_schema_node_invalid(self):
74
        data = {'test': -5}
75
        with self.assertRaises(colander.Invalid) as e:
76
            self.test_oe_id_schema.deserialize(data)
77
        self.assertIn('Geen geldig ID (-5< 0)', e.exception.__str__())
78
79
    def test_oe_id_schema_node_no_int(self):
80
        data = {'test': 'test'}
81
        with self.assertRaises(colander.Invalid) as e:
82
            self.test_oe_id_schema.deserialize(data)
83
        self.assertIn('is not a valid Integer.', e.exception.__str__())
84
85
    def test_oe_id_schema_node(self):
86
        data = {'test': 5}
87
        self.test_oe_id_schema.deserialize(data)
88
89
    def test_oe_email_schema_node_invalid(self):
90
        data = {'test': 'x'*256}
91
        with self.assertRaises(colander.Invalid) as e:
92
            self.test_oe_email_schema.deserialize(data)
93
        self.assertIn('Dit is geen geldig emailadres.', e.exception.__str__())
94
95
    def test_oe_email_schema_node(self):
96
        data = {'test': '[email protected]'}
97
        self.test_oe_email_schema.deserialize(data)
98
99
    def test_oe_email_schema_node_invalid(self):
100
        data = {'test': 'x'*256}
101
        with self.assertRaises(colander.Invalid) as e:
102
            self.test_oe_email_schema.deserialize(data)
103
        self.assertIn('Dit is geen geldig emailadres.', e.exception.__str__())
104
105
    def test_oe_bool_schema_node_no_bool(self):
106
        data = {'test': 'test'}
107
        with self.assertRaises(colander.Invalid) as e:
108
            self.test_oe_bool_schema.deserialize(data)
109
        self.assertIn('is not a valid Boolean.', e.exception.__str__())
110
111
    def test_url_schema_node_to_long(self):
112
        data = {'test': 'a' * 2084}
113
        with self.assertRaises(colander.Invalid) as e:
114
            self.test_url_schema.deserialize(data)
115
        self.assertIn('URI is too long (max=2083).', e.exception.__str__())
116
117
    def test_url_schema_node_not_rfc3987_compliant(self):
118
        data = {'test': 'https://[email protected] test'}
119
        with self.assertRaises(colander.Invalid) as e:
120
            self.test_url_schema.deserialize(data)
121
        self.assertIn('https://[email protected] test is geen geldige URL.', e.exception.__str__())
122
123
    def test_url_schema_node_not_existing(self):
124
        data = {'test': 'https://id.erfgoed.net/inventaris/teksten/987654321'}
125
        with self.assertRaises(colander.Invalid) as e:
126
            self.test_url_schema.deserialize(data)
127
        self.assertIn('URL bestaat niet.', e.exception.__str__())
128
129
    @responses.activate
130
    def test_url_schema_node(self):
131
        data = {'test': 'https://id.erfgoed.net/inventaris/teksten/1'}
132
        self.test_url_schema.deserialize(data)
133
134
    def test_uri_schema_node_to_long(self):
135
        data = {'test': 'a' * 101}
136
        with self.assertRaises(colander.Invalid) as e:
137
            self.test_uri_schema.deserialize(data)
138
        self.assertIn('URI is too long (max=100).', e.exception.__str__())
139
140
    def test_uri_schema_node_not_rfc3987_compliable(self):
141
        data = {'test': 'https://[email protected] test'}
142
        with self.assertRaises(colander.Invalid) as e:
143
            self.test_uri_schema.deserialize(data)
144
        self.assertIn('https://[email protected] test is geen geldige URI.', e.exception.__str__())
145
146
    def test_extarnal_uri_schema_node_not_existing(self):
147
        data = {'test': 'https://id.erfgoed.net/inventaris/teksten/987654321'}
148
        with self.assertRaises(colander.Invalid) as e:
149
            self.test_external_uri_schema.deserialize(data)
150
        self.assertIn('URI bestaat niet.', e.exception.__str__())
151
152
    @responses.activate
153
    def test_extarnal_uri_schema_node(self):
154
        data = {'test': 'https://id.erfgoed.net/inventaris/teksten/1'}
155
        self.test_external_uri_schema.deserialize(data)
156
157
158
# Helper test classes
159
class TestOEIDSchema(colander.MappingSchema):
160
    test = OEIDSchemaNode()
161
162
163
class TestOEEmailSchema(colander.MappingSchema):
164
    test = OEEmailSchemaNode()
165
166
167
class TestOEBoolSchema(colander.MappingSchema):
168
    test = OEBoolSchemaNode()
169
170
171
class TestUrlSchema(colander.MappingSchema):
172
    test = UrlSchemaNode()
173
174
175
class TestUriSchema(colander.MappingSchema):
176
    test = UriSchemaNode()
177
178
179
class TestExternalUriSchema(colander.MappingSchema):
180
    test = ExternalUriSchemaNode()