summaryrefslogtreecommitdiff
path: root/service/test/support/integration
diff options
context:
space:
mode:
authorNavaL <ayoyo@thoughtworks.com>2016-10-26 15:55:29 +0200
committerNavaL <ayoyo@thoughtworks.com>2016-10-28 18:02:25 +0200
commit423ca8f9fb7636b336b24ba28bde5d61538bf5fc (patch)
tree6d8f95fafe4f08b4ca557d52bc45d310fa8c37af /service/test/support/integration
parent3df56a4f3c411c3bde51c88e6e0bf34d5e582119 (diff)
authentication now returns Authentication
leap session creation is only done post-interstitial and that logic is also extracted into its own class #795
Diffstat (limited to 'service/test/support/integration')
-rw-r--r--service/test/support/integration/app_test_client.py31
-rw-r--r--service/test/support/integration/multi_user_client.py13
2 files changed, 39 insertions, 5 deletions
diff --git a/service/test/support/integration/app_test_client.py b/service/test/support/integration/app_test_client.py
index f982407e..93dfd812 100644
--- a/service/test/support/integration/app_test_client.py
+++ b/service/test/support/integration/app_test_client.py
@@ -52,6 +52,7 @@ from pixelated.resources.root_resource import RootResource
from test.support.integration.model import MailBuilder
from test.support.test_helper import request_mock
from test.support.integration.model import ResponseMail
+from pixelated.config.sessions import SessionCache
class AppTestAccount(object):
@@ -143,6 +144,27 @@ class StubSRPChecker(object):
return defer.fail()
+class StubAuthenticator(object):
+ def __init__(self, provider, credentials={}):
+ self._leap_provider = provider
+ self._credentials = credentials.copy()
+
+ def add_user(self, username, password):
+ self._credentials[username] = password
+
+ def _set_leap_session_cache(self, auth):
+ key = SessionCache.session_key(self._leap_provider, 'username')
+ SessionCache.remember_session(key, LeapSession(self._leap_provider, auth, None, None, None, None))
+
+ def authenticate(self, username, password):
+ if self._credentials[username] == password:
+ leap_auth = Authentication(username, uuid.uuid4(), uuid.uuid4(), uuid.uuid4(), {})
+ self._set_leap_session_cache(leap_auth)
+ return defer.succeed(leap_auth)
+ else:
+ return defer.fail()
+
+
class StubServicesFactory(ServicesFactory):
def __init__(self, accounts, mode):
@@ -196,13 +218,16 @@ class AppTestClient(object):
self.service_factory.add_session('someuserid', services)
self.resource = RootResource(self.service_factory)
- self.resource.initialize()
+ provider = mock()
+ self.resource.initialize(provider)
else:
self.service_factory = StubServicesFactory(self.accounts, mode)
provider = mock()
srp_checker = StubSRPChecker(provider)
- srp_checker.add_user('username', 'password')
- self.resource = set_up_protected_resources(RootResource(self.service_factory), provider, self.service_factory, checker=srp_checker)
+ bonafide_checker = StubAuthenticator(provider)
+ bonafide_checker.add_user('username', 'password')
+
+ self.resource = set_up_protected_resources(RootResource(self.service_factory), provider, self.service_factory, checker=srp_checker, authenticator=bonafide_checker)
@defer.inlineCallbacks
def create_user(self, account_name):
diff --git a/service/test/support/integration/multi_user_client.py b/service/test/support/integration/multi_user_client.py
index 0257214f..28316d5b 100644
--- a/service/test/support/integration/multi_user_client.py
+++ b/service/test/support/integration/multi_user_client.py
@@ -13,8 +13,10 @@
#
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
+from leap.bitmask.bonafide._srp import SRPAuthError
from mock import patch
from mockito import mock, when, any as ANY
+from pixelated.authentication import Authenticator
from pixelated.config.leap import Authentication
from twisted.internet import defer
@@ -49,9 +51,14 @@ class MultiUserClient(AppTestClient):
self.credentials_checker = StubSRPChecker(leap_provider)
self.resource = set_up_protected_resources(root_resource, leap_provider, self.service_factory, checker=self.credentials_checker)
- def login(self, username='username', password='password'):
- if(username == 'username' and password == 'password'):
+ def _mock_bonafide_auth(self, username, password):
+ if username == 'username' and password == 'password':
self.credentials_checker.add_user(username, password)
+ when(Authenticator)._bonafide_auth(username, password).thenReturn(self.user_auth)
+ else:
+ when(Authenticator)._bonafide_auth(username, password).thenRaise(SRPAuthError)
+
+ def login(self, username='username', password='password'):
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
@@ -63,6 +70,8 @@ class MultiUserClient(AppTestClient):
self.services = self._test_account.services
self.user_auth = session
+ self._mock_bonafide_auth(username, password)
+
when(LeapSessionFactory).create(username, password, session).thenReturn(leap_session)
with patch('mockito.invocation.AnswerSelector', AnswerSelector):
when(leap_session).initial_sync().thenAnswer(lambda: defer.succeed(None))