1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
''' |
3
|
|
|
Testing of the initialization. |
4
|
|
|
.. versionadded:: 0.1.0 |
5
|
|
|
''' |
6
|
|
|
|
7
|
|
|
from pyramid import testing |
8
|
|
|
from crabpy.gateway.capakey import CapakeyGateway |
9
|
|
|
from crabpy.gateway.crab import CrabGateway |
10
|
|
|
import os |
11
|
|
|
import warnings |
12
|
|
|
|
13
|
|
|
from crabpy_pyramid import ( |
14
|
|
|
includeme, |
15
|
|
|
ICapakey, |
16
|
|
|
ICrab, |
17
|
|
|
_filter_settings, |
18
|
|
|
_get_proxy_settings |
19
|
|
|
) |
20
|
|
|
|
21
|
|
|
import warnings |
22
|
|
|
|
23
|
|
|
try: |
24
|
|
|
import unittest2 as unittest |
25
|
|
|
except ImportError: |
26
|
|
|
import unittest # noqa |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
class TestSettings(unittest.TestCase): |
30
|
|
|
|
31
|
|
|
def setUp(self): |
32
|
|
|
self.config = testing.setUp( |
33
|
|
|
settings = { |
34
|
|
|
'crabpy.capakey.include': True, |
35
|
|
|
'crabpy.capakey.user': 'Talissa', |
36
|
|
|
'crabpy.capakey.password': 'TalissaWachtwoord', |
37
|
|
|
'crabpy.cache.file.root': './dogpile_data/', |
38
|
|
|
'crabpy.capakey.cache_config.permanent.backend': 'dogpile.cache.dbm', |
39
|
|
|
'crabpy.capakey.cache_config.permanent.expiration_time': 604800, |
40
|
|
|
'crabpy.capakey.cache_config.permanent.arguments.filename': 'dogpile_data/capakey_permanent.dbm', |
41
|
|
|
'crabpy.capakey.cache_config.long.backend': 'dogpile.cache.dbm', |
42
|
|
|
'crabpy.capakey.cache_config.long.expiration_time': 86400, |
43
|
|
|
'crabpy.capakey.cache_config.long.arguments.filename': 'dogpile_data/capakey_long.dbm', |
44
|
|
|
'crabpy.capakey.cache_config.short.backend': 'dogpile.cache.dbm', |
45
|
|
|
'crabpy.capakey.cache_config.short.expiration_time': 3600, |
46
|
|
|
'crabpy.capakey.cache_config.short.arguments.filename': 'dogpile_data/capakey_short.dbm', |
47
|
|
|
'crabpy.crab.include': True, |
48
|
|
|
'crabpy.crab.cache_config.permanent.backend': 'dogpile.cache.dbm', |
49
|
|
|
'crabpy.crab.cache_config.permanent.expiration_time': 604800, |
50
|
|
|
'crabpy.crab.cache_config.permanent.arguments.filename': 'dogpile_data/crab_permanent.dbm', |
51
|
|
|
'crabpy.crab.cache_config.long.backend': 'dogpile.cache.dbm', |
52
|
|
|
'crabpy.crab.cache_config.long.expiration_time': 86400, |
53
|
|
|
'crabpy.crab.cache_config.long.arguments.filename': 'dogpile_data/crab_long.dbm', |
54
|
|
|
'crabpy.crab.cache_config.short.backend': 'dogpile.cache.dbm', |
55
|
|
|
'crabpy.crab.cache_config.short.expiration_time': 3600, |
56
|
|
|
'crabpy.crab.cache_config.short.arguments.filename': 'dogpile_data/crab_short.dbm' |
57
|
|
|
} |
58
|
|
|
) |
59
|
|
|
|
60
|
|
|
def tearDown(self): |
61
|
|
|
del self.config |
62
|
|
|
|
63
|
|
|
def test_filter_settings(self): |
64
|
|
|
settings = _filter_settings( |
65
|
|
|
{ |
66
|
|
|
'cache.file.root' : '/tmp', |
67
|
|
|
'crab.include': True, |
68
|
|
|
'capakey.include': False, |
69
|
|
|
}, |
70
|
|
|
'capakey.' |
71
|
|
|
) |
72
|
|
|
self.assertEquals(1, len(settings)) |
73
|
|
|
self.assertFalse(settings['include']) |
74
|
|
|
self.assertNotIn('cache.file.root', settings) |
75
|
|
|
|
76
|
|
|
def test_filter_settings_with_proxy(self): |
77
|
|
|
settings = { |
78
|
|
|
'proxy.http' : 'http://proxy.example.com:3128', |
79
|
|
|
'proxy.https' : 'https://httpsproxy.example.com:3128', |
80
|
|
|
'crab.include': True, |
81
|
|
|
'crab.cache_config.permanent.backend': 'dogpile.cache.dbm', |
82
|
|
|
} |
83
|
|
|
base_settings = _get_proxy_settings(settings) |
84
|
|
|
crab_settings = dict(_filter_settings(settings, 'crab.'), **base_settings) |
85
|
|
|
self.assertIn('proxy', crab_settings) |
86
|
|
|
self.assertIn('http', crab_settings["proxy"]) |
87
|
|
|
self.assertIn('https', crab_settings["proxy"]) |
88
|
|
|
|
89
|
|
|
def test_empty_proxy_settings(self): |
90
|
|
|
settings = { |
91
|
|
|
'proxy.http' : '', |
92
|
|
|
'proxy.https' : '', |
93
|
|
|
} |
94
|
|
|
base_settings = _get_proxy_settings(settings) |
95
|
|
|
self.assertNotIn('proxy', base_settings) |
96
|
|
|
|
97
|
|
|
def test_includeme_existing_root(self): |
98
|
|
|
includeme(self.config) |
99
|
|
|
capakey = self.config.registry.queryUtility(ICapakey) |
100
|
|
|
self.assertIsInstance(capakey, CapakeyGateway) |
101
|
|
|
crab = self.config.registry.queryUtility(ICrab) |
102
|
|
|
self.assertIsInstance(crab, CrabGateway) |
103
|
|
|
|
104
|
|
|
def test_includeme_nonexisting_root(self): |
105
|
|
|
root = './testdir/' |
106
|
|
|
self.config.registry.settings['crabpy.cache.file.root'] = root |
107
|
|
|
includeme(self.config) |
108
|
|
|
capakey = self.config.registry.queryUtility(ICapakey) |
109
|
|
|
self.assertIsInstance(capakey, CapakeyGateway) |
110
|
|
|
crab = self.config.registry.queryUtility(ICrab) |
111
|
|
|
self.assertIsInstance(crab, CrabGateway) |
112
|
|
|
os.rmdir(root) |
113
|
|
|
|
114
|
|
|
def test_directive_was_added(self): |
115
|
|
|
includeme(self.config) |
116
|
|
|
r = self.config.registry.settings |
117
|
|
|
self.assertEqual('Talissa', r['crabpy.capakey.user']) |
118
|
|
|
|
119
|
|
|
def test_includeme_no_capakey_auth_warning(self): |
120
|
|
|
with warnings.catch_warnings(record=True) as w: |
121
|
|
|
warnings.simplefilter('always') |
122
|
|
|
del self.config.registry.settings['crabpy.capakey.user'] |
123
|
|
|
del self.config.registry.settings['crabpy.capakey.password'] |
124
|
|
|
includeme(self.config) |
125
|
|
|
self.assertGreater(len(w), 0) |
126
|
|
|
|