コード例 #1
0
 def test_acl_principal(self):
     annotation = Annotation({'permissions': {
         'read': ['saoirse'],
     }})
     actual = annotation.__acl__()
     expect = [(security.Allow, 'saoirse', 'read'), security.DENY_ALL]
     assert actual == expect
コード例 #2
0
 def test_acl_deny_system_role(self):
     annotation = Annotation(
         {'permissions': {
             'read': [security.Everyone],
         }})
     actual = annotation.__acl__()
     expect = [security.DENY_ALL]
     assert actual == expect
コード例 #3
0
 def test_acl_group_world(self):
     annotation = Annotation(
         {'permissions': {
             'read': ['group:__world__'],
         }})
     actual = annotation.__acl__()
     expect = [(security.Allow, security.Everyone, 'read'),
               security.DENY_ALL]
     assert actual == expect
コード例 #4
0
 def test_acl_group(self):
     annotation = Annotation(
         {'permissions': {
             'read': ['group:lulapalooza'],
         }})
     actual = annotation.__acl__()
     expect = [(security.Allow, 'group:lulapalooza', 'read'),
               security.DENY_ALL]
     assert actual == expect
コード例 #5
0
ファイル: elastic_test.py プロジェクト: hashin/h
 def test_acl_group_world(self):
     annotation = Annotation({
         'permissions': {
             'read': ['group:__world__'],
         }
     })
     actual = annotation.__acl__()
     expect = [(security.Allow, security.Everyone, 'read'), security.DENY_ALL]
     assert actual == expect
コード例 #6
0
ファイル: elastic_test.py プロジェクト: hashin/h
 def test_acl_group(self):
     annotation = Annotation({
         'permissions': {
             'read': ['group:lulapalooza'],
         }
     })
     actual = annotation.__acl__()
     expect = [(security.Allow, 'group:lulapalooza', 'read'), security.DENY_ALL]
     assert actual == expect
コード例 #7
0
ファイル: elastic_test.py プロジェクト: hashin/h
 def test_acl_deny_system_role(self):
     annotation = Annotation({
         'permissions': {
             'read': [security.Everyone],
         }
     })
     actual = annotation.__acl__()
     expect = [security.DENY_ALL]
     assert actual == expect
コード例 #8
0
ファイル: elastic_test.py プロジェクト: hashin/h
 def test_acl_principal(self):
     annotation = Annotation({
         'permissions': {
             'read': ['saoirse'],
         }
     })
     actual = annotation.__acl__()
     expect = [(security.Allow, 'saoirse', 'read'), security.DENY_ALL]
     assert actual == expect
コード例 #9
0
ファイル: test_api.py プロジェクト: ackermann/h
def annotation(config):
    from h.api.models.elastic import Annotation
    ann = Annotation({
        'created': '2016-01-01T00:00:00.000000+00:00',
        'updated': '2016-01-01T00:00:00.000000+00:00',
        'user': '******',
        'target': [{'source': 'http://foobar.com', 'selector': []}],
        'text': 'My test annotation',
        'permissions': {'read': ['group:__world__'],
                        'update': ['acct:testuser@localhost'],
                        'delete': ['acct:testuser@localhost'],
                        'admin': ['acct:testuser@localhost']},
    })
    ann.save()
    return ann
コード例 #10
0
ファイル: test_api.py プロジェクト: ackermann/h
def annotation(config):
    from h.api.models.elastic import Annotation
    ann = Annotation({
        'created':
        '2016-01-01T00:00:00.000000+00:00',
        'updated':
        '2016-01-01T00:00:00.000000+00:00',
        'user':
        '******',
        'target': [{
            'source': 'http://foobar.com',
            'selector': []
        }],
        'text':
        'My test annotation',
        'permissions': {
            'read': ['group:__world__'],
            'update': ['acct:testuser@localhost'],
            'delete': ['acct:testuser@localhost'],
            'admin': ['acct:testuser@localhost']
        },
    })
    ann.save()
    return ann
