diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/__init__.py | 25 | ||||
-rwxr-xr-x | tests/units/db/routes.py | 258 | ||||
-rwxr-xr-x | tests/units/model/context.py | 16 | ||||
-rwxr-xr-x | tests/units/model/model.py | 47 | ||||
-rwxr-xr-x | tests/units/node/node.py | 6 | ||||
-rwxr-xr-x | tests/units/toolkit/http.py | 83 | ||||
-rwxr-xr-x | tests/units/toolkit/router.py | 41 | ||||
-rwxr-xr-x | tests/units/toolkit/toolkit.py | 11 |
8 files changed, 142 insertions, 345 deletions
diff --git a/tests/__init__.py b/tests/__init__.py index dea79f6..6a997e7 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -18,9 +18,9 @@ from M2Crypto import DSA from gevent import monkey from sugar_network.toolkit import coroutine, http, mountpoints, Option, gbus, i18n, languages -from sugar_network.toolkit.router import Router +from sugar_network.toolkit.router import Router, File from sugar_network.toolkit.coroutine import this -from sugar_network.db import files +from sugar_network.db import blobs from sugar_network.client import IPCConnection, journal, routes as client_routes from sugar_network.client.routes import ClientRoutes, _Auth from sugar_network import db, client, node, toolkit, model @@ -158,17 +158,17 @@ class Test(unittest.TestCase): os.makedirs('blobs') self.blobs = {} - def files_post(content, meta=None, digest_to_assert=None): + def files_post(content, mime_type=None, digest_to_assert=None): if hasattr(content, 'read'): content = content.read() - digest = files.Digest(hash(content)) + digest = File.Digest(hash(content)) if digest_to_assert: assert digest == digest_to_assert path = join('blobs', digest) with file(path, 'w') as f: f.write(content) - self.blobs[digest] = meta or {} - return toolkit.File(path, meta=meta, digest=digest) + self.blobs[digest] = {'content-type': mime_type or 'application/octet-stream'} + return File(path, digest, self.blobs[digest].items()) def files_update(digest, meta): self.blobs.setdefault(digest, {}).update(meta) @@ -176,11 +176,8 @@ class Test(unittest.TestCase): def files_get(digest): if digest not in self.blobs: return None - meta = toolkit.File(meta=self.blobs[digest]) path = join('blobs', digest) - if exists(path): - meta.path = path - return meta + return File(path, digest, self.blobs[digest].items()) def files_delete(digest): path = join('blobs', digest) @@ -189,10 +186,10 @@ class Test(unittest.TestCase): if digest in self.blobs: del self.blobs[digest] - self.override(files, 'post', files_post) - self.override(files, 'update', files_update) - self.override(files, 'get', files_get) - self.override(files, 'delete', files_delete) + self.override(blobs, 'post', files_post) + self.override(blobs, 'update', files_update) + self.override(blobs, 'get', files_get) + self.override(blobs, 'delete', files_delete) def stop_nodes(self): if self.client is not None: diff --git a/tests/units/db/routes.py b/tests/units/db/routes.py index 8824ca8..7da2f75 100755 --- a/tests/units/db/routes.py +++ b/tests/units/db/routes.py @@ -16,7 +16,7 @@ src_root = abspath(dirname(__file__)) from __init__ import tests from sugar_network import db, toolkit -from sugar_network.db import files +from sugar_network.db import blobs from sugar_network.model.user import User from sugar_network.toolkit.router import Router, Request, Response, fallbackroute, ACL from sugar_network.toolkit.coroutine import this @@ -195,17 +195,28 @@ class RoutesTest(tests.Test): self.assertRaises(http.NotFound, this.call, method='GET', path=['testdocument', guid, 'blob']) self.assertRaises(http.BadRequest, this.call, method='PUT', path=['testdocument', guid, 'blob'], - content={'url': 'foo'}, content_type='application/json') + content={'location': 'foo'}, content_type='application/json') self.assertRaises(http.NotFound, this.call, method='GET', path=['testdocument', guid, 'blob']) this.call(method='PUT', path=['testdocument', guid, 'blob'], - content={'url': 'url', 'digest': 'digest', 'foo': 'bar'}, content_type='application/json') - self.assertEqual({ - 'mime_type': 'application/octet-stream', - 'url': 'url', - 'foo': 'bar', - }, - this.call(method='GET', path=['testdocument', guid, 'blob'])) + content={'location': 'url', 'digest': 'digest', 'foo': 'bar', 'content-type': 'foo/bar'}, content_type='application/json') + self.assertEqual( + {'blob': 'url'}, + this.call(method='GET', path=['testdocument', guid], reply='blob')) + response = [] + [i for i in router({ + 'REQUEST_METHOD': 'HEAD', + 'PATH_INFO': '/testdocument/%s/blob' % guid, + }, lambda status, headers: response.extend([status, headers]))] + self.assertEqual('303 See Other', response[0]) + self.assertEqual( + sorted([ + ('last-modified', formatdate(os.stat('testdocument/%s/%s/mtime' % (guid[:2], guid)).st_mtime, localtime=False, usegmt=True)), + ('location', 'url'), + ('content-type', 'foo/bar'), + ('foo', 'bar'), + ]), + sorted(response[1])) def test_UpdateUrlBLOBsWithMeta(self): @@ -218,26 +229,26 @@ class RoutesTest(tests.Test): volume = db.Volume(tests.tmpdir, [TestDocument]) router = Router(db.Routes(volume)) - guid = this.call(method='POST', path=['testdocument'], content={'blob': {'digest': 'digest', 'url': 'url'}}) + guid = this.call(method='POST', path=['testdocument'], content={'blob': {'digest': 'digest', 'location': 'url'}}) self.assertEqual({ - 'mime_type': 'application/octet-stream', - 'url': 'url', + 'content-type': 'application/octet-stream', + 'location': 'url', }, this.call(method='GET', path=['testdocument', guid, 'blob'])) self.assertRaises(http.BadRequest, this.call, method='PUT', path=['testdocument', guid, 'blob'], content={'digest': 'fake'}, content_type='application/json') self.assertEqual({ - 'mime_type': 'application/octet-stream', - 'url': 'url', + 'content-type': 'application/octet-stream', + 'location': 'url', }, this.call(method='GET', path=['testdocument', guid, 'blob'])) this.call(method='PUT', path=['testdocument', guid, 'blob'], content={'foo': 'bar'}, content_type='application/json') self.assertEqual({ - 'mime_type': 'application/octet-stream', - 'url': 'url', + 'content-type': 'application/octet-stream', + 'location': 'url', 'foo': 'bar', }, this.call(method='GET', path=['testdocument', guid, 'blob'])) @@ -256,11 +267,7 @@ class RoutesTest(tests.Test): guid = this.call(method='POST', path=['testdocument'], content={'blob': 'blob'}) blob = this.call(method='GET', path=['testdocument', guid, 'blob'], environ={'HTTP_HOST': 'localhost'}) self.assertEqual({ - 'mime_type': 'application/octet-stream', - 'size': os.stat(blob.path).st_size, - 'mtime': int(os.stat(blob.path).st_mtime), - 'url': 'http://localhost/blobs/%s' % hash('blob'), - 'digest': str(hash('blob')), + 'content-type': 'application/octet-stream', }, blob) self.assertEqual('blob', file(blob.path).read()) @@ -269,11 +276,7 @@ class RoutesTest(tests.Test): content={'digest': 'fake'}, content_type='application/json') blob = this.call(method='GET', path=['testdocument', guid, 'blob'], environ={'HTTP_HOST': 'localhost'}) self.assertEqual({ - 'mime_type': 'application/octet-stream', - 'size': os.stat(blob.path).st_size, - 'mtime': int(os.stat(blob.path).st_mtime), - 'digest': str(hash('blob')), - 'url': 'http://localhost/blobs/%s' % hash('blob'), + 'content-type': 'application/octet-stream', }, blob) self.assertEqual('blob', file(blob.path).read()) @@ -282,11 +285,7 @@ class RoutesTest(tests.Test): content={'foo': 'bar'}, content_type='application/json') blob = this.call(method='GET', path=['testdocument', guid, 'blob'], environ={'HTTP_HOST': 'localhost'}) self.assertEqual({ - 'mime_type': 'application/octet-stream', - 'size': os.stat(blob.path).st_size, - 'mtime': int(os.stat(blob.path).st_mtime), - 'digest': str(hash('blob')), - 'url': 'http://localhost/blobs/%s' % hash('blob'), + 'content-type': 'application/octet-stream', 'foo': 'bar', }, blob) @@ -302,41 +301,35 @@ class RoutesTest(tests.Test): volume = db.Volume(tests.tmpdir, [TestDocument]) router = Router(db.Routes(volume)) - guid = this.call(method='POST', path=['testdocument'], content={'blob': 'blob'}) - this.call(method='PUT', path=['testdocument', guid, 'blob'], - content={'foo': 'bar'}, content_type='application/json') - file_blob = this.call(method='GET', path=['testdocument', guid, 'blob'], environ={'HTTP_HOST': 'localhost'}) - self.assertEqual({ - 'mime_type': 'application/octet-stream', - 'size': os.stat(file_blob.path).st_size, - 'mtime': int(os.stat(file_blob.path).st_mtime), - 'digest': str(hash('blob')), - 'url': 'http://localhost/blobs/%s' % hash('blob'), - 'foo': 'bar', - }, file_blob) - self.assertEqual('blob', file(file_blob.path).read()) + self.assertEqual( + {'blob': 'http://localhost/blobs/%s' % hash('blob')}, + this.call(method='GET', path=['testdocument', guid], reply='blob', environ={'HTTP_HOST': 'localhost'})) + self.assertEqual( + ['blob'], + [i for i in router({ + 'REQUEST_METHOD': 'GET', + 'PATH_INFO': '/testdocument/%s/blob' % guid, + }, lambda *args: None)]) + assert exists(this.call(method='GET', path=['testdocument', guid, 'blob']).path) this.call(method='PUT', path=['testdocument', guid, 'blob'], - content={'url': 'url'}, content_type='application/json') - self.assertEqual({ - 'mime_type': 'application/octet-stream', - 'url': 'url', - 'foo': 'bar', - }, this.call(method='GET', path=['testdocument', guid, 'blob'])) - assert not exists(file_blob.path) + content={'location': 'url'}, content_type='application/json') + self.assertEqual( + {'blob': 'url'}, + this.call(method='GET', path=['testdocument', guid], reply='blob', environ={'HTTP_HOST': 'localhost'})) + assert not exists(this.call(method='GET', path=['testdocument', guid, 'blob']).path) this.call(method='PUT', path=['testdocument', guid, 'blob'], - content='blob', content_type='application/octet-stream', environ={'HTTP_HOST': 'localhost'}) - self.assertEqual({ - 'mime_type': 'application/octet-stream', - 'size': os.stat(file_blob.path).st_size, - 'mtime': int(os.stat(file_blob.path).st_mtime), - 'digest': str(hash('blob')), - 'url': 'http://localhost/blobs/%s' % hash('blob'), - }, this.call(method='GET', path=['testdocument', guid, 'blob'], environ={'HTTP_HOST': 'localhost'})) - self.assertEqual('blob', file(file_blob.path).read()) + content='new_blob', content_type='application/octet-stream', environ={'HTTP_HOST': 'localhost'}) + self.assertEqual( + ['new_blob'], + [i for i in router({ + 'REQUEST_METHOD': 'GET', + 'PATH_INFO': '/testdocument/%s/blob' % guid, + }, lambda *args: None)]) + assert exists(this.call(method='GET', path=['testdocument', guid, 'blob']).path) def test_RemoveBLOBs(self): @@ -412,21 +405,23 @@ class RoutesTest(tests.Test): router = Router(db.Routes(volume)) guid = this.call(method='POST', path=['testdocument'], content={}) - response = Response() - this.call(response=response, - method='PUT', path=['testdocument', guid, 'blob'], content='blob1') - response = Response() - self.assertEqual('default', this.call(response=response, - method='GET', path=['testdocument', guid, 'blob'])['mime_type']) - self.assertEqual('default', response.content_type) - - response = Response() - this.call(response=response, - method='PUT', path=['testdocument', guid, 'blob'], content='blob1', content_type='foo') - response = Response() - self.assertEqual('foo', this.call(response=response, - method='GET', path=['testdocument', guid, 'blob'])['mime_type']) - self.assertEqual('foo', response.content_type) + this.call(method='PUT', path=['testdocument', guid, 'blob'], content='blob1') + response = [] + [i for i in router({ + 'REQUEST_METHOD': 'GET', + 'PATH_INFO': '/testdocument/%s/blob' % guid, + }, lambda status, headers: response.extend([status, headers]))] + self.assertEqual('200 OK', response[0]) + self.assertEqual('default', dict(response[1]).get('content-type')) + + this.call(method='PUT', path=['testdocument', guid, 'blob'], content='blob1', content_type='foo') + response = [] + [i for i in router({ + 'REQUEST_METHOD': 'GET', + 'PATH_INFO': '/testdocument/%s/blob' % guid, + }, lambda status, headers: response.extend([status, headers]))] + self.assertEqual('200 OK', response[0]) + self.assertEqual('foo', dict(response[1]).get('content-type')) def test_GetBLOBs(self): @@ -448,24 +443,12 @@ class RoutesTest(tests.Test): self.assertEqual('blob', file(this.call(method='GET', path=['testdocument', guid, 'blob']).path).read()) self.assertEqual({ - 'blob': { - 'mime_type': u'application/octet-stream', - 'url': 'http://localhost/blobs/%s' % digest, - 'size': len(blob), - 'digest': digest, - 'mtime': int(os.stat(blob_path).st_mtime), - }, + 'blob': 'http://localhost/blobs/%s' % digest, }, this.call(method='GET', path=['testdocument', guid], reply=['blob'], environ={'HTTP_HOST': 'localhost'})) self.assertEqual([{ - 'blob': { - 'mime_type': u'application/octet-stream', - 'url': 'http://localhost/blobs/%s' % digest, - 'size': len(blob), - 'digest': digest, - 'mtime': int(os.stat(blob_path).st_mtime), - }, + 'blob': 'http://localhost/blobs/%s' % digest, }], this.call(method='GET', path=['testdocument'], reply=['blob'], environ={'HTTP_HOST': 'localhost'})['result']) @@ -483,30 +466,30 @@ class RoutesTest(tests.Test): self.assertRaises(http.NotFound, this.call, method='GET', path=['testdocument', guid1, 'blob']) self.assertEqual( - {'blob': {}}, + {'blob': ''}, this.call(method='GET', path=['testdocument', guid1], reply=['blob'], environ={'HTTP_HOST': '127.0.0.1'})) blob = 'file' guid2 = this.call(method='POST', path=['testdocument'], content={'blob': blob}) self.assertEqual( 'http://127.0.0.1/blobs/%s' % hash(blob), - this.call(method='GET', path=['testdocument', guid2], reply=['blob'], environ={'HTTP_HOST': '127.0.0.1'})['blob']['url']) + this.call(method='GET', path=['testdocument', guid2], reply=['blob'], environ={'HTTP_HOST': '127.0.0.1'})['blob']) - guid3 = this.call(method='POST', path=['testdocument'], content={'blob': {'url': 'http://foo', 'digest': 'digest'}}, content_type='application/json') + guid3 = this.call(method='POST', path=['testdocument'], content={'blob': {'location': 'http://foo', 'digest': 'digest'}}, content_type='application/json') self.assertEqual( 'http://foo', - this.call(method='GET', path=['testdocument', guid3, 'blob'])['url']) + this.call(method='GET', path=['testdocument', guid3, 'blob'])['location']) self.assertEqual( 'http://foo', - this.call(method='GET', path=['testdocument', guid3], reply=['blob'], environ={'HTTP_HOST': '127.0.0.1'})['blob']['url']) + this.call(method='GET', path=['testdocument', guid3], reply=['blob'], environ={'HTTP_HOST': '127.0.0.1'})['blob']) self.assertEqual( sorted([ - None, + '', 'http://127.0.0.1/blobs/%s' % hash(blob), 'http://foo', ]), - sorted([i['blob'].get('url') for i in this.call(method='GET', path=['testdocument'], reply=['blob'], + sorted([i['blob'] for i in this.call(method='GET', path=['testdocument'], reply=['blob'], environ={'HTTP_HOST': '127.0.0.1'})['result']])) def test_CommandsGetAbsentBlobs(self): @@ -523,7 +506,7 @@ class RoutesTest(tests.Test): guid = this.call(method='POST', path=['testdocument'], content={}) self.assertRaises(http.NotFound, this.call, method='GET', path=['testdocument', guid, 'blob']) self.assertEqual( - {'blob': {}}, + {'blob': ''}, this.call(method='GET', path=['testdocument', guid], reply=['blob'], environ={'HTTP_HOST': 'localhost'})) def test_Command_ReplyForGET(self): @@ -740,11 +723,11 @@ class RoutesTest(tests.Test): guid = this.call(method='POST', path=['testdocument'], content={}) this.call(method='PUT', path=['testdocument', guid, 'blob'], content={ 'digest': 'digest', - 'url': 'http://sugarlabs.org', + 'location': 'http://sugarlabs.org', }, content_type='application/json') self.assertEqual( 'http://sugarlabs.org', - this.call(method='GET', path=['testdocument', guid, 'blob'])['url']) + this.call(method='GET', path=['testdocument', guid, 'blob'])['location']) def test_on_create(self): @@ -1231,65 +1214,6 @@ class RoutesTest(tests.Test): 'default', this.call(method='GET', path=['testdocument', guid, 'prop'])) - def test_prop_meta(self): - files.update('url', {'url': 'http://new', 'foo': 'bar', 'size': 100}) - - class TestDocument(db.Resource): - - @db.indexed_property(slot=1, default='') - def prop(self, value): - return value - - @db.stored_property(db.Blob) - def blob1(self, value): - return value - - @db.stored_property(db.Blob) - def blob2(self, value): - return value - - @blob2.setter - def blob2(self, value): - return 'url' - - volume = db.Volume(tests.tmpdir, [TestDocument]) - router = Router(db.Routes(volume)) - guid = this.call(method='POST', path=['testdocument'], content = {'prop': 'prop', 'blob1': 'blob', 'blob2': ''}) - - response = Response() - assert this.call(response=response, - method='HEAD', path=['testdocument', guid, 'prop']) is None - meta = volume['testdocument'].get(guid).meta('prop') - meta.pop('value') - self.assertEqual(meta, response.meta) - self.assertEqual(formatdate(meta['mtime'], localtime=False, usegmt=True), response.last_modified) - - response = Response() - assert this.call(response=response, - method='HEAD', path=['testdocument', guid, 'blob1'], environ={'HTTP_HOST': 'localhost'}) is None - meta = volume['testdocument'].get(guid).meta('blob1') - meta.pop('value') - self.assertEqual(meta, response.meta) - self.assertEqual(len('blob'), response.content_length) - self.assertEqual(formatdate(meta['mtime'], localtime=False, usegmt=True), response.last_modified) - - response = Response() - assert this.call(response=response, - method='HEAD', path=['testdocument', guid, 'blob2']) is None - meta = volume['testdocument'].get(guid).meta('blob2') - meta.pop('value') - self.assertEqual(meta, response.meta) - self.assertEqual(100, response.content_length) - self.assertEqual(formatdate(meta['mtime'], localtime=False, usegmt=True), response.last_modified) - - response = Response() - assert this.call(response=response, - method='GET', path=['testdocument', guid, 'blob2']) is not None - meta = volume['testdocument'].get(guid).meta('blob2') - meta.pop('value') - self.assertEqual(meta, response.meta) - self.assertEqual(formatdate(meta['mtime'], localtime=False, usegmt=True), response.last_modified) - def test_DefaultAuthor(self): class User(db.Resource): @@ -1888,7 +1812,7 @@ class RoutesTest(tests.Test): agg1: {'seqno': 2, 'value': str(hash('blob1'))}, }, volume['document'].get(guid)['blobs']) - assert files.get(str(hash('blob1'))) + assert blobs.get(str(hash('blob1'))) agg2 = this.call(method='POST', path=['document', guid, 'blobs'], content='blob2') self.assertEqual({ @@ -1896,7 +1820,7 @@ class RoutesTest(tests.Test): agg2: {'seqno': 3, 'value': str(hash('blob2'))}, }, volume['document'].get(guid)['blobs']) - assert files.get(str(hash('blob2'))) + assert blobs.get(str(hash('blob2'))) this.call(method='DELETE', path=['document', guid, 'blobs', agg1]) self.assertEqual({ @@ -1904,8 +1828,8 @@ class RoutesTest(tests.Test): agg2: {'seqno': 3, 'value': str(hash('blob2'))}, }, volume['document'].get(guid)['blobs']) - assert files.get(str(hash('blob1'))) is None - assert files.get(str(hash('blob2'))) + assert blobs.get(str(hash('blob1'))) is None + assert blobs.get(str(hash('blob2'))) this.call(method='DELETE', path=['document', guid, 'blobs', agg2]) self.assertEqual({ @@ -1913,8 +1837,8 @@ class RoutesTest(tests.Test): agg2: {'seqno': 5}, }, volume['document'].get(guid)['blobs']) - assert files.get(str(hash('blob1'))) is None - assert files.get(str(hash('blob2'))) is None + assert blobs.get(str(hash('blob1'))) is None + assert blobs.get(str(hash('blob2'))) is None agg3 = this.call(method='POST', path=['document', guid, 'blobs'], content='blob3') self.assertEqual({ @@ -1923,9 +1847,9 @@ class RoutesTest(tests.Test): agg3: {'seqno': 6, 'value': str(hash('blob3'))}, }, volume['document'].get(guid)['blobs']) - assert files.get(str(hash('blob1'))) is None - assert files.get(str(hash('blob2'))) is None - assert files.get(str(hash('blob3'))) + assert blobs.get(str(hash('blob1'))) is None + assert blobs.get(str(hash('blob2'))) is None + assert blobs.get(str(hash('blob3'))) def test_AggregatedSearch(self): diff --git a/tests/units/model/context.py b/tests/units/model/context.py index ef505cc..8fd5b56 100755 --- a/tests/units/model/context.py +++ b/tests/units/model/context.py @@ -7,7 +7,7 @@ from os.path import exists from __init__ import tests from sugar_network import db -from sugar_network.db import files +from sugar_network.db import blobs from sugar_network.client import IPCConnection, Connection, keyfile from sugar_network.model.context import Context from sugar_network.toolkit.coroutine import this @@ -86,7 +86,7 @@ class ContextTest(tests.Test): }, }, }, conn.get(['context', context, 'releases'])) - assert files.get(str(hash(bundle1))) + assert blobs.get(str(hash(bundle1))) activity_info2 = '\n'.join([ '[Activity]', @@ -130,8 +130,8 @@ class ContextTest(tests.Test): }, }, }, conn.get(['context', context, 'releases'])) - assert files.get(str(hash(bundle1))) - assert files.get(str(hash(bundle2))) + assert blobs.get(str(hash(bundle1))) + assert blobs.get(str(hash(bundle2))) conn.delete(['context', context, 'releases', release1]) self.assertEqual({ @@ -154,8 +154,8 @@ class ContextTest(tests.Test): }, }, }, conn.get(['context', context, 'releases'])) - assert files.get(str(hash(bundle1))) is None - assert files.get(str(hash(bundle2))) + assert blobs.get(str(hash(bundle1))) is None + assert blobs.get(str(hash(bundle2))) conn.delete(['context', context, 'releases', release2]) self.assertEqual({ @@ -168,8 +168,8 @@ class ContextTest(tests.Test): 'author': {tests.UID: {'name': tests.UID, 'order': 0, 'role': 3}}, }, }, conn.get(['context', context, 'releases'])) - assert files.get(str(hash(bundle1))) is None - assert files.get(str(hash(bundle2))) is None + assert blobs.get(str(hash(bundle1))) is None + assert blobs.get(str(hash(bundle2))) is None def test_IncrementReleasesSeqnoOnNewReleases(self): events = [] diff --git a/tests/units/model/model.py b/tests/units/model/model.py index f3e4442..28d8244 100755 --- a/tests/units/model/model.py +++ b/tests/units/model/model.py @@ -3,11 +3,12 @@ # sugar-lint: disable import base64 +import mimetypes from __init__ import tests from sugar_network import db -from sugar_network.db import files +from sugar_network.db import blobs from sugar_network.model import load_bundle from sugar_network.model.post import Post from sugar_network.client import IPCConnection, Connection, keyfile @@ -64,15 +65,15 @@ class ModelTest(tests.Test): ('topdir/activity/activity.info', activity_info), ('topdir/CHANGELOG', changelog), ) - blob = files.post(bundle) + blob = blobs.post(bundle) this.request = Request(method='POST', path=['context', 'bundle_id'], principal=tests.UID) context, release = load_bundle(blob, 'bundle_id') self.assertEqual({ - 'mime_type': 'application/vnd.olpc-sugar', - 'name': 'Activity-1', - }, files.get(blob.digest)) + 'content-type': 'application/vnd.olpc-sugar', + 'content-disposition': 'attachment; filename="Activity-1%s"' % (mimetypes.guess_extension('application/vnd.olpc-sugar') or ''), + }, blobs.get(blob.digest)) self.assertEqual('bundle_id', context) self.assertEqual([[1], 0], release['version']) self.assertEqual('developer', release['stability']) @@ -113,16 +114,16 @@ class ModelTest(tests.Test): 'description': 'description', }) bundle = 'non-activity' - blob = files.post(bundle) + blob = blobs.post(bundle) + blob['content-type'] = 'application/pdf' - this.request = Request(method='POST', path=['context', 'bundle_id'], principal=tests.UID, - content_type = 'content/type', version='2', license='GPL') + this.request = Request(method='POST', path=['context', 'bundle_id'], principal=tests.UID, version='2', license='GPL') context, release = load_bundle(blob, 'bundle_id') self.assertEqual({ - 'mime_type': 'content/type', - 'name': 'NonActivity-2', - }, files.get(blob.digest)) + 'content-type': 'application/pdf', + 'content-disposition': 'attachment; filename="NonActivity-2.pdf"', + }, blobs.get(blob.digest)) self.assertEqual('bundle_id', context) self.assertEqual([[2], 0], release['version']) self.assertEqual(['GPL'], release['license']) @@ -160,7 +161,7 @@ class ModelTest(tests.Test): 'activity_version = 1', ]) bundle = self.zips(('topdir/activity/activity.info', activity_info_wo_license)) - blob_wo_license = files.post(bundle) + blob_wo_license = blobs.post(bundle) self.assertRaises(http.BadRequest, load_bundle, blob_wo_license, 'bundle_id') volume['context'].update('bundle_id', {'releases': { @@ -199,7 +200,7 @@ class ModelTest(tests.Test): 'description': 'description', }) - blob = files.post('non-activity') + blob = blobs.post('non-activity') this.request = Request(method='POST', path=['context', 'bundle_id'], principal=tests.UID, version='1') self.assertRaises(http.BadRequest, load_bundle, blob, 'bundle_id') @@ -239,7 +240,7 @@ class ModelTest(tests.Test): 'description': 'description', }) - blob = files.post('non-activity') + blob = blobs.post('non-activity') this.request = Request(method='POST', path=['context', 'bundle_id'], principal=tests.UID, version='2', license='GPL') self.assertRaises(http.BadRequest, load_bundle, blob, 'bundle_id') @@ -259,7 +260,7 @@ class ModelTest(tests.Test): ('topdir/activity/activity.info', activity_info), ('topdir/CHANGELOG', changelog), ) - blob = files.post(bundle) + blob = blobs.post(bundle) self.assertRaises(http.BadRequest, load_bundle, blob, 'bundle_id') def test_load_bundle_MissedContext(self): @@ -278,7 +279,7 @@ class ModelTest(tests.Test): 'stability = developer', 'requires = sugar>=0.88; dep' ]))) - blob = files.post(bundle) + blob = blobs.post(bundle) this.request = Request(principal=tests.UID) self.assertRaises(http.NotFound, load_bundle, blob, initial=False) @@ -304,7 +305,7 @@ class ModelTest(tests.Test): ])), ('ImageViewer.activity/activity/activity-imageviewer.svg', ''), ) - blob = files.post(bundle) + blob = blobs.post(bundle) this.request = Request(principal=tests.UID) context, release = load_bundle(blob, initial=True) @@ -377,7 +378,7 @@ class ModelTest(tests.Test): ('ImageViewer.activity/activity/activity-imageviewer.svg', svg), ) - blob = files.post(bundle) + blob = blobs.post(bundle) this.request = Request(method='POST', path=['context', 'org.laptop.ImageViewerActivity'], principal=tests.UID) context, release = load_bundle(blob, initial=True) @@ -395,7 +396,7 @@ class ModelTest(tests.Test): 'en': 'It has features one would expect of a standard image viewer, like zoom, rotate, etc.', }, context['description']) - self.assertEqual(svg, file(files.get(context['artifact_icon']).path).read()) + self.assertEqual(svg, file(blobs.get(context['artifact_icon']).path).read()) assert context['icon'] != 'missing.png' assert context['logo'] != 'missing-logo.png' self.assertEqual('http://wiki.sugarlabs.org/go/Activities/Image_Viewer', context['homepage']) @@ -425,7 +426,7 @@ class ModelTest(tests.Test): 'license = Public Domain', 'stability = developer', ]))) - blob = files.post(bundle) + blob = blobs.post(bundle) this.request = Request(method='POST', path=['context', 'bundle_id'], principal=tests.UID2) context, release = load_bundle(blob, 'bundle_id') @@ -443,8 +444,8 @@ class ModelTest(tests.Test): 'fr': 'Activity 1 third-party release', }, post['title']) - files.delete(blob.digest) - blob = files.post(bundle) + blobs.delete(blob.digest) + blob = blobs.post(bundle) this.request = Request(method='POST', path=['context', 'bundle_id'], principal=tests.UID) context, release = load_bundle(blob, 'bundle_id') @@ -486,7 +487,7 @@ class ModelTest(tests.Test): ])), ('ImageViewer.activity/activity/activity-imageviewer.svg', ''), ) - blob = files.post(bundle) + blob = blobs.post(bundle) this.request = Request(method='POST', path=['context', 'bundle_id'], principal=tests.UID) context, release = load_bundle(blob, 'bundle_id') diff --git a/tests/units/node/node.py b/tests/units/node/node.py index 9b1f702..025a0c0 100755 --- a/tests/units/node/node.py +++ b/tests/units/node/node.py @@ -722,12 +722,6 @@ class NodeTest(tests.Test): response = Response() reply = conn.call(Request(method='GET', path=['context', 'activity'], cmd='clone'), response) assert activity_blob == reply.read() - self.assertEqual({ - 'commands': {'activity': {'exec': 'true'}}, - 'files': {'activity': activity_file, 'dep': dep_file}, - 'packages': {'package': ['package.bin']}, - }, - response.meta) def test_AggpropInsertAccess(self): diff --git a/tests/units/toolkit/http.py b/tests/units/toolkit/http.py index d21af37..2ac3cab 100755 --- a/tests/units/toolkit/http.py +++ b/tests/units/toolkit/http.py @@ -107,89 +107,6 @@ class HTTPTest(tests.Test): }) self.assertEqual('result', json.load(client.call(request))) - def test_call_ReturnMeta(self): - - class Commands(object): - - @route('HEAD') - def f1(self, response): - response.meta['str'] = 'str' - response.meta['bool'] = True - response.meta['int'] = -1 - - @route('POST') - def f2(self): - response.meta['str'] = 'STR' - response.meta['bool'] = False - response.meta['int'] = 1 - - @route('GET') - def f3(self): - response.meta['str'] = 'FOO' - response.meta['bool'] = True - response.meta['int'] = 10 - - server = coroutine.WSGIServer(('127.0.0.1', local.ipc_port.value), Router(Commands())) - coroutine.spawn(server.serve_forever) - coroutine.dispatch() - conn = http.Connection('http://127.0.0.1:%s' % local.ipc_port.value) - - request = Request({ - 'REQUEST_METHOD': 'HEAD', - 'PATH_INFO': '/', - }) - response = Response() - conn.call(request, response) - self.assertEqual({ - 'int': -1, - 'bool': True, - 'str': 'str', - }, response.meta) - - response = Response() - conn.call(Request(method='POST'), response) - self.assertEqual({ - 'int': 1, - 'bool': False, - 'str': 'STR', - }, response.meta) - - response = Response() - conn.call(Request(method='GET'), response) - self.assertEqual({ - 'int': 10, - 'bool': True, - 'str': 'FOO', - }, response.meta) - - def test_call_ReturnMetaOnRedirects(self): - - class Front(object): - - @route('GET') - def get(self, response): - response.meta['front'] = 'value1' - raise http.Redirect('http://127.0.0.1:%s' % (local.ipc_port.value + 1)) - - server = coroutine.WSGIServer(('127.0.0.1', local.ipc_port.value), Router(Front())) - coroutine.spawn(server.serve_forever) - - class Back(object): - - @route('GET') - def get(self, response): - response.meta['back'] = 'value2' - - server = coroutine.WSGIServer(('127.0.0.1', local.ipc_port.value + 1), Router(Back())) - coroutine.spawn(server.serve_forever) - - conn = http.Connection('http://127.0.0.1:%s' % local.ipc_port.value) - coroutine.dispatch() - - response = Response() - stream = conn.call(Request(method='GET'), response) - self.assertEqual({'front': 'value1', 'back': 'value2'}, response.meta) - if __name__ == '__main__': tests.main() diff --git a/tests/units/toolkit/router.py b/tests/units/toolkit/router.py index 3dd1306..7d8af39 100755 --- a/tests/units/toolkit/router.py +++ b/tests/units/toolkit/router.py @@ -10,7 +10,7 @@ from cStringIO import StringIO from __init__ import tests, src_root from sugar_network import db, client, toolkit -from sugar_network.toolkit.router import Router, Request, _parse_accept_language, route, fallbackroute, preroute, postroute +from sugar_network.toolkit.router import Router, Request, _parse_accept_language, route, fallbackroute, preroute, postroute, File from sugar_network.toolkit.coroutine import this from sugar_network.toolkit import http, coroutine @@ -984,8 +984,8 @@ class RouterTest(tests.Test): lambda status, headers: response.extend([status, dict(headers)])) self.assertEqual('', ''.join([i for i in reply])) self.assertEqual([ - '001 Status', - {'X-SN-error': '"Status-Error"'}, + '001 Status-Error', + {}, ], response) @@ -1025,7 +1025,7 @@ class RouterTest(tests.Test): @route('GET') def get(self, response): - return toolkit.File(meta={'url': URL}) + return File(None, meta=[('location', URL)]) router = Router(CommandsProcessor()) @@ -1226,13 +1226,9 @@ class RouterTest(tests.Test): class CommandsProcessor(object): - @route('GET', [], '1') - def cmd1(self, request): - return toolkit.File('blob.data', {'name': 'foo', 'mime_type': 'application/octet-stream'}) - - @route('GET', [], cmd='2') - def cmd2(self, request): - return toolkit.File('blob.data', {'filename': 'foo.bar'}) + @route('GET', []) + def probe(self, request): + return File('blob.data', meta=[('content-disposition', 'attachment; filename="foo.bar"')]) router = Router(CommandsProcessor()) @@ -1240,27 +1236,6 @@ class RouterTest(tests.Test): reply = router({ 'PATH_INFO': '/', 'REQUEST_METHOD': 'GET', - 'QUERY_STRING': 'cmd=1', - }, - lambda status, headers: response.extend([status, dict(headers)])) - result = 'value' - self.assertEqual(result, ''.join([i for i in reply])) - self.assertEqual([ - '200 OK', - { - 'last-modified': formatdate(os.stat('blob.data').st_mtime, localtime=False, usegmt=True), - 'content-length': str(len(result)), - 'content-type': 'application/octet-stream', - 'content-disposition': 'attachment; filename="foo.obj"', - } - ], - response) - - response = [] - reply = router({ - 'PATH_INFO': '/', - 'REQUEST_METHOD': 'GET', - 'QUERY_STRING': 'cmd=2', }, lambda status, headers: response.extend([status, dict(headers)])) result = 'value' @@ -1268,9 +1243,7 @@ class RouterTest(tests.Test): self.assertEqual([ '200 OK', { - 'last-modified': formatdate(os.stat('blob.data').st_mtime, localtime=False, usegmt=True), 'content-length': str(len(result)), - 'content-type': 'application/octet-stream', 'content-disposition': 'attachment; filename="foo.bar"', } ], diff --git a/tests/units/toolkit/toolkit.py b/tests/units/toolkit/toolkit.py index 07ed9c6..87f0c2f 100755 --- a/tests/units/toolkit/toolkit.py +++ b/tests/units/toolkit/toolkit.py @@ -8,7 +8,7 @@ from cStringIO import StringIO from __init__ import tests from sugar_network import toolkit -from sugar_network.toolkit import Seqno, Sequence, File +from sugar_network.toolkit import Seqno, Sequence class UtilTest(tests.Test): @@ -421,15 +421,6 @@ class UtilTest(tests.Test): ['d', 'a', 'b', 'c'], [i for i in stack]) - def test_FileName(self): - self.assertEqual('blob', File().name) - self.assertEqual('blob', File('foo/bar').name) - self.assertEqual('digest', File(digest='digest').name) - self.assertEqual('foo', File(meta={'filename': 'foo'}).name) - self.assertEqual('foo', File(meta={'name': 'foo'}).name) - self.assertEqual('foo', File(meta={'filename': 'foo', 'mime_type': 'image/png'}).name) - self.assertEqual('digest.png', File(digest='digest', meta={'mime_type': 'image/png'}).name) - if __name__ == '__main__': tests.main() |