"Fossies" - the Fresh Open Source Software Archive

Member "LinOTP-release-2.10.5.2/linotpd/src/linotp/tests/unit/lib/user/test_user_from_options.py" (13 May 2019, 5556 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 import json
   28 import unittest
   29 from mock import patch
   30 
   31 from linotp.lib.user import get_user_from_options
   32 from linotp.lib.user import User
   33 
   34 
   35 class TestGetUserFromOptions(unittest.TestCase):
   36 
   37 
   38     @patch('linotp.lib.user.getUserFromParam')
   39     def test_run_test_vector(self,
   40                          mock_getUserFromParam
   41                          ):
   42         """
   43         test a set of options dict values with fallback user and fallback realm
   44         """
   45 
   46         test_sets = [
   47                     # 1. test simple login name in options
   48                     {'options_dict': {'user': 'amigo'},
   49                       'user_from_param' : User('amigo', 'defRealm'),
   50                       'result': {'login': 'amigo', 'realm': 'defRealm'}},
   51 
   52                      # 2. test login and realn in options
   53                      {'options_dict': {'user': 'amigo', 'realm': 'mexico'},
   54                       'user_from_param' : User('amigo', 'mexico'),
   55                       'result': {'login': 'amigo', 'realm': 'mexico'}},
   56 
   57                      # 3. test user object in options
   58                      {'options_dict': {'user': User('amigo', 'mexico')},
   59                       'user_from_param' : User('amigo', 'mexico'),
   60                       'result': {'login': 'amigo', 'realm': 'mexico'}},
   61 
   62                      # 4. test no login and no realn in options
   63                      {'options_dict': {},
   64                       'user_from_param' : User('', ''),
   65                       'result': {'login': 'token_owner', 'realm': 'norealm'}},
   66 
   67                      # 5. test no login and realn in options
   68                      {'options_dict': { 'user': '', 'realm': 'norway'},
   69                       'user_from_param' : User('', 'norway'),
   70                       'result': {'login': 'token_owner', 'realm': 'norealm'}},
   71         ]  # eof test sets
   72 
   73 
   74         for run in test_sets:
   75 
   76             options_dict = run['options_dict']
   77             mock_getUserFromParam.return_value = run['user_from_param']
   78 
   79             result = run['result']
   80 
   81             login, realm = get_user_from_options(
   82                                 options_dict=options_dict,
   83                                 fallback_user=User('token_owner', 'norealm'),
   84                                 fallback_realm='norealm')
   85 
   86             assert (login == result['login'] and realm == result['realm'],
   87                         "failed on run %r:%r:%r" % (login, realm, run))
   88 
   89         return
   90 
   91     @patch('linotp.lib.user.getUserFromParam')
   92     def test_run_test_vector_without_fallback_user(self,
   93                          mock_getUserFromParam
   94                          ):
   95         """
   96         test a set of options dict values without fallback user and fallback realm
   97         """
   98 
   99         test_sets = [
  100                     # 1. test simple login name in options
  101                     {'options_dict': {'user': 'amigo'},
  102                       'user_from_param' : User('amigo', 'defRealm'),
  103                       'result': {'login': 'amigo', 'realm': 'defRealm'}},
  104 
  105                      # 2. test login and realn in options
  106                      {'options_dict': {'user': 'amigo', 'realm': 'mexico'},
  107                       'user_from_param' : User('amigo', 'mexico'),
  108                       'result': {'login': 'amigo', 'realm': 'mexico'}},
  109 
  110                      # 3. test user object in options
  111                      {'options_dict': {'user': User('amigo', 'mexico')},
  112                       'user_from_param' : User('amigo', 'mexico'),
  113                       'result': {'login': 'amigo', 'realm': 'mexico'}},
  114 
  115                      # 4. test no login and no realn in options
  116                      {'options_dict': {},
  117                       'user_from_param' : User('', ''),
  118                       'result': {'login': '', 'realm': 'norealm'}},
  119 
  120                      # 5. test no login and realn in options
  121                      {'options_dict': { 'user': '', 'realm': 'norway'},
  122                       'user_from_param' : User('', 'norway'),
  123                       'result': {'login': '', 'realm': 'norealm'}},
  124 
  125         ]  # eof test sets
  126 
  127 
  128         for run in test_sets:
  129 
  130             options_dict = run['options_dict']
  131             mock_getUserFromParam.return_value = run['user_from_param']
  132 
  133             result = run['result']
  134 
  135             login, realm = get_user_from_options(
  136                                 options_dict=options_dict,
  137                                 fallback_user=None,
  138                                 fallback_realm='norealm')
  139 
  140             assert (login == result['login'] and realm == result['realm'],
  141                         "failed on run %r:%r:%r" % (login, realm, run))
  142 
  143         return
  144 
  145 # eof #