From ad1e8d322e98c50793749e87e56ace9cccc0ef18 Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Mon, 21 Nov 2016 18:57:26 +0100 Subject: add test stub for PixelatedAuthSessionWrapper --- service/test/unit/resources/test_auth.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 service/test/unit/resources/test_auth.py (limited to 'service/test') diff --git a/service/test/unit/resources/test_auth.py b/service/test/unit/resources/test_auth.py new file mode 100644 index 00000000..5f65e199 --- /dev/null +++ b/service/test/unit/resources/test_auth.py @@ -0,0 +1,30 @@ +import unittest + +from mockito import mock + +from test.unit.resources import DummySite +from twisted.web.test.requesthelper import DummyRequest +from pixelated.resources.auth import PixelatedAuthSessionWrapper + + +class TestRootResource(unittest.TestCase): + + def setUp(self): + self.portal = mock() + self.mock_root_resource = mock() + self.anonymous_resource = mock() + self.credential_factories = mock() + + self.session_wrapper = PixelatedAuthSessionWrapper(self.portal, self.mock_root_resource, self.anonymous_resource, self.credential_factories) + self.web = DummySite(self.session_wrapper) + + def test_should_use_login_resource_when_the_user_is_not_logged_in (self): + request = DummyRequest(['']) + self.session_wrapper.getChildWithDefault('/', request) + + def assert_response(_): + self.assertEquals(len(matches), 1) + + d.addCallback(assert_response) + return d + -- cgit v1.2.3 From d5de22115b4d091469f1abad8d8b9ae7651caa3b Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Tue, 22 Nov 2016 16:16:20 +0100 Subject: fix first test for auth session wrapper --- service/test/unit/resources/test_auth.py | 44 +++++++++++++---------- service/test/unit/resources/test_root_resource.py | 2 +- 2 files changed, 26 insertions(+), 20 deletions(-) (limited to 'service/test') diff --git a/service/test/unit/resources/test_auth.py b/service/test/unit/resources/test_auth.py index 5f65e199..ac2529dd 100644 --- a/service/test/unit/resources/test_auth.py +++ b/service/test/unit/resources/test_auth.py @@ -1,30 +1,36 @@ -import unittest - -from mockito import mock - +from mockito import mock, when, any as ANY +from pixelated.resources.auth import PixelatedAuthSessionWrapper +from pixelated.resources.login_resource import LoginResource +from pixelated.resources.root_resource import RootResource from test.unit.resources import DummySite +from twisted.cred.checkers import ANONYMOUS +from twisted.internet.defer import succeed +from twisted.trial import unittest +from twisted.web.resource import IResource from twisted.web.test.requesthelper import DummyRequest -from pixelated.resources.auth import PixelatedAuthSessionWrapper -class TestRootResource(unittest.TestCase): +class TestPixelatedAuthSessionWrapper(unittest.TestCase): def setUp(self): - self.portal = mock() - self.mock_root_resource = mock() - self.anonymous_resource = mock() - self.credential_factories = mock() + self.portal_mock = mock() + self.root_resource_mock = mock() + self.anonymous_resource_mock = mock() + credential_factories_mock = mock() - self.session_wrapper = PixelatedAuthSessionWrapper(self.portal, self.mock_root_resource, self.anonymous_resource, self.credential_factories) - self.web = DummySite(self.session_wrapper) + self.session_wrapper = PixelatedAuthSessionWrapper(self.portal_mock, self.root_resource_mock, self.anonymous_resource_mock, credential_factories_mock) - def test_should_use_login_resource_when_the_user_is_not_logged_in (self): - request = DummyRequest(['']) - self.session_wrapper.getChildWithDefault('/', request) + def test_should_use_login_resource_when_the_user_is_not_logged_in(self): + request = DummyRequest([]) + request.prepath = [''] + request.path = '/' + when(self.portal_mock).login(ANY(), None, IResource).thenReturn(succeed((IResource, ANONYMOUS, lambda: None))) - def assert_response(_): - self.assertEquals(len(matches), 1) + deferred_resource = self.session_wrapper.getChildWithDefault('/', request) + d = deferred_resource.d - d.addCallback(assert_response) - return d + def assert_anonymous_resource(resource): + self.assertIs(resource, self.anonymous_resource_mock) + d.addCallback(assert_anonymous_resource) + return d diff --git a/service/test/unit/resources/test_root_resource.py b/service/test/unit/resources/test_root_resource.py index 4ff11ce8..7a7b2005 100644 --- a/service/test/unit/resources/test_root_resource.py +++ b/service/test/unit/resources/test_root_resource.py @@ -1,4 +1,3 @@ -import unittest import re from mock import MagicMock, patch @@ -7,6 +6,7 @@ from mockito import mock, when, any as ANY from pixelated.application import UserAgentMode from pixelated.resources.features_resource import FeaturesResource from test.unit.resources import DummySite +from twisted.trial import unittest from twisted.web.test.requesthelper import DummyRequest from pixelated.resources.root_resource import RootResource, MODE_STARTUP, MODE_RUNNING -- cgit v1.2.3 From 68c2667fc568055c7bf6a676e1f12e61154fbab6 Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Tue, 22 Nov 2016 16:20:53 +0100 Subject: add test for logged-in resource --- service/test/unit/resources/test_auth.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'service/test') diff --git a/service/test/unit/resources/test_auth.py b/service/test/unit/resources/test_auth.py index ac2529dd..adff1083 100644 --- a/service/test/unit/resources/test_auth.py +++ b/service/test/unit/resources/test_auth.py @@ -14,19 +14,20 @@ class TestPixelatedAuthSessionWrapper(unittest.TestCase): def setUp(self): self.portal_mock = mock() + self.user_uuid_mock = mock() self.root_resource_mock = mock() self.anonymous_resource_mock = mock() credential_factories_mock = mock() self.session_wrapper = PixelatedAuthSessionWrapper(self.portal_mock, self.root_resource_mock, self.anonymous_resource_mock, credential_factories_mock) + self.request = DummyRequest([]) + self.request.prepath = [''] + self.request.path = '/' - def test_should_use_login_resource_when_the_user_is_not_logged_in(self): - request = DummyRequest([]) - request.prepath = [''] - request.path = '/' + def test_should_proxy_to_login_resource_when_the_user_is_not_logged_in(self): when(self.portal_mock).login(ANY(), None, IResource).thenReturn(succeed((IResource, ANONYMOUS, lambda: None))) - deferred_resource = self.session_wrapper.getChildWithDefault('/', request) + deferred_resource = self.session_wrapper.getChildWithDefault('/', self.request) d = deferred_resource.d def assert_anonymous_resource(resource): @@ -34,3 +35,15 @@ class TestPixelatedAuthSessionWrapper(unittest.TestCase): d.addCallback(assert_anonymous_resource) return d + + def test_should_proxy_to_root_resource_when_the_user_is_logged_in(self): + when(self.portal_mock).login(ANY(), None, IResource).thenReturn(succeed((IResource, self.user_uuid_mock, lambda: None))) + + deferred_resource = self.session_wrapper.getChildWithDefault('/', self.request) + d = deferred_resource.d + + def assert_root_resource(resource): + self.assertIs(resource, self.root_resource_mock) + + d.addCallback(assert_root_resource) + return d -- cgit v1.2.3 From 59644fafb501422295d430912d8711a7d11195b5 Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Wed, 23 Nov 2016 10:02:38 +0100 Subject: fix archive resource unit test --- service/test/unit/resources/test_archive_resource.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'service/test') diff --git a/service/test/unit/resources/test_archive_resource.py b/service/test/unit/resources/test_archive_resource.py index 28078222..186078a5 100644 --- a/service/test/unit/resources/test_archive_resource.py +++ b/service/test/unit/resources/test_archive_resource.py @@ -1,4 +1,4 @@ -import unittest +from twisted.trial import unittest import json from mockito import mock, when, verify from test.unit.resources import DummySite @@ -15,11 +15,16 @@ class TestArchiveResource(unittest.TestCase): def test_render_POST_should_archive_mails(self): request = DummyRequest(['/mails/archive']) request.method = 'POST' + idents = ['1', '2'] content = mock() when(content).read().thenReturn(json.dumps({'idents': ['1', '2']})) - when(self.mail_service).archive_mail('1').thenReturn(defer.Deferred()) - when(self.mail_service).archive_mail('2').thenReturn(defer.Deferred()) + d1 = defer.Deferred() + d1.callback(None) + when(self.mail_service).archive_mail('1').thenReturn(d1) + d2 = defer.Deferred() + d2.callback(None) + when(self.mail_service).archive_mail('2').thenReturn(d2) request.content = content d = self.web.get(request) -- cgit v1.2.3 From 20df0b4236b9939776bf15d955d36501566cb486 Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Wed, 23 Nov 2016 11:12:26 +0100 Subject: readability --- service/test/unit/resources/test_archive_resource.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'service/test') diff --git a/service/test/unit/resources/test_archive_resource.py b/service/test/unit/resources/test_archive_resource.py index 186078a5..1876f897 100644 --- a/service/test/unit/resources/test_archive_resource.py +++ b/service/test/unit/resources/test_archive_resource.py @@ -19,12 +19,8 @@ class TestArchiveResource(unittest.TestCase): content = mock() when(content).read().thenReturn(json.dumps({'idents': ['1', '2']})) - d1 = defer.Deferred() - d1.callback(None) - when(self.mail_service).archive_mail('1').thenReturn(d1) - d2 = defer.Deferred() - d2.callback(None) - when(self.mail_service).archive_mail('2').thenReturn(d2) + when(self.mail_service).archive_mail('1').thenReturn(defer.succeed(None)) + when(self.mail_service).archive_mail('2').thenReturn(defer.succeed(None)) request.content = content d = self.web.get(request) -- cgit v1.2.3 From 819d3b0c974fe1b937adbdc5205d7810b88faa4e Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Wed, 23 Nov 2016 11:13:13 +0100 Subject: fix mails resource unit test --- service/test/unit/resources/test_mails_resource.py | 25 +++++++++------------- 1 file changed, 10 insertions(+), 15 deletions(-) (limited to 'service/test') diff --git a/service/test/unit/resources/test_mails_resource.py b/service/test/unit/resources/test_mails_resource.py index 2d9cb33c..bdd15657 100644 --- a/service/test/unit/resources/test_mails_resource.py +++ b/service/test/unit/resources/test_mails_resource.py @@ -14,7 +14,7 @@ # # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . -import unittest +from twisted.trial import unittest from mock import patch from mockito import mock, when, verify, any as ANY @@ -39,17 +39,16 @@ class TestMailsResource(unittest.TestCase): @patch('leap.common.events.register') def test_render_GET_should_unicode_mails_search_query(self, mock_register): - request = DummyRequest(['/mails']) + request = DummyRequest([]) non_unicode_search_term = 'coração' request.addArg('q', non_unicode_search_term) request.addArg('w', 25) request.addArg('p', 1) unicodified_search_term = u'coração' - when(self.mail_service).mails(unicodified_search_term, 25, 1).thenReturn(defer.Deferred()) + when(self.mail_service).mails(unicodified_search_term, 25, 1).thenReturn(defer.succeed(([], 0))) mails_resource = MailsResource(self.services_factory) - mails_resource.isLeaf = True web = DummySite(mails_resource) d = web.get(request) @@ -61,15 +60,13 @@ class TestMailsResource(unittest.TestCase): @patch('leap.common.events.register') def test_render_PUT_should_store_draft_with_attachments(self, mock_register): - request = DummyRequest(['/mails']) + request = DummyRequest([]) request.method = 'PUT' - content = mock() - when(content).read().thenReturn('{"attachments": [{"ident": "some fake attachment id"}]}') - when(self.mail_service).attachment('some fake attachment id').thenReturn(defer.Deferred()) - request.content = content + request.content = mock() + when(request.content).read().thenReturn('{"attachments": [{"ident": "some fake attachment id"}]}') + when(self.mail_service).attachment('some fake attachment id').thenReturn(defer.succeed({'content': mock()})) mails_resource = MailsResource(self.services_factory) - mails_resource.isLeaf = True web = DummySite(mails_resource) d = web.get(request) @@ -81,19 +78,17 @@ class TestMailsResource(unittest.TestCase): @patch('leap.common.events.register') def test_render_POST_should_send_email_with_attachments(self, mock_register): - request = DummyRequest(['/mails']) + request = DummyRequest([]) request.method = 'POST' - content = mock() - when(content).read().thenReturn('{"attachments": [{"ident": "some fake attachment id"}]}') + request.content = mock() + when(request.content).read().thenReturn('{"attachments": [{"ident": "some fake attachment id"}]}') when(self.mail_service).attachment('some fake attachment id').thenReturn(defer.succeed({"content": "some content"})) as_dictable = mock() when(as_dictable).as_dict().thenReturn({}) when(self.mail_service).send_mail({"attachments": [{"ident": "some fake attachment id", "raw": "some content"}]})\ .thenReturn(defer.succeed(as_dictable)) - request.content = content mails_resource = MailsResource(self.services_factory) - mails_resource.isLeaf = True web = DummySite(mails_resource) d = web.get(request) -- cgit v1.2.3 From ced565121604f9834f83ec485538bb2e2a0c9232 Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Wed, 23 Nov 2016 11:31:22 +0100 Subject: replace stdlib's unittest with trials's unittest for all unittest --- service/test/unit/adapter/mailstore/test_body_parser.py | 2 +- service/test/unit/adapter/search/test_search.py | 2 +- service/test/unit/adapter/test_contacts.py | 2 +- service/test/unit/adapter/test_draft_service.py | 2 +- service/test/unit/adapter/test_status.py | 2 +- service/test/unit/adapter/test_tag.py | 2 +- service/test/unit/bitmask_libraries/test_abstract_leap.py | 2 +- service/test/unit/bitmask_libraries/test_certs.py | 2 +- service/test/unit/bitmask_libraries/test_smtp_client_certificate.py | 2 +- service/test/unit/config/test_register.py | 2 +- service/test/unit/config/test_services.py | 2 +- service/test/unit/config/test_sessions.py | 1 + service/test/unit/config/test_site.py | 2 +- service/test/unit/maintenance/test_commands.py | 2 +- service/test/unit/resources/test_attachments_resource.py | 2 +- service/test/unit/resources/test_helpers.py | 2 +- service/test/unit/resources/test_sandbox_resource.py | 2 +- service/test/unit/resources/test_user_settings_resource.py | 2 +- service/test/unit/support/mail_generator_test.py | 2 +- service/test/unit/support/test_encrypted_file_storage.py | 2 +- service/test/unit/support/test_functional.py | 2 +- service/test/unit/support/test_markov.py | 2 +- service/test/unit/support/test_replier.py | 2 +- service/test/unit/test_application.py | 2 +- service/test/unit/test_welcome_mail.py | 2 +- 25 files changed, 25 insertions(+), 24 deletions(-) (limited to 'service/test') diff --git a/service/test/unit/adapter/mailstore/test_body_parser.py b/service/test/unit/adapter/mailstore/test_body_parser.py index 155b326c..cff0b09e 100644 --- a/service/test/unit/adapter/mailstore/test_body_parser.py +++ b/service/test/unit/adapter/mailstore/test_body_parser.py @@ -14,7 +14,7 @@ # # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . -import unittest +from twisted.trial import unittest from mock import patch from pixelated.adapter.mailstore.body_parser import BodyParser diff --git a/service/test/unit/adapter/search/test_search.py b/service/test/unit/adapter/search/test_search.py index be37257c..1465961d 100644 --- a/service/test/unit/adapter/search/test_search.py +++ b/service/test/unit/adapter/search/test_search.py @@ -16,7 +16,7 @@ # along with Pixelated. If not, see . -import unittest +from twisted.trial import unittest from pixelated.adapter.mailstore.leap_mailstore import LeapMail from pixelated.adapter.search import SearchEngine from tempdir import TempDir diff --git a/service/test/unit/adapter/test_contacts.py b/service/test/unit/adapter/test_contacts.py index 3510faf5..83383755 100644 --- a/service/test/unit/adapter/test_contacts.py +++ b/service/test/unit/adapter/test_contacts.py @@ -13,7 +13,7 @@ # # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . -import unittest +from twisted.trial import unittest from pixelated.adapter.search.contacts import address_duplication_filter from pixelated.adapter.search.contacts import extract_mail_address diff --git a/service/test/unit/adapter/test_draft_service.py b/service/test/unit/adapter/test_draft_service.py index c2516013..e18589eb 100644 --- a/service/test/unit/adapter/test_draft_service.py +++ b/service/test/unit/adapter/test_draft_service.py @@ -1,4 +1,4 @@ -import unittest +from twisted.trial import unittest from twisted.internet import defer from pixelated.adapter.mailstore.leap_mailstore import LeapMail diff --git a/service/test/unit/adapter/test_status.py b/service/test/unit/adapter/test_status.py index 5cd0fa1e..4624dcee 100644 --- a/service/test/unit/adapter/test_status.py +++ b/service/test/unit/adapter/test_status.py @@ -13,7 +13,7 @@ # # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . -import unittest +from twisted.trial import unittest from pixelated.adapter.model.status import Status diff --git a/service/test/unit/adapter/test_tag.py b/service/test/unit/adapter/test_tag.py index a4fa819e..e6d2771d 100644 --- a/service/test/unit/adapter/test_tag.py +++ b/service/test/unit/adapter/test_tag.py @@ -13,7 +13,7 @@ # # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . -import unittest +from twisted.trial import unittest from pixelated.adapter.model.tag import Tag diff --git a/service/test/unit/bitmask_libraries/test_abstract_leap.py b/service/test/unit/bitmask_libraries/test_abstract_leap.py index 237a1152..2fed2a4c 100644 --- a/service/test/unit/bitmask_libraries/test_abstract_leap.py +++ b/service/test/unit/bitmask_libraries/test_abstract_leap.py @@ -14,7 +14,7 @@ # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . import tempfile -import unittest +from twisted.trial import unittest from uuid import uuid4 import os diff --git a/service/test/unit/bitmask_libraries/test_certs.py b/service/test/unit/bitmask_libraries/test_certs.py index 9885759e..300830be 100644 --- a/service/test/unit/bitmask_libraries/test_certs.py +++ b/service/test/unit/bitmask_libraries/test_certs.py @@ -1,4 +1,4 @@ -import unittest +from twisted.trial import unittest from pixelated.bitmask_libraries.certs import LeapCertificate from pixelated.config import leap_config diff --git a/service/test/unit/bitmask_libraries/test_smtp_client_certificate.py b/service/test/unit/bitmask_libraries/test_smtp_client_certificate.py index 241dcbae..1ed08653 100644 --- a/service/test/unit/bitmask_libraries/test_smtp_client_certificate.py +++ b/service/test/unit/bitmask_libraries/test_smtp_client_certificate.py @@ -14,7 +14,7 @@ # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . import os -import unittest +from twisted.trial import unittest import tempdir import leap.common.certs as certs from mockito import mock, unstub, when, any as ANY diff --git a/service/test/unit/config/test_register.py b/service/test/unit/config/test_register.py index ca1e3a01..1d7918c4 100644 --- a/service/test/unit/config/test_register.py +++ b/service/test/unit/config/test_register.py @@ -1,4 +1,4 @@ -import unittest +from twisted.trial import unittest from mock import patch, Mock from pixelated.register import validate_username, validate_password, _set_provider, register diff --git a/service/test/unit/config/test_services.py b/service/test/unit/config/test_services.py index ed221261..6361a3da 100644 --- a/service/test/unit/config/test_services.py +++ b/service/test/unit/config/test_services.py @@ -13,7 +13,7 @@ # # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . -import unittest +from twisted.trial import unittest from mock import Mock, ANY, patch from mockito import mock, verify diff --git a/service/test/unit/config/test_sessions.py b/service/test/unit/config/test_sessions.py index 7ac6f8d1..5c5cf9be 100644 --- a/service/test/unit/config/test_sessions.py +++ b/service/test/unit/config/test_sessions.py @@ -53,6 +53,7 @@ class SessionTest(AbstractLeapTest): session.close() mail_fetcher_mock.stopService.assert_called_once() + @defer.inlineCallbacks def test_that_sync_defers_to_soledad(self): with patch('pixelated.config.sessions.reactor.callFromThread', new=_execute_func) as _: with patch('pixelated.config.sessions.LeapSession._create_incoming_mail_fetcher') as mail_fetcher_mock: diff --git a/service/test/unit/config/test_site.py b/service/test/unit/config/test_site.py index b8b23ef0..6911b4a5 100644 --- a/service/test/unit/config/test_site.py +++ b/service/test/unit/config/test_site.py @@ -1,4 +1,4 @@ -import unittest +from twisted.trial import unittest from mockito import mock from pixelated.config.site import PixelatedSite from twisted.protocols.basic import LineReceiver diff --git a/service/test/unit/maintenance/test_commands.py b/service/test/unit/maintenance/test_commands.py index 812c1bc2..0a8ffe53 100644 --- a/service/test/unit/maintenance/test_commands.py +++ b/service/test/unit/maintenance/test_commands.py @@ -13,7 +13,7 @@ # # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . -import unittest +from twisted.trial import unittest import email from pixelated.maintenance import delete_all_mails, load_mails diff --git a/service/test/unit/resources/test_attachments_resource.py b/service/test/unit/resources/test_attachments_resource.py index 06ae765f..15f38406 100644 --- a/service/test/unit/resources/test_attachments_resource.py +++ b/service/test/unit/resources/test_attachments_resource.py @@ -1,5 +1,5 @@ import json -import unittest +from twisted.trial import unittest from mock import patch, MagicMock from mockito import mock, when, verify, any as ANY diff --git a/service/test/unit/resources/test_helpers.py b/service/test/unit/resources/test_helpers.py index a17ce755..25a52da2 100644 --- a/service/test/unit/resources/test_helpers.py +++ b/service/test/unit/resources/test_helpers.py @@ -1,4 +1,4 @@ -import unittest +from twisted.trial import unittest import re from pixelated.resources import respond_json, respond_json_deferred diff --git a/service/test/unit/resources/test_sandbox_resource.py b/service/test/unit/resources/test_sandbox_resource.py index 98b88b2d..4f263af1 100644 --- a/service/test/unit/resources/test_sandbox_resource.py +++ b/service/test/unit/resources/test_sandbox_resource.py @@ -1,5 +1,5 @@ import os -import unittest +from twisted.trial import unittest from twisted.internet import defer from twisted.web.test.requesthelper import DummyRequest diff --git a/service/test/unit/resources/test_user_settings_resource.py b/service/test/unit/resources/test_user_settings_resource.py index 30a3c4cd..e9748e72 100644 --- a/service/test/unit/resources/test_user_settings_resource.py +++ b/service/test/unit/resources/test_user_settings_resource.py @@ -1,4 +1,4 @@ -import unittest +from twisted.trial import unittest import json import ast diff --git a/service/test/unit/support/mail_generator_test.py b/service/test/unit/support/mail_generator_test.py index 9d604378..dd6da522 100644 --- a/service/test/unit/support/mail_generator_test.py +++ b/service/test/unit/support/mail_generator_test.py @@ -14,7 +14,7 @@ # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . from mailbox import mbox -import unittest +from twisted.trial import unittest import pkg_resources import random from mock import patch diff --git a/service/test/unit/support/test_encrypted_file_storage.py b/service/test/unit/support/test_encrypted_file_storage.py index 69b82f3d..8083430e 100644 --- a/service/test/unit/support/test_encrypted_file_storage.py +++ b/service/test/unit/support/test_encrypted_file_storage.py @@ -15,7 +15,7 @@ # along with Pixelated. If not, see . import os import shutil -import unittest +from twisted.trial import unittest from pixelated.support.encrypted_file_storage import EncryptedFileStorage diff --git a/service/test/unit/support/test_functional.py b/service/test/unit/support/test_functional.py index ad3cb16c..0b117032 100644 --- a/service/test/unit/support/test_functional.py +++ b/service/test/unit/support/test_functional.py @@ -16,7 +16,7 @@ # You should have received a copy of the GNU Affero General Public License -import unittest +from twisted.trial import unittest from pixelated.support.functional import to_unicode diff --git a/service/test/unit/support/test_markov.py b/service/test/unit/support/test_markov.py index f0b0277d..911cef30 100644 --- a/service/test/unit/support/test_markov.py +++ b/service/test/unit/support/test_markov.py @@ -15,7 +15,7 @@ # along with Pixelated. If not, see . -import unittest +from twisted.trial import unittest from pixelated.support.markov import MarkovGenerator import random diff --git a/service/test/unit/support/test_replier.py b/service/test/unit/support/test_replier.py index 5e1c234a..ef9b321c 100644 --- a/service/test/unit/support/test_replier.py +++ b/service/test/unit/support/test_replier.py @@ -1,4 +1,4 @@ -import unittest +from twisted.trial import unittest from pixelated.support import replier diff --git a/service/test/unit/test_application.py b/service/test/unit/test_application.py index 80d9ec14..67c044c2 100644 --- a/service/test/unit/test_application.py +++ b/service/test/unit/test_application.py @@ -1,4 +1,4 @@ -import unittest +from twisted.trial import unittest from leap.common.events import catalog as events from mock import patch, MagicMock, ANY diff --git a/service/test/unit/test_welcome_mail.py b/service/test/unit/test_welcome_mail.py index 6462dceb..7eb65903 100644 --- a/service/test/unit/test_welcome_mail.py +++ b/service/test/unit/test_welcome_mail.py @@ -16,7 +16,7 @@ import os import re -import unittest +from twisted.trial import unittest from mockito import verify, mock from mockito.matchers import Matcher from email import message_from_file -- cgit v1.2.3 From 8640658dca9c2a37f01922a127749af7eec7501e Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Wed, 23 Nov 2016 16:52:05 +0100 Subject: remove an unnecessary patch --- service/test/unit/config/test_sessions.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'service/test') diff --git a/service/test/unit/config/test_sessions.py b/service/test/unit/config/test_sessions.py index 5c5cf9be..abae46d8 100644 --- a/service/test/unit/config/test_sessions.py +++ b/service/test/unit/config/test_sessions.py @@ -55,11 +55,10 @@ class SessionTest(AbstractLeapTest): @defer.inlineCallbacks def test_that_sync_defers_to_soledad(self): - with patch('pixelated.config.sessions.reactor.callFromThread', new=_execute_func) as _: - with patch('pixelated.config.sessions.LeapSession._create_incoming_mail_fetcher') as mail_fetcher_mock: - session = self._create_session() - yield session.sync() - self.soledad_session.sync.assert_called_once() + with patch('pixelated.config.sessions.LeapSession._create_incoming_mail_fetcher') as mail_fetcher_mock: + session = self._create_session() + yield session.sync() + self.soledad_session.sync.assert_called_once() def test_session_registers_to_generated_keys(self): email = 'someone@somedomain.tld' @@ -159,4 +158,6 @@ class SessionTest(AbstractLeapTest): def _execute_func(func): + print 'in _execute_func, before executing', func func() + print 'in _execute_func, after executing', func -- cgit v1.2.3 From 3d9d3a407de9e179d4f7be055a24c02fcf9bb418 Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Wed, 23 Nov 2016 17:40:24 +0100 Subject: remove another unnecessary patch --- service/test/unit/config/test_sessions.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'service/test') diff --git a/service/test/unit/config/test_sessions.py b/service/test/unit/config/test_sessions.py index abae46d8..b2aa2939 100644 --- a/service/test/unit/config/test_sessions.py +++ b/service/test/unit/config/test_sessions.py @@ -55,10 +55,9 @@ class SessionTest(AbstractLeapTest): @defer.inlineCallbacks def test_that_sync_defers_to_soledad(self): - with patch('pixelated.config.sessions.LeapSession._create_incoming_mail_fetcher') as mail_fetcher_mock: - session = self._create_session() - yield session.sync() - self.soledad_session.sync.assert_called_once() + session = self._create_session() + yield session.sync() + self.soledad_session.sync.assert_called_once() def test_session_registers_to_generated_keys(self): email = 'someone@somedomain.tld' @@ -158,6 +157,4 @@ class SessionTest(AbstractLeapTest): def _execute_func(func): - print 'in _execute_func, before executing', func func() - print 'in _execute_func, after executing', func -- cgit v1.2.3 From 2884195e96728c0432a8e8d0d2f747ae2baefd06 Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Wed, 23 Nov 2016 17:59:31 +0100 Subject: mock out event registration for failing test in SnapCI --- service/test/unit/config/test_sessions.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'service/test') diff --git a/service/test/unit/config/test_sessions.py b/service/test/unit/config/test_sessions.py index b2aa2939..98e5c69e 100644 --- a/service/test/unit/config/test_sessions.py +++ b/service/test/unit/config/test_sessions.py @@ -53,8 +53,9 @@ class SessionTest(AbstractLeapTest): session.close() mail_fetcher_mock.stopService.assert_called_once() + @patch('pixelated.config.sessions.register') @defer.inlineCallbacks - def test_that_sync_defers_to_soledad(self): + def test_that_sync_defers_to_soledad(self, *unused): session = self._create_session() yield session.sync() self.soledad_session.sync.assert_called_once() -- cgit v1.2.3 From c39921ef6ba7ed299a125b530b770b0e1ec16203 Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Thu, 24 Nov 2016 10:56:59 +0100 Subject: add test for unauthorized resource --- service/test/unit/resources/test_auth.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'service/test') diff --git a/service/test/unit/resources/test_auth.py b/service/test/unit/resources/test_auth.py index adff1083..80f1ebb0 100644 --- a/service/test/unit/resources/test_auth.py +++ b/service/test/unit/resources/test_auth.py @@ -3,9 +3,12 @@ from pixelated.resources.auth import PixelatedAuthSessionWrapper from pixelated.resources.login_resource import LoginResource from pixelated.resources.root_resource import RootResource from test.unit.resources import DummySite +from twisted.cred import error from twisted.cred.checkers import ANONYMOUS -from twisted.internet.defer import succeed +from twisted.internet.defer import succeed, fail +from twisted.python import failure from twisted.trial import unittest +from twisted.web._auth.wrapper import UnauthorizedResource from twisted.web.resource import IResource from twisted.web.test.requesthelper import DummyRequest @@ -47,3 +50,15 @@ class TestPixelatedAuthSessionWrapper(unittest.TestCase): d.addCallback(assert_root_resource) return d + + def test_should_proxy_to_unauthorized_resource_when_login_fails(self): + when(self.portal_mock).login(ANY(), None, IResource).thenReturn(fail(failure.Failure(error.UnhandledCredentials('dummy message')))) + + deferred_resource = self.session_wrapper.getChildWithDefault('/', self.request) + d = deferred_resource.d + + def assert_unauthorized_resource(resource): + self.assertIsInstance(resource, UnauthorizedResource) + + d.addCallback(assert_unauthorized_resource) + return d -- cgit v1.2.3 From c2088d90c080eb56a0d6edd714ade80525e1ac00 Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Thu, 24 Nov 2016 11:15:20 +0100 Subject: remove use of stdlib unittest from integration tests --- service/test/integration/test_feedback_service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'service/test') diff --git a/service/test/integration/test_feedback_service.py b/service/test/integration/test_feedback_service.py index c50c1883..ff659396 100644 --- a/service/test/integration/test_feedback_service.py +++ b/service/test/integration/test_feedback_service.py @@ -1,4 +1,4 @@ -import unittest +from twisted.trial import unittest from httmock import urlmatch, HTTMock from mockito import when from twisted.internet import defer -- cgit v1.2.3 From b00b7ff5d828099e8f0190fee44ad1daf2054717 Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Thu, 24 Nov 2016 16:41:49 +0100 Subject: add public root resource to serve static files --- service/test/unit/resources/test_root_resource.py | 44 ++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to 'service/test') diff --git a/service/test/unit/resources/test_root_resource.py b/service/test/unit/resources/test_root_resource.py index 7a7b2005..082f2b22 100644 --- a/service/test/unit/resources/test_root_resource.py +++ b/service/test/unit/resources/test_root_resource.py @@ -1,14 +1,42 @@ +import os import re from mock import MagicMock, patch from mockito import mock, when, any as ANY +import pixelated from pixelated.application import UserAgentMode from pixelated.resources.features_resource import FeaturesResource from test.unit.resources import DummySite +from twisted.cred.checkers import ANONYMOUS +from twisted.internet.defer import succeed from twisted.trial import unittest +from twisted.web.resource import IResource +from twisted.web.static import File from twisted.web.test.requesthelper import DummyRequest -from pixelated.resources.root_resource import RootResource, MODE_STARTUP, MODE_RUNNING +from pixelated.resources.root_resource import PublicRootResource, RootResource, MODE_STARTUP, MODE_RUNNING + + +class TestPublicRootResource(unittest.TestCase): + + def setUp(self): + self.portal_mock = mock() + assets_path = os.path.abspath( + os.path.join(os.path.abspath(pixelated.__file__), '..', '..', '..', 'web-ui', 'public') + ) + services_factory = mock() + self.public_root_resource = PublicRootResource(services_factory, assets_path=assets_path) + self.web = DummySite(self.public_root_resource) + self.request = DummyRequest(['assets', 'dummy.json']) + + def test_assets_should_be_available(self): + d = self.web.get(self.request) + + def assert_response(_): + self.assertEqual(200, self.request.responseCode) + + d.addCallback(assert_response) + return d class TestRootResource(unittest.TestCase): @@ -103,6 +131,20 @@ class TestRootResource(unittest.TestCase): d.addCallback(assert_unauthorized) return d + def test_GET_should_return_404_for_non_existing_resource(self): + request = DummyRequest(['/non-existing-child']) + request.method = 'GET' + + request.getCookie = MagicMock(return_value='stubbed csrf token') + + d = self.web.get(request) + + def assert_not_found(_): + self.assertEqual(404, request.responseCode) + + d.addCallback(assert_not_found) + return d + def test_should_404_non_existing_resource_with_valid_csrf(self): request = DummyRequest(['/non-existing-child']) request.method = 'POST' -- cgit v1.2.3 From 7802cf70c3b2ec3c14fd735dc211b00914c731cb Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Thu, 24 Nov 2016 17:01:15 +0100 Subject: add login resource as child of public root resource --- service/test/unit/resources/test_root_resource.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'service/test') diff --git a/service/test/unit/resources/test_root_resource.py b/service/test/unit/resources/test_root_resource.py index 082f2b22..9b3042a8 100644 --- a/service/test/unit/resources/test_root_resource.py +++ b/service/test/unit/resources/test_root_resource.py @@ -25,15 +25,25 @@ class TestPublicRootResource(unittest.TestCase): os.path.join(os.path.abspath(pixelated.__file__), '..', '..', '..', 'web-ui', 'public') ) services_factory = mock() - self.public_root_resource = PublicRootResource(services_factory, assets_path=assets_path) + self.public_root_resource = PublicRootResource(services_factory, assets_path=assets_path, provider=mock()) self.web = DummySite(self.public_root_resource) - self.request = DummyRequest(['assets', 'dummy.json']) def test_assets_should_be_available(self): - d = self.web.get(self.request) + request = DummyRequest(['assets', 'dummy.json']) + d = self.web.get(request) + + def assert_response(_): + self.assertEqual(200, request.responseCode) + + d.addCallback(assert_response) + return d + + def test_login_should_be_available(self): + request = DummyRequest(['login']) + d = self.web.get(request) def assert_response(_): - self.assertEqual(200, self.request.responseCode) + self.assertEqual(200, request.responseCode) d.addCallback(assert_response) return d -- cgit v1.2.3 From e313d7c8880192ab3261cdd8cb263f5eef28d40a Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Fri, 25 Nov 2016 15:50:50 +0100 Subject: make credentialsFactories parameter to auth session wrapper optional --- service/test/unit/resources/test_auth.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'service/test') diff --git a/service/test/unit/resources/test_auth.py b/service/test/unit/resources/test_auth.py index 80f1ebb0..10650e53 100644 --- a/service/test/unit/resources/test_auth.py +++ b/service/test/unit/resources/test_auth.py @@ -20,9 +20,8 @@ class TestPixelatedAuthSessionWrapper(unittest.TestCase): self.user_uuid_mock = mock() self.root_resource_mock = mock() self.anonymous_resource_mock = mock() - credential_factories_mock = mock() - self.session_wrapper = PixelatedAuthSessionWrapper(self.portal_mock, self.root_resource_mock, self.anonymous_resource_mock, credential_factories_mock) + self.session_wrapper = PixelatedAuthSessionWrapper(self.portal_mock, self.root_resource_mock, self.anonymous_resource_mock) self.request = DummyRequest([]) self.request.prepath = [''] self.request.path = '/' -- cgit v1.2.3 From 8dbd0210911475ec48d23e741de192a09e23f101 Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Fri, 25 Nov 2016 15:52:52 +0100 Subject: pass url *fragment* as path argument to getChildWithDefault --- service/test/unit/resources/test_auth.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'service/test') diff --git a/service/test/unit/resources/test_auth.py b/service/test/unit/resources/test_auth.py index 10650e53..05d07130 100644 --- a/service/test/unit/resources/test_auth.py +++ b/service/test/unit/resources/test_auth.py @@ -29,7 +29,7 @@ class TestPixelatedAuthSessionWrapper(unittest.TestCase): def test_should_proxy_to_login_resource_when_the_user_is_not_logged_in(self): when(self.portal_mock).login(ANY(), None, IResource).thenReturn(succeed((IResource, ANONYMOUS, lambda: None))) - deferred_resource = self.session_wrapper.getChildWithDefault('/', self.request) + deferred_resource = self.session_wrapper.getChildWithDefault('', self.request) d = deferred_resource.d def assert_anonymous_resource(resource): @@ -41,7 +41,7 @@ class TestPixelatedAuthSessionWrapper(unittest.TestCase): def test_should_proxy_to_root_resource_when_the_user_is_logged_in(self): when(self.portal_mock).login(ANY(), None, IResource).thenReturn(succeed((IResource, self.user_uuid_mock, lambda: None))) - deferred_resource = self.session_wrapper.getChildWithDefault('/', self.request) + deferred_resource = self.session_wrapper.getChildWithDefault('', self.request) d = deferred_resource.d def assert_root_resource(resource): @@ -53,7 +53,7 @@ class TestPixelatedAuthSessionWrapper(unittest.TestCase): def test_should_proxy_to_unauthorized_resource_when_login_fails(self): when(self.portal_mock).login(ANY(), None, IResource).thenReturn(fail(failure.Failure(error.UnhandledCredentials('dummy message')))) - deferred_resource = self.session_wrapper.getChildWithDefault('/', self.request) + deferred_resource = self.session_wrapper.getChildWithDefault('', self.request) d = deferred_resource.d def assert_unauthorized_resource(resource): -- cgit v1.2.3 From b97e47d564cc4bbd6b0f0ac2cccc0fa46490c764 Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Fri, 25 Nov 2016 15:53:53 +0100 Subject: don't mock the root resource in auth wrapper test --- service/test/unit/resources/test_auth.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'service/test') diff --git a/service/test/unit/resources/test_auth.py b/service/test/unit/resources/test_auth.py index 05d07130..2b85a3cf 100644 --- a/service/test/unit/resources/test_auth.py +++ b/service/test/unit/resources/test_auth.py @@ -18,10 +18,11 @@ class TestPixelatedAuthSessionWrapper(unittest.TestCase): def setUp(self): self.portal_mock = mock() self.user_uuid_mock = mock() - self.root_resource_mock = mock() + services_factory = mock() + self.root_resource = RootResource(services_factory) self.anonymous_resource_mock = mock() - self.session_wrapper = PixelatedAuthSessionWrapper(self.portal_mock, self.root_resource_mock, self.anonymous_resource_mock) + self.session_wrapper = PixelatedAuthSessionWrapper(self.portal_mock, self.root_resource, self.anonymous_resource_mock) self.request = DummyRequest([]) self.request.prepath = [''] self.request.path = '/' @@ -45,7 +46,7 @@ class TestPixelatedAuthSessionWrapper(unittest.TestCase): d = deferred_resource.d def assert_root_resource(resource): - self.assertIs(resource, self.root_resource_mock) + self.assertIs(resource, self.root_resource) d.addCallback(assert_root_resource) return d -- cgit v1.2.3 From 77cc41204e3cd8144187ad8cf50fffb3d00080f1 Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Mon, 28 Nov 2016 12:00:06 +0100 Subject: split inbox resource out of root resource --- service/test/unit/resources/test_root_resource.py | 107 ++++++++++++++-------- 1 file changed, 69 insertions(+), 38 deletions(-) (limited to 'service/test') diff --git a/service/test/unit/resources/test_root_resource.py b/service/test/unit/resources/test_root_resource.py index 9b3042a8..2c74d7b9 100644 --- a/service/test/unit/resources/test_root_resource.py +++ b/service/test/unit/resources/test_root_resource.py @@ -14,39 +14,7 @@ from twisted.trial import unittest from twisted.web.resource import IResource from twisted.web.static import File from twisted.web.test.requesthelper import DummyRequest -from pixelated.resources.root_resource import PublicRootResource, RootResource, MODE_STARTUP, MODE_RUNNING - - -class TestPublicRootResource(unittest.TestCase): - - def setUp(self): - self.portal_mock = mock() - assets_path = os.path.abspath( - os.path.join(os.path.abspath(pixelated.__file__), '..', '..', '..', 'web-ui', 'public') - ) - services_factory = mock() - self.public_root_resource = PublicRootResource(services_factory, assets_path=assets_path, provider=mock()) - self.web = DummySite(self.public_root_resource) - - def test_assets_should_be_available(self): - request = DummyRequest(['assets', 'dummy.json']) - d = self.web.get(request) - - def assert_response(_): - self.assertEqual(200, request.responseCode) - - d.addCallback(assert_response) - return d - - def test_login_should_be_available(self): - request = DummyRequest(['login']) - d = self.web.get(request) - - def assert_response(_): - self.assertEqual(200, request.responseCode) - - d.addCallback(assert_response) - return d +from pixelated.resources.root_resource import InboxResource, RootResource, MODE_STARTUP, MODE_RUNNING class TestRootResource(unittest.TestCase): @@ -63,12 +31,13 @@ class TestRootResource(unittest.TestCase): self.mail_service.account_email = self.MAIL_ADDRESS root_resource = RootResource(self.services_factory) - root_resource._html_template = "$account_email" - root_resource._mode = root_resource self.web = DummySite(root_resource) self.root_resource = root_resource def test_render_GET_should_template_account_email(self): + self.root_resource._inbox_resource._html_template = "$account_email" + self.root_resource.initialize(provider=mock(), authenticator=mock()) + request = DummyRequest(['']) request.addCookie = lambda key, value: 'stubbed' @@ -126,6 +95,8 @@ class TestRootResource(unittest.TestCase): request.requestHeaders.setRawHeaders('x-xsrf-token', [csrf_token]) def test_should_unauthorize_child_resource_ajax_requests_when_csrf_mismatch(self): + self.root_resource.initialize(provider=mock(), authenticator=mock()) + request = DummyRequest(['/child']) request.method = 'POST' self._mock_ajax_csrf(request, 'stubbed csrf token') @@ -141,10 +112,25 @@ class TestRootResource(unittest.TestCase): d.addCallback(assert_unauthorized) return d + def test_GET_should_return_503_for_uninitialized_resource(self): + request = DummyRequest(['/sandbox/']) + request.method = 'GET' + + request.getCookie = MagicMock(return_value='stubbed csrf token') + + d = self.web.get(request) + + def assert_unavailable(_): + self.assertEqual(503, request.responseCode) + + d.addCallback(assert_unavailable) + return d + def test_GET_should_return_404_for_non_existing_resource(self): + self.root_resource.initialize(provider=mock(), authenticator=mock()) + request = DummyRequest(['/non-existing-child']) request.method = 'GET' - request.getCookie = MagicMock(return_value='stubbed csrf token') d = self.web.get(request) @@ -156,10 +142,11 @@ class TestRootResource(unittest.TestCase): return d def test_should_404_non_existing_resource_with_valid_csrf(self): + self.root_resource.initialize(provider=mock(), authenticator=mock()) + request = DummyRequest(['/non-existing-child']) request.method = 'POST' self._mock_ajax_csrf(request, 'stubbed csrf token') - request.getCookie = MagicMock(return_value='stubbed csrf token') d = self.web.get(request) @@ -175,7 +162,7 @@ class TestRootResource(unittest.TestCase): request = DummyRequest(['features']) request.getCookie = MagicMock(return_value='irrelevant -- stubbed') - self.root_resource._child_resources.add('features', FeaturesResource()) + self.root_resource.putChild('features', FeaturesResource()) self.root_resource._mode = MODE_RUNNING d = self.web.get(request) @@ -187,6 +174,8 @@ class TestRootResource(unittest.TestCase): return d def test_should_unauthorize_child_resource_non_ajax_POST_requests_when_csrf_input_mismatch(self): + self.root_resource.initialize(provider=mock(), authenticator=mock()) + request = DummyRequest(['mails']) request.method = 'POST' request.addArg('csrftoken', 'some csrf token') @@ -204,3 +193,45 @@ class TestRootResource(unittest.TestCase): d.addCallback(assert_unauthorized) return d + + def test_assets_should_be_publicly_available(self): + self.root_resource.initialize(provider=mock(), authenticator=mock()) + + request = DummyRequest(['assets', 'dummy.json']) + d = self.web.get(request) + + def assert_response(_): + self.assertEqual(200, request.responseCode) + + d.addCallback(assert_response) + return d + + def test_login_should_be_publicly_available(self): + self.root_resource.initialize(provider=mock(), authenticator=mock()) + + request = DummyRequest(['login']) + d = self.web.get(request) + + def assert_response(_): + self.assertEqual(200, request.responseCode) + + d.addCallback(assert_response) + return d + + def test_root_should_be_handled_by_inbox_resource(self): + request = DummyRequest([]) + request.prepath = [''] + request.path = '/' + # TODO: setup mocked portal + + resource = self.root_resource.getChildWithDefault(request.prepath[-1], request) + self.assertIsInstance(resource, InboxResource) + + def test_inbox_should_not_be_public(self): + request = DummyRequest([]) + request.prepath = [''] + request.path = '/' + # TODO: setup mocked portal + + resource = self.root_resource.getChildWithDefault(request.prepath[-1], request) + self.assertIsInstance(resource, InboxResource) -- cgit v1.2.3 From b50db20c0a6603a3ea5f0b704baee1983fc34c1d Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Tue, 29 Nov 2016 10:16:38 +0100 Subject: return resource instead of username/avatarId as avatar --- service/test/unit/resources/test_auth.py | 43 ++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 11 deletions(-) (limited to 'service/test') diff --git a/service/test/unit/resources/test_auth.py b/service/test/unit/resources/test_auth.py index 2b85a3cf..6bd0338a 100644 --- a/service/test/unit/resources/test_auth.py +++ b/service/test/unit/resources/test_auth.py @@ -1,34 +1,55 @@ from mockito import mock, when, any as ANY -from pixelated.resources.auth import PixelatedAuthSessionWrapper +from pixelated.resources.auth import SessionChecker, PixelatedRealm, PixelatedAuthSessionWrapper from pixelated.resources.login_resource import LoginResource from pixelated.resources.root_resource import RootResource from test.unit.resources import DummySite from twisted.cred import error -from twisted.cred.checkers import ANONYMOUS +from twisted.cred.checkers import ANONYMOUS, AllowAnonymousAccess +from twisted.cred.portal import Portal from twisted.internet.defer import succeed, fail from twisted.python import failure from twisted.trial import unittest from twisted.web._auth.wrapper import UnauthorizedResource -from twisted.web.resource import IResource +from twisted.web.resource import IResource, getChildForRequest from twisted.web.test.requesthelper import DummyRequest +class TestPixelatedRealm(unittest.TestCase): + + def setUp(self): + self.authenticated_root_resource = mock() + self.public_root_resource = mock() + self.realm = PixelatedRealm(self.authenticated_root_resource, self.public_root_resource) + + def test_anonymous_user_gets_anonymous_resource(self): + interface, avatar, logout_handler = self.realm.requestAvatar(ANONYMOUS, None, IResource) + self.assertEqual(interface, IResource) + self.assertIs(avatar, self.public_root_resource) + + def test_authenticated_user_gets_root_resource(self): + interface, avatar, logout_handler = self.realm.requestAvatar('username', None, IResource) + self.assertEqual(interface, IResource) + self.assertIs(avatar, self.authenticated_root_resource) + + class TestPixelatedAuthSessionWrapper(unittest.TestCase): def setUp(self): - self.portal_mock = mock() - self.user_uuid_mock = mock() + self.realm_mock = mock() services_factory = mock() + session_checker = SessionChecker(services_factory) + self.portal = Portal(self.realm_mock, [session_checker, AllowAnonymousAccess()]) + self.user_uuid_mock = mock() self.root_resource = RootResource(services_factory) self.anonymous_resource_mock = mock() - self.session_wrapper = PixelatedAuthSessionWrapper(self.portal_mock, self.root_resource, self.anonymous_resource_mock) + self.session_wrapper = PixelatedAuthSessionWrapper(self.portal, self.root_resource, self.anonymous_resource_mock) self.request = DummyRequest([]) self.request.prepath = [''] self.request.path = '/' def test_should_proxy_to_login_resource_when_the_user_is_not_logged_in(self): - when(self.portal_mock).login(ANY(), None, IResource).thenReturn(succeed((IResource, ANONYMOUS, lambda: None))) + when(self.realm_mock).requestAvatar(ANONYMOUS, None, IResource).thenReturn((IResource, self.anonymous_resource_mock, lambda: None)) deferred_resource = self.session_wrapper.getChildWithDefault('', self.request) d = deferred_resource.d @@ -40,7 +61,7 @@ class TestPixelatedAuthSessionWrapper(unittest.TestCase): return d def test_should_proxy_to_root_resource_when_the_user_is_logged_in(self): - when(self.portal_mock).login(ANY(), None, IResource).thenReturn(succeed((IResource, self.user_uuid_mock, lambda: None))) + when(self.realm_mock).requestAvatar(ANY(), None, IResource).thenReturn((IResource, self.root_resource, lambda: None)) deferred_resource = self.session_wrapper.getChildWithDefault('', self.request) d = deferred_resource.d @@ -51,14 +72,14 @@ class TestPixelatedAuthSessionWrapper(unittest.TestCase): d.addCallback(assert_root_resource) return d - def test_should_proxy_to_unauthorized_resource_when_login_fails(self): - when(self.portal_mock).login(ANY(), None, IResource).thenReturn(fail(failure.Failure(error.UnhandledCredentials('dummy message')))) + def test_should_X_when_unauthenticated_user_requests_non_public_resource(self): + when(self.realm_mock).requestAvatar(ANONYMOUS, None, IResource).thenReturn((IResource, self.anonymous_resource_mock, lambda: None)) deferred_resource = self.session_wrapper.getChildWithDefault('', self.request) d = deferred_resource.d def assert_unauthorized_resource(resource): - self.assertIsInstance(resource, UnauthorizedResource) + self.assertIs(resource, self.anonymous_resource_mock) d.addCallback(assert_unauthorized_resource) return d -- cgit v1.2.3 From 3f97f5c444ea4caa01111f3902871975430d9d97 Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Tue, 29 Nov 2016 17:34:41 +0100 Subject: add inbox resource --- service/test/unit/resources/test_inbox_resource.py | 72 ++++++++++++++++++++++ service/test/unit/resources/test_root_resource.py | 19 ++---- 2 files changed, 76 insertions(+), 15 deletions(-) create mode 100644 service/test/unit/resources/test_inbox_resource.py (limited to 'service/test') diff --git a/service/test/unit/resources/test_inbox_resource.py b/service/test/unit/resources/test_inbox_resource.py new file mode 100644 index 00000000..03fe6f1a --- /dev/null +++ b/service/test/unit/resources/test_inbox_resource.py @@ -0,0 +1,72 @@ +import re + +from mock import MagicMock, patch +from mockito import mock, when, any as ANY + +from pixelated.application import UserAgentMode +from pixelated.resources.features_resource import FeaturesResource +from test.unit.resources import DummySite +from twisted.trial import unittest +from twisted.web.test.requesthelper import DummyRequest +from pixelated.resources.inbox_resource import InboxResource, MODE_STARTUP, MODE_RUNNING + + +class TestInboxResource(unittest.TestCase): + MAIL_ADDRESS = 'test_user@pixelated-project.org' + + def setUp(self): + mail_service = mock() + mail_service.account_email = self.MAIL_ADDRESS + + services = mock() + services.mail_service = mail_service + + services_factory = mock() + services_factory.mode = mock() + when(services_factory).services(ANY()).thenReturn(services) + + self.inbox_resource = InboxResource(services_factory) + self.web = DummySite(self.inbox_resource) + + def test_render_GET_should_template_account_email(self): + self.inbox_resource._html_template = "$account_email" + self.inbox_resource.initialize() + + request = DummyRequest(['']) + request.addCookie = lambda key, value: 'stubbed' + + d = self.web.get(request) + + def assert_response(_): + expected = "{0}".format(self.MAIL_ADDRESS) + matches = re.findall(expected, request.written[0]) + self.assertEquals(len(matches), 1) + + d.addCallback(assert_response) + return d + + def _test_should_renew_xsrf_cookie(self): + request = DummyRequest(['']) + request.addCookie = MagicMock() + generated_csrf_token = 'csrf_token' + mock_sha = MagicMock() + mock_sha.hexdigest = MagicMock(return_value=generated_csrf_token) + + with patch('hashlib.sha256', return_value=mock_sha): + d = self.web.get(request) + + def assert_csrf_cookie(_): + request.addCookie.assert_called_once_with('XSRF-TOKEN', generated_csrf_token) + + d.addCallback(assert_csrf_cookie) + return d + + # TODO should this be here or just in the root resource test? + def test_should_renew_xsrf_cookie_on_startup_mode(self): + self.inbox_resource._mode = MODE_STARTUP + self._test_should_renew_xsrf_cookie() + + # TODO should this be here or just in the root resource test? + def test_should_renew_xsrf_cookie_on_running_mode(self): + self.inbox_resource._mode = MODE_RUNNING + self._test_should_renew_xsrf_cookie() diff --git a/service/test/unit/resources/test_root_resource.py b/service/test/unit/resources/test_root_resource.py index 2c74d7b9..079793b5 100644 --- a/service/test/unit/resources/test_root_resource.py +++ b/service/test/unit/resources/test_root_resource.py @@ -11,7 +11,7 @@ from test.unit.resources import DummySite from twisted.cred.checkers import ANONYMOUS from twisted.internet.defer import succeed from twisted.trial import unittest -from twisted.web.resource import IResource +from twisted.web.resource import IResource, getChildForRequest from twisted.web.static import File from twisted.web.test.requesthelper import DummyRequest from pixelated.resources.root_resource import InboxResource, RootResource, MODE_STARTUP, MODE_RUNNING @@ -34,22 +34,11 @@ class TestRootResource(unittest.TestCase): self.web = DummySite(root_resource) self.root_resource = root_resource - def test_render_GET_should_template_account_email(self): - self.root_resource._inbox_resource._html_template = "$account_email" - self.root_resource.initialize(provider=mock(), authenticator=mock()) - + def test_root_should_delegate_to_inbox(self): request = DummyRequest(['']) request.addCookie = lambda key, value: 'stubbed' - - d = self.web.get(request) - - def assert_response(_): - expected = "{0}".format(self.MAIL_ADDRESS) - matches = re.findall(expected, request.written[0]) - self.assertEquals(len(matches), 1) - - d.addCallback(assert_response) - return d + child_resource = getChildForRequest(self.root_resource, request) + self.assertIsInstance(child_resource, InboxResource) def _test_should_renew_xsrf_cookie(self): request = DummyRequest(['']) -- cgit v1.2.3 From 798858c79c0b10565f42365c6cdbf7d0549d0a2e Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Tue, 29 Nov 2016 17:40:15 +0100 Subject: assert login url is delegated correctly --- service/test/unit/resources/test_root_resource.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'service/test') diff --git a/service/test/unit/resources/test_root_resource.py b/service/test/unit/resources/test_root_resource.py index 079793b5..8d658d7e 100644 --- a/service/test/unit/resources/test_root_resource.py +++ b/service/test/unit/resources/test_root_resource.py @@ -7,6 +7,7 @@ from mockito import mock, when, any as ANY import pixelated from pixelated.application import UserAgentMode from pixelated.resources.features_resource import FeaturesResource +from pixelated.resources.login_resource import LoginResource from test.unit.resources import DummySite from twisted.cred.checkers import ANONYMOUS from twisted.internet.defer import succeed @@ -14,7 +15,11 @@ from twisted.trial import unittest from twisted.web.resource import IResource, getChildForRequest from twisted.web.static import File from twisted.web.test.requesthelper import DummyRequest -from pixelated.resources.root_resource import InboxResource, RootResource, MODE_STARTUP, MODE_RUNNING +from pixelated.resources.root_resource import InboxResource, PublicRootResource, RootResource, MODE_STARTUP, MODE_RUNNING + + +class TestPublicRootResource(unittest.TestCase): + pass class TestRootResource(unittest.TestCase): @@ -34,12 +39,19 @@ class TestRootResource(unittest.TestCase): self.web = DummySite(root_resource) self.root_resource = root_resource - def test_root_should_delegate_to_inbox(self): + def test_root_url_should_delegate_to_inbox(self): request = DummyRequest(['']) request.addCookie = lambda key, value: 'stubbed' child_resource = getChildForRequest(self.root_resource, request) self.assertIsInstance(child_resource, InboxResource) + def test_login_url_should_delegate_to_login_resource(self): + self.root_resource.initialize(provider=mock(), authenticator=mock()) + request = DummyRequest(['login']) + request.addCookie = lambda key, value: 'stubbed' + child_resource = getChildForRequest(self.root_resource, request) + self.assertIsInstance(child_resource, LoginResource) + def _test_should_renew_xsrf_cookie(self): request = DummyRequest(['']) request.addCookie = MagicMock() -- cgit v1.2.3 From 9b5d5a797c9f407183d1b9a6a2aea552a06c5ea1 Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Tue, 29 Nov 2016 17:47:07 +0100 Subject: make login resource part of the public root resource --- service/test/unit/resources/test_root_resource.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'service/test') diff --git a/service/test/unit/resources/test_root_resource.py b/service/test/unit/resources/test_root_resource.py index 8d658d7e..06eaf1ad 100644 --- a/service/test/unit/resources/test_root_resource.py +++ b/service/test/unit/resources/test_root_resource.py @@ -19,7 +19,17 @@ from pixelated.resources.root_resource import InboxResource, PublicRootResource, class TestPublicRootResource(unittest.TestCase): - pass + + def setUp(self): + self.public_root_resource = PublicRootResource(mock()) + self.web = DummySite(self.public_root_resource) + + def test_login_url_should_delegate_to_login_resource(self): + self.public_root_resource.initialize(provider=mock(), authenticator=mock()) + request = DummyRequest(['login']) + request.addCookie = lambda key, value: 'stubbed' + child_resource = getChildForRequest(self.public_root_resource, request) + self.assertIsInstance(child_resource, LoginResource) class TestRootResource(unittest.TestCase): @@ -35,9 +45,8 @@ class TestRootResource(unittest.TestCase): when(self.services_factory).services(ANY()).thenReturn(self.services) self.mail_service.account_email = self.MAIL_ADDRESS - root_resource = RootResource(self.services_factory) - self.web = DummySite(root_resource) - self.root_resource = root_resource + self.root_resource = RootResource(self.services_factory) + self.web = DummySite(self.root_resource) def test_root_url_should_delegate_to_inbox(self): request = DummyRequest(['']) -- cgit v1.2.3 From 6d82cddcb9a6f217dcb341e248124f00e613b48c Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Tue, 29 Nov 2016 17:59:58 +0100 Subject: test root resource delegation on a bit higher level --- service/test/unit/resources/test_auth.py | 46 +++++++++++--------------------- 1 file changed, 16 insertions(+), 30 deletions(-) (limited to 'service/test') diff --git a/service/test/unit/resources/test_auth.py b/service/test/unit/resources/test_auth.py index 6bd0338a..793069dd 100644 --- a/service/test/unit/resources/test_auth.py +++ b/service/test/unit/resources/test_auth.py @@ -1,7 +1,7 @@ from mockito import mock, when, any as ANY from pixelated.resources.auth import SessionChecker, PixelatedRealm, PixelatedAuthSessionWrapper from pixelated.resources.login_resource import LoginResource -from pixelated.resources.root_resource import RootResource +from pixelated.resources.root_resource import PublicRootResource, RootResource from test.unit.resources import DummySite from twisted.cred import error from twisted.cred.checkers import ANONYMOUS, AllowAnonymousAccess @@ -41,45 +41,31 @@ class TestPixelatedAuthSessionWrapper(unittest.TestCase): self.portal = Portal(self.realm_mock, [session_checker, AllowAnonymousAccess()]) self.user_uuid_mock = mock() self.root_resource = RootResource(services_factory) - self.anonymous_resource_mock = mock() + self.anonymous_resource = PublicRootResource(services_factory) - self.session_wrapper = PixelatedAuthSessionWrapper(self.portal, self.root_resource, self.anonymous_resource_mock) + self.session_wrapper = PixelatedAuthSessionWrapper(self.portal, self.root_resource, self.anonymous_resource) self.request = DummyRequest([]) self.request.prepath = [''] self.request.path = '/' - def test_should_proxy_to_login_resource_when_the_user_is_not_logged_in(self): - when(self.realm_mock).requestAvatar(ANONYMOUS, None, IResource).thenReturn((IResource, self.anonymous_resource_mock, lambda: None)) - - deferred_resource = self.session_wrapper.getChildWithDefault('', self.request) + def test_root_url_should_delegate_to_public_root_resource_for_unauthenticated_user(self): + when(self.realm_mock).requestAvatar(ANONYMOUS, None, IResource).thenReturn((IResource, self.anonymous_resource, lambda: None)) + request = DummyRequest(['']) + deferred_resource = getChildForRequest(self.session_wrapper, request) d = deferred_resource.d - def assert_anonymous_resource(resource): - self.assertIs(resource, self.anonymous_resource_mock) + def assert_public_root_resource(resource): + self.assertIsInstance(resource, PublicRootResource) - d.addCallback(assert_anonymous_resource) - return d + return d.addCallback(assert_public_root_resource) - def test_should_proxy_to_root_resource_when_the_user_is_logged_in(self): + def test_root_url_should_delegate_to_protected_root_resource_for_authenticated_user(self): when(self.realm_mock).requestAvatar(ANY(), None, IResource).thenReturn((IResource, self.root_resource, lambda: None)) - - deferred_resource = self.session_wrapper.getChildWithDefault('', self.request) - d = deferred_resource.d - - def assert_root_resource(resource): - self.assertIs(resource, self.root_resource) - - d.addCallback(assert_root_resource) - return d - - def test_should_X_when_unauthenticated_user_requests_non_public_resource(self): - when(self.realm_mock).requestAvatar(ANONYMOUS, None, IResource).thenReturn((IResource, self.anonymous_resource_mock, lambda: None)) - - deferred_resource = self.session_wrapper.getChildWithDefault('', self.request) + request = DummyRequest(['']) + deferred_resource = getChildForRequest(self.session_wrapper, request) d = deferred_resource.d - def assert_unauthorized_resource(resource): - self.assertIs(resource, self.anonymous_resource_mock) + def assert_protected_root_resource(resource): + self.assertIsInstance(resource, RootResource) - d.addCallback(assert_unauthorized_resource) - return d + return d.addCallback(assert_protected_root_resource) -- cgit v1.2.3 From b785705033d70725eb979f54bb3c248c82d648af Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Tue, 29 Nov 2016 18:17:16 +0100 Subject: mock out usage or ZMQ --- service/test/unit/resources/test_root_resource.py | 25 +++++++++++++++-------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'service/test') diff --git a/service/test/unit/resources/test_root_resource.py b/service/test/unit/resources/test_root_resource.py index 06eaf1ad..0db3bdbe 100644 --- a/service/test/unit/resources/test_root_resource.py +++ b/service/test/unit/resources/test_root_resource.py @@ -54,7 +54,8 @@ class TestRootResource(unittest.TestCase): child_resource = getChildForRequest(self.root_resource, request) self.assertIsInstance(child_resource, InboxResource) - def test_login_url_should_delegate_to_login_resource(self): + @patch('pixelated.config.sessions.register') + def test_login_url_should_delegate_to_login_resource(self, *mocks): self.root_resource.initialize(provider=mock(), authenticator=mock()) request = DummyRequest(['login']) request.addCookie = lambda key, value: 'stubbed' @@ -104,7 +105,8 @@ class TestRootResource(unittest.TestCase): request.requestHeaders.setRawHeaders('x-requested-with', ['XMLHttpRequest']) request.requestHeaders.setRawHeaders('x-xsrf-token', [csrf_token]) - def test_should_unauthorize_child_resource_ajax_requests_when_csrf_mismatch(self): + @patch('pixelated.config.sessions.register') + def test_should_unauthorize_child_resource_ajax_requests_when_csrf_mismatch(self, *mocks): self.root_resource.initialize(provider=mock(), authenticator=mock()) request = DummyRequest(['/child']) @@ -136,10 +138,11 @@ class TestRootResource(unittest.TestCase): d.addCallback(assert_unavailable) return d - def test_GET_should_return_404_for_non_existing_resource(self): + @patch('pixelated.config.sessions.register') + def test_GET_should_return_404_for_non_existing_resource(self, *mocks): self.root_resource.initialize(provider=mock(), authenticator=mock()) - request = DummyRequest(['/non-existing-child']) + request = DummyRequest(['non-existing-child']) request.method = 'GET' request.getCookie = MagicMock(return_value='stubbed csrf token') @@ -151,10 +154,11 @@ class TestRootResource(unittest.TestCase): d.addCallback(assert_not_found) return d - def test_should_404_non_existing_resource_with_valid_csrf(self): + @patch('pixelated.config.sessions.register') + def test_should_404_non_existing_resource_with_valid_csrf(self, *mocks): self.root_resource.initialize(provider=mock(), authenticator=mock()) - request = DummyRequest(['/non-existing-child']) + request = DummyRequest(['non-existing-child']) request.method = 'POST' self._mock_ajax_csrf(request, 'stubbed csrf token') request.getCookie = MagicMock(return_value='stubbed csrf token') @@ -183,7 +187,8 @@ class TestRootResource(unittest.TestCase): d.addCallback(assert_unauthorized) return d - def test_should_unauthorize_child_resource_non_ajax_POST_requests_when_csrf_input_mismatch(self): + @patch('pixelated.config.sessions.register') + def test_should_unauthorize_child_resource_non_ajax_POST_requests_when_csrf_input_mismatch(self, *mocks): self.root_resource.initialize(provider=mock(), authenticator=mock()) request = DummyRequest(['mails']) @@ -204,7 +209,8 @@ class TestRootResource(unittest.TestCase): d.addCallback(assert_unauthorized) return d - def test_assets_should_be_publicly_available(self): + @patch('pixelated.config.sessions.register') + def test_assets_should_be_publicly_available(self, *mocks): self.root_resource.initialize(provider=mock(), authenticator=mock()) request = DummyRequest(['assets', 'dummy.json']) @@ -216,7 +222,8 @@ class TestRootResource(unittest.TestCase): d.addCallback(assert_response) return d - def test_login_should_be_publicly_available(self): + @patch('pixelated.config.sessions.register') + def test_login_should_be_publicly_available(self, *mocks): self.root_resource.initialize(provider=mock(), authenticator=mock()) request = DummyRequest(['login']) -- cgit v1.2.3 From 54600b0454809eeed12b01960a1d0ecaeb0d86a9 Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Wed, 30 Nov 2016 10:01:20 +0100 Subject: mock out usage of ZMQ in the right place --- service/test/unit/resources/test_root_resource.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'service/test') diff --git a/service/test/unit/resources/test_root_resource.py b/service/test/unit/resources/test_root_resource.py index 0db3bdbe..443a00e6 100644 --- a/service/test/unit/resources/test_root_resource.py +++ b/service/test/unit/resources/test_root_resource.py @@ -54,7 +54,7 @@ class TestRootResource(unittest.TestCase): child_resource = getChildForRequest(self.root_resource, request) self.assertIsInstance(child_resource, InboxResource) - @patch('pixelated.config.sessions.register') + @patch('pixelated.resources.mails_resource.events.register') def test_login_url_should_delegate_to_login_resource(self, *mocks): self.root_resource.initialize(provider=mock(), authenticator=mock()) request = DummyRequest(['login']) @@ -105,7 +105,7 @@ class TestRootResource(unittest.TestCase): request.requestHeaders.setRawHeaders('x-requested-with', ['XMLHttpRequest']) request.requestHeaders.setRawHeaders('x-xsrf-token', [csrf_token]) - @patch('pixelated.config.sessions.register') + @patch('pixelated.resources.mails_resource.events.register') def test_should_unauthorize_child_resource_ajax_requests_when_csrf_mismatch(self, *mocks): self.root_resource.initialize(provider=mock(), authenticator=mock()) @@ -138,7 +138,7 @@ class TestRootResource(unittest.TestCase): d.addCallback(assert_unavailable) return d - @patch('pixelated.config.sessions.register') + @patch('pixelated.resources.mails_resource.events.register') def test_GET_should_return_404_for_non_existing_resource(self, *mocks): self.root_resource.initialize(provider=mock(), authenticator=mock()) @@ -154,7 +154,7 @@ class TestRootResource(unittest.TestCase): d.addCallback(assert_not_found) return d - @patch('pixelated.config.sessions.register') + @patch('pixelated.resources.mails_resource.events.register') def test_should_404_non_existing_resource_with_valid_csrf(self, *mocks): self.root_resource.initialize(provider=mock(), authenticator=mock()) @@ -187,7 +187,7 @@ class TestRootResource(unittest.TestCase): d.addCallback(assert_unauthorized) return d - @patch('pixelated.config.sessions.register') + @patch('pixelated.resources.mails_resource.events.register') def test_should_unauthorize_child_resource_non_ajax_POST_requests_when_csrf_input_mismatch(self, *mocks): self.root_resource.initialize(provider=mock(), authenticator=mock()) @@ -209,7 +209,7 @@ class TestRootResource(unittest.TestCase): d.addCallback(assert_unauthorized) return d - @patch('pixelated.config.sessions.register') + @patch('pixelated.resources.mails_resource.events.register') def test_assets_should_be_publicly_available(self, *mocks): self.root_resource.initialize(provider=mock(), authenticator=mock()) @@ -222,7 +222,7 @@ class TestRootResource(unittest.TestCase): d.addCallback(assert_response) return d - @patch('pixelated.config.sessions.register') + @patch('pixelated.resources.mails_resource.events.register') def test_login_should_be_publicly_available(self, *mocks): self.root_resource.initialize(provider=mock(), authenticator=mock()) -- cgit v1.2.3 From c10c6fb76f06e0cfc6f061a1bd9df14d689fb176 Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Wed, 30 Nov 2016 10:29:27 +0100 Subject: redirect to login from root url when not logged in --- service/test/unit/resources/test_root_resource.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'service/test') diff --git a/service/test/unit/resources/test_root_resource.py b/service/test/unit/resources/test_root_resource.py index 443a00e6..1543f650 100644 --- a/service/test/unit/resources/test_root_resource.py +++ b/service/test/unit/resources/test_root_resource.py @@ -31,6 +31,19 @@ class TestPublicRootResource(unittest.TestCase): child_resource = getChildForRequest(self.public_root_resource, request) self.assertIsInstance(child_resource, LoginResource) + def test_root_url_should_redirect_to_login_resource(self): + self.public_root_resource.initialize(provider=mock(), authenticator=mock()) + request = DummyRequest(['']) + request.addCookie = lambda key, value: 'stubbed' + d = self.web.get(request) + + def assert_redirect(request): + self.assertEqual(302, request.responseCode) + self.assertEqual(["login"], request.responseHeaders.getRawHeaders('location', [None])) + + d.addCallback(assert_redirect) + return d + class TestRootResource(unittest.TestCase): MAIL_ADDRESS = 'test_user@pixelated-project.org' -- cgit v1.2.3 From 41f0886aeac43387dc8b4d54b1ca69f21e2ec2a8 Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Wed, 30 Nov 2016 15:00:58 +0100 Subject: remove PublicRootResource and use a flag on RootResource instead --- service/test/unit/resources/test_auth.py | 6 +-- service/test/unit/resources/test_root_resource.py | 63 ++++++++++++++++++++++- 2 files changed, 64 insertions(+), 5 deletions(-) (limited to 'service/test') diff --git a/service/test/unit/resources/test_auth.py b/service/test/unit/resources/test_auth.py index 793069dd..f4012b1b 100644 --- a/service/test/unit/resources/test_auth.py +++ b/service/test/unit/resources/test_auth.py @@ -1,7 +1,7 @@ from mockito import mock, when, any as ANY from pixelated.resources.auth import SessionChecker, PixelatedRealm, PixelatedAuthSessionWrapper from pixelated.resources.login_resource import LoginResource -from pixelated.resources.root_resource import PublicRootResource, RootResource +from pixelated.resources.root_resource import RootResource from test.unit.resources import DummySite from twisted.cred import error from twisted.cred.checkers import ANONYMOUS, AllowAnonymousAccess @@ -41,7 +41,7 @@ class TestPixelatedAuthSessionWrapper(unittest.TestCase): self.portal = Portal(self.realm_mock, [session_checker, AllowAnonymousAccess()]) self.user_uuid_mock = mock() self.root_resource = RootResource(services_factory) - self.anonymous_resource = PublicRootResource(services_factory) + self.anonymous_resource = RootResource(services_factory, public=True) self.session_wrapper = PixelatedAuthSessionWrapper(self.portal, self.root_resource, self.anonymous_resource) self.request = DummyRequest([]) @@ -55,7 +55,7 @@ class TestPixelatedAuthSessionWrapper(unittest.TestCase): d = deferred_resource.d def assert_public_root_resource(resource): - self.assertIsInstance(resource, PublicRootResource) + self.assertIs(resource, self.anonymous_resource) return d.addCallback(assert_public_root_resource) diff --git a/service/test/unit/resources/test_root_resource.py b/service/test/unit/resources/test_root_resource.py index 1543f650..b674103c 100644 --- a/service/test/unit/resources/test_root_resource.py +++ b/service/test/unit/resources/test_root_resource.py @@ -6,6 +6,7 @@ from mockito import mock, when, any as ANY import pixelated from pixelated.application import UserAgentMode +from pixelated.resources import UnAuthorizedResource from pixelated.resources.features_resource import FeaturesResource from pixelated.resources.login_resource import LoginResource from test.unit.resources import DummySite @@ -15,15 +16,55 @@ from twisted.trial import unittest from twisted.web.resource import IResource, getChildForRequest from twisted.web.static import File from twisted.web.test.requesthelper import DummyRequest -from pixelated.resources.root_resource import InboxResource, PublicRootResource, RootResource, MODE_STARTUP, MODE_RUNNING +from pixelated.resources.root_resource import InboxResource, RootResource, MODE_STARTUP, MODE_RUNNING class TestPublicRootResource(unittest.TestCase): def setUp(self): - self.public_root_resource = PublicRootResource(mock()) + self.public_root_resource = RootResource(mock(), public=True) self.web = DummySite(self.public_root_resource) + def test_put_child_public_adds_resource(self): + self.public_root_resource.initialize(provider=mock(), authenticator=mock()) + url_fragment, resource_mock = 'some-url-fragment', mock() + self.public_root_resource.putChildPublic(url_fragment, resource_mock) + request = DummyRequest([url_fragment]) + request.addCookie = lambda key, value: 'stubbed' + child_resource = getChildForRequest(self.public_root_resource, request) + self.assertIs(child_resource, resource_mock) + + def test_put_child_protected_adds_unauthorized(self): + self.public_root_resource.initialize(provider=mock(), authenticator=mock()) + url_fragment, resource_mock = 'some-url-fragment', mock() + self.public_root_resource.putChildProtected(url_fragment, resource_mock) + request = DummyRequest([url_fragment]) + request.addCookie = lambda key, value: 'stubbed' + child_resource = getChildForRequest(self.public_root_resource, request) + self.assertIsInstance(child_resource, UnAuthorizedResource) + + def test_put_child_adds_unauthorized(self): + self.public_root_resource.initialize(provider=mock(), authenticator=mock()) + url_fragment, resource_mock = 'some-url-fragment', mock() + self.public_root_resource.putChild(url_fragment, resource_mock) + request = DummyRequest([url_fragment]) + request.addCookie = lambda key, value: 'stubbed' + child_resource = getChildForRequest(self.public_root_resource, request) + self.assertIsInstance(child_resource, UnAuthorizedResource) + + def test_private_resource_returns_401(self): + self.public_root_resource.initialize(provider=mock(), authenticator=mock()) + request = DummyRequest(['mails']) + request.addCookie = lambda key, value: 'stubbed' + d = self.web.get(request) + + def assert_unauthorized(request): + self.assertEqual(401, request.responseCode) + self.assertEqual("Unauthorized!", request.written[0]) + + d.addCallback(assert_unauthorized) + return d + def test_login_url_should_delegate_to_login_resource(self): self.public_root_resource.initialize(provider=mock(), authenticator=mock()) request = DummyRequest(['login']) @@ -61,6 +102,24 @@ class TestRootResource(unittest.TestCase): self.root_resource = RootResource(self.services_factory) self.web = DummySite(self.root_resource) + def test_put_child_protected_adds_resource(self): + self.root_resource.initialize(provider=mock(), authenticator=mock()) + url_fragment, resource_mock = 'some-url-fragment', mock() + self.root_resource.putChildProtected(url_fragment, resource_mock) + request = DummyRequest([url_fragment]) + request.addCookie = lambda key, value: 'stubbed' + child_resource = getChildForRequest(self.root_resource, request) + self.assertIs(child_resource, resource_mock) + + def test_put_child_adds_resource(self): + self.root_resource.initialize(provider=mock(), authenticator=mock()) + url_fragment, resource_mock = 'some-url-fragment', mock() + self.root_resource.putChild(url_fragment, resource_mock) + request = DummyRequest([url_fragment]) + request.addCookie = lambda key, value: 'stubbed' + child_resource = getChildForRequest(self.root_resource, request) + self.assertIs(child_resource, resource_mock) + def test_root_url_should_delegate_to_inbox(self): request = DummyRequest(['']) request.addCookie = lambda key, value: 'stubbed' -- cgit v1.2.3 From 13378255c02b97184132881599ed47826963f54a Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Wed, 30 Nov 2016 16:11:27 +0100 Subject: add csrf token to login form --- service/test/unit/resources/test_login_resource.py | 13 +++++++++++ service/test/unit/resources/test_session.py | 25 ++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 service/test/unit/resources/test_session.py (limited to 'service/test') diff --git a/service/test/unit/resources/test_login_resource.py b/service/test/unit/resources/test_login_resource.py index d3d7ba64..696b0c46 100644 --- a/service/test/unit/resources/test_login_resource.py +++ b/service/test/unit/resources/test_login_resource.py @@ -157,6 +157,19 @@ class TestLoginResource(unittest.TestCase): d.addCallback(assert_default_invalid_banner_disclaimer_rendered) return d + def test_form_should_contain_csrftoken_input(self): + request = DummyRequest(['']) + + d = self.web.get(request) + + def assert_form_has_csrftoken_input(_): + input_username = 'name="csrftoken"' + written_response = ''.join(request.written) + self.assertIn(input_username, written_response) + + d.addCallback(assert_form_has_csrftoken_input) + return d + class TestLoginPOST(unittest.TestCase): def setUp(self): diff --git a/service/test/unit/resources/test_session.py b/service/test/unit/resources/test_session.py new file mode 100644 index 00000000..fe47483d --- /dev/null +++ b/service/test/unit/resources/test_session.py @@ -0,0 +1,25 @@ +from twisted.trial import unittest +from mockito import mock +from pixelated.resources.session import CSRF_TOKEN_LENGTH, PixelatedSession + + +class TestPixelatedSession(unittest.TestCase): + + def setUp(self): + self.pixelated_session = PixelatedSession(mock()) + + def test_csrf_token_should_be_configured_length(self): + self.assertEqual(len(self.pixelated_session.get_csrf_token()), 2 * CSRF_TOKEN_LENGTH) + + def test_csrf_token_should_be_hexdigested(self): + self.assertTrue(all(c in '0123456789abcdef' for c in self.pixelated_session.get_csrf_token())) + + def test_csrf_token_should_always_be_the_same_for_one_session(self): + first_csrf_token = self.pixelated_session.get_csrf_token() + second_csrf_token = self.pixelated_session.get_csrf_token() + self.assertEqual(first_csrf_token, second_csrf_token) + + def test_csrf_token_should_be_different_for_different_session(self): + first_csrf_token = self.pixelated_session.get_csrf_token() + second_csrf_token = PixelatedSession(mock()).get_csrf_token() + self.assertNotEqual(first_csrf_token, second_csrf_token) -- cgit v1.2.3 From 770b439c8495c3a0b16550c2f04740f31646d66b Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Thu, 1 Dec 2016 10:36:29 +0100 Subject: WIP: add csrf token to every request --- service/test/integration/test_delete_mail.py | 5 ++- service/test/integration/test_logout.py | 4 +- .../test/support/integration/app_test_client.py | 4 +- .../test/support/integration/multi_user_client.py | 8 +++- service/test/unit/resources/test_inbox_resource.py | 26 ------------ service/test/unit/resources/test_root_resource.py | 48 ++++++++++++++++------ 6 files changed, 51 insertions(+), 44 deletions(-) (limited to 'service/test') diff --git a/service/test/integration/test_delete_mail.py b/service/test/integration/test_delete_mail.py index a912f9f0..6cb9ceb6 100644 --- a/service/test/integration/test_delete_mail.py +++ b/service/test/integration/test_delete_mail.py @@ -15,6 +15,7 @@ # along with Pixelated. If not, see . from twisted.internet import defer from test.support.integration import SoledadTestBase, MailBuilder +from pixelated.resources import IPixelatedSession class DeleteMailTest(SoledadTestBase): @@ -27,7 +28,9 @@ class DeleteMailTest(SoledadTestBase): inbox_mails = yield self.app_test_client.get_mails_by_tag('inbox') self.assertEquals(1, len(inbox_mails)) - yield self.app_test_client.delete_mail(mail.mail_id) + response, first_request = yield self.app_test_client.get('/', as_json=False) + csrftoken = IPixelatedSession(first_request.getSession()).get_csrf_token() + yield self.app_test_client.delete_mail(mail.mail_id, csrf=csrftoken) inbox_mails = yield self.app_test_client.get_mails_by_tag('inbox') self.assertEquals(0, len(inbox_mails)) diff --git a/service/test/integration/test_logout.py b/service/test/integration/test_logout.py index c9d39d17..b4f8ebf3 100644 --- a/service/test/integration/test_logout.py +++ b/service/test/integration/test_logout.py @@ -29,7 +29,8 @@ class MultiUserLogoutTest(MultiUserSoledadTestBase): @defer.inlineCallbacks def test_logout_deletes_services_stop_background_reactor_tasks_and_closes_soledad(self): - response, login_request = yield self.app_test_client.login() + response, first_request = yield self.app_test_client.get('/login', as_json=False) + response, login_request = yield self.app_test_client.login(from_request=first_request) yield response yield self.wait_for_session_user_id_to_finish() @@ -37,6 +38,7 @@ class MultiUserLogoutTest(MultiUserSoledadTestBase): response, request = self.app_test_client.post( "/logout", json.dumps({'csrftoken': [login_request.getCookie('XSRF-TOKEN')]}), + ajax=False, from_request=login_request, as_json=False) yield response diff --git a/service/test/support/integration/app_test_client.py b/service/test/support/integration/app_test_client.py index d52c85c0..ee5a1df2 100644 --- a/service/test/support/integration/app_test_client.py +++ b/service/test/support/integration/app_test_client.py @@ -387,8 +387,8 @@ class AppTestClient(object): return res # TODO: remove - def delete_mail(self, mail_ident): - res, req = self.delete("/mail/%s" % mail_ident) + def delete_mail(self, mail_ident, csrf='token'): + res, req = self.delete("/mail/%s" % mail_ident, csrf=csrf) return res def delete_mails(self, idents): diff --git a/service/test/support/integration/multi_user_client.py b/service/test/support/integration/multi_user_client.py index 82acb210..fe8595fb 100644 --- a/service/test/support/integration/multi_user_client.py +++ b/service/test/support/integration/multi_user_client.py @@ -24,6 +24,7 @@ from pixelated.config.services import ServicesFactory from pixelated.config.sessions import LeapSessionFactory import pixelated.config.services +from pixelated.resources import IPixelatedSession from pixelated.resources.root_resource import RootResource from test.support.integration import AppTestClient from test.support.integration.app_test_client import AppTestAccount, StubSRPChecker @@ -57,7 +58,7 @@ class MultiUserClient(AppTestClient): else: when(Authenticator)._bonafide_auth(username, password).thenRaise(SRPAuthError) - def login(self, username='username', password='password'): + def login(self, username='username', password='password', from_request=None): session = Authentication(username, 'some_user_token', 'some_user_uuid', 'session_id', {'is_admin': False}) leap_session = self._test_account.leap_session leap_session.user_auth = session @@ -76,7 +77,10 @@ class MultiUserClient(AppTestClient): when(leap_session).initial_sync().thenAnswer(lambda: defer.succeed(None)) when(pixelated.config.services).Services(ANY()).thenReturn(self.services) - request = request_mock(path='/login', method="POST", body={'username': username, 'password': password}) + session = from_request.getSession() + csrftoken = IPixelatedSession(session).get_csrf_token() + request = request_mock(path='/login', method="POST", body={'username': username, 'password': password, 'csrftoken': csrftoken}, ajax=False) + request.session = session return self._render(request, as_json=False) def get(self, path, get_args='', as_json=True, from_request=None): diff --git a/service/test/unit/resources/test_inbox_resource.py b/service/test/unit/resources/test_inbox_resource.py index 03fe6f1a..9af355ca 100644 --- a/service/test/unit/resources/test_inbox_resource.py +++ b/service/test/unit/resources/test_inbox_resource.py @@ -44,29 +44,3 @@ class TestInboxResource(unittest.TestCase): d.addCallback(assert_response) return d - - def _test_should_renew_xsrf_cookie(self): - request = DummyRequest(['']) - request.addCookie = MagicMock() - generated_csrf_token = 'csrf_token' - mock_sha = MagicMock() - mock_sha.hexdigest = MagicMock(return_value=generated_csrf_token) - - with patch('hashlib.sha256', return_value=mock_sha): - d = self.web.get(request) - - def assert_csrf_cookie(_): - request.addCookie.assert_called_once_with('XSRF-TOKEN', generated_csrf_token) - - d.addCallback(assert_csrf_cookie) - return d - - # TODO should this be here or just in the root resource test? - def test_should_renew_xsrf_cookie_on_startup_mode(self): - self.inbox_resource._mode = MODE_STARTUP - self._test_should_renew_xsrf_cookie() - - # TODO should this be here or just in the root resource test? - def test_should_renew_xsrf_cookie_on_running_mode(self): - self.inbox_resource._mode = MODE_RUNNING - self._test_should_renew_xsrf_cookie() diff --git a/service/test/unit/resources/test_root_resource.py b/service/test/unit/resources/test_root_resource.py index b674103c..2dfe3e5a 100644 --- a/service/test/unit/resources/test_root_resource.py +++ b/service/test/unit/resources/test_root_resource.py @@ -6,7 +6,7 @@ from mockito import mock, when, any as ANY import pixelated from pixelated.application import UserAgentMode -from pixelated.resources import UnAuthorizedResource +from pixelated.resources import IPixelatedSession, UnAuthorizedResource from pixelated.resources.features_resource import FeaturesResource from pixelated.resources.login_resource import LoginResource from test.unit.resources import DummySite @@ -30,7 +30,7 @@ class TestPublicRootResource(unittest.TestCase): url_fragment, resource_mock = 'some-url-fragment', mock() self.public_root_resource.putChildPublic(url_fragment, resource_mock) request = DummyRequest([url_fragment]) - request.addCookie = lambda key, value: 'stubbed' + request.addCookie = MagicMock(return_value='stubbed') child_resource = getChildForRequest(self.public_root_resource, request) self.assertIs(child_resource, resource_mock) @@ -39,7 +39,7 @@ class TestPublicRootResource(unittest.TestCase): url_fragment, resource_mock = 'some-url-fragment', mock() self.public_root_resource.putChildProtected(url_fragment, resource_mock) request = DummyRequest([url_fragment]) - request.addCookie = lambda key, value: 'stubbed' + request.addCookie = MagicMock(return_value='stubbed') child_resource = getChildForRequest(self.public_root_resource, request) self.assertIsInstance(child_resource, UnAuthorizedResource) @@ -48,14 +48,14 @@ class TestPublicRootResource(unittest.TestCase): url_fragment, resource_mock = 'some-url-fragment', mock() self.public_root_resource.putChild(url_fragment, resource_mock) request = DummyRequest([url_fragment]) - request.addCookie = lambda key, value: 'stubbed' + request.addCookie = MagicMock(return_value='stubbed') child_resource = getChildForRequest(self.public_root_resource, request) self.assertIsInstance(child_resource, UnAuthorizedResource) def test_private_resource_returns_401(self): self.public_root_resource.initialize(provider=mock(), authenticator=mock()) request = DummyRequest(['mails']) - request.addCookie = lambda key, value: 'stubbed' + request.addCookie = MagicMock(return_value='stubbed') d = self.web.get(request) def assert_unauthorized(request): @@ -68,14 +68,14 @@ class TestPublicRootResource(unittest.TestCase): def test_login_url_should_delegate_to_login_resource(self): self.public_root_resource.initialize(provider=mock(), authenticator=mock()) request = DummyRequest(['login']) - request.addCookie = lambda key, value: 'stubbed' + request.addCookie = MagicMock(return_value='stubbed') child_resource = getChildForRequest(self.public_root_resource, request) self.assertIsInstance(child_resource, LoginResource) def test_root_url_should_redirect_to_login_resource(self): self.public_root_resource.initialize(provider=mock(), authenticator=mock()) request = DummyRequest(['']) - request.addCookie = lambda key, value: 'stubbed' + request.addCookie = MagicMock(return_value='stubbed') d = self.web.get(request) def assert_redirect(request): @@ -107,7 +107,7 @@ class TestRootResource(unittest.TestCase): url_fragment, resource_mock = 'some-url-fragment', mock() self.root_resource.putChildProtected(url_fragment, resource_mock) request = DummyRequest([url_fragment]) - request.addCookie = lambda key, value: 'stubbed' + request.addCookie = MagicMock(return_value='stubbed') child_resource = getChildForRequest(self.root_resource, request) self.assertIs(child_resource, resource_mock) @@ -116,13 +116,13 @@ class TestRootResource(unittest.TestCase): url_fragment, resource_mock = 'some-url-fragment', mock() self.root_resource.putChild(url_fragment, resource_mock) request = DummyRequest([url_fragment]) - request.addCookie = lambda key, value: 'stubbed' + request.addCookie = MagicMock(return_value='stubbed') child_resource = getChildForRequest(self.root_resource, request) self.assertIs(child_resource, resource_mock) def test_root_url_should_delegate_to_inbox(self): request = DummyRequest(['']) - request.addCookie = lambda key, value: 'stubbed' + request.addCookie = MagicMock(return_value='stubbed') child_resource = getChildForRequest(self.root_resource, request) self.assertIsInstance(child_resource, InboxResource) @@ -130,13 +130,13 @@ class TestRootResource(unittest.TestCase): def test_login_url_should_delegate_to_login_resource(self, *mocks): self.root_resource.initialize(provider=mock(), authenticator=mock()) request = DummyRequest(['login']) - request.addCookie = lambda key, value: 'stubbed' + request.addCookie = MagicMock(return_value='stubbed') child_resource = getChildForRequest(self.root_resource, request) self.assertIsInstance(child_resource, LoginResource) def _test_should_renew_xsrf_cookie(self): request = DummyRequest(['']) - request.addCookie = MagicMock() + request.addCookie = MagicMock(return_value='stubbed') generated_csrf_token = 'csrf_token' mock_sha = MagicMock() mock_sha.hexdigest = MagicMock(return_value=generated_csrf_token) @@ -162,6 +162,7 @@ class TestRootResource(unittest.TestCase): self.root_resource._mode = MODE_STARTUP request = DummyRequest(['/child']) + request.addCookie = MagicMock(return_value='stubbed') request.getCookie = MagicMock(return_value='irrelevant -- stubbed') d = self.web.get(request) @@ -182,6 +183,7 @@ class TestRootResource(unittest.TestCase): self.root_resource.initialize(provider=mock(), authenticator=mock()) request = DummyRequest(['/child']) + request.addCookie = MagicMock(return_value='stubbed') request.method = 'POST' self._mock_ajax_csrf(request, 'stubbed csrf token') @@ -198,6 +200,7 @@ class TestRootResource(unittest.TestCase): def test_GET_should_return_503_for_uninitialized_resource(self): request = DummyRequest(['/sandbox/']) + request.addCookie = MagicMock(return_value='stubbed') request.method = 'GET' request.getCookie = MagicMock(return_value='stubbed csrf token') @@ -215,6 +218,7 @@ class TestRootResource(unittest.TestCase): self.root_resource.initialize(provider=mock(), authenticator=mock()) request = DummyRequest(['non-existing-child']) + request.addCookie = MagicMock(return_value='stubbed') request.method = 'GET' request.getCookie = MagicMock(return_value='stubbed csrf token') @@ -231,6 +235,7 @@ class TestRootResource(unittest.TestCase): self.root_resource.initialize(provider=mock(), authenticator=mock()) request = DummyRequest(['non-existing-child']) + request.addCookie = MagicMock(return_value='stubbed') request.method = 'POST' self._mock_ajax_csrf(request, 'stubbed csrf token') request.getCookie = MagicMock(return_value='stubbed csrf token') @@ -246,6 +251,7 @@ class TestRootResource(unittest.TestCase): def test_should_authorize_child_resource_non_ajax_GET_requests(self): request = DummyRequest(['features']) + request.addCookie = MagicMock(return_value='stubbed') request.getCookie = MagicMock(return_value='irrelevant -- stubbed') self.root_resource.putChild('features', FeaturesResource()) @@ -270,6 +276,7 @@ class TestRootResource(unittest.TestCase): mock_content.read = MagicMock(return_value={}) request.content = mock_content + request.addCookie = MagicMock(return_value='stubbed') request.getCookie = MagicMock(return_value='mismatched csrf token') d = self.web.get(request) @@ -286,6 +293,7 @@ class TestRootResource(unittest.TestCase): self.root_resource.initialize(provider=mock(), authenticator=mock()) request = DummyRequest(['assets', 'dummy.json']) + request.addCookie = MagicMock(return_value='stubbed') d = self.web.get(request) def assert_response(_): @@ -299,6 +307,7 @@ class TestRootResource(unittest.TestCase): self.root_resource.initialize(provider=mock(), authenticator=mock()) request = DummyRequest(['login']) + request.addCookie = MagicMock(return_value='stubbed') d = self.web.get(request) def assert_response(_): @@ -309,6 +318,7 @@ class TestRootResource(unittest.TestCase): def test_root_should_be_handled_by_inbox_resource(self): request = DummyRequest([]) + request.addCookie = MagicMock(return_value='stubbed') request.prepath = [''] request.path = '/' # TODO: setup mocked portal @@ -318,9 +328,23 @@ class TestRootResource(unittest.TestCase): def test_inbox_should_not_be_public(self): request = DummyRequest([]) + request.addCookie = MagicMock(return_value='stubbed') request.prepath = [''] request.path = '/' # TODO: setup mocked portal resource = self.root_resource.getChildWithDefault(request.prepath[-1], request) self.assertIsInstance(resource, InboxResource) + + def test_every_url_should_get_csrftoken_header(self): + # self.root_resource.initialize(provider=mock(), authenticator=mock()) + request = DummyRequest(['any']) + request.addCookie = MagicMock(return_value='stubbed') + d = self.web.get(request) + + def assert_add_cookie_called_for_csrftoken(request): + csrftoken = IPixelatedSession(request.getSession()).get_csrf_token() + self.assertEqual([(('XSRF-TOKEN', csrftoken),)], request.addCookie.call_args_list) + + d.addCallback(assert_add_cookie_called_for_csrftoken) + return d -- cgit v1.2.3 From 875249af34fc5a53b727fe8b8296a5d4206c11c7 Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Thu, 1 Dec 2016 13:39:37 +0100 Subject: fix root resource tests when zmq is not available --- service/test/unit/resources/test_root_resource.py | 24 +++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'service/test') diff --git a/service/test/unit/resources/test_root_resource.py b/service/test/unit/resources/test_root_resource.py index 2dfe3e5a..9d738a83 100644 --- a/service/test/unit/resources/test_root_resource.py +++ b/service/test/unit/resources/test_root_resource.py @@ -25,7 +25,8 @@ class TestPublicRootResource(unittest.TestCase): self.public_root_resource = RootResource(mock(), public=True) self.web = DummySite(self.public_root_resource) - def test_put_child_public_adds_resource(self): + @patch('pixelated.resources.mails_resource.events.register') + def test_put_child_public_adds_resource(self, *mocks): self.public_root_resource.initialize(provider=mock(), authenticator=mock()) url_fragment, resource_mock = 'some-url-fragment', mock() self.public_root_resource.putChildPublic(url_fragment, resource_mock) @@ -34,7 +35,8 @@ class TestPublicRootResource(unittest.TestCase): child_resource = getChildForRequest(self.public_root_resource, request) self.assertIs(child_resource, resource_mock) - def test_put_child_protected_adds_unauthorized(self): + @patch('pixelated.resources.mails_resource.events.register') + def test_put_child_protected_adds_unauthorized(self, *mocks): self.public_root_resource.initialize(provider=mock(), authenticator=mock()) url_fragment, resource_mock = 'some-url-fragment', mock() self.public_root_resource.putChildProtected(url_fragment, resource_mock) @@ -43,7 +45,8 @@ class TestPublicRootResource(unittest.TestCase): child_resource = getChildForRequest(self.public_root_resource, request) self.assertIsInstance(child_resource, UnAuthorizedResource) - def test_put_child_adds_unauthorized(self): + @patch('pixelated.resources.mails_resource.events.register') + def test_put_child_adds_unauthorized(self, *mocks): self.public_root_resource.initialize(provider=mock(), authenticator=mock()) url_fragment, resource_mock = 'some-url-fragment', mock() self.public_root_resource.putChild(url_fragment, resource_mock) @@ -52,7 +55,8 @@ class TestPublicRootResource(unittest.TestCase): child_resource = getChildForRequest(self.public_root_resource, request) self.assertIsInstance(child_resource, UnAuthorizedResource) - def test_private_resource_returns_401(self): + @patch('pixelated.resources.mails_resource.events.register') + def test_private_resource_returns_401(self, *mocks): self.public_root_resource.initialize(provider=mock(), authenticator=mock()) request = DummyRequest(['mails']) request.addCookie = MagicMock(return_value='stubbed') @@ -65,14 +69,16 @@ class TestPublicRootResource(unittest.TestCase): d.addCallback(assert_unauthorized) return d - def test_login_url_should_delegate_to_login_resource(self): + @patch('pixelated.resources.mails_resource.events.register') + def test_login_url_should_delegate_to_login_resource(self, *mocks): self.public_root_resource.initialize(provider=mock(), authenticator=mock()) request = DummyRequest(['login']) request.addCookie = MagicMock(return_value='stubbed') child_resource = getChildForRequest(self.public_root_resource, request) self.assertIsInstance(child_resource, LoginResource) - def test_root_url_should_redirect_to_login_resource(self): + @patch('pixelated.resources.mails_resource.events.register') + def test_root_url_should_redirect_to_login_resource(self, *mocks): self.public_root_resource.initialize(provider=mock(), authenticator=mock()) request = DummyRequest(['']) request.addCookie = MagicMock(return_value='stubbed') @@ -102,7 +108,8 @@ class TestRootResource(unittest.TestCase): self.root_resource = RootResource(self.services_factory) self.web = DummySite(self.root_resource) - def test_put_child_protected_adds_resource(self): + @patch('pixelated.resources.mails_resource.events.register') + def test_put_child_protected_adds_resource(self, *mocks): self.root_resource.initialize(provider=mock(), authenticator=mock()) url_fragment, resource_mock = 'some-url-fragment', mock() self.root_resource.putChildProtected(url_fragment, resource_mock) @@ -111,7 +118,8 @@ class TestRootResource(unittest.TestCase): child_resource = getChildForRequest(self.root_resource, request) self.assertIs(child_resource, resource_mock) - def test_put_child_adds_resource(self): + @patch('pixelated.resources.mails_resource.events.register') + def test_put_child_adds_resource(self, *mocks): self.root_resource.initialize(provider=mock(), authenticator=mock()) url_fragment, resource_mock = 'some-url-fragment', mock() self.root_resource.putChild(url_fragment, resource_mock) -- cgit v1.2.3 From f0880aff32bbb30c6a8a0d4e078e563d24b97909 Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Thu, 1 Dec 2016 15:56:57 +0100 Subject: fix csrf for some integration tests --- service/test/integration/test_delete_mail.py | 12 +++++++----- service/test/integration/test_logout.py | 4 ++-- service/test/integration/test_multi_user_login.py | 8 +++++--- service/test/integration/test_users_count.py | 5 +++-- service/test/support/integration/app_test_client.py | 19 +++++++++++++------ .../test/support/integration/multi_user_client.py | 21 +++++++++------------ 6 files changed, 39 insertions(+), 30 deletions(-) (limited to 'service/test') diff --git a/service/test/integration/test_delete_mail.py b/service/test/integration/test_delete_mail.py index 6cb9ceb6..34ea5048 100644 --- a/service/test/integration/test_delete_mail.py +++ b/service/test/integration/test_delete_mail.py @@ -29,8 +29,7 @@ class DeleteMailTest(SoledadTestBase): self.assertEquals(1, len(inbox_mails)) response, first_request = yield self.app_test_client.get('/', as_json=False) - csrftoken = IPixelatedSession(first_request.getSession()).get_csrf_token() - yield self.app_test_client.delete_mail(mail.mail_id, csrf=csrftoken) + yield self.app_test_client.delete_mail(mail.mail_id, session=first_request.getSession()) inbox_mails = yield self.app_test_client.get_mails_by_tag('inbox') self.assertEquals(0, len(inbox_mails)) @@ -40,7 +39,8 @@ class DeleteMailTest(SoledadTestBase): @defer.inlineCallbacks def test_delete_mail_when_trashing_mail_from_trash_mailbox(self): mails = yield self.app_test_client.add_multiple_to_mailbox(1, 'trash') - yield self.app_test_client.delete_mails([mails[0].ident]) + response, first_request = yield self.app_test_client.get('/', as_json=False) + yield self.app_test_client.delete_mails([mails[0].ident], session=first_request.getSession()) trash_mails = yield self.app_test_client.get_mails_by_tag('trash') @@ -52,7 +52,8 @@ class DeleteMailTest(SoledadTestBase): mails = yield self.app_test_client.add_multiple_to_mailbox(5, 'inbox') mail_idents = [m.ident for m in mails] - yield self.app_test_client.delete_mails(mail_idents) + response, first_request = yield self.app_test_client.get('/', as_json=False) + yield self.app_test_client.delete_mails(mail_idents, session=first_request.getSession()) inbox = yield self.app_test_client.get_mails_by_tag('inbox') self.assertEquals(0, len(inbox)) @@ -62,7 +63,8 @@ class DeleteMailTest(SoledadTestBase): mails = yield self.app_test_client.add_multiple_to_mailbox(5, 'trash') mail_idents = [m.ident for m in mails] - yield self.app_test_client.delete_mails(mail_idents) + response, first_request = yield self.app_test_client.get('/', as_json=False) + yield self.app_test_client.delete_mails(mail_idents, session=first_request.getSession()) trash = yield self.app_test_client.get_mails_by_tag('trash') self.assertEquals(0, len(trash)) diff --git a/service/test/integration/test_logout.py b/service/test/integration/test_logout.py index b4f8ebf3..92c2afe5 100644 --- a/service/test/integration/test_logout.py +++ b/service/test/integration/test_logout.py @@ -30,7 +30,7 @@ class MultiUserLogoutTest(MultiUserSoledadTestBase): @defer.inlineCallbacks def test_logout_deletes_services_stop_background_reactor_tasks_and_closes_soledad(self): response, first_request = yield self.app_test_client.get('/login', as_json=False) - response, login_request = yield self.app_test_client.login(from_request=first_request) + response, login_request = yield self.app_test_client.login(session=first_request.getSession()) yield response yield self.wait_for_session_user_id_to_finish() @@ -39,7 +39,7 @@ class MultiUserLogoutTest(MultiUserSoledadTestBase): "/logout", json.dumps({'csrftoken': [login_request.getCookie('XSRF-TOKEN')]}), ajax=False, - from_request=login_request, + session=login_request.getSession(), as_json=False) yield response diff --git a/service/test/integration/test_multi_user_login.py b/service/test/integration/test_multi_user_login.py index af2a81ac..e1f58202 100644 --- a/service/test/integration/test_multi_user_login.py +++ b/service/test/integration/test_multi_user_login.py @@ -33,13 +33,14 @@ class MultiUserLoginTest(MultiUserSoledadTestBase): @defer.inlineCallbacks def test_logged_in_users_sees_resources(self): - response, login_request = yield self.app_test_client.login() + response, first_request = yield self.app_test_client.get('/login', as_json=False) + response, login_request = yield self.app_test_client.login(session=first_request.getSession()) yield response mail = load_mail_from_file('mbox00000000') mail_id = yield self._create_mail_in_soledad(mail) expected_mail_dict = {'body': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', 'header': {u'date': u'Tue, 21 Apr 2015 08:43:27 +0000 (UTC)', u'to': [u'carmel@murazikortiz.name'], u'x-tw-pixelated-tags': u'nite, macro, trash', u'from': u'darby.senger@zemlak.biz', u'subject': u'Itaque consequatur repellendus provident sunt quia.'}, 'ident': mail_id, 'status': [], 'tags': [], 'textPlainBody': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', 'mailbox': u'inbox', 'attachments': [], 'security_casing': {'imprints': [{'state': 'no_signature_information'}], 'locks': []}} - response, request = self.app_test_client.get("/mail/%s" % mail_id, from_request=login_request) + response, request = self.app_test_client.get("/mail/%s" % mail_id, session=login_request.getSession()) response = yield response self.assertEqual(200, request.code) @@ -48,7 +49,8 @@ class MultiUserLoginTest(MultiUserSoledadTestBase): @defer.inlineCallbacks def test_wrong_credentials_cannot_access_resources(self): - response, login_request = self.app_test_client.login('username', 'wrong_password') + response, first_request = yield self.app_test_client.get('/login', as_json=False) + response, login_request = self.app_test_client.login('username', 'wrong_password', session=first_request.getSession()) response_str = yield response self.assertEqual(401, login_request.responseCode) self.assertIn('Invalid credentials', login_request.written) diff --git a/service/test/integration/test_users_count.py b/service/test/integration/test_users_count.py index a03adacf..a9813b2c 100644 --- a/service/test/integration/test_users_count.py +++ b/service/test/integration/test_users_count.py @@ -31,7 +31,8 @@ class UsersResourceTest(MultiUserSoledadTestBase): @defer.inlineCallbacks def test_online_users_count_uses_leap_auth_privileges(self): - response, login_request = yield self.app_test_client.login() + response, first_request = yield self.app_test_client.get('/', as_json=False) + response, login_request = yield self.app_test_client.login(session=first_request.getSession()) yield response yield self.wait_for_session_user_id_to_finish() @@ -40,7 +41,7 @@ class UsersResourceTest(MultiUserSoledadTestBase): response, request = self.app_test_client.get( "/users", json.dumps({'csrftoken': [login_request.getCookie('XSRF-TOKEN')]}), - from_request=login_request, + session=login_request.getSession(), as_json=False) yield response diff --git a/service/test/support/integration/app_test_client.py b/service/test/support/integration/app_test_client.py index ee5a1df2..9ab74261 100644 --- a/service/test/support/integration/app_test_client.py +++ b/service/test/support/integration/app_test_client.py @@ -49,6 +49,7 @@ from pixelated.adapter.search import SearchEngine from pixelated.adapter.services.draft_service import DraftService from pixelated.adapter.services.mail_service import MailService from pixelated.resources.root_resource import RootResource +from pixelated.resources.session import IPixelatedSession from test.support.integration.model import MailBuilder from test.support.test_helper import request_mock from test.support.integration.model import ResponseMail @@ -278,17 +279,21 @@ class AppTestClient(object): request.args = get_args return self._render(request, as_json) - def post(self, path, body='', headers=None, ajax=True, csrf='token'): + def post(self, path, body='', headers=None, ajax=True, csrf='token', session=None): headers = headers or {'Content-Type': 'application/json'} request = request_mock(path=path, method="POST", body=body, headers=headers, ajax=ajax, csrf=csrf) + if session: + request.session = session return self._render(request) def put(self, path, body, ajax=True, csrf='token'): request = request_mock(path=path, method="PUT", body=body, headers={'Content-Type': ['application/json']}, ajax=ajax, csrf=csrf) return self._render(request) - def delete(self, path, body="", ajax=True, csrf='token'): + def delete(self, path, body="", ajax=True, csrf='token', session=None): request = request_mock(path=path, body=body, headers={'Content-Type': ['application/json']}, method="DELETE", ajax=ajax, csrf=csrf) + if session: + request.session = session return self._render(request) @defer.inlineCallbacks @@ -387,12 +392,14 @@ class AppTestClient(object): return res # TODO: remove - def delete_mail(self, mail_ident, csrf='token'): - res, req = self.delete("/mail/%s" % mail_ident, csrf=csrf) + def delete_mail(self, mail_ident, session): + csrf = IPixelatedSession(session).get_csrf_token() + res, req = self.delete("/mail/%s" % mail_ident, csrf=csrf, session=session) return res - def delete_mails(self, idents): - res, req = self.post("/mails/delete", json.dumps({'idents': idents})) + def delete_mails(self, idents, session): + csrf = IPixelatedSession(session).get_csrf_token() + res, req = self.post("/mails/delete", json.dumps({'idents': idents}), csrf=csrf, session=session) return res def mark_many_as_unread(self, idents): diff --git a/service/test/support/integration/multi_user_client.py b/service/test/support/integration/multi_user_client.py index fe8595fb..4b9b2864 100644 --- a/service/test/support/integration/multi_user_client.py +++ b/service/test/support/integration/multi_user_client.py @@ -58,44 +58,41 @@ class MultiUserClient(AppTestClient): else: when(Authenticator)._bonafide_auth(username, password).thenRaise(SRPAuthError) - def login(self, username='username', password='password', from_request=None): - session = Authentication(username, 'some_user_token', 'some_user_uuid', 'session_id', {'is_admin': False}) + def login(self, username='username', password='password', session=None): + auth_session = Authentication(username, 'some_user_token', 'some_user_uuid', 'session_id', {'is_admin': False}) leap_session = self._test_account.leap_session - leap_session.user_auth = session + leap_session.user_auth = auth_session config = mock() config.leap_home = 'some_folder' leap_session.config = config leap_session.fresh_account = False self.leap_session = leap_session self.services = self._test_account.services - self.user_auth = session + self.user_auth = auth_session self._mock_bonafide_auth(username, password) - when(LeapSessionFactory).create(username, password, session).thenReturn(leap_session) + when(LeapSessionFactory).create(username, password, auth_session).thenReturn(leap_session) with patch('mockito.invocation.AnswerSelector', AnswerSelector): when(leap_session).initial_sync().thenAnswer(lambda: defer.succeed(None)) when(pixelated.config.services).Services(ANY()).thenReturn(self.services) - session = from_request.getSession() csrftoken = IPixelatedSession(session).get_csrf_token() request = request_mock(path='/login', method="POST", body={'username': username, 'password': password, 'csrftoken': csrftoken}, ajax=False) request.session = session return self._render(request, as_json=False) - def get(self, path, get_args='', as_json=True, from_request=None): + def get(self, path, get_args='', as_json=True, session=None): request = request_mock(path) request.args = get_args - if from_request: - session = from_request.getSession() + if session: request.session = session return self._render(request, as_json) - def post(self, path, body='', headers=None, ajax=True, csrf='token', as_json=True, from_request=None): + def post(self, path, body='', headers=None, ajax=True, csrf='token', as_json=True, session=None): headers = headers or {'Content-Type': 'application/json'} request = request_mock(path=path, method="POST", body=body, headers=headers, ajax=ajax, csrf=csrf) - if from_request: - session = from_request.getSession() + if session: request.session = session return self._render(request, as_json) -- cgit v1.2.3 From 20b1922794d3179b32dd930706ec5693a3562464 Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Thu, 1 Dec 2016 18:08:12 +0100 Subject: fix csrf in drafts tests --- service/test/integration/test_drafts.py | 30 ++++++++++++++++------ .../test/support/integration/app_test_client.py | 9 ++++--- 2 files changed, 28 insertions(+), 11 deletions(-) (limited to 'service/test') diff --git a/service/test/integration/test_drafts.py b/service/test/integration/test_drafts.py index 657cfab1..a9c7b3f7 100644 --- a/service/test/integration/test_drafts.py +++ b/service/test/integration/test_drafts.py @@ -17,6 +17,7 @@ from test.support.integration import SoledadTestBase, MailBuilder from mockito import unstub, when, any from twisted.internet import defer +from pixelated.resources import IPixelatedSession class DraftsTest(SoledadTestBase): @@ -26,17 +27,20 @@ class DraftsTest(SoledadTestBase): @defer.inlineCallbacks def test_post_sends_mail_and_deletes_previous_draft_if_it_exists(self): + response, first_request = yield self.app_test_client.get('/', as_json=False) + session = first_request.getSession() + # act as if sending the mail by SMTP succeeded sendmail_deferred = defer.Deferred() when(self.app_test_client.mail_sender).sendmail(any()).thenReturn(sendmail_deferred) # creates one draft first_draft = MailBuilder().with_subject('First draft').build_json() - first_draft_ident = (yield self.app_test_client.put_mail(first_draft)[0])['ident'] + first_draft_ident = (yield self.app_test_client.put_mail(first_draft, session=session)[0])['ident'] # sends an updated version of the draft second_draft = MailBuilder().with_subject('Second draft').with_ident(first_draft_ident).build_json() - deferred_res = self.post_mail(second_draft) + deferred_res = self.post_mail(second_draft, session) sendmail_deferred.callback(None) # SMTP succeeded @@ -54,12 +58,15 @@ class DraftsTest(SoledadTestBase): @defer.inlineCallbacks def test_post_sends_mail_even_when_draft_does_not_exist(self): + response, first_request = yield self.app_test_client.get('/', as_json=False) + session = first_request.getSession() + # act as if sending the mail by SMTP succeeded sendmail_deferred = defer.Deferred() when(self.app_test_client.mail_sender).sendmail(any()).thenReturn(sendmail_deferred) first_draft = MailBuilder().with_subject('First draft').build_json() - res = self.post_mail(first_draft) + res = self.post_mail(first_draft, session) sendmail_deferred.callback(True) yield res @@ -70,25 +77,32 @@ class DraftsTest(SoledadTestBase): self.assertEquals('First draft', sent_mails[0].subject) self.assertEquals(0, len(drafts)) - def post_mail(self, data): - deferred_res, req = self.app_test_client.post('/mails', data) + def post_mail(self, data, session): + csrf = IPixelatedSession(session).get_csrf_token() + deferred_res, req = self.app_test_client.post('/mails', data, csrf=csrf, session=session) return deferred_res @defer.inlineCallbacks def test_put_creates_a_draft_if_it_does_not_exist(self): + response, first_request = yield self.app_test_client.get('/', as_json=False) + session = first_request.getSession() + mail = MailBuilder().with_subject('A new draft').build_json() - yield self.app_test_client.put_mail(mail)[0] + yield self.app_test_client.put_mail(mail, session=session)[0] mails = yield self.app_test_client.get_mails_by_tag('drafts') self.assertEquals('A new draft', mails[0].subject) @defer.inlineCallbacks def test_put_updates_draft_if_it_already_exists(self): + response, first_request = yield self.app_test_client.get('/', as_json=False) + session = first_request.getSession() + draft = MailBuilder().with_subject('First draft').build_json() - draft_ident = (yield self.app_test_client.put_mail(draft)[0])['ident'] + draft_ident = (yield self.app_test_client.put_mail(draft, session=session)[0])['ident'] updated_draft = MailBuilder().with_subject('First draft edited').with_ident(draft_ident).build_json() - yield self.app_test_client.put_mail(updated_draft)[0] + yield self.app_test_client.put_mail(updated_draft, session=session)[0] drafts = yield self.app_test_client.get_mails_by_tag('drafts') diff --git a/service/test/support/integration/app_test_client.py b/service/test/support/integration/app_test_client.py index 9ab74261..f04f67fd 100644 --- a/service/test/support/integration/app_test_client.py +++ b/service/test/support/integration/app_test_client.py @@ -286,8 +286,10 @@ class AppTestClient(object): request.session = session return self._render(request) - def put(self, path, body, ajax=True, csrf='token'): + def put(self, path, body, ajax=True, csrf='token', session=None): request = request_mock(path=path, method="PUT", body=body, headers={'Content-Type': ['application/json']}, ajax=ajax, csrf=csrf) + if session: + request.session = session return self._render(request) def delete(self, path, body="", ajax=True, csrf='token', session=None): @@ -375,8 +377,9 @@ class AppTestClient(object): res = yield deferred_result defer.returnValue((res, req)) - def put_mail(self, data): - res, req = self.put('/mails', data) + def put_mail(self, data, session): + csrf = IPixelatedSession(session).get_csrf_token() + res, req = self.put('/mails', data, csrf=csrf, session=session) return res, req def post_tags(self, mail_ident, tags_json): -- cgit v1.2.3 From 05551265c641ac51d897a49e35f390fde7bc4d8c Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Thu, 1 Dec 2016 18:20:38 +0100 Subject: fix csrf in mark as read/unread tests --- .../test/integration/test_mark_as_read_unread.py | 29 ++++++++++++++++------ .../test/support/integration/app_test_client.py | 10 +++++--- 2 files changed, 27 insertions(+), 12 deletions(-) (limited to 'service/test') diff --git a/service/test/integration/test_mark_as_read_unread.py b/service/test/integration/test_mark_as_read_unread.py index 18c3ddc2..c01deefc 100644 --- a/service/test/integration/test_mark_as_read_unread.py +++ b/service/test/integration/test_mark_as_read_unread.py @@ -30,32 +30,40 @@ class MarkAsReadUnreadTest(SoledadTestBase): mails = yield self.app_test_client.get_mails_by_tag('inbox') self.assertNotIn('read', mails[0].status) - yield self.app_test_client.mark_many_as_read([mail.ident]) + response, first_request = yield self.app_test_client.get('/', as_json=False) + session = first_request.getSession() + yield self.app_test_client.mark_many_as_read([mail.ident], session) mails = yield self.app_test_client.get_mails_by_tag('inbox') self.assertIn('read', mails[0].status) @defer.inlineCallbacks def test_mark_single_as_unread(self): + response, first_request = yield self.app_test_client.get('/', as_json=False) + session = first_request.getSession() + input_mail = MailBuilder().build_input_mail() mail = yield self.app_test_client.add_mail_to_inbox(input_mail) - yield self.app_test_client.mark_many_as_read([mail.ident]) + yield self.app_test_client.mark_many_as_read([mail.ident], session) - yield self.app_test_client.mark_many_as_unread([mail.ident]) + yield self.app_test_client.mark_many_as_unread([mail.ident], session) result = (yield self.app_test_client.get_mails_by_tag('inbox'))[0] self.assertNotIn('read', result.status) @defer.inlineCallbacks def test_mark_many_mails_as_unread(self): + response, first_request = yield self.app_test_client.get('/', as_json=False) + session = first_request.getSession() + input_mail = MailBuilder().with_status([Status.SEEN]).build_input_mail() input_mail2 = MailBuilder().with_status([Status.SEEN]).build_input_mail() mail1 = yield self.app_test_client.add_mail_to_inbox(input_mail) mail2 = yield self.app_test_client.add_mail_to_inbox(input_mail2) - yield self.app_test_client.mark_many_as_read([mail1.ident, mail2.ident]) + yield self.app_test_client.mark_many_as_read([mail1.ident, mail2.ident], session) - yield self.app_test_client.mark_many_as_unread([mail1.ident, mail2.ident]) + yield self.app_test_client.mark_many_as_unread([mail1.ident, mail2.ident], session) mails = yield self.app_test_client.get_mails_by_tag('inbox') @@ -75,7 +83,9 @@ class MarkAsReadUnreadTest(SoledadTestBase): self.assertNotIn('read', mails[0].status) self.assertNotIn('read', mails[1].status) - yield self.app_test_client.mark_many_as_read([mails[0].ident, mails[1].ident]) + response, first_request = yield self.app_test_client.get('/', as_json=False) + session = first_request.getSession() + yield self.app_test_client.mark_many_as_read([mails[0].ident, mails[1].ident], session) mails = yield self.app_test_client.get_mails_by_tag('inbox') @@ -84,12 +94,15 @@ class MarkAsReadUnreadTest(SoledadTestBase): @defer.inlineCallbacks def test_mark_mixed_status_as_read(self): + response, first_request = yield self.app_test_client.get('/', as_json=False) + session = first_request.getSession() + input_mail = MailBuilder().with_subject('first').build_input_mail() input_mail2 = MailBuilder().with_subject('second').build_input_mail() yield self.app_test_client.add_mail_to_inbox(input_mail) mail2 = yield self.app_test_client.add_mail_to_inbox(input_mail2) - yield self.app_test_client.mark_many_as_read([mail2.ident]) + yield self.app_test_client.mark_many_as_read([mail2.ident], session) mails = yield self.app_test_client.get_mails_by_tag('inbox') @@ -98,7 +111,7 @@ class MarkAsReadUnreadTest(SoledadTestBase): self.assertEquals(1, len(unread_mails)) self.assertEquals(1, len(read_mails)) - yield self.app_test_client.mark_many_as_read([mails[0].ident, mails[1].ident]) + yield self.app_test_client.mark_many_as_read([mails[0].ident, mails[1].ident], session) mails = yield self.app_test_client.get_mails_by_tag('inbox') diff --git a/service/test/support/integration/app_test_client.py b/service/test/support/integration/app_test_client.py index f04f67fd..e5d42505 100644 --- a/service/test/support/integration/app_test_client.py +++ b/service/test/support/integration/app_test_client.py @@ -405,12 +405,14 @@ class AppTestClient(object): res, req = self.post("/mails/delete", json.dumps({'idents': idents}), csrf=csrf, session=session) return res - def mark_many_as_unread(self, idents): - res, req = self.post('/mails/unread', json.dumps({'idents': idents})) + def mark_many_as_unread(self, idents, session): + csrf = IPixelatedSession(session).get_csrf_token() + res, req = self.post('/mails/unread', json.dumps({'idents': idents}), csrf=csrf, session=session) return res - def mark_many_as_read(self, idents): - res, req = self.post('/mails/read', json.dumps({'idents': idents})) + def mark_many_as_read(self, idents, session): + csrf = IPixelatedSession(session).get_csrf_token() + res, req = self.post('/mails/read', json.dumps({'idents': idents}), csrf=csrf, session=session) return res def get_contacts(self, query): -- cgit v1.2.3 From 082d6a133a892226e6436aab26dd61f759cad30e Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Thu, 1 Dec 2016 18:25:11 +0100 Subject: fix csrf in retrieve attachment test --- service/test/integration/test_retrieve_attachment.py | 4 +++- service/test/support/integration/app_test_client.py | 5 +++-- 2 files changed, 6 insertions(+), 3 deletions(-) (limited to 'service/test') diff --git a/service/test/integration/test_retrieve_attachment.py b/service/test/integration/test_retrieve_attachment.py index b46d40d5..ac6e52e7 100644 --- a/service/test/integration/test_retrieve_attachment.py +++ b/service/test/integration/test_retrieve_attachment.py @@ -86,7 +86,9 @@ class RetrieveAttachmentTest(SoledadTestBase): datagen, headers = multipart_encode([file]) post_data = "".join(datagen) - _, req = yield self.app_test_client.post_attachment(post_data, headers) + response, first_request = yield self.app_test_client.get('/', as_json=False) + session = first_request.getSession() + _, req = yield self.app_test_client.post_attachment(post_data, headers, session) self.assertEqual(201, req.code) self.assertEqual('/attachment/B5B4ED80AC3B894523D72E375DACAA2FC6606C18EDF680FE95903086C8B5E14A', req.responseHeaders.getRawHeaders('location')[0]) diff --git a/service/test/support/integration/app_test_client.py b/service/test/support/integration/app_test_client.py index e5d42505..0bc2eacb 100644 --- a/service/test/support/integration/app_test_client.py +++ b/service/test/support/integration/app_test_client.py @@ -372,8 +372,9 @@ class AppTestClient(object): defer.returnValue((res, req)) @defer.inlineCallbacks - def post_attachment(self, data, headers): - deferred_result, req = self.post('/attachment', body=data, headers=headers) + def post_attachment(self, data, headers, session): + csrf = IPixelatedSession(session).get_csrf_token() + deferred_result, req = self.post('/attachment', body=data, headers=headers, csrf=csrf, session=session) res = yield deferred_result defer.returnValue((res, req)) -- cgit v1.2.3 From 688a8b42e8ab7c6d4529b6dda66f40eead07ad02 Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Thu, 1 Dec 2016 18:30:25 +0100 Subject: fix csrf in tags tests --- service/test/integration/test_tags.py | 23 ++++++++++++++++------ .../test/support/integration/app_test_client.py | 5 +++-- 2 files changed, 20 insertions(+), 8 deletions(-) (limited to 'service/test') diff --git a/service/test/integration/test_tags.py b/service/test/integration/test_tags.py index 555a7382..d107e320 100644 --- a/service/test/integration/test_tags.py +++ b/service/test/integration/test_tags.py @@ -31,7 +31,9 @@ class TagsTest(SoledadTestBase): input_mail = MailBuilder().with_subject('Mail with tags').build_input_mail() mail = yield self.app_test_client.add_mail_to_inbox(input_mail) - yield self.app_test_client.post_tags(mail.ident, self._tags_json(['IMPORTANT'])) + response, first_request = yield self.app_test_client.get('/', as_json=False) + session = first_request.getSession() + yield self.app_test_client.post_tags(mail.ident, self._tags_json(['IMPORTANT']), session) mails = yield self.app_test_client.get_mails_by_tag('inbox') self.assertEquals({'IMPORTANT'}, set(mails[0].tags)) @@ -41,15 +43,18 @@ class TagsTest(SoledadTestBase): @defer.inlineCallbacks def test_use_old_casing_when_same_tag_with_different_casing_is_posted(self): + response, first_request = yield self.app_test_client.get('/', as_json=False) + session = first_request.getSession() + input_mail = MailBuilder().with_subject('Mail with tags').build_input_mail() mail = yield self.app_test_client.add_mail_to_inbox(input_mail) - yield self.app_test_client.post_tags(mail.ident, self._tags_json(['ImPoRtAnT'])) + yield self.app_test_client.post_tags(mail.ident, self._tags_json(['ImPoRtAnT']), session) mails = yield self.app_test_client.get_mails_by_tag('ImPoRtAnT') self.assertEquals({'ImPoRtAnT'}, set(mails[0].tags)) another_input_mail = MailBuilder().with_subject('Mail with tags').build_input_mail() another_mail = yield self.app_test_client.add_mail_to_inbox(another_input_mail) - yield self.app_test_client.post_tags(another_mail.ident, self._tags_json(['IMPORTANT'])) + yield self.app_test_client.post_tags(another_mail.ident, self._tags_json(['IMPORTANT']), session) mails = yield self.app_test_client.get_mails_by_tag('IMPORTANT') self.assertEquals(0, len(mails)) mails = yield self.app_test_client.get_mails_by_tag('ImPoRtAnT') @@ -62,7 +67,9 @@ class TagsTest(SoledadTestBase): input_mail = MailBuilder().with_subject('Mail with tags').build_input_mail() mail = yield self.app_test_client.add_mail_to_inbox(input_mail) - yield self.app_test_client.post_tags(mail.ident, self._tags_json(['ImPoRtAnT'])) + response, first_request = yield self.app_test_client.get('/', as_json=False) + session = first_request.getSession() + yield self.app_test_client.post_tags(mail.ident, self._tags_json(['ImPoRtAnT']), session) mails = yield self.app_test_client.get_mails_by_tag('important') self.assertEquals(0, len(mails)) @@ -78,7 +85,9 @@ class TagsTest(SoledadTestBase): input_mail = MailBuilder().with_subject('Mail with tags').build_input_mail() mail = yield self.app_test_client.add_mail_to_inbox(input_mail) - yield self.app_test_client.post_tags(mail.ident, self._tags_json(['tag1', ' '])) + response, first_request = yield self.app_test_client.get('/', as_json=False) + session = first_request.getSession() + yield self.app_test_client.post_tags(mail.ident, self._tags_json(['tag1', ' ']), session) mail = yield self.app_test_client.get_mail(mail.ident) @@ -89,8 +98,10 @@ class TagsTest(SoledadTestBase): input_mail = MailBuilder().with_subject('Mail with tags').build_input_mail() mail = yield self.app_test_client.add_mail_to_inbox(input_mail) + response, first_request = yield self.app_test_client.get('/', as_json=False) + session = first_request.getSession() for tag in SPECIAL_TAGS: - response = yield self.app_test_client.post_tags(mail.ident, self._tags_json([tag.name.upper()])) + response = yield self.app_test_client.post_tags(mail.ident, self._tags_json([tag.name.upper()]), session) self.assertEquals("None of the following words can be used as tags: %s" % tag.name, response) mail = yield self.app_test_client.mail_store.get_mail(mail.ident) diff --git a/service/test/support/integration/app_test_client.py b/service/test/support/integration/app_test_client.py index 0bc2eacb..4e3758c5 100644 --- a/service/test/support/integration/app_test_client.py +++ b/service/test/support/integration/app_test_client.py @@ -383,8 +383,9 @@ class AppTestClient(object): res, req = self.put('/mails', data, csrf=csrf, session=session) return res, req - def post_tags(self, mail_ident, tags_json): - res, req = self.post("/mail/%s/tags" % mail_ident, tags_json) + def post_tags(self, mail_ident, tags_json, session): + csrf = IPixelatedSession(session).get_csrf_token() + res, req = self.post("/mail/%s/tags" % mail_ident, tags_json, csrf=csrf, session=session) return res def get_tags(self, **kwargs): -- cgit v1.2.3 From b14833fbb56bcd5bff0750c16fd9214009b955be Mon Sep 17 00:00:00 2001 From: Zara Gebru Date: Fri, 2 Dec 2016 15:25:23 +0100 Subject: [refactor] move app dir into public dir --- service/test/integration/test_static_files.py | 27 ++++++++++++++++++++++ .../test/support/integration/app_test_client.py | 5 ++-- .../test/support/integration/multi_user_client.py | 4 ++-- service/test/unit/resources/test_auth.py | 6 +++-- service/test/unit/resources/test_root_resource.py | 6 ++--- 5 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 service/test/integration/test_static_files.py (limited to 'service/test') diff --git a/service/test/integration/test_static_files.py b/service/test/integration/test_static_files.py new file mode 100644 index 00000000..e3fa8af5 --- /dev/null +++ b/service/test/integration/test_static_files.py @@ -0,0 +1,27 @@ +# +# 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 . + + +from twisted.internet.defer import inlineCallbacks +from test.support.integration import SoledadTestBase + + +class StaticFilesTest(SoledadTestBase): + + @inlineCallbacks + def test_should_find_static_file(self): + _, request = yield self.app_test_client.get('/static/js/main.js', as_json=False, ajax=False) + self.assertEqual(200, request.responseCode) diff --git a/service/test/support/integration/app_test_client.py b/service/test/support/integration/app_test_client.py index 4e3758c5..c611fbd0 100644 --- a/service/test/support/integration/app_test_client.py +++ b/service/test/support/integration/app_test_client.py @@ -38,7 +38,7 @@ from leap.soledad.client import Soledad from leap.bitmask.mail.adaptors.soledad import SoledadMailAdaptor from pixelated.adapter.mailstore.leap_attachment_store import LeapAttachmentStore from pixelated.adapter.services.feedback_service import FeedbackService -from pixelated.application import UserAgentMode, set_up_protected_resources +from pixelated.application import UserAgentMode, set_up_protected_resources, get_static_folder, get_templates_folder from pixelated.config.sessions import LeapSession from pixelated.config.services import Services, ServicesFactory, SingleUserServicesFactory from pixelated.config.site import PixelatedSite @@ -218,10 +218,11 @@ class AppTestClient(object): services = self._test_account.services self.service_factory.add_session('someuserid', services) - self.resource = RootResource(self.service_factory) + self.resource = RootResource(self.service_factory, get_templates_folder(), get_static_folder()) provider = mock() self.resource.initialize(provider) else: + # TODO: write test for me (= self.service_factory = StubServicesFactory(self.accounts, mode) provider = mock() bonafide_checker = StubAuthenticator(provider) diff --git a/service/test/support/integration/multi_user_client.py b/service/test/support/integration/multi_user_client.py index 4b9b2864..d7ab77a0 100644 --- a/service/test/support/integration/multi_user_client.py +++ b/service/test/support/integration/multi_user_client.py @@ -19,7 +19,7 @@ from mockito import mock, when, any as ANY from pixelated.authentication import Authenticator, Authentication from twisted.internet import defer -from pixelated.application import UserAgentMode, set_up_protected_resources +from pixelated.application import UserAgentMode, set_up_protected_resources, get_static_folder, get_templates_folder from pixelated.config.services import ServicesFactory from pixelated.config.sessions import LeapSessionFactory @@ -46,7 +46,7 @@ class MultiUserClient(AppTestClient): self.service_factory = ServicesFactory(UserAgentMode(is_single_user=False)) - root_resource = RootResource(self.service_factory) + root_resource = RootResource(self.service_factory, get_templates_folder(), get_static_folder()) leap_provider = mock() self.credentials_checker = StubSRPChecker(leap_provider) self.resource = set_up_protected_resources(root_resource, leap_provider, self.service_factory) diff --git a/service/test/unit/resources/test_auth.py b/service/test/unit/resources/test_auth.py index f4012b1b..7112ed96 100644 --- a/service/test/unit/resources/test_auth.py +++ b/service/test/unit/resources/test_auth.py @@ -1,4 +1,6 @@ from mockito import mock, when, any as ANY + +from pixelated.application import get_templates_folder, get_static_folder from pixelated.resources.auth import SessionChecker, PixelatedRealm, PixelatedAuthSessionWrapper from pixelated.resources.login_resource import LoginResource from pixelated.resources.root_resource import RootResource @@ -40,8 +42,8 @@ class TestPixelatedAuthSessionWrapper(unittest.TestCase): session_checker = SessionChecker(services_factory) self.portal = Portal(self.realm_mock, [session_checker, AllowAnonymousAccess()]) self.user_uuid_mock = mock() - self.root_resource = RootResource(services_factory) - self.anonymous_resource = RootResource(services_factory, public=True) + self.root_resource = RootResource(services_factory, get_templates_folder(), get_static_folder()) + self.anonymous_resource = RootResource(services_factory, get_templates_folder(), get_static_folder(), public=True) self.session_wrapper = PixelatedAuthSessionWrapper(self.portal, self.root_resource, self.anonymous_resource) self.request = DummyRequest([]) diff --git a/service/test/unit/resources/test_root_resource.py b/service/test/unit/resources/test_root_resource.py index 9d738a83..e72efe59 100644 --- a/service/test/unit/resources/test_root_resource.py +++ b/service/test/unit/resources/test_root_resource.py @@ -5,7 +5,7 @@ from mock import MagicMock, patch from mockito import mock, when, any as ANY import pixelated -from pixelated.application import UserAgentMode +from pixelated.application import UserAgentMode, get_templates_folder, get_static_folder from pixelated.resources import IPixelatedSession, UnAuthorizedResource from pixelated.resources.features_resource import FeaturesResource from pixelated.resources.login_resource import LoginResource @@ -22,7 +22,7 @@ from pixelated.resources.root_resource import InboxResource, RootResource, MODE_ class TestPublicRootResource(unittest.TestCase): def setUp(self): - self.public_root_resource = RootResource(mock(), public=True) + self.public_root_resource = RootResource(mock(), get_templates_folder(), get_static_folder(), public=True) self.web = DummySite(self.public_root_resource) @patch('pixelated.resources.mails_resource.events.register') @@ -105,7 +105,7 @@ class TestRootResource(unittest.TestCase): when(self.services_factory).services(ANY()).thenReturn(self.services) self.mail_service.account_email = self.MAIL_ADDRESS - self.root_resource = RootResource(self.services_factory) + self.root_resource = RootResource(self.services_factory, get_templates_folder(), get_static_folder()) self.web = DummySite(self.root_resource) @patch('pixelated.resources.mails_resource.events.register') -- cgit v1.2.3 From a0de084e04f02a5f09d5a14b86ece156f4f6df5f Mon Sep 17 00:00:00 2001 From: Zara Gebru Date: Fri, 2 Dec 2016 17:55:02 +0100 Subject: [refactor] use static url instead of assets url --- service/test/unit/resources/test_root_resource.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'service/test') diff --git a/service/test/unit/resources/test_root_resource.py b/service/test/unit/resources/test_root_resource.py index e72efe59..d42a4b38 100644 --- a/service/test/unit/resources/test_root_resource.py +++ b/service/test/unit/resources/test_root_resource.py @@ -300,7 +300,7 @@ class TestRootResource(unittest.TestCase): def test_assets_should_be_publicly_available(self, *mocks): self.root_resource.initialize(provider=mock(), authenticator=mock()) - request = DummyRequest(['assets', 'dummy.json']) + request = DummyRequest(['static', 'dummy.json']) request.addCookie = MagicMock(return_value='stubbed') d = self.web.get(request) -- cgit v1.2.3 From 391cc55537a97ec8b2b55662db9c63f86ab885ef Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Mon, 5 Dec 2016 10:32:12 +0100 Subject: get templates from pkg_resources --- service/test/integration/test_contacts.py | 1 - service/test/unit/test_welcome_mail.py | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) (limited to 'service/test') diff --git a/service/test/integration/test_contacts.py b/service/test/integration/test_contacts.py index 946818fd..a890466f 100644 --- a/service/test/integration/test_contacts.py +++ b/service/test/integration/test_contacts.py @@ -16,7 +16,6 @@ from test.support.integration import SoledadTestBase, MailBuilder from twisted.internet import defer import json -import pkg_resources class ContactsTest(SoledadTestBase): diff --git a/service/test/unit/test_welcome_mail.py b/service/test/unit/test_welcome_mail.py index 7eb65903..42e21e8d 100644 --- a/service/test/unit/test_welcome_mail.py +++ b/service/test/unit/test_welcome_mail.py @@ -41,8 +41,7 @@ class TestWelcomeMail(unittest.TestCase): with open(os.path.join(current_path, '..', '..', - 'pixelated', - 'assets', + 'templates', 'welcome.mail.pt-BR')) as mail_template_file: mail_template = message_from_file(mail_template_file) -- cgit v1.2.3 From ed69fa3b7bf0b543b6c5d3f41504965a1b085808 Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Mon, 5 Dec 2016 10:39:10 +0100 Subject: use static instead of {startup,public}-assets --- service/test/unit/resources/test_login_resource.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'service/test') diff --git a/service/test/unit/resources/test_login_resource.py b/service/test/unit/resources/test_login_resource.py index 696b0c46..fcf76f5a 100644 --- a/service/test/unit/resources/test_login_resource.py +++ b/service/test/unit/resources/test_login_resource.py @@ -235,7 +235,7 @@ class TestLoginPOST(unittest.TestCase): def assert_interstitial_in_response(_): mock_authenticate.assert_called_once_with(self.username, self.password) - interstitial_js_in_template = '' + interstitial_js_in_template = '' self.assertIn(interstitial_js_in_template, self.request.written[0]) d.addCallback(assert_interstitial_in_response) -- cgit v1.2.3 From ae871e84f6de213f01299f2754fb2e68d4a3afe2 Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Mon, 5 Dec 2016 10:56:24 +0100 Subject: remove templates folder from root resource parameters --- service/test/support/integration/app_test_client.py | 4 ++-- service/test/support/integration/multi_user_client.py | 4 ++-- service/test/unit/resources/test_auth.py | 6 +++--- service/test/unit/resources/test_root_resource.py | 6 +++--- 4 files changed, 10 insertions(+), 10 deletions(-) (limited to 'service/test') diff --git a/service/test/support/integration/app_test_client.py b/service/test/support/integration/app_test_client.py index c611fbd0..1421b96b 100644 --- a/service/test/support/integration/app_test_client.py +++ b/service/test/support/integration/app_test_client.py @@ -38,7 +38,7 @@ from leap.soledad.client import Soledad from leap.bitmask.mail.adaptors.soledad import SoledadMailAdaptor from pixelated.adapter.mailstore.leap_attachment_store import LeapAttachmentStore from pixelated.adapter.services.feedback_service import FeedbackService -from pixelated.application import UserAgentMode, set_up_protected_resources, get_static_folder, get_templates_folder +from pixelated.application import UserAgentMode, set_up_protected_resources, get_static_folder from pixelated.config.sessions import LeapSession from pixelated.config.services import Services, ServicesFactory, SingleUserServicesFactory from pixelated.config.site import PixelatedSite @@ -218,7 +218,7 @@ class AppTestClient(object): services = self._test_account.services self.service_factory.add_session('someuserid', services) - self.resource = RootResource(self.service_factory, get_templates_folder(), get_static_folder()) + self.resource = RootResource(self.service_factory, get_static_folder()) provider = mock() self.resource.initialize(provider) else: diff --git a/service/test/support/integration/multi_user_client.py b/service/test/support/integration/multi_user_client.py index d7ab77a0..de272e16 100644 --- a/service/test/support/integration/multi_user_client.py +++ b/service/test/support/integration/multi_user_client.py @@ -19,7 +19,7 @@ from mockito import mock, when, any as ANY from pixelated.authentication import Authenticator, Authentication from twisted.internet import defer -from pixelated.application import UserAgentMode, set_up_protected_resources, get_static_folder, get_templates_folder +from pixelated.application import UserAgentMode, set_up_protected_resources, get_static_folder from pixelated.config.services import ServicesFactory from pixelated.config.sessions import LeapSessionFactory @@ -46,7 +46,7 @@ class MultiUserClient(AppTestClient): self.service_factory = ServicesFactory(UserAgentMode(is_single_user=False)) - root_resource = RootResource(self.service_factory, get_templates_folder(), get_static_folder()) + root_resource = RootResource(self.service_factory, get_static_folder()) leap_provider = mock() self.credentials_checker = StubSRPChecker(leap_provider) self.resource = set_up_protected_resources(root_resource, leap_provider, self.service_factory) diff --git a/service/test/unit/resources/test_auth.py b/service/test/unit/resources/test_auth.py index 7112ed96..b43ab15e 100644 --- a/service/test/unit/resources/test_auth.py +++ b/service/test/unit/resources/test_auth.py @@ -1,6 +1,6 @@ from mockito import mock, when, any as ANY -from pixelated.application import get_templates_folder, get_static_folder +from pixelated.application import get_static_folder from pixelated.resources.auth import SessionChecker, PixelatedRealm, PixelatedAuthSessionWrapper from pixelated.resources.login_resource import LoginResource from pixelated.resources.root_resource import RootResource @@ -42,8 +42,8 @@ class TestPixelatedAuthSessionWrapper(unittest.TestCase): session_checker = SessionChecker(services_factory) self.portal = Portal(self.realm_mock, [session_checker, AllowAnonymousAccess()]) self.user_uuid_mock = mock() - self.root_resource = RootResource(services_factory, get_templates_folder(), get_static_folder()) - self.anonymous_resource = RootResource(services_factory, get_templates_folder(), get_static_folder(), public=True) + self.root_resource = RootResource(services_factory, get_static_folder()) + self.anonymous_resource = RootResource(services_factory, get_static_folder(), public=True) self.session_wrapper = PixelatedAuthSessionWrapper(self.portal, self.root_resource, self.anonymous_resource) self.request = DummyRequest([]) diff --git a/service/test/unit/resources/test_root_resource.py b/service/test/unit/resources/test_root_resource.py index d42a4b38..4c9af052 100644 --- a/service/test/unit/resources/test_root_resource.py +++ b/service/test/unit/resources/test_root_resource.py @@ -5,7 +5,7 @@ from mock import MagicMock, patch from mockito import mock, when, any as ANY import pixelated -from pixelated.application import UserAgentMode, get_templates_folder, get_static_folder +from pixelated.application import UserAgentMode, get_static_folder from pixelated.resources import IPixelatedSession, UnAuthorizedResource from pixelated.resources.features_resource import FeaturesResource from pixelated.resources.login_resource import LoginResource @@ -22,7 +22,7 @@ from pixelated.resources.root_resource import InboxResource, RootResource, MODE_ class TestPublicRootResource(unittest.TestCase): def setUp(self): - self.public_root_resource = RootResource(mock(), get_templates_folder(), get_static_folder(), public=True) + self.public_root_resource = RootResource(mock(), get_static_folder(), public=True) self.web = DummySite(self.public_root_resource) @patch('pixelated.resources.mails_resource.events.register') @@ -105,7 +105,7 @@ class TestRootResource(unittest.TestCase): when(self.services_factory).services(ANY()).thenReturn(self.services) self.mail_service.account_email = self.MAIL_ADDRESS - self.root_resource = RootResource(self.services_factory, get_templates_folder(), get_static_folder()) + self.root_resource = RootResource(self.services_factory, get_static_folder()) self.web = DummySite(self.root_resource) @patch('pixelated.resources.mails_resource.events.register') -- cgit v1.2.3 From 784eab38fb5794ef1bac41ae3aeb58520ff48590 Mon Sep 17 00:00:00 2001 From: Zara Gebru Date: Tue, 6 Dec 2016 11:17:11 +0100 Subject: fix parameter for root resource --- service/test/support/integration/app_test_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'service/test') diff --git a/service/test/support/integration/app_test_client.py b/service/test/support/integration/app_test_client.py index 1421b96b..892b73af 100644 --- a/service/test/support/integration/app_test_client.py +++ b/service/test/support/integration/app_test_client.py @@ -228,7 +228,7 @@ class AppTestClient(object): bonafide_checker = StubAuthenticator(provider) bonafide_checker.add_user('username', 'password') - self.resource = set_up_protected_resources(RootResource(self.service_factory), provider, self.service_factory, authenticator=bonafide_checker) + self.resource = set_up_protected_resources(RootResource(self.service_factory, get_static_folder()), provider, self.service_factory, authenticator=bonafide_checker) @defer.inlineCallbacks def create_user(self, account_name): -- cgit v1.2.3 From 206423b83b910308bd9c314af03cf82e9a821974 Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Tue, 6 Dec 2016 11:55:52 +0100 Subject: remove some TODO's --- service/test/support/integration/app_test_client.py | 14 -------------- service/test/unit/resources/test_root_resource.py | 2 -- 2 files changed, 16 deletions(-) (limited to 'service/test') diff --git a/service/test/support/integration/app_test_client.py b/service/test/support/integration/app_test_client.py index 892b73af..fa695708 100644 --- a/service/test/support/integration/app_test_client.py +++ b/service/test/support/integration/app_test_client.py @@ -222,7 +222,6 @@ class AppTestClient(object): provider = mock() self.resource.initialize(provider) else: - # TODO: write test for me (= self.service_factory = StubServicesFactory(self.accounts, mode) provider = mock() bonafide_checker = StubAuthenticator(provider) @@ -307,7 +306,6 @@ class AppTestClient(object): def account_for(self, username): return self.accounts[username] - # TODO: remove def add_mail_to_user_inbox(self, input_mail, username): return self.account_for(username).mail_store.add_mail('INBOX', input_mail.raw) @@ -336,10 +334,6 @@ class AppTestClient(object): mail_sender.sendmail.side_effect = lambda mail: succeed(mail) return mail_sender - # TODO: remove - def _generate_soledad_test_folder_name(self, soledad_test_folder='/tmp/soledad-test/test'): - return os.path.join(soledad_test_folder, str(uuid.uuid4())) - def get_mails_by_tag(self, tag, page=1, window=100): tags = 'tag:%s' % tag return self.search(tags, page, window) @@ -354,13 +348,6 @@ class AppTestClient(object): res = yield res defer.returnValue([ResponseMail(m) for m in res['mails']]) - # TODO: remove - @defer.inlineCallbacks - def get_mails_by_mailbox_name(self, mbox_name): - mail_ids = yield self.mail_store.get_mailbox_mail_ids(mbox_name) - mails = yield self.mail_store.get_mails(mail_ids) - defer.returnValue(mails) - @defer.inlineCallbacks def get_attachment(self, ident, encoding, filename=None, content_type=None, ajax=True, csrf='token'): params = {'encoding': [encoding]} @@ -397,7 +384,6 @@ class AppTestClient(object): res, req = self.get('/mail/%s' % mail_ident) return res - # TODO: remove def delete_mail(self, mail_ident, session): csrf = IPixelatedSession(session).get_csrf_token() res, req = self.delete("/mail/%s" % mail_ident, csrf=csrf, session=session) diff --git a/service/test/unit/resources/test_root_resource.py b/service/test/unit/resources/test_root_resource.py index 4c9af052..1edba98c 100644 --- a/service/test/unit/resources/test_root_resource.py +++ b/service/test/unit/resources/test_root_resource.py @@ -329,7 +329,6 @@ class TestRootResource(unittest.TestCase): request.addCookie = MagicMock(return_value='stubbed') request.prepath = [''] request.path = '/' - # TODO: setup mocked portal resource = self.root_resource.getChildWithDefault(request.prepath[-1], request) self.assertIsInstance(resource, InboxResource) @@ -339,7 +338,6 @@ class TestRootResource(unittest.TestCase): request.addCookie = MagicMock(return_value='stubbed') request.prepath = [''] request.path = '/' - # TODO: setup mocked portal resource = self.root_resource.getChildWithDefault(request.prepath[-1], request) self.assertIsInstance(resource, InboxResource) -- cgit v1.2.3 From eaf2019b6e977d1191e0ee12f694a02bb9612f83 Mon Sep 17 00:00:00 2001 From: Zara Gebru Date: Tue, 6 Dec 2016 15:46:33 +0100 Subject: clean up parameters of authsessionwrapper --- service/test/unit/resources/test_auth.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'service/test') diff --git a/service/test/unit/resources/test_auth.py b/service/test/unit/resources/test_auth.py index b43ab15e..3dc3e29f 100644 --- a/service/test/unit/resources/test_auth.py +++ b/service/test/unit/resources/test_auth.py @@ -45,7 +45,7 @@ class TestPixelatedAuthSessionWrapper(unittest.TestCase): self.root_resource = RootResource(services_factory, get_static_folder()) self.anonymous_resource = RootResource(services_factory, get_static_folder(), public=True) - self.session_wrapper = PixelatedAuthSessionWrapper(self.portal, self.root_resource, self.anonymous_resource) + self.session_wrapper = PixelatedAuthSessionWrapper(self.portal) self.request = DummyRequest([]) self.request.prepath = [''] self.request.path = '/' -- cgit v1.2.3