Ejemplo n.º 1
0
    def test_request(self):
        """
        do some mocking of a requests request
        """

        configDict = {
            "access_certificate": os.path.join(self.fixture_path, 'cert.pem'),
        }

        configDict['twilioConfig'] = {
            'accountSid': 'ACf9095f540f0b090edbd239b99230a8ee',
            'authToken': '8f36aab7ca485b432500ce49c15280c5',
            'callerNumber': '+4989231234567',
            'voice': 'alice',
        }

        configDict['Timeout'] = '30'
        configDict['server_url'] = self.VCS_URL

        voice_provider = CustomVoiceProvider()
        voice_provider.loadConfig(configDict)

        messageTemplate = "Your otp is {otp}"
        otp = '432423'
        locale = 'en'
        calleeNumber = "+49 6151 860 860"

        # set the response status
        TestVoiceProviderController.R_AUTH_STATUS = 200

        # run the fake request
        status, response = voice_provider.submitVoiceMessage(
            calleeNumber, messageTemplate, otp, locale)

        assert status == True
        assert VALID_REQUEST in response

        request_json = json.loads(response)

        target_url = 'https://vcs.keyidentity.com/v1/twilio/call'
        assert target_url in request_json.get('url')

        # compare the request dictionary, with the expected structure

        cmp_content = {
            'call': {
                'messageTemplate': 'Your otp is {otp}',
                'otp': '432423',
                'locale': 'en',
                'calleeNumber': '+49 6151 860 860',
                'twilioConfig': {
                    'authToken': '8f36aab7ca485b432500ce49c15280c5',
                    'accountSid': 'ACf9095f540f0b090edbd239b99230a8ee',
                    'voice': 'alice',
                    'callerNumber': '+4989231234567'
                }
            }
        }

        assert request_json.get('body') == cmp_content
Ejemplo n.º 2
0
    def test_request(self):
        """
        do some mocking of a requests request
        """

        configDict = {
            "access_certificate": os.path.join(self.fixture_path, "cert.pem"),
        }

        configDict["twilioConfig"] = {
            "accountSid": "ACf9095f540f0b090edbd239b99230a8ee",
            "authToken": "8f36aab7ca485b432500ce49c15280c5",
            "callerNumber": "+4989231234567",
            "voice": "alice",
        }

        configDict["Timeout"] = "30"
        configDict["server_url"] = self.VCS_URL

        voice_provider = CustomVoiceProvider()
        voice_provider.loadConfig(configDict)

        messageTemplate = "Your otp is {otp}"
        otp = "432423"
        locale = "en"
        calleeNumber = "+49 6151 860 860"

        # set the response status
        TestVoiceProviderController.R_AUTH_STATUS = 200

        # run the fake request
        status, response = voice_provider.submitVoiceMessage(
            calleeNumber, messageTemplate, otp, locale)

        assert status
        assert VALID_REQUEST in response

        request_json = json.loads(response)

        target_url = "https://vcs.keyidentity.com/v1/twilio/call"
        assert target_url in request_json.get("url")

        # compare the request dictionary, with the expected structure

        cmp_content = {
            "call": {
                "messageTemplate": "Your otp is {otp}",
                "otp": "432423",
                "locale": "en",
                "calleeNumber": "+49 6151 860 860",
                "twilioConfig": {
                    "authToken": "8f36aab7ca485b432500ce49c15280c5",
                    "accountSid": "ACf9095f540f0b090edbd239b99230a8ee",
                    "voice": "alice",
                    "callerNumber": "+4989231234567",
                },
            }
        }

        assert request_json.get("body") == cmp_content
