示例#1
0
    def setup(self):
        super(TestStoreAuthz, self).setup()
        self.app = annotator.app.test_client()

        self.anno_id = '123'
        self.permissions = {
            'read': ['alice', 'bob'],
            'update': ['alice', 'charlie'],
            'admin': ['alice']
        }

        ann = Annotation(id=self.anno_id,
                         user='******',
                         text='Foobar',
                         permissions=self.permissions)
        ann.save()

        self.consumer = Consumer('test-consumer-key')
        save(self.consumer)

        self.user = '******'

        for u in ['alice', 'bob', 'charlie']:
            token = auth.generate_token(self.consumer.key, u)
            setattr(self, '%s_headers' % u, auth.headers_for_token(token))
    def test_search_permissions(self):
        anno = Annotation(text='Foobar', permissions={'read': []})
        anno.save()

        annotator.es.refresh(timesleep=0.01)

        res = Annotation.search()
        h.assert_equal(len(res), 1)

        res = Annotation.search(_user_id='bob')
        h.assert_equal(len(res), 1)

        anno = Annotation(text='Foobar', permissions={'read': ['bob']})
        anno.save()

        annotator.es.refresh(timesleep=0.01)

        res = Annotation.search()
        h.assert_equal(len(res), 1)

        res = Annotation.search(_user_id='alice')
        h.assert_equal(len(res), 1)

        res = Annotation.search(_user_id='bob')
        h.assert_equal(len(res), 2)
    def test_search_permissions(self):
        anno = Annotation(text='Foobar', permissions={'read': []})
        anno.save()

        annotator.es.refresh(timesleep=0.01)

        res = Annotation.search()
        h.assert_equal(len(res), 1)

        res = Annotation.search(_user_id='bob')
        h.assert_equal(len(res), 1)

        anno = Annotation(text='Foobar', permissions={'read': ['bob']})
        anno.save()

        annotator.es.refresh(timesleep=0.01)

        res = Annotation.search()
        h.assert_equal(len(res), 1)

        res = Annotation.search(_user_id='alice')
        h.assert_equal(len(res), 1)

        res = Annotation.search(_user_id='bob')
        h.assert_equal(len(res), 2)
    def test_delete(self):
        id_ = 1
        ann = Annotation(id=id_)
        ann.save()

        newann = Annotation.fetch(id_)
        newann.delete()

        noann = Annotation.fetch(id_)
        assert noann == None
    def test_delete(self):
        id_ = 1
        ann = Annotation(id=id_)
        ann.save()

        newann = Annotation.fetch(id_)
        newann.delete()

        noann = Annotation.fetch(id_)
        assert noann == None
    def test_basics(self):
        user = "******"
        ann = Annotation(text="Hello there", user=user)
        ann['ranges'] = []
        ann['ranges'].append({})
        ann['ranges'].append({})
        ann.save()

        ann = Annotation.fetch(ann.id)
        h.assert_equal(ann['text'], "Hello there")
        h.assert_equal(ann['user'], "alice")
        h.assert_equal(len(ann['ranges']), 2)
    def test_basics(self):
        user = "******"
        ann = Annotation(text="Hello there", user=user)
        ann['ranges'] = []
        ann['ranges'].append({})
        ann['ranges'].append({})
        ann.save()

        ann = Annotation.fetch(ann.id)
        h.assert_equal(ann['text'], "Hello there")
        h.assert_equal(ann['user'], "alice")
        h.assert_equal(len(ann['ranges']), 2)
示例#8
0
 def setup(self):
     self.permissions = dict(
         read=[self.gooduser, self.updateuser],
         update=[self.gooduser, self.updateuser],
         admin=[self.gooduser])
     kwargs = dict(
         id=self.anno_id,
         text=u"Foo",
         permissions=self.permissions
         )
     ann = Annotation(**kwargs)
     ann.save()
     self.app = app.test_client()
示例#9
0
    def test_update_other_users_annotation(self):
        ann = Annotation(id=123,
                         user='******',
                         permissions={'update': []})
        ann.save()

        payload = json.dumps({
            'id': 123,
            'text': 'Foo'
        })

        response = self.app.put('/api/annotations/123',
                                data=payload,
                                content_type='application/json',
                                headers=self.bob_headers)
        assert response.status_code == 200, "response should be 200 OK"
示例#10
0
def create_annotation():
    # Only registered users can create annotations
    if not auth.verify_request(request):
        return _failed_auth_response()

    if request.json:
        annotation = Annotation(_filter_input(request.json))

        annotation['consumer'] = request.headers[auth.HEADER_PREFIX + 'consumer-key']
        annotation['user'] = request.headers[auth.HEADER_PREFIX + 'user-id']

        annotation.save()

        return jsonify(annotation)
    else:
        return jsonify('No JSON payload sent. Annotation not created.', status=400)
