"Fossies" - the Fresh Open Source Software Archive

Member "LinOTP-release-2.11/linotpd/src/linotp/tests/unit/tokens/test_qrtoken.py" (12 Nov 2019, 4937 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 import unittest
   28 import json
   29 
   30 from contextlib import nested
   31 from linotp.lib.context import request_context_safety
   32 from linotp.lib.context import request_context
   33 from linotp.tokens.qrtoken.qrtoken import QrTokenClass
   34 from mock import patch
   35 from pylons import config
   36 
   37 
   38 class FakeHSM(object):
   39 
   40     def isReady(self):
   41         return True
   42 
   43     def hmac_digest(self, key, data, algo):
   44         return 'foo'
   45 
   46 fake_hsm_wrapper = {'obj': FakeHSM()}
   47 
   48 
   49 # ---------------------------------------------------------------------------- -
   50 
   51 
   52 class FakeTokenModel(object):
   53 
   54     def __init__(self):
   55         self.info_dict = {}
   56 
   57     def setInfo(self, json_str):
   58         self.info_dict = json.loads(json_str)
   59 
   60     def getSerial(self):
   61         return 'QRfoo123'
   62 
   63     def setType(self, type_):
   64         pass
   65 
   66     def getInfo(self):
   67         return json.dumps(self.info_dict)
   68 
   69     def get_encrypted_seed(self):
   70         return 'foo', 'bar'
   71 
   72 # ---------------------------------------------------------------------------- -
   73 
   74 
   75 class QRTokenClassUnitTestCase(unittest.TestCase):
   76 
   77     def test_unpair(self):
   78 
   79         """ QRToken unittest: checking if unpairing works """
   80 
   81         fake = FakeTokenModel()
   82 
   83         token = QrTokenClass(fake)
   84 
   85         token.addToTokenInfo('user_token_id', 'bar')
   86         token.addToTokenInfo('user_public_key', 'foo')
   87         token.change_state('baz')
   88 
   89         token.unpair()
   90 
   91         self.assertNotIn('user_token_id', fake.info_dict)
   92         self.assertNotIn('user_public_key', fake.info_dict)
   93         self.assertEqual('pairing_url_sent', token.current_state)
   94 
   95     # ------------------------------------------------------------------------ -
   96 
   97     @patch('linotp.tokens.pushtoken.pushtoken.get_secret_key')
   98     def test_url_protocol_id(self, mocked_get_secret_key):
   99 
  100         """
  101         QRToken unittest: Test url protocol id customization
  102         """
  103 
  104         mocked_get_secret_key.return_value = 'X' * 64
  105         user_public_key = 'MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI='
  106 
  107         fake = FakeTokenModel()
  108 
  109         token = QrTokenClass(fake)
  110         token.addToTokenInfo('user_token_id', 1234)
  111         token.addToTokenInfo('user_public_key', user_public_key)
  112 
  113         with nested(patch.dict(config), request_context_safety()):
  114 
  115             if 'mobile_app_protocol_id' in config:
  116                 del config['mobile_app_protocol_id']
  117 
  118             request_context['hsm'] = fake_hsm_wrapper
  119 
  120             # if no mobile_app_protocol_id is set, it should default
  121             # to lseqr
  122 
  123             message = 'here are the 2,750 quit you asked for. can i move' + \
  124                       'to OT I level now? - tom'
  125 
  126             url, _ = token.create_challenge_url(transaction_id='1234567890',
  127                                                 content_type=0,
  128                                                 message=message,
  129                                                 callback_url='foo',
  130                                                 callback_sms_number='+491234')
  131 
  132             self.assertTrue(url.startswith('lseqr://'))
  133 
  134         # -------------------------------------------------------------------- -
  135 
  136         fake = FakeTokenModel()
  137         token = QrTokenClass(fake)
  138         token.addToTokenInfo('user_token_id', 1234)
  139         token.addToTokenInfo('user_public_key', user_public_key)
  140 
  141         with nested(patch.dict(config, {'mobile_app_protocol_id': 'yolo'}),
  142                     request_context_safety()):
  143 
  144             request_context['hsm'] = fake_hsm_wrapper
  145 
  146             message = 'here are the 2,750 quit you asked for. can i move' + \
  147                       'to OT I level now? - tom'
  148 
  149             url, _ = token.create_challenge_url(transaction_id='1234567890',
  150                                                 content_type=0,
  151                                                 message=message,
  152                                                 callback_url='foo',
  153                                                 callback_sms_number='+491234')
  154 
  155             self.assertTrue(url.startswith('yolo://'))