blob: 4ad7938da15042228c3fbdb8102293af5c212b9e (
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
|
#
# Copyright (c) 2015 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 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
|