예제 #1
0
 def test03_delete(self):
     """Test deletion of resource."""
     s = Store('http://ex.org/')
     uri = s.add(LDPR(content='def'))
     self.assertEqual(s[uri].content, 'def')
     s.delete(uri)
     self.assertRaises(KeyDeleted, s.__getitem__, uri)
예제 #2
0
 def test04_getitem(self):
     """Test getitem."""
     s = Store('http://ex.org/')
     self.assertRaises(KeyError, s.__getitem__, 'http://ex.org/')
     self.assertRaises(KeyError, s.__getitem__, 'http://ex.org/a-z')
     uri = s.add(LDPR(), uri='http://ex.org/bbb')
     self.assertTrue(s['http://ex.org/bbb'])
     s.delete('http://ex.org/bbb')
     self.assertRaises(KeyDeleted, s.__getitem__, 'http://ex.org/bbb')
예제 #3
0
 def test07_individual_acl(self):
     """Test access to individual resource ACL."""
     s = Store('http://x.o/')
     uri = s.add(LDPR(), uri='http://x.o/a/c')
     # no acl defined
     acl = s.individual_acl(uri)
     self.assertNotEqual(uri, acl)
     self.assertRegex(acl, uri + r'''\S+$''')
     # acl defined
     s[uri].acl = 'the_acl'
     self.assertEqual(s.individual_acl(uri), 'the_acl')
예제 #4
0
 def test02_add(self):
     """Test addition of resource."""
     s = Store('http://ex.org')
     # resource with known URI
     r1 = LDPR(content='abc')
     uri = s.add(r1, uri='http://a.b/')
     self.assertEqual(uri, 'http://a.b/')
     # resource with no URI
     r2 = LDPR(content='bcd')
     uri = s.add(r2)
     self.assertRegex(uri, r'''http://ex.org/''')
     # normalization of base_uri to form without trailing /
     uri3 = s.add(LDPR(), uri='http://ex.org/')
     self.assertEqual(uri3, 'http://ex.org')
예제 #5
0
 def test05_resources_access(self):
     """Test other functions providing access to resources dict from store."""
     s = Store('http://x.o/')
     s.add(LDPR())
     uri = s.add(LDPR())
     s.add(LDPR())
     self.assertEqual(len(s), 3)
     self.assertEqual(len(s.items()), 3)
     self.assertEqual(len(set(iter(s))), 3)
     self.assertTrue(uri in s)
     self.assertFalse('abc' in s)
예제 #6
0
 def test43_from_store(self):
     """Test from_store method."""
     h = mockedLDPHandler()
     h.store = Store('http://localhost/')
     self.assertRaises(HTTPError, h.from_store, 'uri:abc')
     r = LDPNR(content=b'hello')
     h.store.add(r, uri='uri:abc')
     self.assertEqual(h.from_store('uri:abc'), r)
     h.store.delete(uri='uri:abc')
     self.assertRaises(HTTPError, h.from_store, 'uri:abc')
예제 #7
0
 def test08_object_references(self):
     """Test object_references()."""
     s = Store('http://x.o/')
     r1 = LDPRS()
     r1.parse(b'<http://ex.org/a1> <http://ex.org/b> <http://ex.org/c1>.')
     s.add(r1, uri='http://ex.org/r1')
     r2 = LDPRS()
     r2.parse(b'<http://ex.org/a2> <http://ex.org/b> <http://ex.org/c2>.')
     s.add(r2, uri='http://ex.org/r2')
     r3 = LDPRS()
     r3.parse(b'<http://ex.org/a3> <http://ex.org/b> <http://ex.org/c1>.')
     s.add(r3, uri='http://ex.org/r3')
     g = set(s.object_references('http://ex.org/c1'))
     self.assertEqual(len(g), 2)
     self.assertEqual(g,
                      set([(URIRef('http://ex.org/a1'), URIRef('http://ex.org/b'), URIRef('http://ex.org/c1')),
                           (URIRef('http://ex.org/a3'), URIRef('http://ex.org/b'), URIRef('http://ex.org/c1'))]))