コード例 #11
0
 def test_extra(self):
     annotation = Annotation({
         'id':
         'AVLBpz--vTW_3w8LyzKg',
         'created':
         '2016-02-08T16:11:49.576908+00:00',
         'updated':
         '2016-02-08T16:11:49.576908+00:00',
         'user':
         '******',
         'group':
         '__world__',
         'uri':
         'https://example.com',
         'text':
         'My comment',
         'tags': ['look'],
         'target': [{
             'source': 'https://example.com',
             'selector': []
         }],
         'references': ['Qe7fpc5ZRgWy0RSHEP9UNg'],
         'permissions': {
             'read': ['group:__world__'],
             'admin': ['luke'],
             'update': ['luke'],
             'delete': ['luke']
         },
         'document': {
             'title': 'Example'
         },
         'somethingelse':
         'foo',
         'extra': {
             'foo': 'bar'
         }
     })
     assert annotation.extra == {
         'somethingelse': 'foo',
         'extra': {
             'foo': 'bar'
         }
     }
コード例 #12
0
 def test_target_selectors(self):
     annotation = Annotation({'target': [{'selector': [{'foo': 'bar'}]}]})
     assert annotation.target_selectors == [{'foo': 'bar'}]
コード例 #13
0
 def test_target_selectors_empty(self):
     annotation = Annotation({'target': {}})
     assert annotation.target_selectors == []
コード例 #14
0
 def test_extra_empty(self):
     annotation = Annotation({'id': 'Qe7fpc5ZRgWy0RSHEP9UNg'})
     assert annotation.extra is None
コード例 #15
0
 def test_tags_string_type(self):
     annotation = Annotation({'tags': 'foo'})
     assert annotation.tags == ['foo']
コード例 #16
0
 def test_text(self):
     annotation = Annotation({'text': 'Lorem ipsum'})
     assert annotation.text == 'Lorem ipsum'
コード例 #17
0
 def test_target_uri_normalized_returns_normalized_uri(self, uri_normalize):
     annotation = Annotation(uri='http://example.com')
     uri_normalize.return_value = 'normalized'
     assert annotation.target_uri_normalized == 'normalized'
コード例 #18
0
 def test_target_uri_from_uri(self):
     annotation = Annotation({'uri': 'http://example.com'})
     assert annotation.target_uri == 'http://example.com'
コード例 #19
0
 def test_target_selectors_missing_target(self):
     annotation = Annotation({})
     assert annotation.target_selectors == []
コード例 #20
0
 def test_parent_id_returns_none_if_empty_references(self):
     annotation = Annotation(references=[])
     assert annotation.parent_id is None
コード例 #21
0
 def test_target_links_from_annotation(self):
     annotation = Annotation(target=[{'source': 'target link'}])
     assert annotation.target_links == ['target link']
コード例 #22
0
 def test_uri_when_uri_is_not_a_string(self):
     for uri in (True, None, 23, 23.7, {"foo": False}, [1, 2, 3]):
         assert isinstance(Annotation(uri=uri).uri, unicode)
コード例 #23
0
 def test_uri_with_no_uri(self):
     assert Annotation().uri == ""
コード例 #24
0
 def test_uri(self):
     assert Annotation(uri="http://foo.com").uri == "http://foo.com"
