Skip to content
This repository has been archived by the owner on Jan 2, 2020. It is now read-only.

Commit

Permalink
multi_user_client now accounts for the login process
Browse files Browse the repository at this point in the history
The login resource was being totally mocked out of the integration
tests, I adapted the test client to touch the actual login code
and fixed the multi_user_client to use the same checker the single
user one was using. With that change we now have tests that cover
the change of authenticating with bonafide
  • Loading branch information
bwagnerr committed Sep 8, 2016
1 parent 7b8d335 commit b777f13
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
9 changes: 5 additions & 4 deletions service/test/support/integration/app_test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
from twisted.internet import reactor, defer
from twisted.internet.defer import succeed
from twisted.web.resource import getChildForRequest
# from twisted.web.server import Site as PixelatedSite
from zope.interface import implementer
from twisted.cred import checkers, credentials
from pixelated.adapter.mailstore.leap_attachment_store import LeapAttachmentStore
Expand Down Expand Up @@ -136,8 +135,11 @@ def add_user(self, username, password):
self._credentials[username] = password

def requestAvatarId(self, credentials):
leap_auth = SRPSession(credentials.username, uuid.uuid4(), uuid.uuid4(), uuid.uuid4(), {})
return defer.succeed(LeapSession(self._leap_provider, leap_auth, None, None, None, None))
if(self._credentials[credentials.username] == credentials.password):
leap_auth = SRPSession(credentials.username, uuid.uuid4(), uuid.uuid4(), uuid.uuid4(), {})
return defer.succeed(LeapSession(self._leap_provider, leap_auth, None, None, None, None))
else:
return defer.fail()


class StubServicesFactory(ServicesFactory):
Expand Down Expand Up @@ -196,7 +198,6 @@ def start_client(self, mode=UserAgentMode(is_single_user=True)):
else:
self.service_factory = StubServicesFactory(self.accounts, mode)
provider = mock()

self.resource = set_up_protected_resources(RootResource(self.service_factory), provider, self.service_factory, checker=StubSRPChecker(provider))

@defer.inlineCallbacks
Expand Down
26 changes: 10 additions & 16 deletions service/test/support/integration/multi_user_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from mockito import mock, when, any as ANY
from twisted.internet import defer

from leap.auth import SRPAuth
from leap.auth import SRPSession

from pixelated.application import UserAgentMode, set_up_protected_resources
from pixelated.config.services import ServicesFactory
Expand All @@ -26,7 +26,7 @@
import pixelated.config.services
from pixelated.resources.root_resource import RootResource
from test.support.integration import AppTestClient
from test.support.integration.app_test_client import AppTestAccount
from test.support.integration.app_test_client import AppTestAccount, StubSRPChecker
import test.support.mockito
from test.support.test_helper import request_mock

Expand All @@ -47,36 +47,30 @@ def start_client(self, mode=UserAgentMode(is_single_user=True)):

root_resource = RootResource(self.service_factory)
leap_provider = mock()
self.resource = set_up_protected_resources(root_resource, leap_provider, self.service_factory)
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'):
self.credentials_checker.add_user(username, password)
session = SRPSession(username, 'some_user_token', 'some_user_uuid', 'session_id', {'is_admin': False})
leap_session = self._test_account.leap_session
user_auth = mock()
user_auth.uuid = 'some_user_uuid'
leap_session.user_auth = user_auth
leap_session.user_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 = user_auth
self.user_auth = session

self._set_leap_srp_auth(username, password, user_auth)
when(LeapSessionFactory).create(username, password, user_auth).thenReturn(leap_session)
when(LeapSessionFactory).create(username, password, session).thenReturn(leap_session)
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})
return self._render(request, as_json=False)

def _set_leap_srp_auth(self, username, password, mock_srp_auth):
auth_dict = {'username': 'password'}
if auth_dict[username] == password:
when(SRPAuth).authenticate(username, password).thenReturn(mock_srp_auth)
else:
when(SRPAuth).authenticate(username, password).thenRaise(SRPAuthenticationError())

def get(self, path, get_args='', as_json=True, from_request=None):
request = request_mock(path)
request.args = get_args
Expand Down

0 comments on commit b777f13

Please sign in to comment.