summaryrefslogtreecommitdiff
path: root/service/test/support/integration/multi_user_client.py
blob: 82acb2109094e65baec7db650394d5c8d896acd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#
# Copyright (c) 2014 ThoughtWorks, Inc.
#
# Pixelated is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pixelated is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
from leap.bitmask.bonafide._srp import SRPAuthError
from mock import patch
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.config.services import ServicesFactory

from pixelated.config.sessions import LeapSessionFactory
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, StubSRPChecker
from test.support.test_helper import request_mock
from test.support.mockito import AnswerSelector


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.credentials_checker = StubSRPChecker(leap_provider)
        self.resource = set_up_protected_resources(root_resource, leap_provider, self.service_factory)

    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
        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._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))
        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 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)