From 9d82ab69877378382efbf4d73afb334b23ab56f6 Mon Sep 17 00:00:00 2001 From: drebs Date: Thu, 3 Jan 2013 17:16:19 -0200 Subject: LeapDatabase passes HTTPDatabase tests. --- src/leap/soledad/backends/leap_backend.py | 6 ++ src/leap/soledad/tests/test_leap_backend.py | 108 ++++++++++++++++++++++++++++ src/leap/soledad/tests/test_sqlcipher.py | 16 ----- 3 files changed, 114 insertions(+), 16 deletions(-) create mode 100644 src/leap/soledad/tests/test_leap_backend.py (limited to 'src') diff --git a/src/leap/soledad/backends/leap_backend.py b/src/leap/soledad/backends/leap_backend.py index ced8734c..a79fc9e7 100644 --- a/src/leap/soledad/backends/leap_backend.py +++ b/src/leap/soledad/backends/leap_backend.py @@ -8,6 +8,8 @@ from u1db.remote.http_target import HTTPSyncTarget from u1db.remote.http_database import HTTPDatabase from leap.soledad.util import GPGWrapper +import uuid + class NoDefaultKey(Exception): pass @@ -70,6 +72,10 @@ class LeapDatabase(HTTPDatabase): db._delete() db.close() + def _allocate_doc_id(self): + """Generate a unique identifier for this document.""" + return 'D-' + uuid.uuid4().hex # 'D-' stands for document + def get_sync_target(self): st = LeapSyncTarget(self._url.geturl()) st._creds = self._creds diff --git a/src/leap/soledad/tests/test_leap_backend.py b/src/leap/soledad/tests/test_leap_backend.py new file mode 100644 index 00000000..4bb1e3b1 --- /dev/null +++ b/src/leap/soledad/tests/test_leap_backend.py @@ -0,0 +1,108 @@ +"""Test LEAP backend internals.""" + +from u1db import ( + errors, + Document, + ) + +from leap.soledad.backends import leap_backend as http_database + +from leap.soledad.tests import u1db_tests as tests +from leap.soledad.tests.u1db_tests.test_http_database import ( + TestHTTPDatabaseSimpleOperations, + TestHTTPDatabaseCtrWithCreds, + TestHTTPDatabaseIntegration, +) + +class TestLeapDatabaseSimpleOperations(TestHTTPDatabaseSimpleOperations): + + def setUp(self): + super(TestHTTPDatabaseSimpleOperations, self).setUp() + self.db = http_database.LeapDatabase('dbase') + self.db._conn = object() # crash if used + self.got = None + self.response_val = None + + def _request(method, url_parts, params=None, body=None, + content_type=None): + self.got = method, url_parts, params, body, content_type + if isinstance(self.response_val, Exception): + raise self.response_val + return self.response_val + + def _request_json(method, url_parts, params=None, body=None, + content_type=None): + self.got = method, url_parts, params, body, content_type + if isinstance(self.response_val, Exception): + raise self.response_val + return self.response_val + + self.db._request = _request + self.db._request_json = _request_json + + def test_create_doc_without_id(self): + self.response_val = {'rev': 'doc-rev-2'}, {} + new_doc = self.db.create_doc_from_json('{"v": 3}') + + +class TestLeapDatabaseCtrWithCreds(TestHTTPDatabaseCtrWithCreds): + + def test_ctr_with_creds(self): + db1 = http_database.LeapDatabase('http://dbs/db', creds={'oauth': { + 'consumer_key': tests.consumer1.key, + 'consumer_secret': tests.consumer1.secret, + 'token_key': tests.token1.key, + 'token_secret': tests.token1.secret + }}) + self.assertIn('oauth', db1._creds) + + +class TestLeapDatabaseIntegration(TestHTTPDatabaseIntegration): + + def test_non_existing_db(self): + db = http_database.LeapDatabase(self.getURL('not-there')) + self.assertRaises(errors.DatabaseDoesNotExist, db.get_doc, 'doc1') + + def test__ensure(self): + db = http_database.LeapDatabase(self.getURL('new')) + db._ensure() + self.assertIs(None, db.get_doc('doc1')) + + def test__delete(self): + self.request_state._create_database('db0') + db = http_database.LeapDatabase(self.getURL('db0')) + db._delete() + self.assertRaises(errors.DatabaseDoesNotExist, + self.request_state.check_database, 'db0') + + def test_open_database_existing(self): + self.request_state._create_database('db0') + db = http_database.LeapDatabase.open_database(self.getURL('db0'), + create=False) + self.assertIs(None, db.get_doc('doc1')) + + def test_open_database_non_existing(self): + self.assertRaises(errors.DatabaseDoesNotExist, + http_database.LeapDatabase.open_database, + self.getURL('not-there'), + create=False) + + def test_open_database_create(self): + db = http_database.LeapDatabase.open_database(self.getURL('new'), + create=True) + self.assertIs(None, db.get_doc('doc1')) + + def test_delete_database_existing(self): + self.request_state._create_database('db0') + http_database.LeapDatabase.delete_database(self.getURL('db0')) + self.assertRaises(errors.DatabaseDoesNotExist, + self.request_state.check_database, 'db0') + + def test_doc_ids_needing_quoting(self): + db0 = self.request_state._create_database('db0') + db = http_database.LeapDatabase.open_database(self.getURL('db0'), + create=False) + doc = Document('%fff', None, '{}') + db.put_doc(doc) + self.assertGetDoc(db0, '%fff', doc.rev, '{}', False) + self.assertGetDoc(db, '%fff', doc.rev, '{}', False) diff --git a/src/leap/soledad/tests/test_sqlcipher.py b/src/leap/soledad/tests/test_sqlcipher.py index fe9f0ca4..a7ba5284 100644 --- a/src/leap/soledad/tests/test_sqlcipher.py +++ b/src/leap/soledad/tests/test_sqlcipher.py @@ -1,19 +1,3 @@ -# Copyright 2011 Canonical Ltd. -# -# This file is part of u1db. -# -# u1db is free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License version 3 -# as published by the Free Software Foundation. -# -# u1db 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 Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with u1db. If not, see . - """Test sqlcipher backend internals.""" import os -- cgit v1.2.3