diff options
author | Zara Gebru <zgebru@thoughtworks.com> | 2016-04-06 14:33:57 +0200 |
---|---|---|
committer | Zara Gebru <zgebru@thoughtworks.com> | 2016-04-06 15:41:04 +0200 |
commit | dc389b7a1424ca39a88b783f77006536031d93bd (patch) | |
tree | c34ca93ea18203814d35491af63be90671860b2c /service | |
parent | 17337680ace5738a5f3e5a003bb9ab953eea8f46 (diff) |
Issue #292: add fingerprint to user settings resource
Diffstat (limited to 'service')
-rw-r--r-- | service/pixelated/resources/user_settings_resource.py | 19 | ||||
-rw-r--r-- | service/test/unit/resources/test_user_settings_resource.py | 64 |
2 files changed, 81 insertions, 2 deletions
diff --git a/service/pixelated/resources/user_settings_resource.py b/service/pixelated/resources/user_settings_resource.py index 5b3f0051..38997b30 100644 --- a/service/pixelated/resources/user_settings_resource.py +++ b/service/pixelated/resources/user_settings_resource.py @@ -14,7 +14,10 @@ # 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 pixelated.resources import respond_json, BaseResource +from pixelated.resources import respond_json_deferred, BaseResource +from twisted.web import server + +FINGERPRINT_NOT_FOUND = 'Fingerprint not found' class UserSettingsResource(BaseResource): @@ -25,4 +28,16 @@ class UserSettingsResource(BaseResource): def render_GET(self, request): _account_email = self.mail_service(request).account_email - return respond_json({'account_email': _account_email}, request) + + def finish_request(key): + _fingerprint = key.fingerprint + respond_json_deferred({'account_email': _account_email, 'fingerprint': _fingerprint}, request) + + def key_not_found(_): + respond_json_deferred({'account_email': _account_email, 'fingerprint': FINGERPRINT_NOT_FOUND}, request) + + d = self.keymanager(request).fetch_key(_account_email) + d.addCallback(finish_request) + d.addErrback(key_not_found) + + return server.NOT_DONE_YET diff --git a/service/test/unit/resources/test_user_settings_resource.py b/service/test/unit/resources/test_user_settings_resource.py new file mode 100644 index 00000000..d69f4895 --- /dev/null +++ b/service/test/unit/resources/test_user_settings_resource.py @@ -0,0 +1,64 @@ +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.keymanager 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).fetch_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).fetch_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 |