Ejemplo n.º 3
0
    def test_request(self):
        """
        do some mocking of a requests request
        """

        configDict = {
            "access_certificate": os.path.join(self.fixture_path, 'cert.pem'),
            'callerNumber': '+4989231234567',
        }

        configDict['twilio'] = {
            'accountSid': 'ACf9095f540f0b090edbd239b99230a8ee',
            'authToken': '8f36aab7ca485b432500ce49c15280c5',
            'voice': 'alice',
        }

        configDict['Timeout'] = '30'
        configDict['server_url'] = self.VCS_URL

        voice_provider = CustomVoiceProvider()
        voice_provider.loadConfig(configDict)

        messageTemplate = "Your otp is {otp}"
        otp = '432423'
        locale = 'en'
        calleeNumber = "+49 6151 860 860"

        # set the response status
        TestVoiceProviderController.R_AUTH_STATUS = 200

        # run the fake request
        status, response = voice_provider.submitVoiceMessage(
            calleeNumber, messageTemplate, otp, locale)

        self.assertEquals(status, True)
        self.assertTrue(VALID_REQUEST in response)

        target_url = 'https://vcs.keyidentity.com/v1/twilio/call'
        self.assertTrue(target_url in response)

        request_content = {
            'call': {
                'authToken': '8f36aab7ca485b432500ce49c15280c5',
                'otp': '432423',
                'calleeNumber': '+49 6151 860 860',
                'messageTemplate': 'Your otp is {otp}',
                'accountSid': 'ACf9095f540f0b090edbd239b99230a8ee',
                'locale': 'en',
                'voice': 'alice'
            }
        }

        for _key, value in request_content['call'].items():
            self.assertTrue(value in response)

        return
Ejemplo n.º 4
0
    def test_warning_called(self):
        """
        test the log.warning, if no {otp} in template
        """
        custom_provider = CustomVoiceProvider()

        configDict = {
            "access_certificate": os.path.join(fixture_path, "cert.pem"),
        }

        configDict["twilioConfig"] = {
            "accountSid": "ACf9095f540f0b090edbd239b99230a8ee",
            "authToken": "8f36aab7ca485b432500ce49c15280c5",
            "voice": "alice",
            "callerNumber": "+4989231234567",
        }

        configDict["Timeout"] = "30"
        vcs_url = "http://vcs-service.keyidentity.com/v1/twilio/call"
        configDict["server_url"] = vcs_url
        custom_provider.loadConfig(configDict)

        with patch(
            "linotp.provider.voiceprovider."
            "custom_voice_provider.log.warning"
        ) as mocked_log_warning:

            custom_provider.submitVoiceMessage(
                "+49 123546891", "your otp", "123456", "en"
            )
            called = mocked_log_warning.called

        with patch(
            "linotp.provider.voiceprovider."
            "custom_voice_provider.log.warning"
        ) as mocked_log_warning:

            custom_provider.submitVoiceMessage(
                "+49 123546891", "your {otp}", "123456", "en"
            )
            called2 = mocked_log_warning.called

        return
Ejemplo n.º 5
0
    def test_warning_called(self):
        """
        test the log.warning, if no {otp} in template
        """
        custom_provider = CustomVoiceProvider()

        configDict = {
            "access_certificate": os.path.join(fixture_path, 'cert.pem'),
            }

        configDict['twilioConfig'] = {
            'accountSid': 'ACf9095f540f0b090edbd239b99230a8ee',
            'authToken': '8f36aab7ca485b432500ce49c15280c5',
            'voice': 'alice',
            'callerNumber': '+4989231234567',
            }

        configDict['Timeout'] = '30'
        configDict['server_url'] = ("http://vcs-service.keyidentity.com"
                                    "/v1/twilio/call")
        custom_provider.loadConfig(configDict)

        with patch("linotp.provider.voiceprovider."
                      "custom_voice_provider.log.warning") as mocked_log_warning:

            custom_provider.submitVoiceMessage(
                "+49 123546891", "your otp", "123456", "en")
            called = mocked_log_warning.called

        with patch("linotp.provider.voiceprovider."
                      "custom_voice_provider.log.warning") as mocked_log_warning:

            custom_provider.submitVoiceMessage(
                "+49 123546891", "your {otp}", "123456", "en")
            called2 = mocked_log_warning.called

        return
Ejemplo n.º 6
0
    def test_warning_called(self):
        """
        test the log.warning, if no {otp} in template
        """
        custom_provider = CustomVoiceProvider()

        configDict = {
            "access_certificate": os.path.join(fixture_path, 'cert.pem'),
            }

        configDict['twilioConfig'] = {
            'accountSid': 'ACf9095f540f0b090edbd239b99230a8ee',
            'authToken': '8f36aab7ca485b432500ce49c15280c5',
            'voice': 'alice',
            'callerNumber': '+4989231234567',
            }

        configDict['Timeout'] = '30'
        configDict['server_url'] = ("http://vcs-service.keyidentity.com"
                                    "/v1/twilio/call")
        custom_provider.loadConfig(configDict)

        with patch("linotp.provider.voiceprovider."
                      "custom_voice_provider.log.warning") as mocked_log_warning:

            custom_provider.submitVoiceMessage(
                "+49 123546891", "your otp", "123456", "en")
            called = mocked_log_warning.called

        with patch("linotp.provider.voiceprovider."
                      "custom_voice_provider.log.warning") as mocked_log_warning:

            custom_provider.submitVoiceMessage(
                "+49 123546891", "your {otp}", "123456", "en")
            called2 = mocked_log_warning.called

        return
