'Test that virtual lib. + search restriction works on all end points'

def test_srv_restrictions(self):

    with self.create_server(auth=True, auth_mode=u'basic') as server:
        db = server.handler.router.ctx.library_broker.get(None)
        db.set_pref(u'virtual_libraries', {u'1': u'id:1', u'12': u'id:1    or    id:2'})
        db.set_field(u'tags', {1: [u'present'], 3: [u'missing']})
        self.assertTrue(db.has_id(3))
        server.handler.ctx.user_manager.add_user(u'12', u'test', restriction={u'library_restrictions': {os.path.basename(db.backend.library_path): u'id:1    or    id:2'}})
        conn = server.connect()
        def url_for(path, **kw):
            (p, q) = path.partition(u'?')[::2]
            ans = server.handler.router.url_for(p, **kw)
            if q:
                ans += (u'?' + q)
            return ans
        ae = self.assertEqual
        def r(path, status=OK, method=u'GET'):
            (r, data) = make_request(conn, path, username=u'12', password=u'test', prefix=u'', method=method)
            ae(status, r.status)
            if (status == NOT_FOUND):
                p = data.partition(u':')[0]
                ae(p, u'No    book    with    id')
            return data
        ok = r
        nf = partial(r, status=NOT_FOUND)
        ok(url_for(u'/ajax/book', book_id=1))
        nf(url_for(u'/ajax/book', book_id=3))
        data = ok(url_for(u'/ajax/books'))
        self.assertIsNone(data[u'3'])
        for i in u'12':
            self.assertIsNotNone(data[i])
        ae(set(r(url_for(u'/ajax/search'))[u'book_ids']), {1, 2})
        ae(set(r(url_for(u'/ajax/search?query=id:2'))[u'book_ids']), {2})
        ae(set(r(url_for(u'/ajax/search?vl=1'))[u'book_ids']), {1})
        nf(url_for(u'/book-manifest', book_id=3, fmt=u'TXT'))
        nf(url_for(u'/book-file', book_id=3, fmt=u'TXT', size=1, mtime=1, name=u'x'))
        data = ok(url_for(u'/book-get-last-read-position', library_id=db.server_library_id, which=u'1-TXT_3-TXT'))
        ae(set(data), {u'1:TXT'})
        nf(url_for(u'/book-set-last-read-position', book_id=3, library_id=db.server_library_id, fmt=u'TXT'), method=u'POST')
        r(url_for(u'/cdb/cmd', which=u'list'), status=FORBIDDEN)
        def sr(path, **k):
            return set(ok(url_for(path, **k))[u'search_result'][u'book_ids'])
        for q in u'books-init    init    get-books'.split():
            ae(sr((u'/interface-data/' + q)), {1, 2})
        ae(sr(u'/interface-data/get-books?vl=1'), {1})
        ok(url_for(u'/interface-data/book-metadata', book_id=1))
        nf(url_for(u'/interface-data/book-metadata', book_id=3))
        ok(url_for(u'/get', what=u'thumb', book_id=1))
        nf(url_for(u'/get', what=u'thumb', book_id=3))
