"Fossies" - the Fresh Open Source Software Archive

Member "LinOTP-release-2.10.5.3/linotpd/src/linotp/tests/functional/test_voicetoken.py" (24 Jun 2019, 7913 Bytes) of package /linux/misc/LinOTP-release-2.10.5.3.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 from mock import patch
   29 from linotp.tests import TestController
   30 
   31 
   32 class TestVoiceToken(TestController):
   33 
   34     def setUp(self):
   35 
   36         self.delete_all_policies()
   37         self.delete_all_token()
   38         self.delete_all_realms()
   39         self.delete_all_resolvers()
   40         super(TestVoiceToken, self).setUp()
   41         self.create_common_resolvers()
   42         self.create_common_realms()
   43         self.create_voice_provider()
   44         self.create_policies()
   45 
   46     def tearDown(self):
   47 
   48         self.delete_all_policies()
   49         self.delete_all_realms()
   50         self.delete_all_resolvers()
   51         self.delete_all_token()
   52         super(TestVoiceToken, self).tearDown()
   53 
   54     def create_voice_provider(self):
   55 
   56         """
   57         Creates a voice provider 'DevVoiceProv' with
   58         dummy configuration
   59         """
   60 
   61         configDict = {
   62         }
   63 
   64         configDict['twilioConfig'] = {
   65             'accountSid': 'ACf9095f540f0b090edbd239b99230a8ee',
   66             'authToken': '8f36aab7ca485b432500ce49c15280c5',
   67             'voice': 'alice',
   68             'callerNumber': '+4989231234567',
   69         }
   70 
   71         configDict['server_url'] = 'https://mydummy'
   72 
   73         params = {'name': 'DefVoiceProv',
   74                   'class': 'CustomVoiceProvider',
   75                   'config': json.dumps(configDict),
   76                   'timeout': '120',
   77                   'type': 'voice'}
   78 
   79         self.make_system_request('setProvider', params=params)
   80 
   81     def create_policies(self):
   82 
   83         """
   84         Creates test values for the following policies:
   85 
   86         * authentication/voice_message
   87         * authentication/voice_language
   88         * authentication/voice_provider
   89         """
   90 
   91         params = {'name': 'voice_policies',
   92                   'scope': 'authentication',
   93                   'action': 'voice_language=de, '
   94                             'voice_message=Hi! {otp},'
   95                             'voice_provider=DefVoiceProv',
   96                   'user': '*',
   97                   'realm': '*',
   98                   'client': '',
   99                   'time': ''}
  100 
  101         self.create_policy(params=params)
  102 
  103     @patch('linotp.provider.voiceprovider.custom_voice_provider.'
  104            'CustomVoiceProvider.submitVoiceMessage')
  105     def test_validate_check(self, mocked_submit_method):
  106 
  107         """ VoiceToken: Check if validate/check works correctly """
  108 
  109         serial = 'KIVO123foobar789'
  110         token_phone_number = '123987456787'
  111 
  112         params = {'type': 'voice',
  113                   'serial': serial,
  114                   'pin': '1234',
  115                   'phone': token_phone_number,
  116                   'user': 'passthru_user1@myDefRealm'}
  117 
  118         response = self.make_admin_request('init', params)
  119         response_dict = json.loads(response.body)
  120         self.assertIn('detail', response_dict)
  121 
  122         # trigger voice challenge
  123 
  124         mocked_submit_method.return_value = (True, 'lmao')
  125 
  126         params = {'user': 'passthru_user1@myDefRealm',
  127                   'pass': '1234'}
  128 
  129         response = self.make_validate_request('check', params)
  130         response_dict = json.loads(response.body)
  131         result = response_dict.get('result', {})
  132         status = result.get('status')
  133         value = result.get('value')
  134         detail = response_dict.get('detail', {})
  135         transaction_id = detail.get('transactionid')
  136         self.assertIsNotNone(transaction_id)
  137 
  138         self.assertTrue(status)
  139         self.assertFalse(value)
  140 
  141         _, call_kwargs = mocked_submit_method.call_args
  142         callee_number = call_kwargs.get('calleeNumber')
  143         self.assertEqual(callee_number, token_phone_number)
  144         otp = call_kwargs.get('otp')
  145 
  146         # respond to challenge with correct otp
  147 
  148         params = {'user': 'passthru_user1@myDefRealm',
  149                   'transactionid': transaction_id,
  150                   'pass': otp}
  151 
  152         response = self.make_validate_request('check', params)
  153         response_dict = json.loads(response.body)
  154         result = response_dict.get('result', {})
  155         status = result.get('status')
  156         value = result.get('value')
  157 
  158         self.assertTrue(status)
  159         self.assertTrue(value)
  160 
  161         # trigger another voice challenge
  162 
  163         mocked_submit_method.return_value = (True, 'lmao')
  164 
  165         params = {'user': 'passthru_user1@myDefRealm',
  166                   'pass': '1234'}
  167 
  168         response = self.make_validate_request('check', params)
  169         response_dict = json.loads(response.body)
  170         result = response_dict.get('result', {})
  171         status = result.get('status')
  172         value = result.get('value')
  173         detail = response_dict.get('detail', {})
  174         transaction_id = detail.get('transactionid')
  175         self.assertIsNotNone(transaction_id)
  176 
  177         self.assertTrue(status)
  178         self.assertFalse(value)
  179 
  180         _, call_kwargs = mocked_submit_method.call_args
  181         callee_number = call_kwargs.get('calleeNumber')
  182         self.assertEqual(callee_number, token_phone_number)
  183         otp = call_kwargs.get('otp')
  184 
  185         # respond to challenge with a wrong otp
  186 
  187         # generate an otp that has the same length, but is
  188         # is guaranteed to be different
  189         wrong_otp = str((int(otp) + 1) % 10**len(otp)).zfill(len(otp))
  190 
  191         params = {'user': 'passthru_user1@myDefRealm',
  192                   'transactionid': transaction_id,
  193                   'pass': wrong_otp}
  194 
  195         response = self.make_validate_request('check', params)
  196         response_dict = json.loads(response.body)
  197         result = response_dict.get('result', {})
  198         status = result.get('status')
  199         value = result.get('value')
  200 
  201         self.assertTrue(status)
  202         self.assertFalse(value)
  203 
  204         # trigger another voice challenge for check with pin+otp
  205 
  206         mocked_submit_method.return_value = (True, 'lmao')
  207 
  208         params = {'user': 'passthru_user1@myDefRealm',
  209                   'pass': '1234'}
  210 
  211         response = self.make_validate_request('check', params)
  212         response_dict = json.loads(response.body)
  213         result = response_dict.get('result', {})
  214         status = result.get('status')
  215         value = result.get('value')
  216         detail = response_dict.get('detail', {})
  217         transaction_id = detail.get('transactionid')
  218         self.assertIsNotNone(transaction_id)
  219 
  220         self.assertTrue(status)
  221         self.assertFalse(value)
  222 
  223         _, call_kwargs = mocked_submit_method.call_args
  224         callee_number = call_kwargs.get('calleeNumber')
  225         self.assertEqual(callee_number, token_phone_number)
  226         otp = call_kwargs.get('otp')
  227 
  228         # respond to challenge with pin+otp and without
  229         # transaction_id
  230 
  231         params = {'user': 'passthru_user1@myDefRealm',
  232                   'pass': '1234' + otp}
  233 
  234         response = self.make_validate_request('check', params)
  235         response_dict = json.loads(response.body)
  236         result = response_dict.get('result', {})
  237         status = result.get('status')
  238         value = result.get('value')
  239 
  240         self.assertTrue(status)
  241         self.assertTrue(value)