"Fossies" - the Fresh Open Source Software Archive

Member "LinOTP-release-2.11/linotpd/src/linotp/tests/unit/lib/enrollment/test_autoassign_wo_pass.py" (12 Nov 2019, 6768 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@lsexperts.de
   23 #    Contact: www.linotp.org
   24 #    Support: www.lsexperts.de
   25 #
   26 
   27 import unittest
   28 
   29 from mock import patch
   30 
   31 from linotp.lib.token import TokenHandler
   32 from linotp.lib.user import User
   33 
   34 from linotp.lib.policy import get_autoassignment_without_pass
   35 from linotp.lib.policy import get_autoassignment_from_realm
   36 from linotp.tokens.passwordtoken import PasswordTokenClass
   37 
   38 
   39 class TestAutoEnroll(unittest.TestCase):
   40 
   41     @patch('linotp.lib.token.context')
   42     @patch('linotp.lib.token.TokenHandler.assignToken')
   43     @patch('linotp.lib.token.TokenHandler.getTokensOfType')
   44     @patch('linotp.lib.token.getTokens4UserOrSerial')
   45     @patch('linotp.lib.token.get_autoassignment_without_pass')
   46     @patch('linotp.lib.token.get_autoassignment_from_realm')
   47     def test_autenroll_wo_pass(self,
   48                                mocked_policy_src_realm,
   49                                mocked_policy_autosignment_wo,
   50                                mockedgetTokens4UserOrSerial,
   51                                mocked_getTokensOfType,
   52                                mocked_assignToken,
   53                                mocked_context
   54                                ):
   55 
   56         thdle = TokenHandler()
   57 
   58         options = {}
   59         user = User("Hugo", realm="def_realm")
   60         otp = '123467'
   61 
   62         class Token(object):
   63 
   64             LinOtpCountWindow = 10
   65             typ = ""
   66 
   67             def setType(self, type_name):
   68                 self.typ = type_name
   69 
   70             def getType(self):
   71                 return self.typ
   72 
   73             def getSerial(self):
   74                 return 'ABCDEFG'
   75 
   76         aToken = Token()
   77 
   78         class MockPasswordTokenClass(PasswordTokenClass):
   79 
   80             def check_otp_exist(self, *args, **kwargs):
   81                 return 1
   82 
   83         pwtoken = MockPasswordTokenClass(aToken)
   84 
   85         mocked_policy_src_realm.return_value = None
   86         mocked_policy_autosignment_wo.return_value = True
   87         mockedgetTokens4UserOrSerial.return_value = []
   88         mocked_getTokensOfType.return_value = [pwtoken]
   89         mocked_assignToken.return_value = True
   90 
   91         mocked_context = {'audit': {}}
   92 
   93         res = thdle.auto_assign_otp_only(otp, user, options)
   94 
   95         self.assertTrue(res)
   96         self.assertTrue(mocked_assignToken.called)
   97 
   98         return
   99 
  100     @patch('linotp.lib.policy._get_client')
  101     @patch('linotp.lib.policy.get_client_policy')
  102     def test_get_autoassignment_without_pass(self,
  103                                      mocked_get_client_policy,
  104                                      mocked_get_client,
  105                                      ):
  106 
  107         user = User('Hugo', realm='Home_realm')
  108 
  109         mocked_get_client_policy.return_value = {
  110             'my_autoassign_policy_wo_pass': {
  111                 'realm': 'mydefrealm',
  112                 'active': 'True',
  113                 'client': '*',
  114                 'user': '*',
  115                 'time': '*',
  116                 'action': 'autoassignment_without_password=True',
  117                 'scope': 'enrollment'}}
  118         mocked_get_client.return_value = '127.0.0.1'
  119 
  120         res = get_autoassignment_without_pass(user=user)
  121         self.assertTrue(res)
  122 
  123         mocked_get_client_policy.return_value = {
  124             'my_autoassign_policy_wo_pass': {
  125                 'realm': 'mydefrealm',
  126                 'active': 'True',
  127                 'client': '*',
  128                 'user': '*',
  129                 'time': '*',
  130                 'action': 'autoassignment_without_password',
  131                 'scope': 'enrollment'}}
  132 
  133         res = get_autoassignment_without_pass(user=user)
  134         self.assertTrue(res)
  135 
  136         mocked_get_client_policy.return_value = {
  137             'my_autoassign_policy_wo_pass': {
  138                 'realm': 'mydefrealm',
  139                 'active': 'True',
  140                 'client': '*',
  141                 'user': '*',
  142                 'time': '*',
  143                 'action': 'autoassignment_without_password=False',
  144                 'scope': 'enrollment'}}
  145 
  146         res = get_autoassignment_without_pass(user=user)
  147         self.assertFalse(res)
  148 
  149         mocked_get_client_policy.return_value = {
  150             'my_autoassign_policy_wo_pass': {
  151                 'realm': 'mydefrealm',
  152                 'active': 'True',
  153                 'client': '*',
  154                 'user': '*',
  155                 'time': '*',
  156                 'action': 'autoassignment_without_password=error',
  157                 'scope': 'enrollment'}}
  158 
  159         res = get_autoassignment_without_pass(user=user)
  160         self.assertFalse(res)
  161 
  162     @patch('linotp.lib.policy._get_client')
  163     @patch('linotp.lib.policy.get_client_policy')
  164     def test_get_autoassignment_from_realm(self,
  165                                            mocked_get_client_policy,
  166                                            mocked_get_client,
  167                                            ):
  168 
  169         user = User('Hugo', realm='Home_realm')
  170         mocked_get_client.return_value = '127.0.0.1'
  171         src_realm = 'token-realm '
  172 
  173         mocked_get_client_policy.return_value = {
  174             'my_autoassign_policy_wo_pass': {
  175                 'realm': 'mydefrealm',
  176                 'active': 'True',
  177                 'client': '*',
  178                 'user': '*',
  179                 'time': '*',
  180                 'action': 'autoassignment_from_realm=%s' % src_realm,
  181                 'scope': 'enrollment'}}
  182 
  183         realm = get_autoassignment_from_realm(user)
  184         self.assertTrue(src_realm.strip() == realm)
  185 
  186         src_realm = ' '
  187         mocked_get_client_policy.return_value = {
  188             'my_autoassign_policy_wo_pass': {
  189                 'realm': 'mydefrealm',
  190                 'active': 'True',
  191                 'client': '127.0.0.1',
  192                 'user': '*',
  193                 'time': '*',
  194                 'action': 'autoassignment_from_realm=%s' % src_realm,
  195                 'scope': 'enrollment'}}
  196 
  197         realm = get_autoassignment_from_realm(user)
  198         self.assertTrue(not realm)
  199 
  200         return
  201 
  202 # eof #