Ejemplo n.º 7
0
    def test_read_config(self):
        """
        test push provider configuration handling
        """

        voice_provider = CustomVoiceProvider()

        #
        # first test the valid configuration
        #
        configDict = {
            "access_certificate": os.path.join(self.fixture_path, 'cert.pem'),
        }

        configDict['twilioConfig'] = {
            'accountSid': 'ACf9095f540f0b090edbd239b99230a8ee',
            'authToken': '8f36aab7ca485b432500ce49c15280c5',
            'voice': 'alice',
            'callerNumber': '+4989231234567',
        }

        configDict['Timeout'] = '30'
        configDict['server_url'] = self.VCS_URL

        voice_provider.loadConfig(configDict)

        #
        # verify server url check
        #

        with pytest.raises(requests.exceptions.InvalidSchema):
            configDict['server_url'] = "hXXXs://vcs.keyidentity.com:8800/send"
            voice_provider.loadConfig(configDict)

        #
        # restore configuration for server_url
        #

        configDict['server_url'] = self.VCS_URL

        #
        # extended option: proxy
        #

        configDict['proxy'] = "https://proxy.keyidentity.com:8800/"
        voice_provider.loadConfig(configDict)

        #
        # extended option: proxy with wrong url scheme
        #

        with pytest.raises(requests.exceptions.InvalidSchema):
            configDict['proxy'] = "hXXXs://proxy.keyidentity.com:8800/"
            voice_provider.loadConfig(configDict)

        # restore valid proxy url
        configDict['proxy'] = "https://proxy.keyidentity.com:8800/"

        #
        # valid extended timeout format
        #

        configDict['timeout'] = '3,10'
        voice_provider.loadConfig(configDict)

        del configDict['timeout']

        #
        # invalid timeout format: "invalid literal for float()"
        #

        with pytest.raises(ValueError):
            configDict['Timeout'] = '30s'
            voice_provider.loadConfig(configDict)

        # timeout has a default and is not required
        del configDict['Timeout']

        #
        # non existing certificate file - should raise exception
        # 'required authenticating client cert could not be found'
        #

        with pytest.raises(IOError):
            cert_file_name = os.path.join(self.fixture_path, 'non_exist.pem')
            configDict['access_certificate'] = cert_file_name
            voice_provider.loadConfig(configDict)

        #
        # test if access_certificate is optional
        #

        del configDict['access_certificate']
        voice_provider.loadConfig(configDict)

        # restore access certificate parameter
        cert_file_name = os.path.join(self.fixture_path, 'cert.pem')
        configDict['access_certificate'] = cert_file_name

        # check if missing server_url is as well detected
        with pytest.raises(KeyError):
            del configDict['server_url']
            voice_provider.loadConfig(configDict)

        # restore required server_url
        configDict['server_url'] = self.VCS_URL

        #
        # check if server cert is provided, the existance of directory or
        # file is made
        #

        server_cert_file_name = os.path.join(self.fixture_path, 'cert.pem')
        configDict['server_certificate'] = server_cert_file_name
        voice_provider.loadConfig(configDict)

        with pytest.raises(IOError):
            server_cert_file_name = '/abc/ssl/certs'
            configDict['server_certificate'] = server_cert_file_name
            voice_provider.loadConfig(configDict)

        return
