"Fossies" - the Fresh Open Source Software Archive

Member "LinOTP-release-2.11/linotpd/src/linotp/tests/unit/lib/test_chunk_config.py" (12 Nov 2019, 17101 Bytes) of package /linux/misc/LinOTP-release-2.11.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # -*- coding: utf-8 -*-
    2 #
    3 #    LinOTP - the open source solution for two factor authentication
    4 #    Copyright (C) 2010 - 2019 KeyIdentity GmbH
    5 #
    6 #    This file is part of LinOTP server.
    7 #
    8 #    This program is free software: you can redistribute it and/or
    9 #    modify it under the terms of the GNU Affero General Public
   10 #    License, version 3, as published by the Free Software Foundation.
   11 #
   12 #    This program is distributed in the hope that it will be useful,
   13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
   14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15 #    GNU Affero General Public License for more details.
   16 #
   17 #    You should have received a copy of the
   18 #               GNU Affero General Public License
   19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
   20 #
   21 #
   22 #    E-mail: linotp@keyidentity.com
   23 #    Contact: www.linotp.org
   24 #    Support: www.keyidentity.com
   25 #
   26 """
   27 Tests the chunked data handling in the config
   28 """
   29 
   30 
   31 import unittest
   32 
   33 from mock import patch
   34 
   35 from linotp.lib.config.db_api import _store_continous_entry_db, _storeConfigDB,\
   36     _retrieveConfigDB
   37 from linotp.model import init_model
   38 from linotp.model.meta import metadata, Session
   39 from linotp.model import Config
   40 from sqlalchemy.engine import create_engine
   41 
   42 big_value = """-----BEGIN CERTIFICATE-----
   43 MIIGlTCCBH2gAwIBAgIED////zANBgkqhkiG9w0BAQsFADBaMQswCQYDVQQGEwJO
   44 TDEXMBUGA1UECgwORGlnaU5vdGFyIEIuVi4xMjAwBgNVBAMMKURpZ2lOb3RhciBQ
   45 S0lvdmVyaGVpZCBDQSBPcmdhbmlzYXRpZSAtIEcyMB4X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7sNpaIxmCJ+fHzn2qSt7LcuGhlx+EhQ2kOPLITdwUAn47XwFU6W1Phc
  141 44YpRivXOwfrOH/IhleX0+8qM8QXUNVkaWsra0VeXS8XylpOz8PXOTz1O59GuZvn
  142 DkmXndbV4xsP6o8BTpoTlFkKAgdISxpgq39P7QvYVQ1ob1WcaWUVQuzA3N1srMMW
  143 zgsdVpukxMTSLuAP4kQnK1BppNxi6IohKUJszAA6lnab70DApF53hDJsJio5Zq5d
  144 47m5sixoHx6akAM58KqzpMxJixg06TfJeynHhHxvRBUv7GFZBMlFy6LWUqJ8fymS
  145 1krFi0Ko1P7q2MeHIxjknXp9c0BSmKCubuMFPwUP4KXGbU3tgzeInMfz3EKaarbX
  146 IUk2d/LvGE/FcNme6d63K4v0vH4o3w1AyYVcrp3FMf/QXA61qH7w6S+6r4iu5bXR
  147 WKWvnHGnKQGQg2k3ggW6/AnBCG6MeDvDMwKAP0SFCB3fVVYIrSyFLV2xA+GuqnTF
  148 pPNOujeYe4K5
  149 -----END CERTIFICATE-----"""
  150 
  151 TestConfigEntries = {}
  152 
  153 
  154 def storeConfigEntryDB(key, val, typ=None, desc=None):
  155 
  156     TestConfigEntries[key] = {'type': typ,
  157                               'value': val,
  158                               'desc': desc}
  159 
  160 
  161 class ContEntries(object):
  162     """
  163     mock class for db config entries
  164     """
  165 
  166     def delete(self, synchronize_session=False):
  167         return None
  168 
  169     def count(self):
  170         return 0
  171 
  172     def __iter__(self):
  173         return iter([])
  174 
  175 
  176 class TestChunkConfigCase(unittest.TestCase):
  177 
  178     @patch('linotp.lib.config.db_api._storeConfigEntryDB', storeConfigEntryDB)
  179     def test_chunked_config(self):
  180         """
  181         test for storing long values
  182         """
  183 
  184         from linotp.lib.text_utils import simple_slice
  185         from linotp.lib.config.db_api import MAX_VALUE_LEN
  186 
  187         key_name = 'linotp.chunk_test'
  188         key_type = 'text'
  189         key_desc = 'description'
  190 
  191         chunks = []
  192         for cont_value in simple_slice(big_value, MAX_VALUE_LEN):
  193             chunks.append(cont_value)
  194 
  195         _store_continous_entry_db(chunks, key=key_name, val=big_value,
  196                                   typ=key_type, desc=key_desc)
  197 
  198         conf_keys = TestConfigEntries.keys()
  199 
  200         # ------------------------------------------------------------------ --
  201 
  202         # make sure that the first key entry 'test' is avaliable
  203         # and that the keys are representing the calculated number
  204 
  205         self.assertTrue(key_name in conf_keys, TestConfigEntries)
  206 
  207         entry = TestConfigEntries[key_name]
  208         value = entry['value']
  209         from_, to_ = entry['desc'].split(':')
  210 
  211         # we count from 0 to eg 3 so we have 4 entries
  212         self.assertTrue(len(conf_keys) == int(to_) + 1, conf_keys)
  213 
  214         # ------------------------------------------------------------------ --
  215 
  216         # check that all entries have the extended key format
  217 
  218         for i in range(int(from_) + 1, int(to_) + 1):
  219             entry_key = "%s__[%d:%d]" % (key_name, i, int(to_))
  220             self.assertTrue(entry_key in TestConfigEntries)
  221 
  222             value += TestConfigEntries[entry_key]['value']
  223 
  224         self.assertTrue(value == big_value)
  225 
  226         # finally we check if the original type and description is in the
  227         # last entry
  228 
  229         entry_key = "%s__[%d:%d]" % (key_name, int(to_), int(to_))
  230         entry_type = TestConfigEntries[entry_key]['type']
  231         entry_desc = TestConfigEntries[entry_key]['desc']
  232 
  233         self.assertTrue(entry_type == key_type)
  234         self.assertTrue(entry_desc == key_desc)
  235 
  236         # --------------------------------------------------------------------
  237 
  238         # cleanup the shared dictionary
  239 
  240         for key in TestConfigEntries.keys():
  241             del TestConfigEntries[key]
  242 
  243         return
  244 
  245     @patch('linotp.lib.config.db_api._storeConfigEntryDB', storeConfigEntryDB)
  246     @patch('linotp.lib.config.db_api.Session')
  247     def test__storeConfigDB_text(self, mock_session):
  248         """
  249         test for storing long text entries
  250         """
  251 
  252         key = 'linotp.test_data'
  253         val = big_value
  254         typ = 'text'
  255         desc = None
  256 
  257         continous_entries = ContEntries()
  258 
  259         mock_session.query.return_value.filter.return_value = continous_entries
  260         _storeConfigDB(key, val, typ=typ, desc=desc)
  261 
  262         conf_keys = TestConfigEntries.keys()
  263 
  264         self.assertTrue(key in conf_keys, TestConfigEntries)
  265 
  266         entry = TestConfigEntries[key]
  267         _from_, to_ = entry['desc'].split(':')
  268 
  269         # we count from 0 to eg 3 so we have 4 entries
  270         self.assertTrue(len(conf_keys) == int(to_) + 1, conf_keys)
  271 
  272         # --------------------------------------------------------------------
  273 
  274         # cleanup the shared dictionary
  275 
  276         for key in TestConfigEntries.keys():
  277             del TestConfigEntries[key]
  278 
  279         return
  280 
  281     @patch('linotp.lib.config.db_api.encryptPassword')
  282     @patch('linotp.lib.config.db_api._storeConfigEntryDB', storeConfigEntryDB)
  283     @patch('linotp.lib.config.db_api.Session')
  284     def test__storeConfigDB_password(self, mock_session, mock_encryptPassword):
  285         """
  286         test for storing long crypted password entries
  287         """
  288 
  289         key = 'linotp.test_data'
  290         val = big_value
  291         typ = 'password'
  292         desc = None
  293 
  294         mock_encryptPassword.return_value = big_value
  295         continous_entries = ContEntries()
  296 
  297         mock_session.query.return_value.filter.return_value = continous_entries
  298         _storeConfigDB(key, val, typ=typ, desc=desc)
  299 
  300         # check that the value is realy stored
  301 
  302         conf_keys = TestConfigEntries.keys()
  303 
  304         self.assertTrue(key in conf_keys, TestConfigEntries)
  305 
  306         entry = TestConfigEntries[key]
  307         _from_, to_ = entry['desc'].split(':')
  308 
  309         # we count from 0 to eg 3 so we have 4 entries
  310         self.assertTrue(len(conf_keys) == int(to_) + 1, conf_keys)
  311 
  312         # --------------------------------------------------------------------
  313 
  314         # cleanup the shared dictionary
  315 
  316         for key in TestConfigEntries.keys():
  317             del TestConfigEntries[key]
  318 
  319         return
  320 
  321     @patch('linotp.lib.config.db_api._storeConfigEntryDB', storeConfigEntryDB)
  322     @patch('linotp.lib.config.db_api.Session')
  323     def test__storeConfigDB_int(self, mock_session):
  324         """
  325         test for storing int values
  326         """
  327 
  328         key = 'linotp.test_data'
  329         val = 1313123131231231313213
  330         typ = 'int'
  331         desc = 'long int'
  332 
  333         continous_entries = ContEntries()
  334 
  335         mock_session.query.return_value.filter.return_value = continous_entries
  336         _storeConfigDB(key, val, typ=typ, desc=desc)
  337 
  338         # check that the value is realy stored
  339 
  340         conf_keys = TestConfigEntries.keys()
  341 
  342         self.assertTrue(key in conf_keys, TestConfigEntries)
  343 
  344         self.assertTrue(len(TestConfigEntries) == 1)
  345 
  346         entry = TestConfigEntries['linotp.test_data']
  347         self.assertTrue(entry['value'] == val)
  348 
  349         # --------------------------------------------------------------------
  350 
  351         # cleanup the shared dictionary
  352 
  353         for key in TestConfigEntries.keys():
  354             del TestConfigEntries[key]
  355 
  356         return
  357 
  358 
  359 class TestConfigStoreCase(unittest.TestCase):
  360     @classmethod
  361     def setUpClass(cls):
  362         # we need a clean Session context to setup new sqlite db
  363         # no matter what other unittests did
  364         Session.close_all()
  365         Session.remove()
  366         # Use an in memory empty Sqlite database
  367         super(TestConfigStoreCase, cls).setUpClass()
  368         cls.engine = create_engine('sqlite:///:memory:')
  369         metadata.create_all(cls.engine)
  370         init_model(cls.engine)
  371 
  372     def setUp(self):
  373         unittest.TestCase.setUp(self)
  374 
  375         # Clear all config entries before starting each test
  376         Session.query(Config).delete(synchronize_session='fetch')
  377 
  378     def tearDown(self):
  379         Session.remove()
  380 
  381     def test_storeConfigDB_encoding(self):
  382         # Test round trip of _storeConfigDB with entries that require
  383         # encoding of special characters
  384         conf = {
  385             'Key': u'linotp.TËST',
  386             'Value': u'VALUEÄ',
  387             'Type': u'TYPEß',
  388             'Description': u'DESCRIPTIÖN',
  389         }
  390 
  391         _storeConfigDB(conf['Key'], conf['Value'],
  392                        conf['Type'], conf['Description'])
  393 
  394         # Check value is correctly returned
  395         stored_value = _retrieveConfigDB(conf['Key'])
  396         self.assertEqual(conf['Value'], stored_value)
  397 
  398         # Check type, description in database
  399         entries = Session.query(Config).all()
  400 
  401         assert(len(entries) == 1)
  402         stored_conf = entries[0]
  403 
  404         for key in conf.keys():
  405             self.assertEqual(conf[key], getattr(stored_conf, key),
  406                              "Key should match key:%s - expected %r, recevied %r" % (key, conf[key], getattr(stored_conf, key)))
  407 
  408     def test_updateExisting(self):
  409         # Test the following conditions:
  410         # - An entry is created with chunklength > 1
  411         # - The type and description are not set
  412         # - The entry is reduced to one chunk
  413         # Verify that the resulting config entry has
  414         # correctly set the type and description
  415 
  416         key = 'linotp.testupdate'
  417         longvalue = '*' * 2000
  418         value = 'value'
  419         typ = None
  420         description = None
  421 
  422         _storeConfigDB(key, longvalue, typ, description)
  423         self.assertEqual(Session.query(Config).count(), 2)
  424         oldentries = Session.query(Config).all()
  425         self.assertEqual(len(oldentries), 2)
  426 
  427         _storeConfigDB(key, value, typ, description)
  428         entries = Session.query(Config).all()
  429         self.assertEqual(len(entries), 1)
  430 
  431         entry = entries[0]
  432         self.assertEqual(entry.Key, key)
  433         self.assertEqual(entry.Value, value)
  434         self.assertEqual(entry.Description, '')  # None is converted to ''
  435         self.assertEqual(entry.Type, typ)
  436 
  437 # eof #