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/tests/test_leap_backend.py | 108 ++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/leap/soledad/tests/test_leap_backend.py (limited to 'src/leap/soledad/tests/test_leap_backend.py') 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) -- cgit v1.2.3 From 1b1044b2b6812f04c7d60d177e23d422035451b0 Mon Sep 17 00:00:00 2001 From: drebs Date: Thu, 3 Jan 2013 17:18:38 -0200 Subject: LeapDatabase passes HTTPClientBase tests. --- src/leap/soledad/tests/test_leap_backend.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/leap/soledad/tests/test_leap_backend.py') diff --git a/src/leap/soledad/tests/test_leap_backend.py b/src/leap/soledad/tests/test_leap_backend.py index 4bb1e3b1..70cd8710 100644 --- a/src/leap/soledad/tests/test_leap_backend.py +++ b/src/leap/soledad/tests/test_leap_backend.py @@ -13,6 +13,7 @@ from leap.soledad.tests.u1db_tests.test_http_database import ( TestHTTPDatabaseCtrWithCreds, TestHTTPDatabaseIntegration, ) +from leap.soledad.tests.u1db_tests.test_http_client import TestHTTPClientBase class TestLeapDatabaseSimpleOperations(TestHTTPDatabaseSimpleOperations): @@ -106,3 +107,7 @@ class TestLeapDatabaseIntegration(TestHTTPDatabaseIntegration): db.put_doc(doc) self.assertGetDoc(db0, '%fff', doc.rev, '{}', False) self.assertGetDoc(db, '%fff', doc.rev, '{}', False) + + +class TestLeapClientBase(TestHTTPClientBase): + pass -- cgit v1.2.3 From b3e8449b0e920a725036bc611ff28f83b9c1a55c Mon Sep 17 00:00:00 2001 From: drebs Date: Thu, 3 Jan 2013 17:23:42 -0200 Subject: LeapDocument passes Document tests. --- src/leap/soledad/tests/test_leap_backend.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/leap/soledad/tests/test_leap_backend.py') diff --git a/src/leap/soledad/tests/test_leap_backend.py b/src/leap/soledad/tests/test_leap_backend.py index 70cd8710..9a8daf1a 100644 --- a/src/leap/soledad/tests/test_leap_backend.py +++ b/src/leap/soledad/tests/test_leap_backend.py @@ -6,6 +6,7 @@ from u1db import ( ) from leap.soledad.backends import leap_backend as http_database +from leap.soledad.backends.leap_backend import LeapDocument from leap.soledad.tests import u1db_tests as tests from leap.soledad.tests.u1db_tests.test_http_database import ( @@ -14,6 +15,10 @@ from leap.soledad.tests.u1db_tests.test_http_database import ( TestHTTPDatabaseIntegration, ) from leap.soledad.tests.u1db_tests.test_http_client import TestHTTPClientBase +from leap.soledad.tests.u1db_tests.test_document import ( + TestDocument, + TestPyDocument, +) class TestLeapDatabaseSimpleOperations(TestHTTPDatabaseSimpleOperations): @@ -111,3 +116,20 @@ class TestLeapDatabaseIntegration(TestHTTPDatabaseIntegration): class TestLeapClientBase(TestHTTPClientBase): pass + + +def make_document_for_test(test, doc_id, rev, content, has_conflicts=False): + return LeapDocument(doc_id, rev, content, has_conflicts=has_conflicts) + + +class TestLeapDocument(TestDocument): + + scenarios = ([( + 'py', {'make_document_for_test': make_document_for_test})]) + + +class TestLeapPyDocument(TestPyDocument): + + scenarios = ([( + 'py', {'make_document_for_test': make_document_for_test})]) + -- cgit v1.2.3 From b57548757480aa33e1ba6e4ebf626721a4aa43bc Mon Sep 17 00:00:00 2001 From: drebs Date: Thu, 3 Jan 2013 17:42:34 -0200 Subject: LeapSyncTarget passes HTTPSyncTarget tests. --- src/leap/soledad/tests/test_leap_backend.py | 93 +++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) (limited to 'src/leap/soledad/tests/test_leap_backend.py') diff --git a/src/leap/soledad/tests/test_leap_backend.py b/src/leap/soledad/tests/test_leap_backend.py index 9a8daf1a..1e46051c 100644 --- a/src/leap/soledad/tests/test_leap_backend.py +++ b/src/leap/soledad/tests/test_leap_backend.py @@ -19,6 +19,15 @@ from leap.soledad.tests.u1db_tests.test_document import ( TestDocument, TestPyDocument, ) +from leap.soledad.tests.u1db_tests.test_remote_sync_target import ( + TestHTTPSyncTargetBasics, + TestParsingSyncStream, +) + + +#----------------------------------------------------------------------------- +# LeapDatabase +#----------------------------------------------------------------------------- class TestLeapDatabaseSimpleOperations(TestHTTPDatabaseSimpleOperations): @@ -118,6 +127,10 @@ class TestLeapClientBase(TestHTTPClientBase): pass +#----------------------------------------------------------------------------- +# LeapDocument +#----------------------------------------------------------------------------- + def make_document_for_test(test, doc_id, rev, content, has_conflicts=False): return LeapDocument(doc_id, rev, content, has_conflicts=has_conflicts) @@ -133,3 +146,83 @@ class TestLeapPyDocument(TestPyDocument): scenarios = ([( 'py', {'make_document_for_test': make_document_for_test})]) + +#----------------------------------------------------------------------------- +# LeapSyncTarget +#----------------------------------------------------------------------------- + +class TestLeapSyncTargetBasics(TestHTTPSyncTargetBasics): + + def test_parse_url(self): + remote_target = http_database.LeapSyncTarget('http://127.0.0.1:12345/') + self.assertEqual('http', remote_target._url.scheme) + self.assertEqual('127.0.0.1', remote_target._url.hostname) + self.assertEqual(12345, remote_target._url.port) + self.assertEqual('/', remote_target._url.path) + +class TestLeapParsingSyncStream(TestParsingSyncStream): + + def test_wrong_start(self): + tgt = http_database.LeapSyncTarget("http://foo/foo") + + self.assertRaises(errors.BrokenSyncStream, + tgt._parse_sync_stream, "{}\r\n]", None) + + self.assertRaises(errors.BrokenSyncStream, + tgt._parse_sync_stream, "\r\n{}\r\n]", None) + + self.assertRaises(errors.BrokenSyncStream, + tgt._parse_sync_stream, "", None) + + def test_wrong_end(self): + tgt = http_database.LeapSyncTarget("http://foo/foo") + + self.assertRaises(errors.BrokenSyncStream, + tgt._parse_sync_stream, "[\r\n{}", None) + + self.assertRaises(errors.BrokenSyncStream, + tgt._parse_sync_stream, "[\r\n", None) + + def test_missing_comma(self): + tgt = http_database.LeapSyncTarget("http://foo/foo") + + self.assertRaises(errors.BrokenSyncStream, + tgt._parse_sync_stream, + '[\r\n{}\r\n{"id": "i", "rev": "r", ' + '"content": "c", "gen": 3}\r\n]', None) + + def test_no_entries(self): + tgt = http_database.LeapSyncTarget("http://foo/foo") + + self.assertRaises(errors.BrokenSyncStream, + tgt._parse_sync_stream, "[\r\n]", None) + + def test_extra_comma(self): + tgt = http_database.LeapSyncTarget("http://foo/foo") + + self.assertRaises(errors.BrokenSyncStream, + tgt._parse_sync_stream, "[\r\n{},\r\n]", None) + + self.assertRaises(http_database.NoSoledadInstance, + tgt._parse_sync_stream, + '[\r\n{},\r\n{"id": "i", "rev": "r", ' + '"content": "{}", "gen": 3, "trans_id": "T-sid"}' + ',\r\n]', + lambda doc, gen, trans_id: None) + + def test_error_in_stream(self): + tgt = http_database.LeapSyncTarget("http://foo/foo") + + self.assertRaises(errors.Unavailable, + tgt._parse_sync_stream, + '[\r\n{"new_generation": 0},' + '\r\n{"error": "unavailable"}\r\n', None) + + self.assertRaises(errors.Unavailable, + tgt._parse_sync_stream, + '[\r\n{"error": "unavailable"}\r\n', None) + + self.assertRaises(errors.BrokenSyncStream, + tgt._parse_sync_stream, + '[\r\n{"error": "?"}\r\n', None) + -- cgit v1.2.3 From bb4d7cff049243856231afc207734af7edaabbae Mon Sep 17 00:00:00 2001 From: drebs Date: Thu, 10 Jan 2013 15:19:34 -0200 Subject: LeapDatabase passes u1db AllDatabaseTests. --- src/leap/soledad/tests/test_leap_backend.py | 249 ++++++---------------------- 1 file changed, 47 insertions(+), 202 deletions(-) (limited to 'src/leap/soledad/tests/test_leap_backend.py') diff --git a/src/leap/soledad/tests/test_leap_backend.py b/src/leap/soledad/tests/test_leap_backend.py index 1e46051c..4491fe86 100644 --- a/src/leap/soledad/tests/test_leap_backend.py +++ b/src/leap/soledad/tests/test_leap_backend.py @@ -1,228 +1,73 @@ -"""Test LEAP backend internals.""" +"""Test ObjectStore backend bits. -from u1db import ( - errors, - Document, - ) - -from leap.soledad.backends import leap_backend as http_database -from leap.soledad.backends.leap_backend import LeapDocument +For these tests to run, a leap server has to be running on (default) port +5984. +""" +import sys +import copy +import testtools +import testscenarios +from leap.soledad.backends import leap_backend from leap.soledad.tests import u1db_tests as tests -from leap.soledad.tests.u1db_tests.test_http_database import ( - TestHTTPDatabaseSimpleOperations, - TestHTTPDatabaseCtrWithCreds, - TestHTTPDatabaseIntegration, -) -from leap.soledad.tests.u1db_tests.test_http_client import TestHTTPClientBase -from leap.soledad.tests.u1db_tests.test_document import ( - TestDocument, - TestPyDocument, -) -from leap.soledad.tests.u1db_tests.test_remote_sync_target import ( - TestHTTPSyncTargetBasics, - TestParsingSyncStream, +from leap.soledad.tests.u1db_tests.test_remote_sync_target import make_http_app +from leap.soledad.tests.u1db_tests.test_backends import ( + AllDatabaseTests, ) #----------------------------------------------------------------------------- -# LeapDatabase -#----------------------------------------------------------------------------- - -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) - - -class TestLeapClientBase(TestHTTPClientBase): - pass - - -#----------------------------------------------------------------------------- -# LeapDocument +# The following tests come from `u1db.tests.test_common_backends`. #----------------------------------------------------------------------------- -def make_document_for_test(test, doc_id, rev, content, has_conflicts=False): - return LeapDocument(doc_id, rev, content, has_conflicts=has_conflicts) - - -class TestLeapDocument(TestDocument): - - scenarios = ([( - 'py', {'make_document_for_test': make_document_for_test})]) - +class TestLeapBackendImpl(tests.TestCase): -class TestLeapPyDocument(TestPyDocument): - - scenarios = ([( - 'py', {'make_document_for_test': make_document_for_test})]) + def test__allocate_doc_id(self): + db = leap_backend.LeapDatabase('test') + doc_id1 = db._allocate_doc_id() + self.assertTrue(doc_id1.startswith('D-')) + self.assertEqual(34, len(doc_id1)) + int(doc_id1[len('D-'):], 16) + self.assertNotEqual(doc_id1, db._allocate_doc_id()) #----------------------------------------------------------------------------- -# LeapSyncTarget +# The following tests come from `u1db.tests.test_backends`. #----------------------------------------------------------------------------- -class TestLeapSyncTargetBasics(TestHTTPSyncTargetBasics): - - def test_parse_url(self): - remote_target = http_database.LeapSyncTarget('http://127.0.0.1:12345/') - self.assertEqual('http', remote_target._url.scheme) - self.assertEqual('127.0.0.1', remote_target._url.hostname) - self.assertEqual(12345, remote_target._url.port) - self.assertEqual('/', remote_target._url.path) - -class TestLeapParsingSyncStream(TestParsingSyncStream): - - def test_wrong_start(self): - tgt = http_database.LeapSyncTarget("http://foo/foo") - - self.assertRaises(errors.BrokenSyncStream, - tgt._parse_sync_stream, "{}\r\n]", None) - - self.assertRaises(errors.BrokenSyncStream, - tgt._parse_sync_stream, "\r\n{}\r\n]", None) - - self.assertRaises(errors.BrokenSyncStream, - tgt._parse_sync_stream, "", None) - - def test_wrong_end(self): - tgt = http_database.LeapSyncTarget("http://foo/foo") - - self.assertRaises(errors.BrokenSyncStream, - tgt._parse_sync_stream, "[\r\n{}", None) - - self.assertRaises(errors.BrokenSyncStream, - tgt._parse_sync_stream, "[\r\n", None) - - def test_missing_comma(self): - tgt = http_database.LeapSyncTarget("http://foo/foo") +def make_leap_database_for_test(test, replica_uid, path='test'): + test.startServer() + test.request_state._create_database(replica_uid) + return leap_backend.LeapDatabase(test.getURL(path)) - self.assertRaises(errors.BrokenSyncStream, - tgt._parse_sync_stream, - '[\r\n{}\r\n{"id": "i", "rev": "r", ' - '"content": "c", "gen": 3}\r\n]', None) - def test_no_entries(self): - tgt = http_database.LeapSyncTarget("http://foo/foo") +def copy_leap_database_for_test(test, db): + # DO NOT COPY OR REUSE THIS CODE OUTSIDE TESTS: COPYING U1DB DATABASES IS + # THE WRONG THING TO DO, THE ONLY REASON WE DO SO HERE IS TO TEST THAT WE + # CORRECTLY DETECT IT HAPPENING SO THAT WE CAN RAISE ERRORS RATHER THAN + # CORRUPT USER DATA. USE SYNC INSTEAD, OR WE WILL SEND NINJA TO YOUR + # HOUSE. + return test.request_state._copy_database(db) - self.assertRaises(errors.BrokenSyncStream, - tgt._parse_sync_stream, "[\r\n]", None) - def test_extra_comma(self): - tgt = http_database.LeapSyncTarget("http://foo/foo") +def make_oauth_leap_database_for_test(test, replica_uid): + http_db = make_leap_database_for_test(test, replica_uid, '~/test') + http_db.set_oauth_credentials(tests.consumer1.key, tests.consumer1.secret, + tests.token1.key, tests.token1.secret) + return http_db - self.assertRaises(errors.BrokenSyncStream, - tgt._parse_sync_stream, "[\r\n{},\r\n]", None) - self.assertRaises(http_database.NoSoledadInstance, - tgt._parse_sync_stream, - '[\r\n{},\r\n{"id": "i", "rev": "r", ' - '"content": "{}", "gen": 3, "trans_id": "T-sid"}' - ',\r\n]', - lambda doc, gen, trans_id: None) +LEAP_SCENARIOS = [ + ('http', {'make_database_for_test': make_leap_database_for_test, + 'copy_database_for_test': copy_leap_database_for_test, + 'make_document_for_test': tests.make_document_for_test, + 'make_app_with_state': make_http_app}), + ] - def test_error_in_stream(self): - tgt = http_database.LeapSyncTarget("http://foo/foo") - self.assertRaises(errors.Unavailable, - tgt._parse_sync_stream, - '[\r\n{"new_generation": 0},' - '\r\n{"error": "unavailable"}\r\n', None) +class LeapTests(AllDatabaseTests): - self.assertRaises(errors.Unavailable, - tgt._parse_sync_stream, - '[\r\n{"error": "unavailable"}\r\n', None) + scenarios = LEAP_SCENARIOS - self.assertRaises(errors.BrokenSyncStream, - tgt._parse_sync_stream, - '[\r\n{"error": "?"}\r\n', None) +load_tests = tests.load_with_scenarios -- cgit v1.2.3 From 17c3c2d2d18abac6f63f82beed9ccc875f2b16af Mon Sep 17 00:00:00 2001 From: drebs Date: Thu, 10 Jan 2013 15:39:42 -0200 Subject: LeapDatabase passes u1db TestHTTPDatabaseSimpleOperations. --- src/leap/soledad/tests/test_leap_backend.py | 35 +++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) (limited to 'src/leap/soledad/tests/test_leap_backend.py') diff --git a/src/leap/soledad/tests/test_leap_backend.py b/src/leap/soledad/tests/test_leap_backend.py index 4491fe86..1759faad 100644 --- a/src/leap/soledad/tests/test_leap_backend.py +++ b/src/leap/soledad/tests/test_leap_backend.py @@ -11,8 +11,9 @@ import testscenarios from leap.soledad.backends import leap_backend from leap.soledad.tests import u1db_tests as tests from leap.soledad.tests.u1db_tests.test_remote_sync_target import make_http_app -from leap.soledad.tests.u1db_tests.test_backends import ( - AllDatabaseTests, +from leap.soledad.tests.u1db_tests.test_backends import AllDatabaseTests +from leap.soledad.tests.u1db_tests.test_http_database import ( + TestHTTPDatabaseSimpleOperations, ) @@ -70,4 +71,34 @@ class LeapTests(AllDatabaseTests): scenarios = LEAP_SCENARIOS +#----------------------------------------------------------------------------- +# The following tests come from `u1db.tests.test_http_client`. +#----------------------------------------------------------------------------- + +class TestLeapDatabaseSimpleOperations(TestHTTPDatabaseSimpleOperations): + + def setUp(self): + super(TestHTTPDatabaseSimpleOperations, self).setUp() + self.db = leap_backend.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 + load_tests = tests.load_with_scenarios -- cgit v1.2.3 From 449e036985bb19a833858cf64e049b6ee34936c1 Mon Sep 17 00:00:00 2001 From: drebs Date: Thu, 10 Jan 2013 15:40:29 -0200 Subject: LeapDatabase passes u1db TestHTTPDatabaseCtrWithCreds. --- src/leap/soledad/tests/test_leap_backend.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/leap/soledad/tests/test_leap_backend.py') diff --git a/src/leap/soledad/tests/test_leap_backend.py b/src/leap/soledad/tests/test_leap_backend.py index 1759faad..4acec6ef 100644 --- a/src/leap/soledad/tests/test_leap_backend.py +++ b/src/leap/soledad/tests/test_leap_backend.py @@ -14,6 +14,7 @@ from leap.soledad.tests.u1db_tests.test_remote_sync_target import make_http_app from leap.soledad.tests.u1db_tests.test_backends import AllDatabaseTests from leap.soledad.tests.u1db_tests.test_http_database import ( TestHTTPDatabaseSimpleOperations, + TestHTTPDatabaseCtrWithCreds, ) @@ -101,4 +102,8 @@ class TestLeapDatabaseSimpleOperations(TestHTTPDatabaseSimpleOperations): self.db._request = _request self.db._request_json = _request_json +class TestLeapDatabaseCtrWithCreds(TestHTTPDatabaseCtrWithCreds): + pass + + load_tests = tests.load_with_scenarios -- cgit v1.2.3 From e819be96ad829875c1ec533d9ab415e576e63587 Mon Sep 17 00:00:00 2001 From: drebs Date: Thu, 10 Jan 2013 15:48:55 -0200 Subject: LeapDatabase passes u1db TestHTTPDatabaseIntegration. --- src/leap/soledad/tests/test_leap_backend.py | 53 +++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'src/leap/soledad/tests/test_leap_backend.py') diff --git a/src/leap/soledad/tests/test_leap_backend.py b/src/leap/soledad/tests/test_leap_backend.py index 4acec6ef..5a992e76 100644 --- a/src/leap/soledad/tests/test_leap_backend.py +++ b/src/leap/soledad/tests/test_leap_backend.py @@ -8,6 +8,7 @@ import sys import copy import testtools import testscenarios +from u1db import errors, Document from leap.soledad.backends import leap_backend from leap.soledad.tests import u1db_tests as tests from leap.soledad.tests.u1db_tests.test_remote_sync_target import make_http_app @@ -15,6 +16,7 @@ from leap.soledad.tests.u1db_tests.test_backends import AllDatabaseTests from leap.soledad.tests.u1db_tests.test_http_database import ( TestHTTPDatabaseSimpleOperations, TestHTTPDatabaseCtrWithCreds, + TestHTTPDatabaseIntegration ) @@ -102,8 +104,59 @@ class TestLeapDatabaseSimpleOperations(TestHTTPDatabaseSimpleOperations): self.db._request = _request self.db._request_json = _request_json + class TestLeapDatabaseCtrWithCreds(TestHTTPDatabaseCtrWithCreds): pass +class TestLeapDatabaseIntegration(TestHTTPDatabaseIntegration): + + def test_non_existing_db(self): + db = leap_backend.LeapDatabase(self.getURL('not-there')) + self.assertRaises(errors.DatabaseDoesNotExist, db.get_doc, 'doc1') + + def test__ensure(self): + db = leap_backend.LeapDatabase(self.getURL('new')) + db._ensure() + self.assertIs(None, db.get_doc('doc1')) + + def test__delete(self): + self.request_state._create_database('db0') + db = leap_backend.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 = leap_backend.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, + leap_backend.LeapDatabase.open_database, + self.getURL('not-there'), + create=False) + + def test_open_database_create(self): + db = leap_backend.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') + leap_backend.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 = leap_backend.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) + load_tests = tests.load_with_scenarios -- cgit v1.2.3 From c317af1da8060f66576c817ea7073e9c73f43916 Mon Sep 17 00:00:00 2001 From: drebs Date: Thu, 10 Jan 2013 15:55:21 -0200 Subject: LeapDatabase passes u1db TestHTTPClientBase. --- src/leap/soledad/tests/test_leap_backend.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src/leap/soledad/tests/test_leap_backend.py') diff --git a/src/leap/soledad/tests/test_leap_backend.py b/src/leap/soledad/tests/test_leap_backend.py index 5a992e76..5f58c832 100644 --- a/src/leap/soledad/tests/test_leap_backend.py +++ b/src/leap/soledad/tests/test_leap_backend.py @@ -16,7 +16,10 @@ from leap.soledad.tests.u1db_tests.test_backends import AllDatabaseTests from leap.soledad.tests.u1db_tests.test_http_database import ( TestHTTPDatabaseSimpleOperations, TestHTTPDatabaseCtrWithCreds, - TestHTTPDatabaseIntegration + TestHTTPDatabaseIntegration, +) +from leap.soledad.tests.u1db_tests.test_http_client import ( + TestHTTPClientBase, ) @@ -75,7 +78,7 @@ class LeapTests(AllDatabaseTests): #----------------------------------------------------------------------------- -# The following tests come from `u1db.tests.test_http_client`. +# The following tests come from `u1db.tests.test_http_database`. #----------------------------------------------------------------------------- class TestLeapDatabaseSimpleOperations(TestHTTPDatabaseSimpleOperations): @@ -159,4 +162,13 @@ class TestLeapDatabaseIntegration(TestHTTPDatabaseIntegration): self.assertGetDoc(db0, '%fff', doc.rev, '{}', False) self.assertGetDoc(db, '%fff', doc.rev, '{}', False) + +#----------------------------------------------------------------------------- +# The following tests come from `u1db.tests.test_http_client`. +#----------------------------------------------------------------------------- + +class TestLeapClientBase(TestHTTPClientBase): + pass + + load_tests = tests.load_with_scenarios -- cgit v1.2.3 From 7194324acca0f0ba2cb05718ad524ae313b2ba35 Mon Sep 17 00:00:00 2001 From: drebs Date: Thu, 10 Jan 2013 15:58:00 -0200 Subject: LeapDocument and LeapSyncTarget pass u1db tests. --- src/leap/soledad/tests/test_leap_backend.py | 141 +++++++++++++++++++++++++++- 1 file changed, 140 insertions(+), 1 deletion(-) (limited to 'src/leap/soledad/tests/test_leap_backend.py') diff --git a/src/leap/soledad/tests/test_leap_backend.py b/src/leap/soledad/tests/test_leap_backend.py index 5f58c832..72106aef 100644 --- a/src/leap/soledad/tests/test_leap_backend.py +++ b/src/leap/soledad/tests/test_leap_backend.py @@ -21,6 +21,15 @@ from leap.soledad.tests.u1db_tests.test_http_database import ( from leap.soledad.tests.u1db_tests.test_http_client import ( TestHTTPClientBase, ) +from leap.soledad.tests.u1db_tests.test_document import ( + TestDocument, + TestPyDocument, +) +from leap.soledad.tests.u1db_tests.test_remote_sync_target import ( + TestHTTPSyncTargetBasics, + TestParsingSyncStream, +) + #----------------------------------------------------------------------------- @@ -78,7 +87,7 @@ class LeapTests(AllDatabaseTests): #----------------------------------------------------------------------------- -# The following tests come from `u1db.tests.test_http_database`. +# The following tests come from `u1db.tests.test_leap_backend`. #----------------------------------------------------------------------------- class TestLeapDatabaseSimpleOperations(TestHTTPDatabaseSimpleOperations): @@ -171,4 +180,134 @@ class TestLeapClientBase(TestHTTPClientBase): pass +#----------------------------------------------------------------------------- +# The following tests come from `u1db.tests.test_document`. +#----------------------------------------------------------------------------- + +def make_document_for_test(test, doc_id, rev, content, has_conflicts=False): + return leap_backend.LeapDocument( + doc_id, rev, content, has_conflicts=has_conflicts) + + +class TestLeapDocument(TestDocument): + + scenarios = ([( + 'leap', {'make_document_for_test': make_document_for_test})]) + + +class TestLeapPyDocument(TestPyDocument): + + scenarios = ([( + 'leap', {'make_document_for_test': make_document_for_test})]) + + +#----------------------------------------------------------------------------- +# The following tests come from `u1db.tests.test_sync_target`. +#----------------------------------------------------------------------------- + +class TestLeapSyncTargetBasics(TestHTTPSyncTargetBasics): + + def test_parse_url(self): + remote_target = leap_backend.LeapSyncTarget('http://127.0.0.1:12345/') + self.assertEqual('http', remote_target._url.scheme) + self.assertEqual('127.0.0.1', remote_target._url.hostname) + self.assertEqual(12345, remote_target._url.port) + self.assertEqual('/', remote_target._url.path) + +class TestLeapParsingSyncStream(TestParsingSyncStream): + + def test_wrong_start(self): + tgt = leap_backend.LeapSyncTarget("http://foo/foo") + + self.assertRaises(errors.BrokenSyncStream, + tgt._parse_sync_stream, "{}\r\n]", None) + + self.assertRaises(errors.BrokenSyncStream, + tgt._parse_sync_stream, "\r\n{}\r\n]", None) + + self.assertRaises(errors.BrokenSyncStream, + tgt._parse_sync_stream, "", None) + + def test_wrong_end(self): + tgt = leap_backend.LeapSyncTarget("http://foo/foo") + + self.assertRaises(errors.BrokenSyncStream, + tgt._parse_sync_stream, "[\r\n{}", None) + + self.assertRaises(errors.BrokenSyncStream, + tgt._parse_sync_stream, "[\r\n", None) + + def test_missing_comma(self): + tgt = leap_backend.LeapSyncTarget("http://foo/foo") + + self.assertRaises(errors.BrokenSyncStream, + tgt._parse_sync_stream, + '[\r\n{}\r\n{"id": "i", "rev": "r", ' + '"content": "c", "gen": 3}\r\n]', None) + + def test_no_entries(self): + tgt = leap_backend.LeapSyncTarget("http://foo/foo") + + self.assertRaises(errors.BrokenSyncStream, + tgt._parse_sync_stream, "[\r\n]", None) + + def test_extra_comma(self): + tgt = leap_backend.LeapSyncTarget("http://foo/foo") + + self.assertRaises(errors.BrokenSyncStream, + tgt._parse_sync_stream, "[\r\n{},\r\n]", None) + + self.assertRaises(leap_backend.NoSoledadInstance, + tgt._parse_sync_stream, + '[\r\n{},\r\n{"id": "i", "rev": "r", ' + '"content": "{}", "gen": 3, "trans_id": "T-sid"}' + ',\r\n]', + lambda doc, gen, trans_id: None) + + def test_error_in_stream(self): + tgt = leap_backend.LeapSyncTarget("http://foo/foo") + + self.assertRaises(errors.Unavailable, + tgt._parse_sync_stream, + '[\r\n{"new_generation": 0},' + '\r\n{"error": "unavailable"}\r\n', None) + + self.assertRaises(errors.Unavailable, + tgt._parse_sync_stream, + '[\r\n{"error": "unavailable"}\r\n', None) + + self.assertRaises(errors.BrokenSyncStream, + tgt._parse_sync_stream, + '[\r\n{"error": "?"}\r\n', None) + + +def leap_sync_target(test, path): + return leap_backend.LeapSyncTarget(test.getURL(path)) + + +def make_oauth_http_app(state): + app = http_app.HTTPApp(state) + application = oauth_middleware.OAuthMiddleware(app, None, prefix='/~/') + application.get_oauth_data_store = lambda: tests.testingOAuthStore + return application + + +def oauth_leap_sync_target(test, path): + st = leap_sync_target(test, '~/' + path) + st.set_oauth_credentials(tests.consumer1.key, tests.consumer1.secret, + tests.token1.key, tests.token1.secret) + return st + + +class TestRemoteSyncTargets(tests.TestCaseWithServer): + + scenarios = [ + ('http', {'make_app_with_state': make_http_app, + 'make_document_for_test': tests.make_document_for_test, + 'sync_target': leap_sync_target}), + ('oauth_http', {'make_app_with_state': make_oauth_http_app, + 'make_document_for_test': tests.make_document_for_test, + 'sync_target': oauth_leap_sync_target}), + ] + load_tests = tests.load_with_scenarios -- cgit v1.2.3 From ecbe66c10805f6f4866c27fa17952cc5f5ca4a2c Mon Sep 17 00:00:00 2001 From: drebs Date: Mon, 14 Jan 2013 13:22:58 -0200 Subject: LeapDatabase passes u1db test_https. --- src/leap/soledad/tests/test_leap_backend.py | 168 +++++++++++++++++++++------- 1 file changed, 127 insertions(+), 41 deletions(-) (limited to 'src/leap/soledad/tests/test_leap_backend.py') diff --git a/src/leap/soledad/tests/test_leap_backend.py b/src/leap/soledad/tests/test_leap_backend.py index 72106aef..97de101f 100644 --- a/src/leap/soledad/tests/test_leap_backend.py +++ b/src/leap/soledad/tests/test_leap_backend.py @@ -4,14 +4,20 @@ For these tests to run, a leap server has to be running on (default) port 5984. """ -import sys +from shutil import rmtree +import os import copy +import unittest2 as unittest import testtools import testscenarios -from u1db import errors, Document +import u1db +from leap.soledad import Soledad from leap.soledad.backends import leap_backend from leap.soledad.tests import u1db_tests as tests -from leap.soledad.tests.u1db_tests.test_remote_sync_target import make_http_app +from leap.soledad.tests.u1db_tests.test_remote_sync_target import ( + make_http_app, + make_oauth_http_app, +) from leap.soledad.tests.u1db_tests.test_backends import AllDatabaseTests from leap.soledad.tests.u1db_tests.test_http_database import ( TestHTTPDatabaseSimpleOperations, @@ -29,7 +35,53 @@ from leap.soledad.tests.u1db_tests.test_remote_sync_target import ( TestHTTPSyncTargetBasics, TestParsingSyncStream, ) +from leap.soledad.tests.u1db_tests.test_sync import ( + _make_local_db_and_target, + DatabaseSyncTargetTests, +) +from leap.soledad.tests.u1db_tests.test_https import ( + TestHttpSyncTargetHttpsSupport, + https_server_def, +) +from leap.soledad.tests.test_encrypted import ( + PUBLIC_KEY, + PRIVATE_KEY, + KEY_FINGERPRINT, +) + +try: + import simplejson as json +except ImportError: + import json # noqa + +#----------------------------------------------------------------------------- +# The EncryptedSyncTest is used with multiple inheritance to guarantee that we +# have a working Soledad instance in each test. +#----------------------------------------------------------------------------- + +class SoledadTest(unittest.TestCase): + + PREFIX = "/var/tmp" + GNUPG_HOME = "%s/gnupg" % PREFIX + DB1_FILE = "%s/db1.u1db" % PREFIX + DB2_FILE = "%s/db2.u1db" % PREFIX + EMAIL = 'leap@leap.se' + def setUp(self): + super(SoledadTest, self).setUp() + self._db1 = u1db.open(self.DB1_FILE, create=True, + document_factory=leap_backend.LeapDocument) + self._db2 = u1db.open(self.DB2_FILE, create=True, + document_factory=leap_backend.LeapDocument) + self._soledad = Soledad(self.EMAIL, gpghome=self.GNUPG_HOME) + self._soledad._gpg.import_keys(PUBLIC_KEY) + self._soledad._gpg.import_keys(PRIVATE_KEY) + + def tearDown(self): + super(SoledadTest, self).tearDown() + os.unlink(self.DB1_FILE) + os.unlink(self.DB2_FILE) + #rmtree(self.GNUPG_HOME) #----------------------------------------------------------------------------- @@ -73,21 +125,40 @@ def make_oauth_leap_database_for_test(test, replica_uid): return http_db +def make_document_for_test(test, doc_id, rev, content, has_conflicts=False): + return leap_backend.LeapDocument( + doc_id, rev, content, has_conflicts=has_conflicts) + + +def make_leap_document_for_test(test, doc_id, rev, content, has_conflicts=False): + return leap_backend.LeapDocument( + doc_id, rev, content, has_conflicts=has_conflicts, + soledad=test._soledad) + + +def make_leap_encrypted_document_for_test(test, doc_id, rev, encrypted_content, + has_conflicts=False): + return leap_backend.LeapDocument( + doc_id, rev, encrypted_json=encrypted_content, + has_conflicts=has_conflicts, + soledad=test._soledad) + + LEAP_SCENARIOS = [ ('http', {'make_database_for_test': make_leap_database_for_test, 'copy_database_for_test': copy_leap_database_for_test, - 'make_document_for_test': tests.make_document_for_test, + 'make_document_for_test': make_leap_document_for_test, 'make_app_with_state': make_http_app}), ] -class LeapTests(AllDatabaseTests): +class LeapTests(AllDatabaseTests, SoledadTest): scenarios = LEAP_SCENARIOS #----------------------------------------------------------------------------- -# The following tests come from `u1db.tests.test_leap_backend`. +# The following tests come from `u1db.tests.test_http_database`. #----------------------------------------------------------------------------- class TestLeapDatabaseSimpleOperations(TestHTTPDatabaseSimpleOperations): @@ -116,6 +187,11 @@ class TestLeapDatabaseSimpleOperations(TestHTTPDatabaseSimpleOperations): self.db._request = _request self.db._request_json = _request_json + def test_get_sync_target(self): + st = self.db.get_sync_target() + self.assertIsInstance(st, leap_backend.LeapSyncTarget) + self.assertEqual(st._url, self.db._url) + class TestLeapDatabaseCtrWithCreds(TestHTTPDatabaseCtrWithCreds): pass @@ -125,7 +201,7 @@ class TestLeapDatabaseIntegration(TestHTTPDatabaseIntegration): def test_non_existing_db(self): db = leap_backend.LeapDatabase(self.getURL('not-there')) - self.assertRaises(errors.DatabaseDoesNotExist, db.get_doc, 'doc1') + self.assertRaises(u1db.errors.DatabaseDoesNotExist, db.get_doc, 'doc1') def test__ensure(self): db = leap_backend.LeapDatabase(self.getURL('new')) @@ -136,7 +212,7 @@ class TestLeapDatabaseIntegration(TestHTTPDatabaseIntegration): self.request_state._create_database('db0') db = leap_backend.LeapDatabase(self.getURL('db0')) db._delete() - self.assertRaises(errors.DatabaseDoesNotExist, + self.assertRaises(u1db.errors.DatabaseDoesNotExist, self.request_state.check_database, 'db0') def test_open_database_existing(self): @@ -146,7 +222,7 @@ class TestLeapDatabaseIntegration(TestHTTPDatabaseIntegration): self.assertIs(None, db.get_doc('doc1')) def test_open_database_non_existing(self): - self.assertRaises(errors.DatabaseDoesNotExist, + self.assertRaises(u1db.errors.DatabaseDoesNotExist, leap_backend.LeapDatabase.open_database, self.getURL('not-there'), create=False) @@ -159,14 +235,14 @@ class TestLeapDatabaseIntegration(TestHTTPDatabaseIntegration): def test_delete_database_existing(self): self.request_state._create_database('db0') leap_backend.LeapDatabase.delete_database(self.getURL('db0')) - self.assertRaises(errors.DatabaseDoesNotExist, + self.assertRaises(u1db.errors.DatabaseDoesNotExist, self.request_state.check_database, 'db0') def test_doc_ids_needing_quoting(self): db0 = self.request_state._create_database('db0') db = leap_backend.LeapDatabase.open_database(self.getURL('db0'), create=False) - doc = Document('%fff', None, '{}') + doc = leap_backend.LeapDocument('%fff', None, '{}') db.put_doc(doc) self.assertGetDoc(db0, '%fff', doc.rev, '{}', False) self.assertGetDoc(db, '%fff', doc.rev, '{}', False) @@ -184,25 +260,20 @@ class TestLeapClientBase(TestHTTPClientBase): # The following tests come from `u1db.tests.test_document`. #----------------------------------------------------------------------------- -def make_document_for_test(test, doc_id, rev, content, has_conflicts=False): - return leap_backend.LeapDocument( - doc_id, rev, content, has_conflicts=has_conflicts) - - -class TestLeapDocument(TestDocument): +class TestLeapDocument(TestDocument, SoledadTest): scenarios = ([( - 'leap', {'make_document_for_test': make_document_for_test})]) + 'leap', {'make_document_for_test': make_leap_document_for_test})]) -class TestLeapPyDocument(TestPyDocument): +class TestLeapPyDocument(TestPyDocument, SoledadTest): scenarios = ([( - 'leap', {'make_document_for_test': make_document_for_test})]) + 'leap', {'make_document_for_test': make_leap_document_for_test})]) #----------------------------------------------------------------------------- -# The following tests come from `u1db.tests.test_sync_target`. +# The following tests come from `u1db.tests.test_remote_sync_target`. #----------------------------------------------------------------------------- class TestLeapSyncTargetBasics(TestHTTPSyncTargetBasics): @@ -219,28 +290,28 @@ class TestLeapParsingSyncStream(TestParsingSyncStream): def test_wrong_start(self): tgt = leap_backend.LeapSyncTarget("http://foo/foo") - self.assertRaises(errors.BrokenSyncStream, + self.assertRaises(u1db.errors.BrokenSyncStream, tgt._parse_sync_stream, "{}\r\n]", None) - self.assertRaises(errors.BrokenSyncStream, + self.assertRaises(u1db.errors.BrokenSyncStream, tgt._parse_sync_stream, "\r\n{}\r\n]", None) - self.assertRaises(errors.BrokenSyncStream, + self.assertRaises(u1db.errors.BrokenSyncStream, tgt._parse_sync_stream, "", None) def test_wrong_end(self): tgt = leap_backend.LeapSyncTarget("http://foo/foo") - self.assertRaises(errors.BrokenSyncStream, + self.assertRaises(u1db.errors.BrokenSyncStream, tgt._parse_sync_stream, "[\r\n{}", None) - self.assertRaises(errors.BrokenSyncStream, + self.assertRaises(u1db.errors.BrokenSyncStream, tgt._parse_sync_stream, "[\r\n", None) def test_missing_comma(self): tgt = leap_backend.LeapSyncTarget("http://foo/foo") - self.assertRaises(errors.BrokenSyncStream, + self.assertRaises(u1db.errors.BrokenSyncStream, tgt._parse_sync_stream, '[\r\n{}\r\n{"id": "i", "rev": "r", ' '"content": "c", "gen": 3}\r\n]', None) @@ -248,13 +319,13 @@ class TestLeapParsingSyncStream(TestParsingSyncStream): def test_no_entries(self): tgt = leap_backend.LeapSyncTarget("http://foo/foo") - self.assertRaises(errors.BrokenSyncStream, + self.assertRaises(u1db.errors.BrokenSyncStream, tgt._parse_sync_stream, "[\r\n]", None) def test_extra_comma(self): tgt = leap_backend.LeapSyncTarget("http://foo/foo") - self.assertRaises(errors.BrokenSyncStream, + self.assertRaises(u1db.errors.BrokenSyncStream, tgt._parse_sync_stream, "[\r\n{},\r\n]", None) self.assertRaises(leap_backend.NoSoledadInstance, @@ -267,16 +338,16 @@ class TestLeapParsingSyncStream(TestParsingSyncStream): def test_error_in_stream(self): tgt = leap_backend.LeapSyncTarget("http://foo/foo") - self.assertRaises(errors.Unavailable, + self.assertRaises(u1db.errors.Unavailable, tgt._parse_sync_stream, '[\r\n{"new_generation": 0},' '\r\n{"error": "unavailable"}\r\n', None) - self.assertRaises(errors.Unavailable, + self.assertRaises(u1db.errors.Unavailable, tgt._parse_sync_stream, '[\r\n{"error": "unavailable"}\r\n', None) - self.assertRaises(errors.BrokenSyncStream, + self.assertRaises(u1db.errors.BrokenSyncStream, tgt._parse_sync_stream, '[\r\n{"error": "?"}\r\n', None) @@ -285,13 +356,6 @@ def leap_sync_target(test, path): return leap_backend.LeapSyncTarget(test.getURL(path)) -def make_oauth_http_app(state): - app = http_app.HTTPApp(state) - application = oauth_middleware.OAuthMiddleware(app, None, prefix='/~/') - application.get_oauth_data_store = lambda: tests.testingOAuthStore - return application - - def oauth_leap_sync_target(test, path): st = leap_sync_target(test, '~/' + path) st.set_oauth_credentials(tests.consumer1.key, tests.consumer1.secret, @@ -303,11 +367,33 @@ class TestRemoteSyncTargets(tests.TestCaseWithServer): scenarios = [ ('http', {'make_app_with_state': make_http_app, - 'make_document_for_test': tests.make_document_for_test, + 'make_document_for_test': make_leap_document_for_test, 'sync_target': leap_sync_target}), ('oauth_http', {'make_app_with_state': make_oauth_http_app, - 'make_document_for_test': tests.make_document_for_test, + 'make_document_for_test': make_leap_document_for_test, 'sync_target': oauth_leap_sync_target}), ] + +#----------------------------------------------------------------------------- +# The following tests come from `u1db.tests.test_https`. +#----------------------------------------------------------------------------- + +def oauth_https_sync_target(test, host, path): + _, port = test.server.server_address + st = leap_backend.LeapSyncTarget('https://%s:%d/~/%s' % (host, port, path)) + st.set_oauth_credentials(tests.consumer1.key, tests.consumer1.secret, + tests.token1.key, tests.token1.secret) + return st + +class TestLeapSyncTargetHttpsSupport(TestHttpSyncTargetHttpsSupport, SoledadTest): + + scenarios = [ + ('oauth_https', {'server_def': https_server_def, + 'make_app_with_state': make_oauth_http_app, + 'make_document_for_test': make_leap_document_for_test, + 'sync_target': oauth_https_sync_target + }), + ] + load_tests = tests.load_with_scenarios -- cgit v1.2.3 From 8ee7ba49fd3ae902fd0a9d8a3a80b7b6a9ab999b Mon Sep 17 00:00:00 2001 From: drebs Date: Wed, 16 Jan 2013 11:04:42 -0200 Subject: Couch backend passes test_sync without indexes. --- src/leap/soledad/tests/test_leap_backend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/leap/soledad/tests/test_leap_backend.py') diff --git a/src/leap/soledad/tests/test_leap_backend.py b/src/leap/soledad/tests/test_leap_backend.py index 97de101f..0fa4033a 100644 --- a/src/leap/soledad/tests/test_leap_backend.py +++ b/src/leap/soledad/tests/test_leap_backend.py @@ -85,7 +85,7 @@ class SoledadTest(unittest.TestCase): #----------------------------------------------------------------------------- -# The following tests come from `u1db.tests.test_common_backends`. +# The following tests come from `u1db.tests.test_common_backend`. #----------------------------------------------------------------------------- class TestLeapBackendImpl(tests.TestCase): -- cgit v1.2.3 From 9fc4829334e337155ef49de168e492ed7cfe58d7 Mon Sep 17 00:00:00 2001 From: drebs Date: Tue, 22 Jan 2013 09:51:08 -0200 Subject: Cleanup tests. --- src/leap/soledad/tests/test_leap_backend.py | 64 ++++++++--------------------- 1 file changed, 18 insertions(+), 46 deletions(-) (limited to 'src/leap/soledad/tests/test_leap_backend.py') diff --git a/src/leap/soledad/tests/test_leap_backend.py b/src/leap/soledad/tests/test_leap_backend.py index 0fa4033a..f19eb360 100644 --- a/src/leap/soledad/tests/test_leap_backend.py +++ b/src/leap/soledad/tests/test_leap_backend.py @@ -4,12 +4,8 @@ For these tests to run, a leap server has to be running on (default) port 5984. """ -from shutil import rmtree import os -import copy import unittest2 as unittest -import testtools -import testscenarios import u1db from leap.soledad import Soledad from leap.soledad.backends import leap_backend @@ -18,41 +14,17 @@ from leap.soledad.tests.u1db_tests.test_remote_sync_target import ( make_http_app, make_oauth_http_app, ) -from leap.soledad.tests.u1db_tests.test_backends import AllDatabaseTests -from leap.soledad.tests.u1db_tests.test_http_database import ( - TestHTTPDatabaseSimpleOperations, - TestHTTPDatabaseCtrWithCreds, - TestHTTPDatabaseIntegration, -) -from leap.soledad.tests.u1db_tests.test_http_client import ( - TestHTTPClientBase, -) -from leap.soledad.tests.u1db_tests.test_document import ( - TestDocument, - TestPyDocument, -) -from leap.soledad.tests.u1db_tests.test_remote_sync_target import ( - TestHTTPSyncTargetBasics, - TestParsingSyncStream, -) -from leap.soledad.tests.u1db_tests.test_sync import ( - _make_local_db_and_target, - DatabaseSyncTargetTests, -) -from leap.soledad.tests.u1db_tests.test_https import ( - TestHttpSyncTargetHttpsSupport, - https_server_def, -) +from leap.soledad.tests.u1db_tests import test_backends +from leap.soledad.tests.u1db_tests import test_http_database +from leap.soledad.tests.u1db_tests import test_http_client +from leap.soledad.tests.u1db_tests import test_document +from leap.soledad.tests.u1db_tests import test_remote_sync_target +from leap.soledad.tests.u1db_tests import test_https from leap.soledad.tests.test_encrypted import ( PUBLIC_KEY, PRIVATE_KEY, - KEY_FINGERPRINT, ) -try: - import simplejson as json -except ImportError: - import json # noqa #----------------------------------------------------------------------------- # The EncryptedSyncTest is used with multiple inheritance to guarantee that we @@ -152,7 +124,7 @@ LEAP_SCENARIOS = [ ] -class LeapTests(AllDatabaseTests, SoledadTest): +class LeapTests(test_backends.AllDatabaseTests, SoledadTest): scenarios = LEAP_SCENARIOS @@ -161,10 +133,10 @@ class LeapTests(AllDatabaseTests, SoledadTest): # The following tests come from `u1db.tests.test_http_database`. #----------------------------------------------------------------------------- -class TestLeapDatabaseSimpleOperations(TestHTTPDatabaseSimpleOperations): +class TestLeapDatabaseSimpleOperations(test_http_database.TestHTTPDatabaseSimpleOperations): def setUp(self): - super(TestHTTPDatabaseSimpleOperations, self).setUp() + super(test_http_database.TestHTTPDatabaseSimpleOperations, self).setUp() self.db = leap_backend.LeapDatabase('dbase') self.db._conn = object() # crash if used self.got = None @@ -193,11 +165,11 @@ class TestLeapDatabaseSimpleOperations(TestHTTPDatabaseSimpleOperations): self.assertEqual(st._url, self.db._url) -class TestLeapDatabaseCtrWithCreds(TestHTTPDatabaseCtrWithCreds): +class TestLeapDatabaseCtrWithCreds(test_http_database.TestHTTPDatabaseCtrWithCreds): pass -class TestLeapDatabaseIntegration(TestHTTPDatabaseIntegration): +class TestLeapDatabaseIntegration(test_http_database.TestHTTPDatabaseIntegration): def test_non_existing_db(self): db = leap_backend.LeapDatabase(self.getURL('not-there')) @@ -252,7 +224,7 @@ class TestLeapDatabaseIntegration(TestHTTPDatabaseIntegration): # The following tests come from `u1db.tests.test_http_client`. #----------------------------------------------------------------------------- -class TestLeapClientBase(TestHTTPClientBase): +class TestLeapClientBase(test_http_client.TestHTTPClientBase): pass @@ -260,13 +232,13 @@ class TestLeapClientBase(TestHTTPClientBase): # The following tests come from `u1db.tests.test_document`. #----------------------------------------------------------------------------- -class TestLeapDocument(TestDocument, SoledadTest): +class TestLeapDocument(test_document.TestDocument, SoledadTest): scenarios = ([( 'leap', {'make_document_for_test': make_leap_document_for_test})]) -class TestLeapPyDocument(TestPyDocument, SoledadTest): +class TestLeapPyDocument(test_document.TestPyDocument, SoledadTest): scenarios = ([( 'leap', {'make_document_for_test': make_leap_document_for_test})]) @@ -276,7 +248,7 @@ class TestLeapPyDocument(TestPyDocument, SoledadTest): # The following tests come from `u1db.tests.test_remote_sync_target`. #----------------------------------------------------------------------------- -class TestLeapSyncTargetBasics(TestHTTPSyncTargetBasics): +class TestLeapSyncTargetBasics(test_remote_sync_target.TestHTTPSyncTargetBasics): def test_parse_url(self): remote_target = leap_backend.LeapSyncTarget('http://127.0.0.1:12345/') @@ -285,7 +257,7 @@ class TestLeapSyncTargetBasics(TestHTTPSyncTargetBasics): self.assertEqual(12345, remote_target._url.port) self.assertEqual('/', remote_target._url.path) -class TestLeapParsingSyncStream(TestParsingSyncStream): +class TestLeapParsingSyncStream(test_remote_sync_target.TestParsingSyncStream): def test_wrong_start(self): tgt = leap_backend.LeapSyncTarget("http://foo/foo") @@ -386,10 +358,10 @@ def oauth_https_sync_target(test, host, path): tests.token1.key, tests.token1.secret) return st -class TestLeapSyncTargetHttpsSupport(TestHttpSyncTargetHttpsSupport, SoledadTest): +class TestLeapSyncTargetHttpsSupport(test_https.TestHttpSyncTargetHttpsSupport, SoledadTest): scenarios = [ - ('oauth_https', {'server_def': https_server_def, + ('oauth_https', {'server_def': test_https.https_server_def, 'make_app_with_state': make_oauth_http_app, 'make_document_for_test': make_leap_document_for_test, 'sync_target': oauth_https_sync_target -- cgit v1.2.3