"Fossies" - the Fresh Open Source Software Archive

Member "LinOTP-release-2.11/linotpd/src/linotp/tests/unit/controllers/test_admin.py" (12 Nov 2019, 4587 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 import copy
   27 import unittest
   28 
   29 from mock import mock
   30 
   31 from linotp.controllers.admin import AdminController
   32 
   33 
   34 class TestAdminController(unittest.TestCase):
   35 
   36     token = {
   37             'LinOtp.TokenId': 201,
   38             'LinOtp.TokenInfo':
   39                 u'{\n"hashlib": "sha1", \n"timeShift": -10.0, \n"timeWindow": 180, \n"validity_period_end": "23/12/23 23:23", \n"validity_period_start": "01/01/01 01:01", \n"timeStep": "30"\n}',
   40             'LinOtp.OtpLen': 6,
   41             'LinOtp.TokenType': u'TOTP',
   42             'LinOtp.TokenSerialnumber': u'F722362',
   43             'LinOtp.CountWindow': 10,
   44             'User.username': u'passthru_user1',
   45             'LinOtp.TokenDesc': u'TestToken1',
   46         }
   47 
   48     token2 = {
   49             'LinOtp.TokenId': 201,
   50             'LinOtp.TokenInfo': '',
   51             'LinOtp.OtpLen': 6,
   52             'LinOtp.TokenType': u'TOTP',
   53             'LinOtp.TokenSerialnumber': u'F722362',
   54             'LinOtp.CountWindow': 10,
   55             'User.username': u'passthru_user1',
   56             'LinOtp.TokenDesc': u'TestToken1',
   57         }
   58 
   59 
   60     expected_subset = {'validity_period_start': '2001-01-01T01:01:00',
   61                        'validity_period_end': '2023-12-23T23:23:00'}
   62 
   63     def test_parse_tokeninfo(self):
   64         """"
   65         check if admin.parse_tokeninfo works
   66         """
   67         tok = copy.deepcopy(self.token)
   68 
   69         AdminController.parse_tokeninfo(tok)
   70 
   71         self.assertIsInstance(tok.get('LinOtp.TokenInfo'),
   72                               dict,
   73                               'TokenInfo is not of type dict!')
   74         self.assertDictContainsSubset(self.expected_subset,
   75                                       tok.get('LinOtp.TokenInfo'),
   76                                       tok.get('LinOtp.TokenInfo'))
   77 
   78     @mock.patch('linotp.controllers.admin.TokenIterator')
   79     @mock.patch('linotp.controllers.admin.c')
   80     @mock.patch('linotp.controllers.admin.checkPolicyPre')
   81     @mock.patch('linotp.controllers.admin.Session')
   82     @mock.patch('linotp.controllers.admin.response')
   83     @mock.patch('linotp.controllers.admin.request')
   84     @mock.patch('linotp.controllers.admin.BaseController.__init__', return_value=None)
   85     def check_token(self, mock_base, mock_request, mock_response, mock_session,
   86                     mock_check_policy_pre, mock_c, mock_TokenIterator,
   87                     with_json):
   88         """
   89         call admin/show with/without argument tokeninfo_format
   90         and return if parse_tokeninfo has been called
   91         """
   92         request_params = {
   93             'tokeninfo_format': with_json,
   94         }
   95         mock_check_policy_pre.return_value = {'active': False,
   96                                               'admin': 'unittest'}
   97         mock_c.audit = {}
   98         tok = copy.deepcopy(self.token)
   99         mock_TokenIterator.return_value = [tok]
  100 
  101         admin = AdminController()
  102         admin.request_params = request_params
  103         admin.show()
  104 
  105     @mock.patch('linotp.controllers.admin.AdminController.parse_tokeninfo')
  106     def test_with_tokeninfo_format(self, mock_parse_tokeninfo):
  107         self.check_token(with_json='json')
  108         mock_parse_tokeninfo.assert_called()
  109 
  110     @mock.patch('linotp.controllers.admin.AdminController.parse_tokeninfo')
  111     def test_without_tokeninfo_format(self,  mock_parse_tokeninfo,):
  112         self.check_token(with_json='')
  113         mock_parse_tokeninfo.assert_not_called()
  114 
  115     def test_parse_empty_tokeninfo(self):
  116         """
  117         verify that token info is valid even if it is initially empty
  118         """
  119         tok = copy.deepcopy(self.token2)
  120         AdminController.parse_tokeninfo(tok)
  121 
  122         self.assertTrue(tok['LinOtp.TokenInfo'] == {})
  123 
  124         return