You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by je...@apache.org on 2015/05/12 14:05:25 UTC
[04/14] allura git commit: [#7868] ticket:759 Add test for phone
service
[#7868] ticket:759 Add test for phone service
Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/8958a996
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/8958a996
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/8958a996
Branch: refs/heads/ib/7868
Commit: 8958a99683d95f6af5a672d6ce8b79f26cbb5d83
Parents: 42b62a2
Author: Igor Bondarenko <je...@gmail.com>
Authored: Mon May 4 17:30:13 2015 +0000
Committer: Igor Bondarenko <je...@gmail.com>
Committed: Mon May 4 17:30:13 2015 +0000
----------------------------------------------------------------------
Allura/allura/lib/phone/nexmo.py | 2 +-
Allura/allura/tests/unit/phone/__init__.py | 16 +++
Allura/allura/tests/unit/phone/test_nexmo.py | 136 +++++++++++++++++++
.../tests/unit/phone/test_phone_service.py | 57 ++++++++
4 files changed, 210 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/allura/blob/8958a996/Allura/allura/lib/phone/nexmo.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/phone/nexmo.py b/Allura/allura/lib/phone/nexmo.py
index 1bf7643..0e53a60 100644
--- a/Allura/allura/lib/phone/nexmo.py
+++ b/Allura/allura/lib/phone/nexmo.py
@@ -67,7 +67,7 @@ class NexmoPhoneService(object):
url += '/'
url = urljoin(url, 'json')
headers = {'Content-Type': 'application/json'}
- params = json.dumps(self.add_common_params(params))
+ params = json.dumps(self.add_common_params(params), sort_keys=True)
log.info('PhoneService (nexmo) request: %s %s', url, params)
try:
resp = requests.post(url, data=params, headers=headers)
http://git-wip-us.apache.org/repos/asf/allura/blob/8958a996/Allura/allura/tests/unit/phone/__init__.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/unit/phone/__init__.py b/Allura/allura/tests/unit/phone/__init__.py
new file mode 100644
index 0000000..144e298
--- /dev/null
+++ b/Allura/allura/tests/unit/phone/__init__.py
@@ -0,0 +1,16 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
http://git-wip-us.apache.org/repos/asf/allura/blob/8958a996/Allura/allura/tests/unit/phone/test_nexmo.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/unit/phone/test_nexmo.py b/Allura/allura/tests/unit/phone/test_nexmo.py
new file mode 100644
index 0000000..6f6ec42
--- /dev/null
+++ b/Allura/allura/tests/unit/phone/test_nexmo.py
@@ -0,0 +1,136 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import json
+from mock import patch
+from datadiff.tools import assert_equal
+
+from allura.lib.phone.nexmo import NexmoPhoneService
+
+
+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)
+
+ def test_error(self):
+ res = self.phone.error('text')
+ expected = {'status': 'error', 'error': 'text'}
+ assert_equal(expected, res)
+
+ 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': '1',
+ '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': '1',
+ '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)
http://git-wip-us.apache.org/repos/asf/allura/blob/8958a996/Allura/allura/tests/unit/phone/test_phone_service.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/unit/phone/test_phone_service.py b/Allura/allura/tests/unit/phone/test_phone_service.py
new file mode 100644
index 0000000..aa6bd51
--- /dev/null
+++ b/Allura/allura/tests/unit/phone/test_phone_service.py
@@ -0,0 +1,57 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from nose.tools import assert_true
+from datadiff.tools import assert_equal
+
+from allura.lib.phone import PhoneService
+
+
+class MockPhoneService(PhoneService):
+
+ def verify(*args, **kw):
+ return {'status': 'ok', 'request_id': 'test-request'}
+
+ def check(*args, **kw):
+ return {'status': 'ok'}
+
+
+class TestPhoneService(object):
+
+ def test_verify(self):
+ res = PhoneService({}).verify('1234567890')
+ expected = {'status': 'error',
+ 'error': 'Phone service is not configured'}
+ assert_equal(res, expected)
+
+ def test_check(self):
+ res = PhoneService({}).check('test-req-id', '1111')
+ expected = {'status': 'error',
+ 'error': 'Phone service is not configured'}
+ assert_equal(res, expected)
+
+ def test_get_default(self):
+ config = {}
+ entry_points = None
+ phone = PhoneService.get(config, entry_points)
+ assert_true(isinstance(phone, PhoneService))
+
+ def test_get_method(self):
+ config = {'phone.method': 'mock'}
+ entry_points = {'mock': MockPhoneService}
+ phone = PhoneService.get(config, entry_points)
+ assert_true(isinstance(phone, MockPhoneService))