Completed
Push — master ( cbf8b1...ab8c51 )
by Koen
03:27
created

TestSettings.test_includeme_no_capakey_auth_warning()   A

Complexity

Conditions 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
dl 0
loc 7
rs 9.4285
c 1
b 0
f 0
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 CapakeyRestGateway
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.cache.file.root': './dogpile_data/',
36
                'crabpy.capakey.cache_config.permanent.backend': 'dogpile.cache.dbm',
37
                'crabpy.capakey.cache_config.permanent.expiration_time': 604800,
38
                'crabpy.capakey.cache_config.permanent.arguments.filename': 'dogpile_data/capakey_permanent.dbm',
39
                'crabpy.capakey.cache_config.long.backend': 'dogpile.cache.dbm',
40
                'crabpy.capakey.cache_config.long.expiration_time': 86400,
41
                'crabpy.capakey.cache_config.long.arguments.filename': 'dogpile_data/capakey_long.dbm',
42
                'crabpy.capakey.cache_config.short.backend': 'dogpile.cache.dbm',
43
                'crabpy.capakey.cache_config.short.expiration_time': 3600,
44
                'crabpy.capakey.cache_config.short.arguments.filename': 'dogpile_data/capakey_short.dbm',
45
                'crabpy.crab.include': True,
46
                'crabpy.crab.cache_config.permanent.backend': 'dogpile.cache.dbm',
47
                'crabpy.crab.cache_config.permanent.expiration_time': 604800,
48
                'crabpy.crab.cache_config.permanent.arguments.filename': 'dogpile_data/crab_permanent.dbm',
49
                'crabpy.crab.cache_config.long.backend': 'dogpile.cache.dbm',
50
                'crabpy.crab.cache_config.long.expiration_time': 86400,
51
                'crabpy.crab.cache_config.long.arguments.filename': 'dogpile_data/crab_long.dbm',
52
                'crabpy.crab.cache_config.short.backend': 'dogpile.cache.dbm',
53
                'crabpy.crab.cache_config.short.expiration_time': 3600,
54
                'crabpy.crab.cache_config.short.arguments.filename': 'dogpile_data/crab_short.dbm'
55
            }
56
        )
57
58
    def tearDown(self):
59
        del self.config
60
61
    def test_filter_settings(self):
62
        settings = _filter_settings(
63
            {
64
                'cache.file.root' : '/tmp',
65
                'crab.include': True,
66
                'capakey.include': False,
67
            },
68
            'capakey.'
69
        )
70
        self.assertEquals(1, len(settings))
71
        self.assertFalse(settings['include'])
72
        self.assertNotIn('cache.file.root', settings)
73
74
    def test_filter_settings_with_proxy(self):
75
        settings = {
76
            'proxy.http' : 'http://proxy.example.com:3128',
77
            'proxy.https' : 'https://httpsproxy.example.com:3128',
78
            'crab.include': True,
79
            'crab.cache_config.permanent.backend': 'dogpile.cache.dbm',
80
        }
81
        base_settings = _get_proxy_settings(settings)
82
        crab_settings = dict(_filter_settings(settings, 'crab.'), **base_settings)
83
        self.assertIn('proxy', crab_settings)
84
        self.assertIn('http', crab_settings["proxy"])
85
        self.assertIn('https', crab_settings["proxy"])
86
87
    def test_empty_proxy_settings(self):
88
        settings = {
89
            'proxy.http' : '',
90
            'proxy.https' : '',
91
        }
92
        base_settings = _get_proxy_settings(settings)
93
        self.assertNotIn('proxy', base_settings)
94
95
    def test_includeme_existing_root(self):
96
        includeme(self.config)
97
        capakey = self.config.registry.queryUtility(ICapakey)
98
        self.assertIsInstance(capakey, CapakeyRestGateway)
99
        crab = self.config.registry.queryUtility(ICrab)
100
        self.assertIsInstance(crab, CrabGateway)
101
102
    def test_includeme_nonexisting_root(self):
103
        root = './testdir/'
104
        self.config.registry.settings['crabpy.cache.file.root'] = root
105
        includeme(self.config)
106
        capakey = self.config.registry.queryUtility(ICapakey)
107
        self.assertIsInstance(capakey, CapakeyRestGateway)
108
        crab = self.config.registry.queryUtility(ICrab)
109
        self.assertIsInstance(crab, CrabGateway)
110
        os.rmdir(root)
111
112
    def test_directive_was_added(self):
113
        includeme(self.config)
114
        r = self.config.registry.settings
115
        self.assertEqual('dogpile.cache.dbm', r['crabpy.capakey.cache_config.permanent.backend'])
116