Ejemplo n.º 8
0
    def test_read_config(self):
        """
        test push provider configuration handling
        """

        voice_provider = CustomVoiceProvider()

        #
        # first test the valid configuration
        #
        configDict = {
            "access_certificate": os.path.join(self.fixture_path, 'cert.pem'),
            }

        configDict['twilioConfig'] = {
            'accountSid': 'ACf9095f540f0b090edbd239b99230a8ee',
            'authToken': '8f36aab7ca485b432500ce49c15280c5',
            'voice': 'alice',
            'callerNumber': '+4989231234567',
            }

        configDict['Timeout'] = '30'
        configDict['server_url'] = self.VCS_URL

        voice_provider.loadConfig(configDict)

        #
        # verify server url check
        #

        with self.assertRaises(requests.exceptions.InvalidSchema):
            configDict['server_url'] = "hXXXs://vcs.keyidentity.com:8800/send"
            voice_provider.loadConfig(configDict)

        #
        # restore configuration for server_url
        #

        configDict['server_url'] = self.VCS_URL

        #
        # extended option: proxy
        #

        configDict['proxy'] = "https://proxy.keyidentity.com:8800/"
        voice_provider.loadConfig(configDict)

        #
        # extended option: proxy with wrong url scheme
        #

        with self.assertRaises(requests.exceptions.InvalidSchema):
            configDict['proxy'] = "hXXXs://proxy.keyidentity.com:8800/"
            voice_provider.loadConfig(configDict)

        # restore valid proxy url
        configDict['proxy'] = "https://proxy.keyidentity.com:8800/"

        #
        # valid extended timeout format
        #

        configDict['timeout'] = '3,10'
        voice_provider.loadConfig(configDict)

        del configDict['timeout']

        #
        # invalid timeout format: "invalid literal for float()"
        #

        with self.assertRaises(ValueError):
            configDict['Timeout'] = '30s'
            voice_provider.loadConfig(configDict)

        # timeout has a default and is not required
        del configDict['Timeout']

        #
        # non existing certificate file - should raise exception
        # 'required authenticating client cert could not be found'
        #

        with self.assertRaises(IOError):
            cert_file_name = os.path.join(self.fixture_path, 'non_exist.pem')
            configDict['access_certificate'] = cert_file_name
            voice_provider.loadConfig(configDict)

        #
        # test if access_certificate is optional
        #

        del configDict['access_certificate']
        voice_provider.loadConfig(configDict)

        # restore access certificate parameter
        cert_file_name = os.path.join(self.fixture_path, 'cert.pem')
        configDict['access_certificate'] = cert_file_name

        # check if missing server_url is as well detected
        with self.assertRaises(KeyError):
            del configDict['server_url']
            voice_provider.loadConfig(configDict)

        # restore required server_url
        configDict['server_url'] = self.VCS_URL

        #
        # check if server cert is provided, the existance of directory or
        # file is made
        #

        server_cert_file_name = os.path.join(self.fixture_path, 'cert.pem')
        configDict['server_certificate'] = server_cert_file_name
        voice_provider.loadConfig(configDict)

        with self.assertRaises(IOError):
            server_cert_file_name = '/abc/ssl/certs'
            configDict['server_certificate'] = server_cert_file_name
            voice_provider.loadConfig(configDict)

        return
Ejemplo n.º 9
0
    def test_request(self):
        """
        do some mocking of a requests request
        """

        configDict = {
            "access_certificate": os.path.join(self.fixture_path, 'cert.pem'),
            }

        configDict['twilioConfig'] = {
            'accountSid': 'ACf9095f540f0b090edbd239b99230a8ee',
            'authToken': '8f36aab7ca485b432500ce49c15280c5',
            'callerNumber': '+4989231234567',
            'voice': 'alice',
            }

        configDict['Timeout'] = '30'
        configDict['server_url'] = self.VCS_URL

        voice_provider = CustomVoiceProvider()
        voice_provider.loadConfig(configDict)

        messageTemplate = "Your otp is {otp}"
        otp = '432423'
        locale = 'en'
        calleeNumber = "+49 6151 860 860"

        # set the response status
        TestVoiceProviderController.R_AUTH_STATUS = 200

        # run the fake request
        status, response = voice_provider.submitVoiceMessage(
                                            calleeNumber,
                                            messageTemplate,
                                            otp,
                                            locale)

        self.assertEquals(status, True)
        self.assertTrue(VALID_REQUEST in response)

        request_json = json.loads(response)

        target_url = 'https://vcs.keyidentity.com/v1/twilio/call'
        self.assertTrue(target_url in request_json.get('url'))

        # compare the request dictionary, with the expected structure

        cmp_content = {
            'call':{
                'messageTemplate': 'Your otp is {otp}',
                'otp': '432423',
                'locale': 'en',
                'calleeNumber': '+49 6151 860 860',
                'twilioConfig': {
                    'authToken': '8f36aab7ca485b432500ce49c15280c5',
                    'accountSid': 'ACf9095f540f0b090edbd239b99230a8ee',
                    'voice': 'alice',
                    'callerNumber': '+4989231234567'}}}

        self.assertTrue(cmp(request_json.get('body'), cmp_content) == 0)

        return