diff options
Diffstat (limited to 'service/test')
5 files changed, 128 insertions, 13 deletions
diff --git a/service/test/integration/test_retrieve_attachment.py b/service/test/integration/test_retrieve_attachment.py index 7de03c59..4aaeadc2 100644 --- a/service/test/integration/test_retrieve_attachment.py +++ b/service/test/integration/test_retrieve_attachment.py @@ -27,22 +27,28 @@ from test.support.integration.soledad_test_base import SoledadTestBase class RetrieveAttachmentTest(SoledadTestBase): - @defer.inlineCallbacks def test_attachment_content_is_retrieved(self): attachment_id, input_mail = self._create_mail_with_attachment() yield self.mail_store.add_mail('INBOX', input_mail.as_string()) - attachment, req = yield self.get_attachment(attachment_id, 'base64') + requested_filename = "file name with space" + expected_content_type = 'text/plain' + expected_content_disposition = 'attachment; filename="file name with space"' + + attachment, req = yield self.get_attachment(attachment_id, 'base64', filename=requested_filename, content_type=expected_content_type) self.assertEqual(200, req.code) self.assertEquals('pretend to be binary attachment data', attachment) + self.assertEquals(expected_content_disposition, req.outgoingHeaders['content-disposition']) + self.assertEquals(expected_content_type, req.outgoingHeaders['content-type']) def _create_mail_with_attachment(self): input_mail = MIMEMultipart() input_mail.attach(MIMEText(u'a utf8 message', _charset='utf-8')) attachment = MIMEApplication('pretend to be binary attachment data') - attachment.add_header('Content-Disposition', 'attachment', filename='filename.txt') + attachment.add_header('Content-Disposition', 'attachment', filename='file name.txt') + attachment.add_header('Content-Type', 'text/plain') input_mail.attach(attachment) attachment_id = 'B5B4ED80AC3B894523D72E375DACAA2FC6606C18EDF680FE95903086C8B5E14A' return attachment_id, input_mail diff --git a/service/test/support/integration/app_test_client.py b/service/test/support/integration/app_test_client.py index 99f4ebc7..8ab58397 100644 --- a/service/test/support/integration/app_test_client.py +++ b/service/test/support/integration/app_test_client.py @@ -322,8 +322,13 @@ class AppTestClient(object): defer.returnValue(mails) @defer.inlineCallbacks - def get_attachment(self, ident, encoding): - deferred_result, req = self.get("/attachment/%s" % ident, {'encoding': [encoding]}, as_json=False) + def get_attachment(self, ident, encoding, filename=None, content_type=None): + params = {'encoding': [encoding]} + if filename: + params['filename'] = [filename] + if content_type: + params['content_type'] = [content_type] + deferred_result, req = self.get("/attachment/%s" % ident, params, as_json=False) res = yield deferred_result defer.returnValue((res, req)) diff --git a/service/test/unit/bitmask_libraries/test_smtp_cert_downloader.py b/service/test/unit/bitmask_libraries/test_smtp_cert_downloader.py index 5644ab6a..cfc9353d 100644 --- a/service/test/unit/bitmask_libraries/test_smtp_cert_downloader.py +++ b/service/test/unit/bitmask_libraries/test_smtp_cert_downloader.py @@ -21,6 +21,7 @@ from tempfile import NamedTemporaryFile from httmock import all_requests, HTTMock, urlmatch CERTIFICATE_DATA = 'some cert data' +USERNAME = 'some_user_name' @all_requests @@ -29,12 +30,17 @@ def not_found_mock(url, request): 'content': 'foobar'} -@urlmatch(netloc='api.some-server.test:4430', path='/1/cert') -def ca_cert_mock(url, request): - return { - "status_code": 200, - "content": CERTIFICATE_DATA - } +@urlmatch(netloc='api.some-server.test:4430', path='/1/smtp_cert', method='POST') +def smtp_cert_mock(url, request): + if request.body == 'address=%s' % USERNAME: + return { + "status_code": 200, + "content": CERTIFICATE_DATA + } + else: + return { + 'status_code': 401 + } class TestSmtpCertDownloader(unittest.TestCase): @@ -50,6 +56,7 @@ class TestSmtpCertDownloader(unittest.TestCase): self._provider.api_version = '1' self._provider.server_name = 'some.host.tld' + self._auth.username = USERNAME self._auth.session_id = 'some session id' self._auth.token = 'some token' @@ -57,7 +64,7 @@ class TestSmtpCertDownloader(unittest.TestCase): unstub() def test_download_certificate(self): - with HTTMock(ca_cert_mock, not_found_mock): + with HTTMock(smtp_cert_mock, not_found_mock): cert_data = SmtpCertDownloader(self._provider, self._auth).download() self.assertEqual(CERTIFICATE_DATA, cert_data) @@ -71,7 +78,7 @@ class TestSmtpCertDownloader(unittest.TestCase): downloader = SmtpCertDownloader(self._provider, self._auth) with NamedTemporaryFile() as tmp_file: - with HTTMock(ca_cert_mock, not_found_mock): + with HTTMock(smtp_cert_mock, not_found_mock): downloader.download_to(tmp_file.name) file_content = open(tmp_file.name).read() diff --git a/service/test/unit/bitmask_libraries/test_smtp_client_certificate.py b/service/test/unit/bitmask_libraries/test_smtp_client_certificate.py new file mode 100644 index 00000000..155f46e9 --- /dev/null +++ b/service/test/unit/bitmask_libraries/test_smtp_client_certificate.py @@ -0,0 +1,66 @@ +# +# Copyright (c) 2016 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 <http://www.gnu.org/licenses/>. +import os +import unittest +import tempdir +from pixelated.bitmask_libraries import session +from leap.srp_session import SRPSession +import leap.common.certs as certs +from mockito import mock, unstub, when, verify, never, any as ANY + +from pixelated.bitmask_libraries.session import SmtpClientCertificate + + +class TestSmtpClientCertificate(unittest.TestCase): + + def setUp(self): + self.tmp_dir = tempdir.TempDir() + self.provider = mock() + self.provider.domain = 'some-provider.tld' + self.auth = SRPSession('username', 'token', 'uuid', 'session_id') + self.pem_path = os.path.join(self.tmp_dir.name, 'providers', 'some-provider.tld', 'keys', 'client', 'smtp.pem') + self.downloader = mock() + when(session).SmtpCertDownloader(self.provider, self.auth).thenReturn(self.downloader) + + def tearDown(self): + self.tmp_dir.dissolve() + unstub() + + def test_download_certificate(self): + cert = SmtpClientCertificate(self.provider, self.auth, self.tmp_dir.name) + result = cert.cert_path() + + self.assertEqual(self.pem_path, result) + verify(self.downloader).download_to(self.pem_path) + + def test_download_certificate_if_redownload_necessary(self): + when(os.path).exists(self.pem_path).thenReturn(True) + when(certs).should_redownload(self.pem_path).thenReturn(True) + + cert = SmtpClientCertificate(self.provider, self.auth, self.tmp_dir.name) + result = cert.cert_path() + + self.assertEqual(self.pem_path, result) + verify(self.downloader).download_to(self.pem_path) + + def test_skip_download_if_already_downloaded_and_still_valid(self): + when(os.path).exists(self.pem_path).thenReturn(True) + when(certs).should_redownload(ANY()).thenReturn(False) + cert = SmtpClientCertificate(self.provider, self.auth, self.tmp_dir.name) + result = cert.cert_path() + + self.assertEqual(self.pem_path, result) + verify(self.downloader, never).download_to(ANY()) diff --git a/service/test/unit/resources/test_login_resource.py b/service/test/unit/resources/test_login_resource.py index bc238ae8..3cd9d3b2 100644 --- a/service/test/unit/resources/test_login_resource.py +++ b/service/test/unit/resources/test_login_resource.py @@ -1,3 +1,5 @@ +import os + import test.support.mockito from leap.exceptions import SRPAuthenticationError @@ -58,16 +60,45 @@ class TestLoginResource(unittest.TestCase): input_username = 'name="username"' input_password = 'name="password"' input_submit = 'name="login"' + default_disclaimer = 'Some disclaimer' written_response = ''.join(request.written) self.assertIn(form_action, written_response) self.assertIn(form_method, written_response) self.assertIn(input_password, written_response) self.assertIn(input_submit, written_response) self.assertIn(input_username, written_response) + self.assertIn(default_disclaimer, written_response) d.addCallback(assert_form_rendered) return d + def _write(self, filename, content): + with open(filename, 'w') as disclaimer_file: + disclaimer_file.write(content) + + def test_override_login_disclaimer_message(self): + request = DummyRequest(['']) + + banner_file_name = 'banner.txt' + banner_disclaimer_content = '<p>some custom disclaimer</p>' + self._write(banner_file_name, banner_disclaimer_content) + + self.resource._disclaimer_banner = 'service/_trial_temp/' + banner_file_name + + d = self.web.get(request) + + def assert_custom_disclaimer_rendered(_): + self.assertEqual(200, request.responseCode) + written_response = ''.join(request.written) + self.assertIn(banner_disclaimer_content, written_response) + + def tear_down(_): + os.remove(banner_file_name) + + d.addCallback(assert_custom_disclaimer_rendered) + d.addCallback(tear_down) + return d + class TestLoginPOST(unittest.TestCase): def setUp(self): |