Esempio n. 1
0
 def setUp(self):
     config = {
         'phone.api_key': 'test-api-key',
         'phone.api_secret': 'test-api-secret',
         'site_name': 'Very loooooooooong site name'
     }
     self.phone = NexmoPhoneService(config)
Esempio n. 2
0
 def setUp(self):
     config = {'phone.api_key': 'test-api-key',
               'phone.api_secret': 'test-api-secret',
               'site_name': 'Very loooooooooong site name'}
     self.phone = NexmoPhoneService(config)
Esempio n. 3
0
class TestPhoneService(object):
    def setUp(self):
        config = {
            'phone.api_key': 'test-api-key',
            'phone.api_secret': 'test-api-secret',
            'site_name': 'Very loooooooooong site name'
        }
        self.phone = NexmoPhoneService(config)

    def test_add_common_params(self):
        params = {'number': '1234567890', 'brand': 'Allura'}
        res = self.phone.add_common_params(params)
        expected = {
            'number': '1234567890',
            'brand': 'Allura',
            'api_key': 'test-api-key',
            'api_secret': 'test-api-secret'
        }
        assert_equal(expected, res)

        self.phone.config['phone.lang'] = 'it-it'
        res = self.phone.add_common_params(params)
        expected['lg'] = 'it-it'
        assert_equal(expected, res)

    def test_error(self):
        res = self.phone.error()
        expected = {
            'status': 'error',
            'error': 'Failed sending request to Nexmo'
        }
        assert_equal(expected, res)
        # not allowed code
        res = self.phone.error(code='2', msg='text')
        assert_equal(expected, res)
        # allowed code
        res = self.phone.error(code='15', msg='text')
        expected = {'status': 'error', 'error': 'text'}
        assert_equal(expected, res)

        # invalid format, possibly US
        res = self.phone.error(code='3',
                               msg='Invalid value for parameter: number',
                               number='8005551234')
        assert_equal(res['status'], 'error')
        assert_in('Invalid value for parameter: number', res['error'])
        assert_in('country code', res['error'])
        assert_in('US', res['error'])

        # invalid format, not US
        res = self.phone.error(code='3',
                               msg='Invalid value for parameter: number',
                               number='738005551234')
        assert_equal(res['status'], 'error')
        assert_in('Invalid value for parameter: number', res['error'])
        assert_in('country code', res['error'])
        assert_not_in('US', res['error'])

    def test_ok(self):
        res = self.phone.ok(request_id='123', other='smth')
        expected = {'status': 'ok', 'request_id': '123', 'other': 'smth'}
        assert_equal(expected, res)

    @patch('allura.lib.phone.nexmo.requests', autospec=True)
    def test_verify(self, req):
        req.post.return_value.json.return_value = {
            'request_id': 'test-req-id',
            'status': '0',
        }
        data = json.dumps(
            {
                'number': '1234567890',
                'api_key': 'test-api-key',
                'api_secret': 'test-api-secret',
                'brand': 'Very loooooooooong',
            },
            sort_keys=True)
        headers = {'Content-Type': 'application/json'}

        resp = self.phone.verify('1234567890')
        expected = {'status': 'ok', 'request_id': 'test-req-id'}
        assert_equal(expected, resp)
        req.post.assert_called_once_with('https://api.nexmo.com/verify/json',
                                         data=data,
                                         headers=headers)

        req.post.reset_mock()
        req.post.return_value.json.return_value = {
            'status': '3',
            'error_text': 'Something went wrong',
        }
        resp = self.phone.verify('1234567890')
        expected = {'status': 'error', 'error': 'Something went wrong'}
        assert_equal(expected, resp)
        req.post.assert_called_once_with('https://api.nexmo.com/verify/json',
                                         data=data,
                                         headers=headers)

    @patch('allura.lib.phone.nexmo.requests', autospec=True)
    def test_verify_exception(self, req):
        req.post.side_effect = Exception('Boom!')
        resp = self.phone.verify('1234567890')
        expected = {
            'status': 'error',
            'error': 'Failed sending request to Nexmo'
        }
        assert_equal(expected, resp)

    @patch('allura.lib.phone.nexmo.requests', autospec=True)
    def test_check(self, req):
        req.post.return_value.json.return_value = {
            'request_id': 'test-req-id',
            'status': '0',
        }
        data = json.dumps(
            {
                'request_id': 'test-req-id',
                'code': '1234',
                'api_key': 'test-api-key',
                'api_secret': 'test-api-secret',
            },
            sort_keys=True)
        headers = {'Content-Type': 'application/json'}

        resp = self.phone.check('test-req-id', '1234')
        expected = {'status': 'ok', 'request_id': 'test-req-id'}
        assert_equal(expected, resp)
        req.post.assert_called_once_with(
            'https://api.nexmo.com/verify/check/json',
            data=data,
            headers=headers)

        req.post.reset_mock()
        req.post.return_value.json.return_value = {
            'status': '3',
            'error_text': 'Something went wrong',
        }
        resp = self.phone.check('test-req-id', '1234')
        expected = {'status': 'error', 'error': 'Something went wrong'}
        assert_equal(expected, resp)
        req.post.assert_called_once_with(
            'https://api.nexmo.com/verify/check/json',
            data=data,
            headers=headers)

    @patch('allura.lib.phone.nexmo.requests', autospec=True)
    def test_check_exception(self, req):
        req.post.side_effect = Exception('Boom!')
        resp = self.phone.check('req-id', '1234')
        expected = {
            'status': 'error',
            'error': 'Failed sending request to Nexmo'
        }
        assert_equal(expected, resp)
