From 30fe9e6c31ed8d5d0ee76c13e3344e67e26f1479 Mon Sep 17 00:00:00 2001 From: rafael lisboa Date: Mon, 4 May 2015 12:52:47 -0300 Subject: update tests --- .../unit/bitmask_libraries/test_abstract_leap.py | 12 +- .../test/unit/bitmask_libraries/test_leap_srp.py | 157 --------------------- .../test/unit/bitmask_libraries/test_nicknym.py | 18 ++- .../test/unit/bitmask_libraries/test_session.py | 2 +- service/test/unit/bitmask_libraries/test_smtp.py | 9 +- .../test/unit/bitmask_libraries/test_soledad.py | 12 +- 6 files changed, 34 insertions(+), 176 deletions(-) delete mode 100644 service/test/unit/bitmask_libraries/test_leap_srp.py (limited to 'service/test/unit') diff --git a/service/test/unit/bitmask_libraries/test_abstract_leap.py b/service/test/unit/bitmask_libraries/test_abstract_leap.py index c11c7ea9..64de09bc 100644 --- a/service/test/unit/bitmask_libraries/test_abstract_leap.py +++ b/service/test/unit/bitmask_libraries/test_abstract_leap.py @@ -22,9 +22,9 @@ from mock import Mock, MagicMock class AbstractLeapTest(unittest.TestCase): - uuid = str(uuid4()) - session_id = str(uuid4()) - token = str(uuid4()) + _uuid = str(uuid4()) + _session_id = str(uuid4()) + _token = str(uuid4()) leap_home = os.path.join(tempfile.mkdtemp(), 'leap') @@ -33,7 +33,11 @@ class AbstractLeapTest(unittest.TestCase): api_uri='https://api.some-server.test:4430', api_version='1') soledad = Mock() soledad_session = Mock(soledad=soledad) - srp_session = Mock(user_name='test_user', api_server_name='some-server.test', uuid=uuid, session_id=session_id, token=token) + auth = Mock(username='test_user', + api_server_name='some-server.test', + uuid=_uuid, + session_id=_session_id, + token=_token) nicknym = MagicMock() diff --git a/service/test/unit/bitmask_libraries/test_leap_srp.py b/service/test/unit/bitmask_libraries/test_leap_srp.py deleted file mode 100644 index 6d067e5d..00000000 --- a/service/test/unit/bitmask_libraries/test_leap_srp.py +++ /dev/null @@ -1,157 +0,0 @@ -# -# Copyright (c) 2014 ThoughtWorks, Inc. -# -# Pixelated is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Pixelated is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with Pixelated. If not, see . -import json -import unittest -import binascii -from urlparse import parse_qs - -from httmock import urlmatch, all_requests, HTTMock, response -from requests.exceptions import Timeout -import srp -from pixelated.bitmask_libraries.leap_srp import LeapSecureRemotePassword, LeapAuthException - - -(salt_bytes, verification_key_bytes) = srp.create_salted_verification_key('username', 'password', hash_alg=srp.SHA256, ng_type=srp.NG_1024) -verifier = None - - -@all_requests -def not_found_mock(url, request): - return {'status_code': 404, - 'content': 'foobar'} - - -@all_requests -def timeout_mock(url, request): - raise Timeout() - - -@urlmatch(netloc=r'(.*\.)?leap\.local$') -def srp_login_server_simulator_mock(url, request): - global verifier - - data = parse_qs(request.body) - if 'login' in data: - # SRP Authentication Step 1 - A = binascii.unhexlify(data.get('A')[0]) - - verifier = srp.Verifier('username', salt_bytes, verification_key_bytes, A, hash_alg=srp.SHA256, ng_type=srp.NG_1024) - (salt, B) = verifier.get_challenge() - - content = { - 'salt': binascii.hexlify(salt), - 'B': binascii.hexlify(B) - } - - return {'status_code': 200, - 'content': json.dumps(content)} - - else: - # SRP Authentication Step 2 - data = parse_qs(request.body) - client_auth = binascii.unhexlify(data.get('client_auth')[0]) - - M2 = verifier.verify_session(client_auth) - - if not verifier.authenticated(): - return {'status_code': 404, - 'content': ''} - - content = { - 'M2': binascii.hexlify(M2), - 'id': 'some id', - 'token': 'some token' - } - headers = { - 'Content-Type': 'application/json', - 'Set-Cookie': '_session_id=some_session_id;'} - return response(200, content, headers, None, 5, request) - - -class LeapSRPTest(unittest.TestCase): - - def test_status_code_is_checked(self): - with HTTMock(not_found_mock): - lsrp = LeapSecureRemotePassword() - self.assertRaises(LeapAuthException, lsrp.authenticate, 'https://api.leap.local', 'username', 'password') - - def test_invalid_username(self): - with HTTMock(srp_login_server_simulator_mock): - lsrp = LeapSecureRemotePassword() - self.assertRaises(LeapAuthException, lsrp.authenticate, 'https://api.leap.local', 'invalid_user', 'password') - - def test_invalid_password(self): - with HTTMock(srp_login_server_simulator_mock): - lsrp = LeapSecureRemotePassword() - self.assertRaises(LeapAuthException, lsrp.authenticate, 'https://api.leap.local', 'username', 'invalid') - - def test_login(self): - with HTTMock(srp_login_server_simulator_mock): - lsrp = LeapSecureRemotePassword() - leap_session = lsrp.authenticate('https://api.leap.local', 'username', 'password') - - self.assertIsNotNone(leap_session) - self.assertEqual('username', leap_session.user_name) - self.assertEqual('1', leap_session.api_version) - self.assertEqual('https://api.leap.local', leap_session.api_server_name) - self.assertEqual('some token', leap_session.token) - self.assertEqual('some_session_id', leap_session.session_id) - - def test_timeout(self): - with HTTMock(timeout_mock): - lrsp = LeapSecureRemotePassword() - self.assertRaises(LeapAuthException, lrsp.authenticate, 'https://api.leap.local', 'username', 'password') - - def test_register_raises_auth_exception_on_error(self): - with HTTMock(not_found_mock): - lsrp = LeapSecureRemotePassword() - self.assertRaises(LeapAuthException, lsrp.register, 'https://api.leap.local', 'username', 'password') - - def test_register(self): - @urlmatch(netloc=r'(.*\.)?leap\.local$', path='/1/users') - def register_success(url, request): - - content = { - 'login': 'username', - 'ok': True - } - - return {'status_code': 201, - 'content': content} - - with HTTMock(register_success, not_found_mock): - lsrp = LeapSecureRemotePassword() - self.assertTrue(lsrp.register('https://api.leap.local', 'username', 'password')) - - def test_register_user_exists(self): - @urlmatch(netloc=r'(.*\.)?leap\.local$', path='/1/users') - def register_error_user_exists(url, request): - content = {"errors": { - "login": [ - "has already been taken", "has already been taken", "has already been taken" - ]}} - - return {'status_code': 422, - 'content': content} - - with HTTMock(register_error_user_exists, not_found_mock): - lsrp = LeapSecureRemotePassword() - self.assertRaises(LeapAuthException, lsrp.register, 'https://api.leap.local', 'username', 'password') - - def test_registration_timeout(self): - with HTTMock(timeout_mock): - lsrp = LeapSecureRemotePassword() - self.assertRaises(LeapAuthException, lsrp.register, 'https://api.leap.local', 'username', 'password') diff --git a/service/test/unit/bitmask_libraries/test_nicknym.py b/service/test/unit/bitmask_libraries/test_nicknym.py index 6e589ae9..b892c22c 100644 --- a/service/test/unit/bitmask_libraries/test_nicknym.py +++ b/service/test/unit/bitmask_libraries/test_nicknym.py @@ -26,12 +26,17 @@ class NickNymTest(AbstractLeapTest): # given # when - NickNym(self.provider, self.config, self.soledad_session, self.srp_session) + NickNym(self.provider, + self.config, + self.soledad_session, + self.auth.username, + self.auth.token, + self.auth.uuid) # then init_mock.assert_called_with('test_user@some-server.test', 'https://nicknym.some-server.test:6425/', - self.soledad, self.token, '/some/path/to/provider_ca_cert', - 'https://api.some-server.test:4430', '1', self.uuid, + self.soledad, self.auth.token, '/some/path/to/provider_ca_cert', + 'https://api.some-server.test:4430', '1', self.auth.uuid, '/path/to/gpg') @patch('pixelated.bitmask_libraries.nicknym.KeyManager') @@ -39,7 +44,12 @@ class NickNymTest(AbstractLeapTest): # given keyman = keymanager_mock.return_value keyman.get_key.side_effect = KeyNotFound - nicknym = NickNym(self.provider, self.config, self.soledad_session, self.srp_session) + nicknym = NickNym(self.provider, + self.config, + self.soledad_session, + self.auth.username, + self.auth.token, + self.auth.uuid) # when/then nicknym.generate_openpgp_key() diff --git a/service/test/unit/bitmask_libraries/test_session.py b/service/test/unit/bitmask_libraries/test_session.py index 0bfd59f2..62330481 100644 --- a/service/test/unit/bitmask_libraries/test_session.py +++ b/service/test/unit/bitmask_libraries/test_session.py @@ -65,7 +65,7 @@ class SessionTest(AbstractLeapTest): self.assertEqual('test_user@some-server.test', session.account_email()) def _create_session(self): - return LeapSession(self.provider, self.srp_session, self.soledad_session, self.nicknym, self.soledad_account, + return LeapSession(self.provider, self.auth, self.soledad_session, self.nicknym, self.soledad_account, self.mail_fetcher_mock, self.smtp_mock) diff --git a/service/test/unit/bitmask_libraries/test_smtp.py b/service/test/unit/bitmask_libraries/test_smtp.py index c0e30573..aa2bc9ba 100644 --- a/service/test/unit/bitmask_libraries/test_smtp.py +++ b/service/test/unit/bitmask_libraries/test_smtp.py @@ -53,7 +53,7 @@ class LeapSmtpTest(AbstractLeapTest): self.config.timeout_in_s = 15 def test_that_client_cert_gets_downloaded(self): - smtp = LeapSmtp(self.provider, self.keymanager, self.srp_session) + smtp = LeapSmtp(self.provider, self.auth.username, self.auth.session_id, self.keymanager) with HTTMock(ca_cert_mock, not_found_mock): smtp._download_client_certificates() @@ -66,7 +66,8 @@ class LeapSmtpTest(AbstractLeapTest): @patch('pixelated.bitmask_libraries.smtp.setup_smtp_gateway') def test_that_start_calls_setup_smtp_gateway(self, gateway_mock): - smtp = LeapSmtp(self.provider, self.keymanager, self.srp_session) + smtp = LeapSmtp(self.provider, self.auth.username, self.auth.session_id, self.keymanager) + port = 500 smtp.TWISTED_PORT = port gateway_mock.return_value = (None, None) @@ -77,14 +78,14 @@ class LeapSmtpTest(AbstractLeapTest): gateway_mock.assert_called_with(keymanager=self.keymanager, smtp_cert=cert_path, smtp_key=cert_path, userid='test_user@some-server.test', smtp_port='1234', encrypted_only=False, smtp_host='smtp.some-sever.test', port=port) def test_that_client_stop_does_nothing_if_not_started(self): - smtp = LeapSmtp(self.provider, self.keymanager, self.srp_session) + smtp = LeapSmtp(self.provider, self.auth.username, self.auth.session_id, self.keymanager) with HTTMock(not_found_mock): smtp.stop() @patch('pixelated.bitmask_libraries.smtp.setup_smtp_gateway') def test_that_running_smtp_sevice_is_stopped(self, gateway_mock): - smtp = LeapSmtp(self.provider, self.keymanager, self.srp_session) + smtp = LeapSmtp(self.provider, self.auth.username, self.auth.session_id, self.keymanager) smtp_service = MagicMock() smtp_port = MagicMock() diff --git a/service/test/unit/bitmask_libraries/test_soledad.py b/service/test/unit/bitmask_libraries/test_soledad.py index a71275e0..a3a1094a 100644 --- a/service/test/unit/bitmask_libraries/test_soledad.py +++ b/service/test/unit/bitmask_libraries/test_soledad.py @@ -34,19 +34,19 @@ class SoledadSessionTest(AbstractLeapTest): @patch('pixelated.bitmask_libraries.soledad.Soledad.__init__') def test_that_soledad_is_created_with_required_params(self, soledad_mock, init_mock): # when - SoledadSession(self.provider, 'any-passphrase', self.srp_session) + SoledadSession(self.provider, 'any-passphrase', self.auth.token, self.auth.uuid) # then - init_mock.assert_called_with(self.uuid, 'any-passphrase', '%s/soledad/%s.secret' % (self.leap_home, self.uuid), - '%s/soledad/%s.db' % (self.leap_home, self.uuid), - 'https://couch1.some-server.test:1234/user-%s' % self.uuid, + init_mock.assert_called_with(self.auth.uuid, 'any-passphrase', '%s/soledad/%s.secret' % (self.leap_home, self.auth.uuid), + '%s/soledad/%s.db' % (self.leap_home, self.auth.uuid), + 'https://couch1.some-server.test:1234/user-%s' % self.auth.uuid, '/some/path/to/ca_cert', self.token, defer_encryption=False) def test_that_sync_is_called(self, soledad_mock): instance = soledad_mock.return_value instance.server_url = '/foo/bar' instance.need_sync.return_value = True - soledad_session = SoledadSession(self.provider, 'any-passphrase', self.srp_session) + soledad_session = SoledadSession(self.provider, 'any-passphrase', self.auth.token, self.auth.uuid) # when soledad_session.sync() @@ -59,7 +59,7 @@ class SoledadSessionTest(AbstractLeapTest): instance = mock.return_value instance.server_url = '/foo/bar' instance.need_sync.return_value = False - soledad_session = SoledadSession(self.provider, 'any-passphrase', self.srp_session) + soledad_session = SoledadSession(self.provider, 'any-passphrase', self.auth.token, self.auth.uuid) # when soledad_session.sync() -- cgit v1.2.3