summaryrefslogtreecommitdiff
path: root/service/test/unit/resources/test_user_settings_resource.py
blob: e9748e72a8abc0a16706c74072b4f86475eb94dc (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
from twisted.trial import unittest
import json
import ast

from pixelated.application import UserAgentMode
from pixelated.resources.user_settings_resource import UserSettingsResource, FINGERPRINT_NOT_FOUND
from mockito import mock, when, any
from test.unit.resources import DummySite
from twisted.web.test.requesthelper import DummyRequest
from leap.bitmask.keymanager.keys import OpenPGPKey
from twisted.internet import defer
from twisted.python.failure import Failure

MAIL_ADDRESS = 'some@key'
FINGERPRINT = '4-8-12-13-23-42'


class TestUserSettingsResource(unittest.TestCase):

    def setUp(self):
        self.services = mock()
        self.mail_service = mock()
        self.mail_service.account_email = MAIL_ADDRESS
        self.keymanager = mock()
        self.services_factory = mock()
        self.services_factory.mode = UserAgentMode(is_single_user=True)
        self.services.mail_service = self.mail_service
        self.services.keymanager = self.keymanager
        self.services_factory._services_by_user = {'someuserid': self.keymanager}
        self.resource = UserSettingsResource(self.services_factory)
        when(self.services_factory).services(any()).thenReturn(self.services)
        self.web = DummySite(self.resource)

    def test_fingerprint_given(self):
        key = OpenPGPKey(MAIL_ADDRESS)
        key.fingerprint = FINGERPRINT
        request = DummyRequest(['/user-settings'])
        when(self.keymanager).get_key(MAIL_ADDRESS).thenReturn(defer.succeed(key))

        d = self.web.get(request)

        def assert_response(_):
            response = json.loads(request.written[0])
            self.assertEqual(FINGERPRINT, response['fingerprint'])
            self.assertEqual(MAIL_ADDRESS, response['account_email'])

        d.addCallback(assert_response)
        return d

    def test_fingerprint_missing(self):
        key = OpenPGPKey(MAIL_ADDRESS)
        key.fingerprint = FINGERPRINT
        request = DummyRequest(['/user-settings'])
        when(self.keymanager).get_key(MAIL_ADDRESS).thenReturn(defer.fail(Failure))

        d = self.web.get(request)

        def assert_response(_):
            response = json.loads(request.written[0])
            self.assertEqual(FINGERPRINT_NOT_FOUND, response['fingerprint'])
            self.assertEqual(MAIL_ADDRESS, response['account_email'])

        d.addCallback(assert_response)
        return d