Esempio n. 4
0
class TestPhoneService(object):

    def setUp(self):
        config = {'phone.api_key': 'test-api-key',
                  'phone.api_secret': 'test-api-secret',
                  'site_name': 'Very loooooooooong site name'}
        self.phone = NexmoPhoneService(config)

    def test_add_common_params(self):
        params = {'number': '1234567890', 'brand': 'Allura'}
        res = self.phone.add_common_params(params)
        expected = {'number': '1234567890',
                    'brand': 'Allura',
                    'api_key': 'test-api-key',
                    'api_secret': 'test-api-secret'}
        assert_equal(expected, res)

        self.phone.config['phone.lang'] = 'it-it'
        res = self.phone.add_common_params(params)
        expected['lg'] = 'it-it'
        assert_equal(expected, res)

    def test_error(self):
        res = self.phone.error()
        expected = {'status': 'error',
                    'error': 'Failed sending request to Nexmo'}
        assert_equal(expected, res)
        # not allowed code
        res = self.phone.error(code='2', msg='text')
        assert_equal(expected, res)
        # allowed code
        res = self.phone.error(code='15', msg='text')
        expected = {'status': 'error', 'error': 'text'}
        assert_equal(expected, res)

        # invalid format, possibly US
        res = self.phone.error(code='3', msg='Invalid value for parameter: number', number='8005551234')
        assert_equal(res['status'], 'error')
        assert_in('Invalid value for parameter: number', res['error'])
        assert_in('country code', res['error'])
        assert_in('US', res['error'])

        # invalid format, not US
        res = self.phone.error(code='3', msg='Invalid value for parameter: number', number='738005551234')
        assert_equal(res['status'], 'error')
        assert_in('Invalid value for parameter: number', res['error'])
        assert_in('country code', res['error'])
        assert_not_in('US', res['error'])

    def test_ok(self):
        res = self.phone.ok(request_id='123', other='smth')
        expected = {'status': 'ok', 'request_id': '123', 'other': 'smth'}
        assert_equal(expected, res)

    @patch('allura.lib.phone.nexmo.requests', autospec=True)
    def test_verify(self, req):
        req.post.return_value.json.return_value = {
            'request_id': 'test-req-id',
            'status': '0',
        }
        data = json.dumps({
            'number': '1234567890',
            'api_key': 'test-api-key',
            'api_secret': 'test-api-secret',
            'brand': 'Very loooooooooong',
        }, sort_keys=True)
        headers = {'Content-Type': 'application/json'}

        resp = self.phone.verify('1234567890')
        expected = {'status': 'ok', 'request_id': 'test-req-id'}
        assert_equal(expected, resp)
        req.post.assert_called_once_with(
            'https://api.nexmo.com/verify/json',
            data=data,
            headers=headers)

        req.post.reset_mock()
        req.post.return_value.json.return_value = {
            'status': '3',
            'error_text': 'Something went wrong',
        }
        resp = self.phone.verify('1234567890')
        expected = {'status': 'error', 'error': 'Something went wrong'}
        assert_equal(expected, resp)
        req.post.assert_called_once_with(
            'https://api.nexmo.com/verify/json',
            data=data,
            headers=headers)

    @patch('allura.lib.phone.nexmo.requests', autospec=True)
    def test_verify_exception(self, req):
        req.post.side_effect = Exception('Boom!')
        resp = self.phone.verify('1234567890')
        expected = {'status': 'error',
                    'error': 'Failed sending request to Nexmo'}
        assert_equal(expected, resp)

    @patch('allura.lib.phone.nexmo.requests', autospec=True)
    def test_check(self, req):
        req.post.return_value.json.return_value = {
            'request_id': 'test-req-id',
            'status': '0',
        }
        data = json.dumps({
            'request_id': 'test-req-id',
            'code': '1234',
            'api_key': 'test-api-key',
            'api_secret': 'test-api-secret',
        }, sort_keys=True)
        headers = {'Content-Type': 'application/json'}

        resp = self.phone.check('test-req-id', '1234')
        expected = {'status': 'ok', 'request_id': 'test-req-id'}
        assert_equal(expected, resp)
        req.post.assert_called_once_with(
            'https://api.nexmo.com/verify/check/json',
            data=data,
            headers=headers)

        req.post.reset_mock()
        req.post.return_value.json.return_value = {
            'status': '3',
            'error_text': 'Something went wrong',
        }
        resp = self.phone.check('test-req-id', '1234')
        expected = {'status': 'error', 'error': 'Something went wrong'}
        assert_equal(expected, resp)
        req.post.assert_called_once_with(
            'https://api.nexmo.com/verify/check/json',
            data=data,
            headers=headers)

    @patch('allura.lib.phone.nexmo.requests', autospec=True)
    def test_check_exception(self, req):
        req.post.side_effect = Exception('Boom!')
        resp = self.phone.check('req-id', '1234')
        expected = {'status': 'error',
                    'error': 'Failed sending request to Nexmo'}
        assert_equal(expected, resp)