コード例 #25
0
class TestAnnotation(object):
    @pytest.mark.parametrize('annotation', [
        Annotation({'created': '2016-02-25T16:45:23.371848+00:00'}),
        Annotation({'created': '2016-02-25T16:45:23.371848'})
    ])
    def test_created(self, annotation):
        assert annotation.created == datetime.datetime(2016, 2, 25, 16, 45, 23,
                                                       371848)

    @pytest.mark.parametrize('annotation', [
        Annotation({'created': 'invalid'}),
        Annotation({'created': ''}),
        Annotation({'created': None})
    ])
    def test_created_invalid(self, annotation):
        assert annotation.created is None

    @pytest.mark.parametrize('annotation', [
        Annotation({'updated': '2016-02-25T16:45:23.371848+00:00'}),
        Annotation({'updated': '2016-02-25T16:45:23.371848'})
    ])
    def test_updated(self, annotation):
        assert annotation.updated == datetime.datetime(2016, 2, 25, 16, 45, 23,
                                                       371848)

    @pytest.mark.parametrize('annotation', [
        Annotation({'updated': 'invalid'}),
        Annotation({'updated': ''}),
        Annotation({'updated': None})
    ])
    def test_updated_invalid(self, annotation):
        assert annotation.updated is None

    def test_target_uri_from_uri(self):
        annotation = Annotation({'uri': 'http://example.com'})
        assert annotation.target_uri == 'http://example.com'

    @pytest.mark.parametrize('annotation', [
        Annotation({
            'uri': '',
            'target': [{
                'source': 'http://example.com'
            }]
        }),
        Annotation({'target': [{
            'source': 'http://example.com'
        }]})
    ])
    def test_target_uri_from_target_source(self, annotation):
        assert annotation.target_uri == 'http://example.com'

    def test_target_uri_normalized_normalizes_target_uri(
            self, target_uri, uri_normalize):
        annotation = Annotation()
        target_uri.return_value = 'http://example.com'
        annotation.target_uri_normalized
        uri_normalize.assert_called_once_with(annotation.target_uri)

    def test_target_uri_normalized_returns_normalized_uri(self, uri_normalize):
        annotation = Annotation(uri='http://example.com')
        uri_normalize.return_value = 'normalized'
        assert annotation.target_uri_normalized == 'normalized'

    def test_target_uri_normalized_returns_none_when_target_uri_not_set(self):
        annotation = Annotation()
        assert annotation.target_uri_normalized is None

    def test_text(self):
        annotation = Annotation({'text': 'Lorem ipsum'})
        assert annotation.text == 'Lorem ipsum'

    @pytest.mark.parametrize(
        'annotation',
        [Annotation({'text': ''}),
         Annotation({'text': None}),
         Annotation({})])
    def test_text_empty(self, annotation):
        assert annotation.text is None

    def test_tags(self):
        annotation = Annotation({'tags': ['foo', 'bar']})
        assert annotation.tags == ['foo', 'bar']

    def test_tags_string_type(self):
        annotation = Annotation({'tags': 'foo'})
        assert annotation.tags == ['foo']

    @pytest.mark.parametrize(
        'annotation',
        [Annotation({'tags': []}),
         Annotation({'tags': None}),
         Annotation({})])
    def test_tags_empty(self, annotation):
        assert annotation.tags == []

    def test_userid(self):
        annotation = Annotation({'user': '******'})
        assert annotation.userid == 'luke'

    def test_groupid(self):
        annotation = Annotation({'group': '__world__'})
        assert annotation.groupid == '__world__'

    def test_references_allows_20_char_ids(self):
        annotation = Annotation({'references': ['AVMG6tocH9ZO4OKSk1WS']})
        assert annotation.references == ['AVMG6tocH9ZO4OKSk1WS']

    def test_references_allows_22_char_ids(self):
        annotation = Annotation({'references': ['AVMG6tocH9ZO4OKSk1WSaa']})
        assert annotation.references == ['AVMG6tocH9ZO4OKSk1WSaa']

    @pytest.mark.parametrize('annotation', [
        Annotation({'references': ['too short']}),
        Annotation(
            {'references': ['this is way too long, it cannot be an id']})
    ])
    def test_references_filters_out_non_ids(self, annotation):
        assert annotation.references == []

    @pytest.mark.parametrize('annotation', [
        Annotation({
            'permissions': {
                'read': ['group:__world__']
            },
            'user': '******',
            'group': '__world__'
        }),
        Annotation({
            'permissions': {
                'read': ['group:tatooine']
            },
            'user': '******',
            'group': 'tatooine'
        })
    ])
    def test_shared_true(self, annotation):
        assert annotation.shared is True

    @pytest.mark.parametrize('annotation', [
        Annotation({
            'permissions': {
                'read': ['luke']
            },
            'user': '******',
            'group': '__world__'
        }),
        Annotation({
            'permissions': {
                'read': ['luke']
            },
            'user': '******',
            'group': 'tatooine'
        }),
        Annotation({
            'permissions': {
                'read': ['hansolo']
            },
            'user': '******',
            'group': 'tatooine'
        })
    ])
    def test_shared_false(self, annotation):
        assert annotation.shared is False

    def test_target_selectors(self):
        annotation = Annotation({'target': [{'selector': [{'foo': 'bar'}]}]})
        assert annotation.target_selectors == [{'foo': 'bar'}]

    def test_target_selectors_empty(self):
        annotation = Annotation({'target': {}})
        assert annotation.target_selectors == []

    def test_target_selectors_missing_target(self):
        annotation = Annotation({})
        assert annotation.target_selectors == []

    def test_extra(self):
        annotation = Annotation({
            'id':
            'AVLBpz--vTW_3w8LyzKg',
            'created':
            '2016-02-08T16:11:49.576908+00:00',
            'updated':
            '2016-02-08T16:11:49.576908+00:00',
            'user':
            '******',
            'group':
            '__world__',
            'uri':
            'https://example.com',
            'text':
            'My comment',
            'tags': ['look'],
            'target': [{
                'source': 'https://example.com',
                'selector': []
            }],
            'references': ['Qe7fpc5ZRgWy0RSHEP9UNg'],
            'permissions': {
                'read': ['group:__world__'],
                'admin': ['luke'],
                'update': ['luke'],
                'delete': ['luke']
            },
            'document': {
                'title': 'Example'
            },
            'somethingelse':
            'foo',
            'extra': {
                'foo': 'bar'
            }
        })
        assert annotation.extra == {
            'somethingelse': 'foo',
            'extra': {
                'foo': 'bar'
            }
        }

    def test_extra_empty(self):
        annotation = Annotation({'id': 'Qe7fpc5ZRgWy0RSHEP9UNg'})
        assert annotation.extra is None

    def test_uri(self):
        assert Annotation(uri="http://foo.com").uri == "http://foo.com"

    def test_uri_with_no_uri(self):
        assert Annotation().uri == ""

    def test_uri_when_uri_is_not_a_string(self):
        for uri in (True, None, 23, 23.7, {"foo": False}, [1, 2, 3]):
            assert isinstance(Annotation(uri=uri).uri, unicode)

    def test_target_links_from_annotation(self):
        annotation = Annotation(target=[{'source': 'target link'}])
        assert annotation.target_links == ['target link']

    def test_parent_id_returns_none_if_no_references(self):
        annotation = Annotation()
        assert annotation.parent_id is None

    def test_parent_id_returns_none_if_empty_references(self):
        annotation = Annotation(references=[])
        assert annotation.parent_id is None

    def test_parent_id_returns_none_if_references_not_list(self):
        annotation = Annotation(references={'foo': 'bar'})
        assert annotation.parent_id is None

    def test_parent_id_returns_thread_parent_id(self):
        annotation = Annotation(references=['abc123', 'def456'])
        assert annotation.parent_id == 'def456'

    def test_document_returns_document_type(self):
        annotation = Annotation(document={'title': 'The title'})
        assert type(annotation.document) == Document

    def test_document_returns_none_without_document(self):
        annotation = Annotation()
        assert annotation.document is None

    def test_document_returns_none_with_empty_document(self):
        annotation = Annotation(document={})
        assert annotation.document is None

    @pytest.mark.parametrize('document', ['a string', [], [1, 2, 3], 11, 12.7])
    def test_document_returns_none_with_non_dict_document(self, document):
        annotation = Annotation(document=document)
        assert annotation.document is None

    def test_acl_principal(self):
        annotation = Annotation({'permissions': {
            'read': ['saoirse'],
        }})
        actual = annotation.__acl__()
        expect = [(security.Allow, 'saoirse', 'read'), security.DENY_ALL]
        assert actual == expect

    def test_acl_deny_system_role(self):
        annotation = Annotation(
            {'permissions': {
                'read': [security.Everyone],
            }})
        actual = annotation.__acl__()
        expect = [security.DENY_ALL]
        assert actual == expect

    def test_acl_group(self):
        annotation = Annotation(
            {'permissions': {
                'read': ['group:lulapalooza'],
            }})
        actual = annotation.__acl__()
        expect = [(security.Allow, 'group:lulapalooza', 'read'),
                  security.DENY_ALL]
        assert actual == expect

    def test_acl_group_world(self):
        annotation = Annotation(
            {'permissions': {
                'read': ['group:__world__'],
            }})
        actual = annotation.__acl__()
        expect = [(security.Allow, security.Everyone, 'read'),
                  security.DENY_ALL]
        assert actual == expect

    @pytest.fixture
    def link_text(self, patch):
        return patch('h.api.models.elastic.Annotation.link_text',
                     autospec=None,
                     new_callable=PropertyMock)

    @pytest.fixture
    def title(self, patch):
        return patch('h.api.models.elastic.Annotation.title',
                     autospec=None,
                     new_callable=PropertyMock)

    @pytest.fixture
    def href(self, patch):
        return patch('h.api.models.elastic.Annotation.href',
                     autospec=None,
                     new_callable=PropertyMock)

    @pytest.fixture
    def hostname_or_filename(self, patch):
        return patch('h.api.models.elastic.Annotation.hostname_or_filename',
                     autospec=None,
                     new_callable=PropertyMock)

    @pytest.fixture
    def target_uri(self, patch):
        return patch('h.api.models.elastic.Annotation.target_uri',
                     autospec=None,
                     new_callable=PropertyMock)

    @pytest.fixture
    def uri_normalize(self, patch):
        return patch('h.api.models.elastic.uri.normalize')
