cubicweb/pyramid/test/test_config.py
changeset 12053 c3c9f2e1424c
child 12562 7bb677060ebd
equal deleted inserted replaced
12052:1a1d2f5faddb 12053:c3c9f2e1424c
       
     1 # copyright 2017 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 """Tests for cubicweb.pyramid.config module."""
       
    19 
       
    20 import os
       
    21 from os import path
       
    22 from unittest import TestCase
       
    23 
       
    24 from mock import patch
       
    25 
       
    26 from cubicweb.devtools.testlib import TemporaryDirectory
       
    27 
       
    28 from cubicweb.pyramid import config
       
    29 
       
    30 
       
    31 class PyramidConfigTC(TestCase):
       
    32 
       
    33     def test_get_random_secret_key(self):
       
    34         with patch('random.choice', return_value='0') as patched_choice:
       
    35             secret = config.get_random_secret_key()
       
    36         self.assertEqual(patched_choice.call_count, 50)
       
    37         self.assertEqual(secret, '0' * 50)
       
    38 
       
    39     def test_write_development_ini(self):
       
    40         with TemporaryDirectory() as instancedir:
       
    41             appid = 'pyramid-instance'
       
    42             os.makedirs(path.join(instancedir, appid))
       
    43             os.environ['CW_INSTANCES_DIR'] = instancedir
       
    44             try:
       
    45                 cfg = config.CubicWebPyramidConfiguration(appid)
       
    46                 with patch('random.choice', return_value='0') as patched_choice:
       
    47                     cfg.write_development_ini(['foo', 'bar'])
       
    48             finally:
       
    49                 os.environ.pop('CW_INSTANCES_DIR')
       
    50             with open(path.join(instancedir, appid, 'development.ini')) as f:
       
    51                 lines = f.readlines()
       
    52         self.assertEqual(patched_choice.call_count, 50 * 3)
       
    53         secret = '0' * 50
       
    54         for option in ('cubicweb.session.secret',
       
    55                        'cubicweb.auth.authtkt.persistent.secret',
       
    56                        'cubicweb.auth.authtkt.session.secret'):
       
    57             self.assertIn('{} = {}\n'.format(option, secret), lines)
       
    58         self.assertIn('cubicweb.instance = {}\n'.format(appid), lines)
       
    59         self.assertIn('    cubicweb_foo\n', lines)
       
    60 
       
    61 
       
    62 if __name__ == '__main__':
       
    63     import unittest
       
    64     unittest.main()