예제 #8
0
def mockedLDPHandler(method='GET',
                     uri='/test',
                     headers=None,
                     body=None,
                     base_uri='http://localhost/'):
    """LDPHandler with appropriate Application and HTTPRequest mocking, and a Store."""
    headers = HTTPHeaders({} if headers is None else headers)
    request = HTTPRequest(method=method,
                          uri=uri,
                          headers=headers,
                          body=body,
                          connection=Mock())
    h = LDPHandler(Application(), request)
    h.base_uri = base_uri
    h.store = Store(h.base_uri)
    h.store.add(LDPC(), uri=h.base_uri)
    return h
예제 #9
0
 def test05_get(self):
     """Test GET method."""
     # auth disabled
     LDPHandler.no_auth = True
     # 404
     h = mockedLDPHandler(uri='/not-present')
     h.write = MagicMock()
     self.assertRaises(HTTPError, h.get, False)
     self.assertRaises(HTTPError, h.get, True)
     #
     h = mockedLDPHandler(uri='/1')
     h.base_uri = 'http://localhost'
     h.store = Store(h.base_uri)
     h.store.add(LDPNR(content=b'hello', content_type='text/plain'),
                 uri='1')
     h.write = MagicMock()
     h.get(False)
     h.write.assert_called_with(b'hello')
예제 #10
0
 def test02_get(self):
     """Test with data in store."""
     h = mockedStatusHandler()
     h.store = Store('uri:')
     r = LDPNR(content=b'hello')
     h.store.add(r, uri='uri:abc1')
     h.store.delete(uri='uri:abc1')
     h.store.add(r, uri='uri:abc2')
     h.store.add(
         Exception(),
         uri='uri:abc3')  # Exception does not have type_label property
     h.write = MagicMock()
     h.get()
     h.write.assert_has_calls([
         call('Store has\n'),
         call('  * 2 active resources\n'),
         call('    * uri:abc2 - LDPNR\n'),
         call("    * uri:abc3 - <class 'Exception'>\n"),
         call('  * 1 deleted resources\n'),
         call('    * uri:abc1 - deleted\n')
     ])
예제 #11
0
 def get_app(self):
     """Get trilpy application with some basic config."""
     return make_app(store=Store('http://localhost/'), no_auth=True)
예제 #12
0
 def test01_create(self):
     """Check create."""
     s = Store('http://ex.org/')
     self.assertEqual(s.base_uri, 'http://ex.org/')
     self.assertRaises(TypeError, Store)
예제 #13
0
 def test08_acl(self):
     """Test location of effective ACL."""
     s = Store('http://x.o/')
     # resource has no acl
     uri = s.add(LDPR())
     self.assertEqual(s.acl(uri), s.acl_default)
     # resource has acl
     acl = s.add(ACLR('http://x.o/an_acl'))
     uri1 = s.add(LDPC(acl=acl))
     self.assertEqual(s.acl(uri1), acl)
     # sub-containers will inherit
     uri2 = s.add(LDPC(), context=uri1)
     self.assertEqual(s.acl(uri2), acl)
     uri3 = s.add(LDPC(), context=uri2)
     self.assertEqual(s.acl(uri3), acl)
     # set small inheritance limit
     s.acl_inheritance_limit = 1
     self.assertRaises(Exception, s.acl, uri3)
     s.acl_inheritance_limit = 2
     self.assertEqual(s.acl(uri3), acl)
예제 #14
0
 def test06_get_uri(self):
     """Test URI generation."""
     s = Store('http://x.o/')
     self.assertEqual(s._get_uri('http://x.o/a/', 'b'), 'http://x.o/a/b')
     self.assertEqual(s._get_uri('http://x.o/a', 'b'), 'http://x.o/a/b')
     s.add(LDPR(), uri='http://x.o/a/c')
     self.assertNotEqual(s._get_uri('http://x.o/a/', 'c'),
                         'a/c')  # can't have same again
     # With no inputs we get a number on the base_uri
     uri1 = s._get_uri()
     s.add(LDPR(), uri=uri1)
     self.assertRegex(uri1, r'''\/(\d+)$''')
     uri2 = s._get_uri()
     self.assertRegex(uri2, r'''\/(\d+)$''')
     self.assertNotEqual(uri1, uri2)