コード例 #26
0
 def test_references_allows_22_char_ids(self):
     annotation = Annotation({'references': ['AVMG6tocH9ZO4OKSk1WSaa']})
     assert annotation.references == ['AVMG6tocH9ZO4OKSk1WSaa']
コード例 #27
0
 def test_parent_id_returns_thread_parent_id(self):
     annotation = Annotation(references=['abc123', 'def456'])
     assert annotation.parent_id == 'def456'
コード例 #28
0
 def test_target_uri_normalized_normalizes_target_uri(
         self, target_uri, uri_normalize):
     annotation = Annotation()
     target_uri.return_value = 'http://example.com'
     annotation.target_uri_normalized
     uri_normalize.assert_called_once_with(annotation.target_uri)
コード例 #29
0
 def test_document_returns_document_type(self):
     annotation = Annotation(document={'title': 'The title'})
     assert type(annotation.document) == Document
コード例 #30
0
 def test_target_uri_normalized_returns_none_when_target_uri_not_set(self):
     annotation = Annotation()
     assert annotation.target_uri_normalized is None
コード例 #31
0
 def test_document_returns_none_without_document(self):
     annotation = Annotation()
     assert annotation.document is None
コード例 #32
0
 def test_tags(self):
     annotation = Annotation({'tags': ['foo', 'bar']})
     assert annotation.tags == ['foo', 'bar']
コード例 #33
0
 def test_document_returns_none_with_empty_document(self):
     annotation = Annotation(document={})
     assert annotation.document is None
コード例 #34
0
 def test_parent_id_returns_none_if_references_not_list(self):
     annotation = Annotation(references={'foo': 'bar'})
     assert annotation.parent_id is None
コード例 #35
0
 def test_document_returns_none_with_non_dict_document(self, document):
     annotation = Annotation(document=document)
     assert annotation.document is None