示例#11
0
def create_annotation():
    # Only registered users can create annotations
    if not auth.verify_request(request):
        return _failed_auth_response()

    if request.json:
        annotation = Annotation(_filter_input(request.json))

        annotation['consumer'] = request.headers[auth.HEADER_PREFIX +
                                                 'consumer-key']
        annotation['user'] = request.headers[auth.HEADER_PREFIX + 'user-id']

        annotation.save()

        return jsonify(annotation)
    else:
        return jsonify('No JSON payload sent. Annotation not created.',
                       status=400)
示例#12
0
    def test_search(self):
        uri1 = u'http://xyz.com'
        uri2 = u'urn:uuid:xxxxx'
        user1 = u'levin'
        user2 = u'anna'
        anno1 = Annotation(uri=uri1, text=uri1, user=user1)
        anno2 = Annotation(uri=uri1, text=uri1 + uri1, user=user2)
        anno3 = Annotation(uri=uri2, text=uri2, user=user1)
        anno1.save()
        anno2.save()
        anno3.save()

        annotator.es.refresh(timesleep=0.01)

        res = Annotation.search()
        h.assert_equal(len(res), 3)

        # ordering (most recent first)
        h.assert_equal(res[0]['text'], uri2)

        res = Annotation.count()
        h.assert_equal(res, 3)

        res = Annotation.search(limit=1)
        h.assert_equal(len(res), 1)
        res = Annotation.count(limit=1)
        h.assert_equal(res, 3)

        res = Annotation.search(uri=uri1)
        h.assert_equal(len(res), 2)
        h.assert_equal(res[0]['uri'], uri1)
        h.assert_equal(res[0]['id'], anno2.id)

        res = Annotation.search(user=user1)
        h.assert_equal(len(res), 2)
        h.assert_equal(res[0]['user'], user1)
        h.assert_equal(res[0]['id'], anno3.id)

        res = Annotation.search(user=user1, uri=uri2)
        h.assert_equal(len(res), 1)
        h.assert_equal(res[0]['user'], user1)
        h.assert_equal(res[0]['id'], anno3.id)

        res = Annotation.count(user=user1, uri=uri2)
        h.assert_equal(res, 1)
示例#13
0
    def test_search(self):
        uri1 = u'http://xyz.com'
        uri2 = u'urn:uuid:xxxxx'
        user1 = u'levin'
        user2 = u'anna'
        anno1 = Annotation(uri=uri1, text=uri1, user=user1)
        anno2 = Annotation(uri=uri1, text=uri1 + uri1, user=user2)
        anno3 = Annotation(uri=uri2, text=uri2, user=user1)
        anno1.save()
        anno2.save()
        anno3.save()

        annotator.es.refresh(timesleep=0.01)

        res = Annotation.search()
        h.assert_equal(len(res), 3)

        # ordering (most recent first)
        h.assert_equal(res[0]['text'], uri2)

        res = Annotation.count()
        h.assert_equal(res, 3)

        res = Annotation.search(limit=1)
        h.assert_equal(len(res), 1)
        res = Annotation.count(limit=1)
        h.assert_equal(res, 3)

        res = Annotation.search(uri=uri1)
        h.assert_equal(len(res), 2)
        h.assert_equal(res[0]['uri'], uri1)
        h.assert_equal(res[0]['id'], anno2.id)

        res = Annotation.search(user=user1)
        h.assert_equal(len(res), 2)
        h.assert_equal(res[0]['user'], user1)
        h.assert_equal(res[0]['id'], anno3.id)

        res = Annotation.search(user=user1, uri=uri2)
        h.assert_equal(len(res), 1)
        h.assert_equal(res[0]['user'], user1)
        h.assert_equal(res[0]['id'], anno3.id)

        res = Annotation.count(user=user1, uri=uri2)
        h.assert_equal(res, 1)
示例#14
0
 def test_fetch(self):
     a = Annotation(foo='bar')
     a.save()
     b = Annotation.fetch(a.id)
     h.assert_equal(b['foo'], 'bar')
示例#15
0
 def test_save(self):
     a = Annotation(name='bob')
     a.save()
     h.assert_in('id', a)
示例#16
0
 def test_fetch(self):
     a = Annotation(foo='bar')
     a.save()
     b = Annotation.fetch(a.id)
     h.assert_equal(b['foo'], 'bar')
示例#17
0
 def _create_annotation(self, **kwargs):
     ann = Annotation(**kwargs)
     ann.save()
     return ann
示例#18
0
 def _create_annotation(self, **kwargs):
     ann = Annotation(**kwargs)
     ann.save()
     return ann
示例#19
0
 def test_save(self):
     a = Annotation(name='bob')
     a.save()
     h.assert_in('id', a)