"Fossies" - the Fresh Open Source Software Archive

Member "LinOTP-release-2.10.5.2/linotpd/src/linotp/tests/functional/test_set_password.py" (13 May 2019, 3361 Bytes) of package /linux/misc/LinOTP-release-2.10.5.2.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 
   28 """
   29 Testing the set password ability
   30 """
   31 
   32 
   33 import logging
   34 
   35 from sqlalchemy.engine import create_engine
   36 from sqlalchemy.orm import sessionmaker
   37 
   38 from sqlalchemy.exc import ProgrammingError
   39 
   40 from linotp.lib.crypto import libcrypt_password
   41 from linotp.lib.tools.set_password import SetPasswordHandler
   42 from linotp.lib.tools.set_password import DataBaseContext
   43 
   44 from linotp.tests import TestController
   45 
   46 log = logging.getLogger(__name__)
   47 
   48 
   49 class TestSetAdminPassword(TestController):
   50 
   51     def drop_admin_user(self):
   52         """
   53         for the tests, we will drop the imported user table
   54         """
   55 
   56         sqlconnect = self.appconf.get('sqlalchemy.url')
   57         engine = create_engine(sqlconnect)
   58 
   59         # create the session for the db operation
   60 
   61         Sessionmaker = sessionmaker()
   62         Sessionmaker.configure(bind=engine)
   63         session = Sessionmaker()
   64 
   65         # we try to delete the table if it exists
   66 
   67         try:
   68 
   69             SetPasswordHandler.AdminUser.__table__.drop(engine)
   70             session.commit()
   71 
   72         except (ProgrammingError, Exception) as exx:
   73 
   74             log.info("Drop Table failed %r", exx)
   75             session.rollback()
   76 
   77         finally:
   78 
   79             session.close()
   80 
   81     def create_admin_user(self):
   82         """
   83         for testing we require the admin user to exist
   84         """
   85 
   86         sqlconnect = self.appconf.get('sqlalchemy.url')
   87         engine = create_engine(sqlconnect)
   88 
   89         db_context = DataBaseContext(engine.url)
   90 
   91         SetPasswordHandler.create_table(db_context)
   92         SetPasswordHandler.create_admin_user(
   93                                 db_context,
   94                                 username='admin',
   95                                 crypted_password=libcrypt_password('nimda'))
   96 
   97     def test_set_simple_password(self):
   98         """
   99         simple functional test
  100         - other aspects are covered by the unit test
  101         """
  102 
  103         self.drop_admin_user()
  104         self.create_admin_user()
  105 
  106         params = {}
  107         params['old_password'] = 'nimda'
  108         params['new_password'] = 'admin'
  109 
  110         response = self.make_tools_request('setPassword',
  111                                            params=params,
  112                                            auth_user='admin')
  113 
  114         msg = '"detail": "password updated for \'admin\'"'
  115         self.assertTrue(msg in response)
  116 
  117         return
  118 
  119 
  120 # eof ########################################################################