class MultiUserClient(AppTestClient):

    @defer.inlineCallbacks
    def start_client(self, mode=UserAgentMode(is_single_user=True)):
        self._initialize()
        self._test_account = AppTestAccount('test', self._tmp_dir.name)

        yield self._test_account.start()

        self.cleanup = lambda: self._test_account.cleanup()
        self.soledad = self._test_account.soledad

        self.service_factory = ServicesFactory(UserAgentMode(is_single_user=False))

        root_resource = RootResource(self.service_factory)
        leap_provider = mock()
        self.resource = set_up_protected_resources(root_resource, leap_provider, self.service_factory)

    def login(self, username='******', password='******'):
        leap_session = self._test_account.leap_session
        user_auth = mock()
        user_auth.uuid = 'some_user_uuid'
        leap_session.user_auth = user_auth
        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._set_leap_srp_auth(username, password, user_auth)
        when(LeapSessionFactory).create(username, password, user_auth).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': '******'}
        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
        if from_request:
            session = from_request.getSession()
            request.session = session
        return self._render(request, as_json)
    def start_client(self, mode=UserAgentMode(is_single_user=True)):
        self._initialize()
        self._test_account = AppTestAccount('test', self._tmp_dir.name)

        yield self._test_account.start()

        self.cleanup = lambda: self._test_account.cleanup()
        self.soledad = self._test_account.soledad

        self.service_factory = ServicesFactory(UserAgentMode(is_single_user=False))

        root_resource = RootResource(self.service_factory)
        leap_provider = mock()
        self.resource = set_up_protected_resources(root_resource, leap_provider, self.service_factory)
    def start_client(self, mode=UserAgentMode(is_single_user=True)):
        self._initialize()
        self._test_account = AppTestAccount('test', self._tmp_dir.name)

        yield self._test_account.start()

        self.cleanup = lambda: self._test_account.cleanup()
        self.soledad = self._test_account.soledad

        self.service_factory = ServicesFactory(UserAgentMode(is_single_user=False))

        root_resource = RootResource(self.service_factory)
        leap_provider = mock()
        self.resource = set_up_protected_resources(root_resource, leap_provider, self.service_factory)
class MultiUserClient(AppTestClient):
    @defer.inlineCallbacks
    def start_client(self, mode=UserAgentMode(is_single_user=True)):
        self._initialize()
        self._test_account = AppTestAccount('test', self._tmp_dir.name)

        yield self._test_account.start()

        self.cleanup = lambda: self._test_account.cleanup()
        self.soledad = self._test_account.soledad

        self.service_factory = ServicesFactory(
            UserAgentMode(is_single_user=False))

        root_resource = RootResource(self.service_factory)
        leap_provider = mock()
        self.resource = set_up_protected_resources(root_resource,
                                                   leap_provider,
                                                   self.service_factory)

    def login(self, username='******', password='******'):
        leap_session = self._test_account.leap_session
        user_auth = mock()
        user_auth.uuid = 'some_user_uuid'
        leap_session.user_auth = user_auth
        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._set_leap_srp_auth(username, password, user_auth)
        when(LeapSessionFactory).create(username, password,
                                        user_auth).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': '******'}
        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
        if from_request:
            session = from_request.getSession()
            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):
        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()
            request.session = session
        return self._render(request, as_json)