Example #1
0
    def test_can_get_as_objid(self):
        from exhibitionist.decorators import http_handler
        import tornado.web
        import requests

        registry = ObjectRegistry()
        basket = []

        @http_handler(r'/{{objid}}', __registry=registry)
        class Handler(ExhibitionistRequestHandler):
            def get(self, *args, **kwds):
                basket.append(context.object)
                self.write("")

        o = object()
        k = registry.register(o)
        self.server.add_handler(Handler)
        self.server.start()
        self.assertEqual(len(basket), 0)

        url = self.server.get_view_url("Handler", k, __registry=registry)
        r = requests.get(url)
        self.assertEqual(r.status_code, 200)

        self.assertTrue(registry.register(o) in url)
        self.assertTrue(k in url)
Example #2
0
    def test_can_use_hash_prefix(self):
        from exhibitionist.decorators import MIN_OBJID_LEN
        class A(object):
            pass

        # keys shorter then MIN_OBJID_LEN are ignored
        r = ObjectRegistry(MIN_OBJID_LEN)
        a = A()
        key = r.register(a)
        for i in range(1, MIN_OBJID_LEN):
            self.assertEqual(r.get(key[:i]), None)

        for i in range(MIN_OBJID_LEN, len(key)):
            self.assertEqual(id(r.get(key[:i])), id(a))
Example #3
0
    def test_use_short_keys(self):

        r=ObjectRegistry(min_objid_len=8,use_short_keys=True)
        HASH_LEN =len(r.hash_obj(object())) # should be 40 for sha1
        self.assertEqual(len(r.register(object())), 8)

        r=ObjectRegistry(min_objid_len=7,use_short_keys=True)
        self.assertEqual(len(r.register(object())), 7)

        r=ObjectRegistry(min_objid_len=7,use_short_keys=False)
        self.assertEqual(len(r.register(object())), HASH_LEN)

        # must use full key when short_keys=False
        self.assertEqual(r.get(r.register(object())[:-1]), None)
Example #4
0
    def test_all_verbs_auto_obj_and_context_injection(self):
        import tornado.web

        registry = ObjectRegistry()

        @http_handler(OBJID_PLACEHOLDER, __registry=registry,__test=True)
        class A(object):
            def get(self, objid, *args, **kwds):
                assert context.object == o
                pass

            def post(self, objid, *args, **kwds):
                assert context.object == o
                pass

            def put(self, objid, *args, **kwds):
                assert context.object == o
                pass

            def delete(self, objid, *args, **kwds):
                assert context.object == o
                pass

            def head(self, objid, *args, **kwds):
                assert context.object == o
                pass

            def options(self, objid, *args, **kwds):
                assert context.object == o
                pass

        for method_name in ['get', 'post', 'put', 'delete', 'head', 'options']:
            o = object()
            objid = registry.register(o)
            handler = A()
            getattr(handler, method_name)(objid=objid)

            try:
                getattr(handler, method_name)(objid="NoSuchObject")
            except tornado.web.HTTPError:
                pass
            else:
                self.fail(method_name)
Example #5
0
    def test_return_min_len_key(self):
        def full_id(partial):
            return [x for x in d if x.startswith(partial)][0]
        # start returning keys with len >=1
        # force collisions by registering lots of objects
        r=ObjectRegistry(min_objid_len=1)
        d=r._registry

        objs = [object()]
        oids = [r.register(objs[-1])]



        for i in range(512): # 16**3
            objs.append(object())
            oids.append(r.register(objs[-1]))
            self.assertEqual(r.get(full_id(oids[-1])),objs[-1])

            # make sure we get back the same key, if we reregister
            l = len(d)
            self.assertEqual(oids[-1],r.register(objs[-1]))
            # no change in number of keys
            self.assertEqual(len(d), l)
Example #6
0
    def test_return_min_len_key_more(self):
        def full_id(partial):
            return [x for x in d if x.startswith(partial)][0]
        # start returning keys with len >=8 (MIN_OBJID_LEN)
        # force collisions by building keys and shoving them
        # into the registry
        # then reregister same object and make sure
        # we get back an objid which is longer then
        # previous iteration, and that we can get back the object
        # # with it.

        r=ObjectRegistry(min_objid_len=8)
        d=r._registry

        HASH_LEN =len(r.hash_obj(object())) # should be 40 for sha1

        objs = [object()]
        partial = r.register(objs[-1])
        oid = full_id(partial)
        for i in range(8,HASH_LEN):
        # now we've yanked a reference out, let's inject
        # back in with the same (i+1)-prefix
            print(d.keys())
            partial += '_'
            new_oid = partial + oid[i+1:]

            # use the registry to store a reference,
            # then yank it out and reinsert with new_oid
            objs.append(object())
            oid = full_id(r.register(objs[-1]))
            d[new_oid] = d.pop(oid)

        # check that the prefix gets us the original object
        # and that the prefix+_ gets us the new object
        # and that registering the same object, gets us the
        # new id
        #     self.assertEqual(r.get(partial[:-1]), objs[-2])

            self.assertEqual(r.get(partial[:-1]), objs[-2])
            self.assertEqual(r.get(partial), objs[-1])
Example #7
0
    def test_return_min_len_key_more2(self):
        def full_id(partial):
            return [x for x in d if x.startswith(partial)][0]
            # start returning keys with len >=8 (MIN_OBJID_LEN)
            # force collisions by building keys and shoving them
            # into the registry
            # then reregister same object and make sure
            # we get back an objid which is longer then
            # previous iteration, and that we can get back the object
            # # with it.

        MIN_OBJID_LEN=8

        r=ObjectRegistry(min_objid_len=MIN_OBJID_LEN)
        d=r._registry

        HASH_LEN =len(r.hash_obj(object())) # should be 40 for sha1

        ##### new test
        for i in range(MIN_OBJID_LEN,HASH_LEN-1):
            o = object()
            oid = r.register(o)
            foid = full_id(oid)
            new_oid = foid[i:] + '_' * (HASH_LEN-i)
            d[new_oid] = d.pop(foid)
            self.assertTrue(len(r.register(o)) >= len(oid))

        for j in range(100):
            import random
            o = object()
            oid = r.register(o)
            foid = full_id(oid)
            i=random.randint(MIN_OBJID_LEN,HASH_LEN-1)
            new_oid = foid[i:] + '_' * (HASH_LEN-i)
            d[new_oid] = d.pop(foid)
            self.assertTrue(len(r.register(o)) >= len(oid))